From 040b516585576b0d6fe99d31e0408da205a2ecb1 Mon Sep 17 00:00:00 2001 From: qgong Date: Tue, 2 Dec 2014 16:07:37 -0800 Subject: [PATCH] Referral code changes actually. --- Branch-SDK-TestBed/project.properties | 2 +- .../branch/testbed/ReferralCodeActivity.java | 45 ++++++++++----- Branch-SDK/AndroidManifest.xml | 2 +- Branch-SDK/project.properties | 2 +- Branch-SDK/src/io/branch/referral/Branch.java | 29 ++++++---- README.md | 55 +++++++++++++++++++ 6 files changed, 109 insertions(+), 26 deletions(-) diff --git a/Branch-SDK-TestBed/project.properties b/Branch-SDK-TestBed/project.properties index 5eee04a18..de542dbf8 100644 --- a/Branch-SDK-TestBed/project.properties +++ b/Branch-SDK-TestBed/project.properties @@ -11,5 +11,5 @@ #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt # Project target. -target=android-19 +target=android-20 android.library.reference.1=../Branch-SDK diff --git a/Branch-SDK-TestBed/src/io/branch/testbed/ReferralCodeActivity.java b/Branch-SDK-TestBed/src/io/branch/testbed/ReferralCodeActivity.java index 562e5d468..111d58cac 100644 --- a/Branch-SDK-TestBed/src/io/branch/testbed/ReferralCodeActivity.java +++ b/Branch-SDK-TestBed/src/io/branch/testbed/ReferralCodeActivity.java @@ -73,13 +73,18 @@ public void onClick(View v) { } } - branch.getReferralCode(prefix, amount, getCalculationType(), getLocation(), new BranchReferralInitListener() { + branch.getReferralCode(prefix, amount, expiration, null, getCalculationType(), getLocation(), new BranchReferralInitListener() { @Override public void onInitFinished(JSONObject referralCode) { try { - txtReferralCode.setText(referralCode.getString("referral_code")); + // Ugly! will add error code soon. + if (!referralCode.has("error_message")) { + txtReferralCode.setText(referralCode.getString("referral_code")); + } else { + txtReferralCode.setText(referralCode.getString("error_message")); + } } catch (JSONException e) { - txtReferralCode.setText("Invalid code"); + txtReferralCode.setText("Error parsing JSON"); } } }); @@ -97,13 +102,20 @@ public void onClick(View v) { @Override public void onInitFinished(JSONObject referralCode) { try { - String code = referralCode.getString("referral_code"); - if (referral_code.equals(code)) { - txtValid.setVisibility(View.VISIBLE); - txtValid.setText("Valid"); + txtValid.setVisibility(View.VISIBLE); + if (!referralCode.has("error_message")) { + + String code = referralCode.getString("referral_code"); + if (referral_code.equals(code)) { + txtValid.setText("Valid"); + } else { + txtValid.setText("Mismatch!"); + } + } else { + txtValid.setText("Invalid"); } } catch (JSONException e) { - e.printStackTrace(); + txtReferralCode.setText("Error parsing JSON"); } } }); @@ -122,13 +134,20 @@ public void onClick(View v) { @Override public void onInitFinished(JSONObject referralCode) { try { - String code = referralCode.getString("referral_code"); - if (referral_code.equals(code)) { - txtValid.setVisibility(View.VISIBLE); - txtValid.setText("Applied"); + txtValid.setVisibility(View.VISIBLE); + if (!referralCode.has("error_message")) { + + String code = referralCode.getString("referral_code"); + if (referral_code.equals(code)) { + txtValid.setText("Applied"); + } else { + txtValid.setText("Mismatch!"); + } + } else { + txtValid.setText("Invalid"); } } catch (JSONException e) { - e.printStackTrace(); + txtReferralCode.setText("Error parsing JSON"); } } }); diff --git a/Branch-SDK/AndroidManifest.xml b/Branch-SDK/AndroidManifest.xml index 9ec2aa03f..d80590e4c 100644 --- a/Branch-SDK/AndroidManifest.xml +++ b/Branch-SDK/AndroidManifest.xml @@ -5,5 +5,5 @@ android:versionName="1.0" > + android:targetSdkVersion="20" /> diff --git a/Branch-SDK/project.properties b/Branch-SDK/project.properties index 91d2b0246..7e2ca64ff 100644 --- a/Branch-SDK/project.properties +++ b/Branch-SDK/project.properties @@ -11,5 +11,5 @@ #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt # Project target. -target=android-19 +target=android-20 android.library=true diff --git a/Branch-SDK/src/io/branch/referral/Branch.java b/Branch-SDK/src/io/branch/referral/Branch.java index 76fc6529f..d8fbb2616 100644 --- a/Branch-SDK/src/io/branch/referral/Branch.java +++ b/Branch-SDK/src/io/branch/referral/Branch.java @@ -27,6 +27,7 @@ public class Branch { public static String REEFERRAL_BUCKET_DEFAULT = "default"; public static String REFERRAL_CODE_TYPE = "credit"; public static int REFERRAL_CREATION_SOURCE_SDK = 2; + public static String REFERRAL_CODE = "referral_code"; public static int REFERRAL_CODE_LOCATION_REFERREE = 0; public static int REFERRAL_CODE_LOCATION_REFERRING_USER = 2; @@ -1034,11 +1035,15 @@ private void processReferralCodeGet(final ServerResponse resp) { public void run() { if (getReferralCodeCallback_ != null) { try { - JSONObject data = resp.getObject(); - String event = data.getString("event"); - String code = event.substring(REDEEM_CODE.length() + 1); - data.put("referral_code", code); - getReferralCodeCallback_.onInitFinished(data); + JSONObject json; + // check if a valid referral code json is returned + if (!resp.getObject().has(REFERRAL_CODE)) { + json = new JSONObject(); + json.put("error_message", "Failed to get referral code"); + } else { + json = resp.getObject(); + } + getReferralCodeCallback_.onInitFinished(json); } catch (JSONException e) { e.printStackTrace(); } @@ -1054,11 +1059,15 @@ private void processReferralCodeValidation(final ServerResponse resp) { public void run() { if (validateReferralCodeCallback_ != null) { try { - JSONObject data = resp.getObject(); - String event = data.getString("event"); - String code = event.substring(REDEEM_CODE.length() + 1); - data.put("referral_code", code); - validateReferralCodeCallback_.onInitFinished(data); + JSONObject json; + // check if a valid referral code json is returned + if (!resp.getObject().has(REFERRAL_CODE)) { + json = new JSONObject(); + json.put("error_message", "Invalid referral code"); + } else { + json = resp.getObject(); + } + validateReferralCodeCallback_.onInitFinished(json); } catch (JSONException e) { e.printStackTrace(); } diff --git a/README.md b/README.md index 07ec11528..cb1c69a61 100644 --- a/README.md +++ b/README.md @@ -377,3 +377,58 @@ branch.getReferralCode("BRANCH", 5, expirationDate, "default", REFERRAL_CODE_AWA } }); ``` + +### Validate referral code + +Validate if a referral code exists in Branch system and is still valid (not expired). If valid, return the referral code JSONObject in the call back. + +**code** _String_ +: The referral code to validate + +```java +Branch branch = Branch.getInstance(getApplicationContext()); +branch.validateReferralCode(referral_code, new BranchReferralInitListener() { + @Override + public void onInitFinished(JSONObject referralCode) { + try { + if (!referralCode.has("error_message")) { // will change to using a second callback parameter for error code soon! + String code = referralCode.getString("referral_code"); + if (referral_code.equals(code)) { + // valid + } else { + // invalid (should never happen) + } + } else { + // invalid + } + } catch (JSONException e) { + e.printStackTrace(); + } + } +}); +``` + +### Apply referral code + +Apply a referral code if it exists in Branch system and is still valid (not expired). If the code is valid, return the referral code JSONObject in the call back. + +**code** _String_ +: The referral code to apply + +```java +Branch branch = Branch.getInstance(getApplicationContext()); +branch.applyReferralCode(referral_code, new BranchReferralInitListener() { + @Override + public void onInitFinished(JSONObject referralCode) { + try { + if (!referralCode.has("error_message")) { + // applied + } else { + // invalid code + } + } catch (JSONException e) { + e.printStackTrace(); + } + } +}); +```