Skip to content

Commit

Permalink
Add suggested changes as per review
Browse files Browse the repository at this point in the history
  • Loading branch information
akshaybhalotia committed Oct 25, 2016
1 parent 21ee2ae commit 91ea1b7
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 43 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ link iOS SDK as explained in the previous section:
1. Import RazorpayCheckout module to your component:
```js
import { RazorpayCheckout } from 'react-native-razorpay';
import RazorpayCheckout from 'react-native-razorpay';
```
2. Call RazorpayCheckout's `open` method with `options` (preferably on a user
action) as a **JS Promise**. The `then` part corresponds to a successful payment,
2. Call `RazorpayCheckout.open` method with the payment `options`. The method
returns a **JS Promise** where `then` part corresponds to a successful payment
and the `catch` part corresponds to payment failure.
```js
<TouchableHighlight onPress={() => {
Expand Down
17 changes: 8 additions & 9 deletions RazorpayCheckout.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,27 @@ import { NativeModules, NativeEventEmitter } from 'react-native';

const razorpayEvents = new NativeEventEmitter(NativeModules.RazorpayEventEmitter);

class RazorpayCheckout {
const removeSubscriptions = () => {
razorpayEvents.removeAllListeners('Razorpay::PAYMENT_SUCCESS');
razorpayEvents.removeAllListeners('Razorpay::PAYMENT_ERROR');
};

class RazorpayCheckout {
static open(options, successCallback, errorCallback) {
return new Promise(function(resolve, reject) {
let removeSubscriptions = () => {
razorpayEvents.removeAllListeners('Razorpay::onPaymentSuccess');
razorpayEvents.removeAllListeners('Razorpay::onPaymentError');
};
razorpayEvents.addListener('Razorpay::onPaymentSuccess', (data) => {
razorpayEvents.addListener('Razorpay::PAYMENT_SUCCESS', (data) => {
let resolveFn = successCallback || resolve;
resolveFn(data);
removeSubscriptions();
});
razorpayEvents.addListener('Razorpay::onPaymentError', (data) => {
razorpayEvents.addListener('Razorpay::PAYMENT_ERROR', (data) => {
let rejectFn = errorCallback || reject;
rejectFn(data);
removeSubscriptions();
});
NativeModules.RazorpayCheckout.open(options);
});
}

}

export {RazorpayCheckout};
export default RazorpayCheckout;
4 changes: 2 additions & 2 deletions android/src/main/java/com/razorpay/rn/RazorpayModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public void onActivityResult(int requestCode, int resultCode, Intent data){
JSONObject resultJson = new JSONObject(result);
WritableMap successParams = Arguments.createMap();
successParams.putString(MAP_KEY_PAYMENT_ID, resultJson.getString(MAP_KEY_RZP_PAYMENT_ID));
sendEvent("Razorpay::onPaymentSuccess", successParams);
sendEvent("Razorpay::PAYMENT_SUCCESS", successParams);
} catch(Exception e){}
}
else {
Expand All @@ -133,7 +133,7 @@ public void onActivityResult(int requestCode, int resultCode, Intent data){
WritableMap errorParams = Arguments.createMap();
errorParams.putInt(MAP_KEY_ERROR_CODE, resultCode);
errorParams.putString(MAP_KEY_ERROR_DESC, result);
sendEvent("Razorpay::onPaymentError", errorParams);
sendEvent("Razorpay::PAYMENT_ERROR", errorParams);
}
}

Expand Down
54 changes: 28 additions & 26 deletions example/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/

import React, { Component } from 'react';
import {
Expand All @@ -15,37 +15,39 @@ import {
NativeEventEmitter
} from 'react-native';

import { RazorpayCheckout } from 'react-native-razorpay';
import RazorpayCheckout from 'react-native-razorpay';

class example extends Component {

render() {
return (
<View style={styles.container}>
<TouchableHighlight onPress={() => {
<TouchableHighlight onPress={() => {
var options = {
description: 'Credits towards consultation',
image: 'https://i.imgur.com/3g7nmJC.png',
currency: 'INR',
key: 'rzp_test_1DP5mmOlF5G5ag',
amount: '5000',
name: 'foo',
prefill: {
email: '[email protected]',
contact: '8955806560',
name: 'Akshay Bhalotia'
},
theme: {color: '#F37254'}
description: 'Credits towards consultation',
image: 'https://i.imgur.com/3g7nmJC.png',
currency: 'INR',
key: 'rzp_test_1DP5mmOlF5G5ag',
amount: '5000',
name: 'foo',
prefill: {
email: '[email protected]',
contact: '8955806560',
name: 'Akshay Bhalotia'
},
theme: {color: '#F37254'}
}
RazorpayCheckout.open(options).then(data =>
{ alert("Success: " + data.payment_id) }
).catch(data =>
{ alert("Error: " + data.code + " | " + data.description) }
);
}}>
RazorpayCheckout.open(options).then((data) => {
// handle success
alert(`Success: ${data.payment_id}`);
}).catch((error) => {
// handle failure
alert(`Error: ${error.code} | ${error.description}`);
});
}}>
<Text style = {styles.text}>Pay</Text>
</TouchableHighlight>
</View>
</TouchableHighlight>
</View>
);
}

Expand Down
6 changes: 3 additions & 3 deletions ios/RazorpayEventEmitter.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ @implementation RazorpayEventEmitter
RCT_EXPORT_MODULE();

- (NSArray<NSString *> *)supportedEvents {
return @[ @"Razorpay::onPaymentSuccess", @"Razorpay::onPaymentError" ];
return @[ @"Razorpay::PAYMENT_SUCCESS", @"Razorpay::PAYMENT_ERROR" ];
}

- (void)startObserving {
Expand All @@ -38,12 +38,12 @@ - (void)stopObserving {
}

- (void)paymentSuccess:(NSNotification *)notification {
[self sendEventWithName:@"Razorpay::onPaymentSuccess"
[self sendEventWithName:@"Razorpay::PAYMENT_SUCCESS"
body:notification.userInfo];
}

- (void)paymentError:(NSNotification *)notification {
[self sendEventWithName:@"Razorpay::onPaymentError"
[self sendEventWithName:@"Razorpay::PAYMENT_ERROR"
body:notification.userInfo];
}

Expand Down

0 comments on commit 91ea1b7

Please sign in to comment.