Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[android] Add support for external wallets and orders API flow #12

Merged
merged 1 commit into from
Mar 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}

71 changes: 18 additions & 53 deletions android/src/main/java/com/razorpay/rn/RazorpayModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,30 @@
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;





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;
public static final String MAP_KEY_RZP_PAYMENT_ID = "razorpay_payment_id";
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);
Expand All @@ -56,72 +58,24 @@ 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);
}

@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);
Expand All @@ -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);
}

Expand All @@ -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);

}

}
131 changes: 131 additions & 0 deletions android/src/main/java/com/razorpay/rn/Utils.java
Original file line number Diff line number Diff line change
@@ -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;
}
}