-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from razorpay/f/promise_based_api
Promise based API
- Loading branch information
Showing
6 changed files
with
78 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.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={() => { | ||
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) } | ||
); | ||
}}> | ||
``` | ||
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
|
@@ -15,47 +15,42 @@ 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'; | ||
|
||
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={() => { | ||
<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'}, | ||
prefill: { | ||
email: '[email protected]', | ||
contact: '8955806560', | ||
name: 'Akshay Bhalotia' | ||
}, | ||
theme: {color: '#F37254'} | ||
} | ||
RazorpayCheckout.open(options) | ||
}}> | ||
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> | ||
); | ||
} | ||
|
||
componentWillUnmount () { | ||
razorpayEvents.remove(); | ||
} | ||
|
||
} | ||
|
||
const styles = StyleSheet.create({ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters