Skip to content

Commit

Permalink
Merge pull request #13 from razorpay/f/features
Browse files Browse the repository at this point in the history
[Urgent] Orders API, External Wallet Support
  • Loading branch information
pronav authored Mar 7, 2017
2 parents a6ccad4 + cf74ef6 commit 857e506
Show file tree
Hide file tree
Showing 17 changed files with 325 additions and 75 deletions.
7 changes: 7 additions & 0 deletions RazorpayCheckout.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const razorpayEvents = new NativeEventEmitter(NativeModules.RazorpayEventEmitter
const removeSubscriptions = () => {
razorpayEvents.removeAllListeners('Razorpay::PAYMENT_SUCCESS');
razorpayEvents.removeAllListeners('Razorpay::PAYMENT_ERROR');
razorpayEvents.removeAllListeners('Razorpay::EXTERNAL_WALLET_SELECTED');
};

class RazorpayCheckout {
Expand All @@ -25,6 +26,12 @@ class RazorpayCheckout {
NativeModules.RazorpayCheckout.open(options);
});
}
static onExternalWalletSelection(externalWalletCallback) {
razorpayEvents.addListener('Razorpay::EXTERNAL_WALLET_SELECTED', (data) => {
externalWalletCallback(data);
removeSubscriptions();
});
}
}

export default RazorpayCheckout;
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;
}
}
10 changes: 8 additions & 2 deletions example/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ class example extends Component {
currency: 'INR',
key: 'rzp_test_1DP5mmOlF5G5ag',
amount: '5000',
external: {
wallets: ['paytm']
},
name: 'foo',
prefill: {
email: '[email protected]',
Expand All @@ -39,11 +42,14 @@ class example extends Component {
}
RazorpayCheckout.open(options).then((data) => {
// handle success
alert(`Success: ${data.payment_id}`);
alert(`Success: ${data.razorpay_payment_id}`);
}).catch((error) => {
// handle failure
alert(`Error: ${error.code} | ${error.description}`);
});
RazorpayCheckout.onExternalWalletSelection(data => {
alert(`External Wallet Selected: ${data.name} | ${data.details}`);
});
}}>
<Text style = {styles.text}>Pay</Text>
</TouchableHighlight>
Expand All @@ -66,4 +72,4 @@ const styles = StyleSheet.create({
}
});

AppRegistry.registerComponent('example', () => example);
AppRegistry.registerComponent('example', () => example);
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// ExternalWalletSelectionProtocol.h
// Razorpay
//
// Created by Akshay Bhalotia on 15/11/16.
// Copyright © 2016 Razorpay. All rights reserved.
//

#import <Foundation/Foundation.h>

@protocol ExternalWalletSelectionProtocol <NSObject>

- (void)onExternalWalletSelected:(nonnull NSString *)walletName
WithPaymentData:(nullable NSDictionary *)paymentData;

@end
14 changes: 13 additions & 1 deletion example/ios/Razorpay.framework/Versions/A/Headers/Razorpay.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,31 @@
// Copyright © 2016 Razorpay. All rights reserved.
//

#import "ExternalWalletSelectionProtocol.h"
#import "RazorpayPaymentCompletionProtocol.h"
#import "RazorpayPaymentCompletionProtocolWithData.h"
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface Razorpay : NSObject

/*!
@deprecated This method is deprecated. Use initWithKey:andDelegate: instead.
See https://docs.razorpay.com/docs/ios for more information.
*/
+ (nonnull instancetype)
initWithKey:(nonnull NSString *)key
andDelegate:(nonnull id<RazorpayPaymentCompletionProtocol>)delegate
forViewController:(nullable UIViewController *)vc;
forViewController:(nullable UIViewController *)vc __attribute__((deprecated));
+ (nonnull instancetype)
initWithKey:(nonnull NSString *)key
andDelegate:(nonnull id<RazorpayPaymentCompletionProtocol>)delegate;
+ (nonnull instancetype)initWithKey:(nonnull NSString *)key
andDelegateWithData:
(nonnull id<RazorpayPaymentCompletionProtocolWithData>)
delegate;
- (void)setExternalWalletSelectionDelegate:
(nonnull id<ExternalWalletSelectionProtocol>)walletDelegate;
- (void)open:(nonnull NSDictionary *)options;
- (void)close;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// RazorpayPaymentCompletionProtocolWithData.h
// Razorpay
//
// Created by Akshay Bhalotia on 27/09/16.
// Copyright © 2016 Razorpay. All rights reserved.
//

#import <Foundation/Foundation.h>

@protocol RazorpayPaymentCompletionProtocolWithData <NSObject>

- (void)onPaymentError:(int)code
description:(nonnull NSString *)str
andData:(nullable NSDictionary *)response;
- (void)onPaymentSuccess:(nonnull NSString *)payment_id
andData:(nullable NSDictionary *)response;

@end
Binary file modified example/ios/Razorpay.framework/Versions/A/Razorpay
Binary file not shown.
Loading

0 comments on commit 857e506

Please sign in to comment.