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

Skip to content

Commit e12646c

Browse files
committed
Modified how the SDK works so that it doesn't break continuity in
third-party apps.
1 parent 218b8fd commit e12646c

File tree

4 files changed

+221
-120
lines changed

4 files changed

+221
-120
lines changed

VenmoResponse.java

Lines changed: 0 additions & 38 deletions
This file was deleted.

VenmoSDK.java

Lines changed: 116 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,33 @@
1-
//insert package name here
1+
package com.venmo.demo; //Replace this with the name of your package
22

33
import java.io.*;
4-
import java.util.*;
54
import java.net.URLEncoder;
65

7-
import org.json.*;
6+
import org.json.simple.JSONArray;
7+
import org.json.simple.JSONObject;
88
import org.json.simple.JSONValue;
99

1010
import javax.crypto.Mac;
1111
import javax.crypto.spec.SecretKeySpec;
1212

13-
import android.app.Activity;
14-
import android.content.ActivityNotFoundException;
1513
import android.content.Intent;
1614
import android.net.Uri;
17-
import android.os.Bundle;
1815
import android.util.Base64;
1916
import android.util.Log;
20-
import android.view.View;
21-
import android.view.View.OnClickListener;
22-
import android.widget.Button;
23-
import android.widget.CheckBox;
24-
import android.widget.EditText;
2517

2618
public class VenmoSDK
2719
{
28-
20+
public VenmoSDK()
21+
{
22+
23+
}
2924
/*
3025
* Takes the recipients, amount, and note, and returns an Intent object
31-
*/
32-
public static Intent openVenmoPayment(String myAppId, String myAppLocalId, String myAppName, String recipients, String amount, String note, String txn)
26+
*/
27+
public static Intent openVenmoPayment(String myAppId, String myAppName, String recipients, String amount, String note, String txn)
3328
{
34-
String venmo_uri = "venmo://paycharge?txn=" + txn;
35-
29+
String venmo_uri = "venmosdk://paycharge?txn=" + txn;
30+
3631

3732
if (!recipients.equals("")) {
3833
try {
@@ -73,16 +68,15 @@ public static Intent openVenmoPayment(String myAppId, String myAppLocalId, Strin
7368
}
7469

7570
try {
76-
venmo_uri+= "&app_local_id=" + URLEncoder.encode(myAppLocalId, "UTF-8");
71+
venmo_uri+= "&app_local_id=" + URLEncoder.encode("abcd", "UTF-8");
7772
}
7873
catch(UnsupportedEncodingException e)
7974
{
8075
Log.e("venmodemo", "cannot encode app local id");
8176
}
8277

83-
84-
venmo_uri += "&using_new_sdk=true";
85-
78+
venmo_uri += "&using_new_sdk=true";
79+
8680
Log.d("VenmoSDK", "URI: " + venmo_uri);
8781

8882
venmo_uri = venmo_uri.replaceAll("\\+", "%20"); // use %20 encoding instead of +
@@ -96,12 +90,12 @@ public static Intent openVenmoPayment(String myAppId, String myAppLocalId, Strin
9690

9791

9892
/*
99-
* Takes the recipients, amount, and note, and returns an Intent object
93+
* Takes the recipients, amount, and note, and returns a String representing the URL to visit to complete the transaction
10094
*/
101-
public static Intent openVenmoPaymentInBrowser(String myAppId, String myAppLocalId, String myAppName, String recipients, String amount, String note, String txn)
95+
public static String openVenmoPaymentInWebView(String myAppId, String myAppName, String recipients, String amount, String note, String txn)
10296
{
10397
String venmo_uri = "https://venmo.com/touch/signup_to_pay?txn=" + txn;
104-
98+
10599
if (!recipients.equals("")) {
106100
try {
107101
venmo_uri += "&recipients=" + URLEncoder.encode(recipients, "UTF-8");
@@ -141,7 +135,7 @@ public static Intent openVenmoPaymentInBrowser(String myAppId, String myAppLocal
141135
}
142136

143137
try {
144-
venmo_uri+= "&app_local_id=" + URLEncoder.encode(myAppLocalId, "UTF-8");
138+
venmo_uri+= "&app_local_id=" + URLEncoder.encode("abcd", "UTF-8");
145139
}
146140
catch(UnsupportedEncodingException e)
147141
{
@@ -161,76 +155,83 @@ public static Intent openVenmoPaymentInBrowser(String myAppId, String myAppLocal
161155

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

164-
Intent nativeIntent= new Intent(Intent.ACTION_VIEW, Uri.parse(venmo_uri));
165-
166-
return nativeIntent;
158+
return venmo_uri;
167159
}
168160

169-
170-
public static VenmoResponse validateVenmoPaymentResponse(String signed_payload, String app_secret)
161+
//Called once control has been given back to your app - it takes the signed_payload, decodes it, and gives you the response object which
162+
//gives you details about the transaction - whether it was successful, the note, the amount, etc.
163+
public VenmoResponse validateVenmoPaymentResponse(String signed_payload, String app_secret)
171164
{
172-
String[] encodedsig_payload_array = signed_payload.split("\\.");
173-
String encoded_signature = encodedsig_payload_array[0];
174-
String payload = encodedsig_payload_array[1];
175-
String decoded_signature = base64_url_decode(encoded_signature);
165+
String encoded_signature;
166+
String payload;
167+
if(signed_payload == null) {
168+
VenmoResponse myVenmoResponse = new VenmoResponse(null, null, null, "0");
169+
return myVenmoResponse;
170+
}
171+
try {
172+
String[] encodedsig_payload_array = signed_payload.split("\\.");
173+
encoded_signature = encodedsig_payload_array[0];
174+
payload = encodedsig_payload_array[1];
175+
}
176+
catch(ArrayIndexOutOfBoundsException e) {
177+
VenmoResponse myVenmoResponse = new VenmoResponse(null, null, null, "0");
178+
return myVenmoResponse;
179+
}
176180

181+
String decoded_signature = base64_url_decode(encoded_signature);
177182
Log.d("VenmoSDK", "decoded_signature: " + decoded_signature);
178183

179184
String data;
180185

181-
// check signature
182-
String expected_sig = hash_hmac(payload, app_secret, "HmacSHA256");
183-
184-
Log.d("VenmoSDK", "expected_sig using HmacSHA256:" + expected_sig);
185-
186-
VenmoResponse myVenmoResponse;
187-
188-
if (decoded_signature.equals(expected_sig))
189-
{
190-
Log.d("VenmoSDK", "Signature matches!");
191-
data = base64_url_decode(payload);
192-
Log.v("VenmoSDK", "base64 decoded payload: " + data);
193-
//need to json decode data
194-
data = base64_url_decode(payload);
195-
Log.v("VenmoSDK", "base64 decoded payload: " + data);
196-
197-
198-
//need to json decode data
199-
try
200-
{
201-
JSONArray rawJSON = new JSONArray(data);
202-
203-
JSONObject obj = (JSONObject)rawJSON.get(0);
204-
205-
206-
String payment_id = obj.get("payment_id").toString();
207-
String note = obj.get("note").toString();
208-
String amount = obj.get("amount").toString();
209-
String success = obj.get("success").toString();
210-
211-
myVenmoResponse = new VenmoResponse(payment_id, note, amount, success);
212-
213-
}
214-
catch(Exception e)
215-
{
216-
Log.d("VenmoSDK", "Exception caught, setting venmo response to null: " + e.getMessage());
217-
myVenmoResponse = new VenmoResponse(null, null, null, "0");
218-
}
219-
}
220-
else
221-
{
222-
Log.d("VenmoSDK", "Signature does NOT match");
223-
myVenmoResponse = new VenmoResponse(null, null, null, "0");
224-
}
186+
// check signature
187+
String expected_sig = hash_hmac(payload, app_secret, "HmacSHA256");
188+
189+
Log.d("VenmoSDK", "expected_sig using HmacSHA256:" + expected_sig);
190+
191+
VenmoResponse myVenmoResponse;
192+
193+
if (decoded_signature.equals(expected_sig))
194+
{
195+
Log.d("VenmoSDK", "Signature matches!");
196+
data = base64_url_decode(payload);
197+
Log.v("VenmoSDK", "base64 decoded payload: " + data);
198+
//need to json decode data
199+
data = base64_url_decode(payload);
200+
Log.v("VenmoSDK", "base64 decoded payload: " + data);
201+
202+
203+
//need to json decode data
204+
try
205+
{
206+
JSONArray response = (JSONArray)JSONValue.parse(data);
207+
208+
JSONObject obj = (JSONObject)response.get(0);
209+
210+
String payment_id = obj.get("payment_id").toString();
211+
String note = obj.get("note").toString();
212+
String amount = obj.get("amount").toString();
213+
String success = obj.get("success").toString();
214+
215+
myVenmoResponse = new VenmoResponse(payment_id, note, amount, success);
216+
217+
}
218+
catch(Exception e)
219+
{
220+
Log.d("VenmoSDK", "Exception caught: " + e.getMessage());
221+
myVenmoResponse = new VenmoResponse(null, null, null, "0");
222+
}
223+
}
224+
else
225+
{
226+
Log.d("VenmoSDK", "Signature does NOT match");
227+
myVenmoResponse = new VenmoResponse(null, null, null, "0");
228+
}
225229

226-
Log.v("VenmoSDK", "venmo response note: " + myVenmoResponse.getNote() + ", amount: " + myVenmoResponse.getAmount() + ", success: " + myVenmoResponse.getSuccess());
227-
return myVenmoResponse;
230+
return myVenmoResponse;
228231

229232
}
230233

231234

232-
233-
234235
private static String hash_hmac(String payload, String app_secret, String algorithm)
235236
{
236237
try
@@ -257,4 +258,37 @@ private static String base64_url_decode(String payload)
257258

258259
return jsonString;
259260
}
260-
}
261+
262+
263+
//This is the object returned to you after a transaction has gone through.
264+
//It tells you whether it was successful, the amount, te note, and the payment id.
265+
public class VenmoResponse
266+
{
267+
private String payment_id, note, amount, success;
268+
public VenmoResponse(String payment_id, String note, String amount, String success)
269+
{
270+
this.payment_id = payment_id;
271+
this.note = note;
272+
this.amount= amount;
273+
this.success = success;
274+
}
275+
public String getPaymentId()
276+
{
277+
return payment_id;
278+
}
279+
public String getNote()
280+
{
281+
return note;
282+
}
283+
public String getAmount()
284+
{
285+
return amount;
286+
}
287+
public String getSuccess()
288+
{
289+
return success;
290+
}
291+
}
292+
293+
294+
}

0 commit comments

Comments
 (0)