1
- //insert package name here
1
+ package com . venmo . demo ; //Replace this with the name of your package
2
2
3
3
import java .io .*;
4
- import java .util .*;
5
4
import java .net .URLEncoder ;
6
5
7
- import org .json .*;
6
+ import org .json .simple .JSONArray ;
7
+ import org .json .simple .JSONObject ;
8
8
import org .json .simple .JSONValue ;
9
9
10
10
import javax .crypto .Mac ;
11
11
import javax .crypto .spec .SecretKeySpec ;
12
12
13
- import android .app .Activity ;
14
- import android .content .ActivityNotFoundException ;
15
13
import android .content .Intent ;
16
14
import android .net .Uri ;
17
- import android .os .Bundle ;
18
15
import android .util .Base64 ;
19
16
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 ;
25
17
26
18
public class VenmoSDK
27
19
{
28
-
20
+ public VenmoSDK ()
21
+ {
22
+
23
+ }
29
24
/*
30
25
* 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 )
33
28
{
34
- String venmo_uri = "venmo ://paycharge?txn=" + txn ;
35
-
29
+ String venmo_uri = "venmosdk ://paycharge?txn=" + txn ;
30
+
36
31
37
32
if (!recipients .equals ("" )) {
38
33
try {
@@ -73,16 +68,15 @@ public static Intent openVenmoPayment(String myAppId, String myAppLocalId, Strin
73
68
}
74
69
75
70
try {
76
- venmo_uri += "&app_local_id=" + URLEncoder .encode (myAppLocalId , "UTF-8" );
71
+ venmo_uri += "&app_local_id=" + URLEncoder .encode ("abcd" , "UTF-8" );
77
72
}
78
73
catch (UnsupportedEncodingException e )
79
74
{
80
75
Log .e ("venmodemo" , "cannot encode app local id" );
81
76
}
82
77
83
-
84
- venmo_uri += "&using_new_sdk=true" ;
85
-
78
+ venmo_uri += "&using_new_sdk=true" ;
79
+
86
80
Log .d ("VenmoSDK" , "URI: " + venmo_uri );
87
81
88
82
venmo_uri = venmo_uri .replaceAll ("\\ +" , "%20" ); // use %20 encoding instead of +
@@ -96,12 +90,12 @@ public static Intent openVenmoPayment(String myAppId, String myAppLocalId, Strin
96
90
97
91
98
92
/*
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
100
94
*/
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 )
102
96
{
103
97
String venmo_uri = "https://venmo.com/touch/signup_to_pay?txn=" + txn ;
104
-
98
+
105
99
if (!recipients .equals ("" )) {
106
100
try {
107
101
venmo_uri += "&recipients=" + URLEncoder .encode (recipients , "UTF-8" );
@@ -141,7 +135,7 @@ public static Intent openVenmoPaymentInBrowser(String myAppId, String myAppLocal
141
135
}
142
136
143
137
try {
144
- venmo_uri += "&app_local_id=" + URLEncoder .encode (myAppLocalId , "UTF-8" );
138
+ venmo_uri += "&app_local_id=" + URLEncoder .encode ("abcd" , "UTF-8" );
145
139
}
146
140
catch (UnsupportedEncodingException e )
147
141
{
@@ -161,76 +155,83 @@ public static Intent openVenmoPaymentInBrowser(String myAppId, String myAppLocal
161
155
162
156
venmo_uri = venmo_uri .replaceAll ("\\ +" , "%20" ); // use %20 encoding instead of +
163
157
164
- Intent nativeIntent = new Intent (Intent .ACTION_VIEW , Uri .parse (venmo_uri ));
165
-
166
- return nativeIntent ;
158
+ return venmo_uri ;
167
159
}
168
160
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 )
171
164
{
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
+ }
176
180
181
+ String decoded_signature = base64_url_decode (encoded_signature );
177
182
Log .d ("VenmoSDK" , "decoded_signature: " + decoded_signature );
178
183
179
184
String data ;
180
185
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
+ }
225
229
226
- Log .v ("VenmoSDK" , "venmo response note: " + myVenmoResponse .getNote () + ", amount: " + myVenmoResponse .getAmount () + ", success: " + myVenmoResponse .getSuccess ());
227
- return myVenmoResponse ;
230
+ return myVenmoResponse ;
228
231
229
232
}
230
233
231
234
232
-
233
-
234
235
private static String hash_hmac (String payload , String app_secret , String algorithm )
235
236
{
236
237
try
@@ -257,4 +258,37 @@ private static String base64_url_decode(String payload)
257
258
258
259
return jsonString ;
259
260
}
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