From 5a6fa6a2e59c1829df7f39c8cbd1b65a1f1f9711 Mon Sep 17 00:00:00 2001 From: Miroojin Bakshi Date: Tue, 7 Mar 2017 17:09:18 +0530 Subject: [PATCH] [android] Add support for external wallets and orders API flow --- android/build.gradle | 2 +- .../java/com/razorpay/rn/RazorpayModule.java | 71 +++------- .../src/main/java/com/razorpay/rn/Utils.java | 131 ++++++++++++++++++ 3 files changed, 150 insertions(+), 54 deletions(-) create mode 100644 android/src/main/java/com/razorpay/rn/Utils.java diff --git a/android/build.gradle b/android/build.gradle index dc678100f..9b1ade1f7 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -16,6 +16,6 @@ android { dependencies { compile 'com.facebook.react:react-native:0.20.+' - compile 'com.razorpay:checkout:1.2.0' + compile 'com.razorpay:checkout:1.2.2' } diff --git a/android/src/main/java/com/razorpay/rn/RazorpayModule.java b/android/src/main/java/com/razorpay/rn/RazorpayModule.java index 8ec9a9a3a..54cad38fb 100644 --- a/android/src/main/java/com/razorpay/rn/RazorpayModule.java +++ b/android/src/main/java/com/razorpay/rn/RazorpayModule.java @@ -18,13 +18,13 @@ import com.razorpay.CheckoutActivity; import com.razorpay.PaymentData; import com.razorpay.PaymentResultWithDataListener; +import com.razorpay.ExternalWalletListener; import com.razorpay.Checkout; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.util.Iterator; import android.app.Activity; -import javax.annotation.Nullable; import android.content.Intent; import android.os.Bundle; @@ -32,7 +32,7 @@ -public class RazorpayModule extends ReactContextBaseJavaModule implements ActivityEventListener, PaymentResultWithDataListener { +public class RazorpayModule extends ReactContextBaseJavaModule implements ActivityEventListener, PaymentResultWithDataListener , ExternalWalletListener { public static final int RZP_REQUEST_CODE = 72967729; @@ -40,6 +40,8 @@ public class RazorpayModule extends ReactContextBaseJavaModule implements Activi public static final String MAP_KEY_PAYMENT_ID = "payment_id"; public static final String MAP_KEY_ERROR_CODE = "code"; public static final String MAP_KEY_ERROR_DESC = "description"; + public static final String MAP_KEY_PAYMENT_DETAILS = "details"; + public static final String MAP_KEY_WALLET_NAME="name"; ReactApplicationContext reactContext; public RazorpayModule(ReactApplicationContext reactContext) { super(reactContext); @@ -56,61 +58,13 @@ public String getName() { public void open(ReadableMap options) { Activity currentActivity = getCurrentActivity(); try { - JSONObject optionsJSON = readableMapToJson(options); + JSONObject optionsJSON = Utils.readableMapToJson(options); Intent intent = new Intent(currentActivity, CheckoutActivity.class); intent.putExtra("OPTIONS", optionsJSON.toString()); intent.putExtra("FRAMEWORK", "react_native"); currentActivity.startActivityForResult(intent, Checkout.RZP_REQUEST_CODE); } catch (Exception e) {} } - - - @Nullable - private static JSONObject readableMapToJson(ReadableMap readableMap) { - JSONObject jsonObject = new JSONObject(); - - if (readableMap == null) { - return null; - } - - ReadableMapKeySetIterator iterator = readableMap.keySetIterator(); - if (!iterator.hasNextKey()) { - return null; - } - - while (iterator.hasNextKey()) { - String key = iterator.nextKey(); - ReadableType readableType = readableMap.getType(key); - - try { - switch (readableType) { - case Null: - jsonObject.put(key, null); - break; - case Boolean: - jsonObject.put(key, readableMap.getBoolean(key)); - break; - case Number: - // Can be int or double. - jsonObject.put(key, readableMap.getInt(key)); - break; - case String: - jsonObject.put(key, readableMap.getString(key)); - break; - case Map: - jsonObject.put(key, readableMapToJson(readableMap.getMap(key))); - break; - case Array: - jsonObject.put(key, readableMap.getArray(key)); - default: - // Do nothing and fail silently - } - } catch (JSONException ex) { - // Do nothing and fail silently - } - } - return jsonObject; - } public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) { onActivityResult(requestCode, resultCode, data); @@ -118,10 +72,10 @@ public void onActivityResult(Activity activity, int requestCode, int resultCode, @Override public void onActivityResult(int requestCode, int resultCode, Intent data){ - Checkout.handleActivityResult(getCurrentActivity(), requestCode, resultCode, data, this); + Checkout.handleActivityResult(getCurrentActivity(), requestCode, resultCode, data, this, this); } - private void sendEvent(String eventName, @Nullable WritableMap params) { + private void sendEvent(String eventName, WritableMap params) { reactContext .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) .emit(eventName, params); @@ -131,6 +85,7 @@ private void sendEvent(String eventName, @Nullable WritableMap params) { public void onPaymentSuccess(String razorpayPaymentId, PaymentData paymentData) { WritableMap successParams = Arguments.createMap(); successParams.putString(MAP_KEY_PAYMENT_ID, razorpayPaymentId); + successParams.putMap(MAP_KEY_PAYMENT_DETAILS, Utils.jsonToWritableMap(paymentData.getData())); sendEvent("Razorpay::PAYMENT_SUCCESS", successParams); } @@ -139,7 +94,17 @@ public void onPaymentError(int code, String description, PaymentData paymentData WritableMap errorParams = Arguments.createMap(); errorParams.putInt(MAP_KEY_ERROR_CODE, code); errorParams.putString(MAP_KEY_ERROR_DESC, description); + errorParams.putMap(MAP_KEY_PAYMENT_DETAILS, Utils.jsonToWritableMap(paymentData.getData())); sendEvent("Razorpay::PAYMENT_ERROR", errorParams); } + @Override + public void onExternalWalletSelected(String walletName, PaymentData paymentData){ + WritableMap params = Arguments.createMap(); + params.putString(MAP_KEY_WALLET_NAME, walletName); + params.putMap(MAP_KEY_PAYMENT_DETAILS, Utils.jsonToWritableMap(paymentData.getData())); + sendEvent("Razorpay::EXTERNAL_WALLET_SELECTED", params); + + } + } diff --git a/android/src/main/java/com/razorpay/rn/Utils.java b/android/src/main/java/com/razorpay/rn/Utils.java new file mode 100644 index 000000000..09e870838 --- /dev/null +++ b/android/src/main/java/com/razorpay/rn/Utils.java @@ -0,0 +1,131 @@ +package com.razorpay.rn; + +import com.facebook.react.bridge.ReadableArray; +import com.facebook.react.bridge.ReadableMap; +import com.facebook.react.bridge.ReadableMapKeySetIterator; +import com.facebook.react.bridge.WritableArray; +import com.facebook.react.bridge.WritableMap; +import com.facebook.react.bridge.WritableNativeArray; +import com.facebook.react.bridge.WritableNativeMap; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import java.util.Iterator; + +public class Utils { + + public static JSONObject readableMapToJson(ReadableMap readableMap) { + JSONObject object = new JSONObject(); + try { + ReadableMapKeySetIterator iterator = readableMap.keySetIterator(); + while (iterator.hasNextKey()) { + String key = iterator.nextKey(); + switch (readableMap.getType(key)) { + case Null: + object.put(key, JSONObject.NULL); + break; + case Boolean: + object.put(key, readableMap.getBoolean(key)); + break; + case Number: + object.put(key, readableMap.getDouble(key)); + break; + case String: + object.put(key, readableMap.getString(key)); + break; + case Map: + object.put(key, readableMapToJson(readableMap.getMap(key))); + break; + case Array: + object.put(key, readableArrayToJson(readableMap.getArray(key))); + break; + } + } + + } catch(JSONException e){ + + } + return object; + } + + public static JSONArray readableArrayToJson(ReadableArray readableArray) throws JSONException { + JSONArray array = new JSONArray(); + for (int i = 0; i < readableArray.size(); i++) { + switch (readableArray.getType(i)) { + case Null: + break; + case Boolean: + array.put(readableArray.getBoolean(i)); + break; + case Number: + array.put(readableArray.getDouble(i)); + break; + case String: + array.put(readableArray.getString(i)); + break; + case Map: + array.put(readableMapToJson(readableArray.getMap(i))); + break; + case Array: + array.put(readableArrayToJson(readableArray.getArray(i))); + break; + } + } + return array; + } + + public static WritableMap jsonToWritableMap(JSONObject jsonObject) { + WritableMap writableMap = new WritableNativeMap(); + try { + Iterator iterator = jsonObject.keys(); + while(iterator.hasNext()) { + String key = (String) iterator.next(); + Object value = jsonObject.get(key); + if (value instanceof Float || value instanceof Double) { + writableMap.putDouble(key, jsonObject.getDouble(key)); + } else if (value instanceof Number) { + writableMap.putInt(key, jsonObject.getInt(key)); + } else if (value instanceof String) { + writableMap.putString(key, jsonObject.getString(key)); + } else if (value instanceof JSONObject) { + writableMap.putMap(key, jsonToWritableMap(jsonObject.getJSONObject(key))); + } else if (value instanceof JSONArray){ + writableMap.putArray(key, jsonToWritableArray(jsonObject.getJSONArray(key))); + } else if (value == JSONObject.NULL){ + writableMap.putNull(key); + } + } + } catch(JSONException e){ + // Fail silently + } + return writableMap; + } + + public static WritableArray jsonToWritableArray(JSONArray jsonArray) { + WritableArray writableArray = new WritableNativeArray(); + try { + for(int i=0; i < jsonArray.length(); i++) { + Object value = jsonArray.get(i); + if (value instanceof Float || value instanceof Double) { + writableArray.pushDouble(jsonArray.getDouble(i)); + } else if (value instanceof Number) { + writableArray.pushInt(jsonArray.getInt(i)); + } else if (value instanceof String) { + writableArray.pushString(jsonArray.getString(i)); + } else if (value instanceof JSONObject) { + writableArray.pushMap(jsonToWritableMap(jsonArray.getJSONObject(i))); + } else if (value instanceof JSONArray){ + writableArray.pushArray(jsonToWritableArray(jsonArray.getJSONArray(i))); + } else if (value == JSONObject.NULL){ + writableArray.pushNull(); + } + } + } catch(JSONException e){ + // Fail silently + } + + return writableArray; + } +} \ No newline at end of file