Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 218b8fd

Browse files
committed
Changed the way the SDK works so it doesn't break continuity in
third-party apps
1 parent 378e9a3 commit 218b8fd

File tree

2 files changed

+50
-35
lines changed

2 files changed

+50
-35
lines changed

README.md

Lines changed: 47 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,59 +3,72 @@ Use this SDK to easily add Venmo payments to your Android app! Just follow thes
33
1) First you need to register your app with Venmo here: https://venmo.com/account/app/new (for a description on what these fields are,
44
go here: https://venmo.com/api#registering-an-application). After your app has been approved, you will need to get your app credentials here: https://venmo.com/account/settings/developers.
55

6-
2) Download the two files listed in this repository, and include them in your directory: VenmoSDK.java and VenmoResponse.java. You'll need to add your package name at the top of each file (e.g. "package com.name;")
6+
2) Download the two following two files and include them in your directory: VenmoSDK.java and VenmoWebViewActivity. You'll need to edit your manifest to add VenmoWebViewActivity as an activity, and you'll need to add your package name at the top of each file (e.g. "package com.name;").
77

8-
3) You'll also need to download json_simple-1.1.jar from here: http://code.google.com/p/json-simple/. Add this to your libs directory (create this folder if it doesn't already exist).
8+
3) Download venmo_webview.xml and add it to your res/layout directory.
99

10-
4) You need to add that json jar to your class path. To do so, in Eclipse go to Project -> Properties, and then click "Java Build Path" on the left. Click the Libraries tab at the top. Click "Add Jar" and then find the .jar file you just put in your libs directory. Select it and click "OK". See screenshot.png for a screenshot of this.
10+
4) You'll also need to download json_simple-1.1.jar from here: http://code.google.com/p/json-simple/. Add this to your libs directory (create this folder if it doesn't already exist). You'll need to add that json jar to your class path. To do so, in Eclipse go to Project -> Properties, and then click "Java Build Path" on the left. Click the Libraries tab at the top. Click "Add Jar" and then find the .jar file you just put in your libs directory. Select it and click "OK". See screenshot.png for a screenshot of this.
1111

12-
5) Now, you're ready to use the SDK! From your app, include the following code when you want the Venmo app to open:
12+
5) Now, you're ready to use the SDK! From the activity in your app where you want to open the Venmo app, include the following code:
1313

14-
Intent sendIntent = VenmoSDK.openVenmoPayment(app_id, local_app_id, app_name, recipient, amount, note, txn);
15-
try{
16-
myListActivity.startActivity(sendIntent);
14+
15+
16+
try {
17+
Intent venmoIntent = VenmoSDK.openVenmoPayment(app_id, app_name, recipient, amount, note, txn);
18+
myListActivity.startActivityForResult(venmoIntent, 1); //1 is the requestCode we are using for Venmo. Feel free to change this to another number.
1719
}
18-
catch (ActivityNotFoundException e) // Exception thrown when Venmo native app not install on device, so fallback to web version
20+
catch (ActivityNotFoundException e) //Venmo native app not install on device, so let's instead open a mobile web version of Venmo in a webview
1921
{
20-
sendIntent = VenmoSDK.openVenmoPaymentInBrowser(app_id, local_app_id, app_name, recipient, amount, note, txn);
21-
myListActivity.startActivity(sendIntent);
22+
Intent venmoIntent = new Intent(MainActivity.this, VenmoWebViewActivity.class);
23+
String venmo_uri = VenmoSDK.openVenmoPaymentInWebView(app_id, app_name, recipients, amount, note, txn);
24+
venmoIntent.putExtra("url", venmo_uri);
25+
startActivityForResult(venmoIntent, 1);
2226
}
2327

2428
where all of these parameters are Strings:
2529

2630
* app_id is the app_id you have registered with venmo.com
27-
* app_local_id is something you make up. An example is "abcd".
2831
* app_name is the name of your app
2932
* recipient is the venmo username, phone number, or email address of the person who is being paid or charged
3033
* amount is the amount to be paid or charged
3134
* note is the note that will be sent with the payment/charge. For example, the note might be "for a drink on me!"
3235
* txn is either "pay" or "charge"
3336

37+
This will open the Venmo app's pay/charge screen if the user has the Venmo app installed on the phone. If they don't have it installed, it will instead send them to the activity you added - VenmoWebViewActivity - which displays a mobile web version of Venmo in a webview. This will allow the user to enter his credit card information and complete the transaction.
38+
39+
6) If you look at the previous step, you'll see that the Venmo activity that allows the transaction to be completed is opened using the "startActivityForResult" method, which means that once the activity is finished, control will be yielded back to your activity. To handle the response (i.e. to know whether the payment was completed successfully), implement Android's onActivityResult method in the same activity where you wrote the code in step 5. This will look like the following:
40+
41+
@Override
42+
protected void onActivityResult(int requestCode, int resultCode, Intent data)
43+
{
44+
switch(requestCode) {
45+
case 1: { //1 is the requestCode we picked for Venmo earlier when we called startActivityForResult
46+
if(resultCode == RESULT_OK) {
47+
String signedrequest = data.getStringExtra("signedrequest");
48+
if(signedrequest != null) {
49+
VenmoResponse response = (new VenmoSDK()).validateVenmoPaymentResponse(signedrequest, app_secret);
50+
if(response.getSuccess().equals("1")) {
51+
//Payment successful. Use data from response object to display a success message
52+
String note = response.getNote();
53+
String amount = response.getAmount();
54+
}
55+
}
56+
else {
57+
String error_message = data.getStringExtra("error_message");
58+
//An error ocurred. Make sure to display the error_message to the user
59+
}
60+
}
61+
else if(resultCode == RESULT_CANCELED) {
62+
//The user cancelled the payment
63+
}
64+
break;
65+
}
66+
}
67+
}
3468

35-
Then, you need to provide a way for the Venmo app to be able to get back to your app after the request goes through. Here's how:
36-
37-
6) Add this to your manifest file, inside of your <application> </application> tags:
38-
39-
<activity android:name=".URLActivity">
40-
<intent-filter>
41-
<action android:name="android.intent.action.VIEW" />
42-
<category android:name="android.intent.category.DEFAULT" />
43-
<category android:name="android.intent.category.BROWSABLE" />
44-
<data android:scheme="venmo9999abcd" />
45-
</intent-filter>
46-
</activity>
47-
48-
where 9999 is your app_id and abcd is the app_local_id you created above.
49-
50-
7) Create a file named URLActivity.java, which should extend Activity. This is the activity that is called when control is given back to your app, after a payment has gone through and the user pressed "back to your app". Inside of the onCreate method, include the following:
51-
52-
Uri data = getIntent().getData();
53-
String signed_request = data.getQueryParameter("signed_request");
54-
VenmoResponse response = VenmoSDK.validateVenmoPaymentResponse(signed_request, app_secret);
55-
56-
where app_secret is the secret key you were given when you registered your app with Venmo.
69+
Note that we're using 1 as the requestCode for Venmo.
5770

58-
If you want to display the results of the transaction, then the response variable contains public methods you can use to access these variables:
71+
Make sure you display the results of the transaction after it is completed. The response variable above contains public methods you can use to access these variables:
5972

6073
* response.getSuccess()
6174
* response.getNote()

VenmoSDK.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ public static Intent openVenmoPayment(String myAppId, String myAppLocalId, Strin
8080
Log.e("venmodemo", "cannot encode app local id");
8181
}
8282

83-
83+
84+
venmo_uri += "&using_new_sdk=true";
85+
8486
Log.d("VenmoSDK", "URI: " + venmo_uri);
8587

8688
venmo_uri = venmo_uri.replaceAll("\\+", "%20"); // use %20 encoding instead of +

0 commit comments

Comments
 (0)