Skip to content

Commit

Permalink
Referral code changes actually.
Browse files Browse the repository at this point in the history
  • Loading branch information
qinweigong committed Dec 3, 2014
1 parent 2fa2c05 commit 040b516
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Branch-SDK-TestBed/project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
45 changes: 32 additions & 13 deletions Branch-SDK-TestBed/src/io/branch/testbed/ReferralCodeActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
});
Expand All @@ -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");
}
}
});
Expand All @@ -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");
}
}
});
Expand Down
2 changes: 1 addition & 1 deletion Branch-SDK/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="19" />
android:targetSdkVersion="20" />
</manifest>
2 changes: 1 addition & 1 deletion Branch-SDK/project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
29 changes: 19 additions & 10 deletions Branch-SDK/src/io/branch/referral/Branch.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand All @@ -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();
}
Expand Down
55 changes: 55 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
});
```

0 comments on commit 040b516

Please sign in to comment.