-
Notifications
You must be signed in to change notification settings - Fork 107
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
Promise based API #3
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,9 @@ | |
|
||
React Native wrapper around our Android and iOS mobile SDKs | ||
|
||
To know more about Razorpay payment flow and steps involved, please read up here: | ||
<https://docs.razorpay.com/docs> | ||
|
||
## Installation | ||
|
||
Run the following on terminal from your project directory: | ||
|
@@ -83,25 +86,13 @@ link iOS SDK as explained in the previous section: | |
|
||
### Steps | ||
|
||
1. Import Razorpay module to your component: | ||
```js | ||
import { Razorpay } from 'react-native-razorpay'; | ||
const { RazorpayCheckout, RazorpayEventEmitter } = Razorpay; | ||
``` | ||
2. Instantiate an event emitter with `RazorpayEventEmitter`: | ||
```js | ||
const razorpayEvents = new NativeEventEmitter(RazorpayEventEmitter); | ||
``` | ||
3. Add payment event listeners to your component, preferably in `componentWillMount`: | ||
1. Import RazorpayCheckout module to your component: | ||
```js | ||
razorpayEvents.addListener('onPaymentError', (data) => { | ||
alert("Error: " + data.code + " | " + data.description) | ||
}); | ||
razorpayEvents.addListener('onPaymentSuccess', (data) => { | ||
alert("Success: " + data.payment_id) | ||
}); | ||
import { RazorpayCheckout } from 'react-native-razorpay'; | ||
``` | ||
4. Call RazorpayCheckout's `open` method with `options`, preferably on a user action: | ||
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, | ||
and the `catch` part corresponds to payment failure. | ||
```js | ||
<TouchableHighlight onPress={() => { | ||
var options = { | ||
|
@@ -111,17 +102,22 @@ razorpayEvents.addListener('onPaymentSuccess', (data) => { | |
key: 'rzp_test_1DP5mmOlF5G5ag', | ||
amount: '5000', | ||
name: 'foo', | ||
prefill: {email: '[email protected]', contact: '8879524924', name: 'Pranav Gupta'}, | ||
prefill: { | ||
email: '[email protected]', | ||
contact: '8955806560', | ||
name: 'Akshay Bhalotia' | ||
}, | ||
theme: {color: '#F37254'} | ||
} | ||
RazorpayCheckout.open(options) | ||
RazorpayCheckout.open(options).then(data => | ||
{ alert("Success: " + data.payment_id) } | ||
).catch(data => | ||
{ alert("Error: " + data.code + " | " + data.description) } | ||
); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. change it to below style RazorpayCheckout.open(options).then((data) => {
// handle success
alert(`Success: ${data.payment_id}`)
}).catch((error) => {
// handle error
alert(`Error: ${error.code} | ${error.description}`)
}) |
||
}}> | ||
``` | ||
5. Stop listening for payment events, preferably in `componentWillMount`: | ||
```js | ||
razorpayEvents.remove(); | ||
``` | ||
|
||
A descriptive [list of valid options for checkout][options] is available (under | ||
Manual Checkout column). | ||
|
||
## Contributing | ||
|
||
|
@@ -143,3 +139,4 @@ or [contact us][contact] to help you with integrations. | |
[integrations]: https://razorpay.com/integrations "List of our integrations" | ||
[ios-docs]: https://docs.razorpay.com/v1/page/ios-integration "Documentation for the iOS Integration" | ||
[LICENSE]: /LICENSE "MIT License" | ||
[options]: https://docs.razorpay.com/docs/checkout-form#checkout-fields "Checkout Options" |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,30 @@ | |
|
||
import { NativeModules, NativeEventEmitter } from 'react-native'; | ||
|
||
export const Razorpay = { | ||
RazorpayCheckout: NativeModules.RazorpayCheckout, | ||
RazorpayEventEmitter: NativeModules.RazorpayEventEmitter | ||
}; | ||
const razorpayEvents = new NativeEventEmitter(NativeModules.RazorpayEventEmitter); | ||
|
||
class RazorpayCheckout { | ||
|
||
static open(options, successCallback, errorCallback) { | ||
return new Promise(function(resolve, reject) { | ||
let removeSubscriptions = () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. move the function outside There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
razorpayEvents.removeAllListeners('Razorpay::onPaymentSuccess'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
How about |
||
razorpayEvents.removeAllListeners('Razorpay::onPaymentError'); | ||
}; | ||
razorpayEvents.addListener('Razorpay::onPaymentSuccess', (data) => { | ||
let resolveFn = successCallback || resolve; | ||
resolveFn(data); | ||
removeSubscriptions(); | ||
}); | ||
razorpayEvents.addListener('Razorpay::onPaymentError', (data) => { | ||
let rejectFn = errorCallback || reject; | ||
rejectFn(data); | ||
removeSubscriptions(); | ||
}); | ||
NativeModules.RazorpayCheckout.open(options); | ||
}); | ||
} | ||
|
||
} | ||
|
||
export {RazorpayCheckout}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. make it as default export like export default RazorpayCheckout |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,47 +15,40 @@ import { | |
NativeEventEmitter | ||
} from 'react-native'; | ||
|
||
import { Razorpay } from 'react-native-razorpay'; | ||
const { RazorpayCheckout, RazorpayEventEmitter } = Razorpay; | ||
|
||
const razorpayEvents = new NativeEventEmitter(RazorpayEventEmitter); | ||
import { RazorpayCheckout } from 'react-native-razorpay'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. with default export, this will be import RazorpayCheckout from 'react-native-razorpay' |
||
|
||
class example extends Component { | ||
componentWillMount() { | ||
razorpayEvents.addListener('onPaymentError', (data) => { | ||
alert("Error: " + data.code + " | " + data.description) | ||
}); | ||
razorpayEvents.addListener('onPaymentSuccess', (data) => { | ||
alert("Success: " + data.payment_id) | ||
}); | ||
} | ||
|
||
render() { | ||
return ( | ||
<View style={styles.container}> | ||
<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: '8879524924', name: 'Pranav Gupta'}, | ||
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) | ||
RazorpayCheckout.open(options).then(data => | ||
{ alert("Success: " + data.payment_id) } | ||
).catch(data => | ||
{ alert("Error: " + data.code + " | " + data.description) } | ||
); | ||
}}> | ||
<Text style = {styles.text}>Pay</Text> | ||
</TouchableHighlight> | ||
</View> | ||
); | ||
} | ||
|
||
componentWillUnmount () { | ||
razorpayEvents.remove(); | ||
} | ||
|
||
} | ||
|
||
const styles = StyleSheet.create({ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sounds a bit weird. How about the below ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, sounds great. Wasn't sure on terminology around Promises.