Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/a-tokyo/react-native-stripe-checkout-webview
💰 React Native implementation for Stripe.js Checkout.
https://github.com/a-tokyo/react-native-stripe-checkout-webview
apple-pay checkout expo flow online-payments payments react-native react-native-expo react-native-stripe stripe stripe-checkout typescript
Last synced: 3 days ago
JSON representation
💰 React Native implementation for Stripe.js Checkout.
- Host: GitHub
- URL: https://github.com/a-tokyo/react-native-stripe-checkout-webview
- Owner: a-tokyo
- License: mit
- Created: 2020-09-01T00:19:17.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2023-03-01T12:02:57.000Z (almost 2 years ago)
- Last Synced: 2024-12-10T04:05:59.818Z (12 days ago)
- Topics: apple-pay, checkout, expo, flow, online-payments, payments, react-native, react-native-expo, react-native-stripe, stripe, stripe-checkout, typescript
- Language: JavaScript
- Homepage: https://stripe.com/payments/checkout
- Size: 813 KB
- Stars: 73
- Watchers: 4
- Forks: 18
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: .github/CODE_OF_CONDUCT.md
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# React Native Stripe Checkout
React Native implementation for [Stripe.js Checkout](https://stripe.com/payments/checkout).
## Description
The library allows you to use [Stripe.js Checkout](https://stripe.com/payments/checkout) with react-native without ejecting. You can use it with both server-side implementations and client-side implementations. Simply ensure you follow the [url structure guidelines below](#important-notes-about-urls).## Prequisites
- This library relies on [React Native Webview](https://www.npmjs.com/package/react-native-webview). Please follow [this guide](https://github.com/react-native-community/react-native-webview/blob/HEAD/docs/Getting-Started.md) to install in your project first.## Installation
- Ensure you've completed the setps in [prequisites.](#prequisites)
- Install package via npm or yarn:
`npm install --save react-native-stripe-checkout-webview` OR `yarn add react-native-stripe-checkout-webview`
- Import in your project
```javascript
import StripeCheckout from 'react-native-stripe-checkout-webview';
```## Usage
```jsx
import StripeCheckout from 'react-native-stripe-checkout-webview';type Props = { STRIPE_PUBLIC_KEY: string, CHECKOUT_SESSION_ID: string };
const MyStripeCheckout = ({ STRIPE_PUBLIC_KEY, CHECKOUT_SESSION_ID }: Props) => (
{
console.log(`Stripe checkout session succeeded. session id: ${checkoutSessionId}.`);
}}
onCancel={() => {
console.log(`Stripe checkout session cancelled.`);
}}
/>
);export default MyStripeCheckout;
```## Important Notes about URLs
- successUrl must have the query string params `?sc_checkout=success&sc_sid={CHECKOUT_SESSION_ID}`
- sc_sid is optional - must be the last param - when passed results in sessionId being passed to the onSuccess function
- cancelUrl must have the query string params `?sc_checkout=cancel`
- A simple way to do this is using [url-join](https://www.npmjs.com/package/url-join). eg: `urlJoin(mySuccessUrl, '?sc_checkout=success&sc_sid={CHECKOUT_SESSION_ID}')`.## Component props
- `stripePublicKey` (String) - Stripe public key of your project.
- `checkoutSessionInput` (Object) - Object to be passed to Stripe's `redirectToCheckout` function. [Docs](https://stripe.com/docs/js/checkout/redirect_to_checkout).
- ```js
{
sessionId: string,
successUrl: string,
cancelUrl: string,
// common
customerEmail?: string,
billingAddressCollection?: 'required' | 'auto',
shippingAddressCollection?: {
allowedCountries: Array,
},
locale?: string,
}
| {
clientReferenceId: string,
successUrl: string,
cancelUrl: string,
items?: Array<{ plan: string, quantity: string }>,
lineItems?: Array<{ price: number, quantity: number }>,
mode?: 'payment' | 'subscription',
submitType?: string,
// common
customerEmail?: string,
billingAddressCollection?: 'required' | 'auto',
shippingAddressCollection?: {
allowedCountries: Array,
},
locale?: string,
}
```
- `onSuccess` (?Function) - Called upon success of the checkout session with `{ ...props, checkoutSessionId: 'CHECKOUT_SESSION_ID' }`
- `onCancel` (?Function) - Called upon success of the checkout session with `{ ...props }`
- `onLoadingComplete` (?Function) - Called when the Stripe checkout session webpage loads successfully.
- `options` (?Object) - custom options to display content in the webview
- `htmlContentLoading` (String) - Html string to display a loading indication. - default: `Loading...
` - note: The loading item is set on the element with id='sc-loading'
- `htmlContentError` (String) - Html string to display stripe errors. - default: `` - note: The error is set on the element with id='sc-error-message'
- `htmlContentHead` (String) - Html string to inject in head. - default: ''
- `webViewProps` (?Object) - WebView Component props, spread on the WebView Component.
- `renderOnComplete` (?(props) => React$Node) - Optional rendering function returning a component to display upon checkout completion. note: You don't need this if your onSuccess and onCancel functions navigate away from the component.## Apple Pay and Google Pay
- This library uses [react-native-webview](https://github.com/react-native-webview) under the hood to render the Stripe Checkout webpage. To get Apple Pay and Google Pay to work we need to pass the context to the browser, [here's how to get it working](https://github.com/react-native-webview/react-native-webview/issues/920#issuecomment-720305564):
- What causes the issue is an injected script by default on webview start named html5HistoryAPIShimSource
How to fix (Note that the fix doesn't fully work on expo, but workarounds can be found in the issue thread):
- Comment this line in /node_modules/react-native-webview/apple/RNCWebView.m like shown below (in v10.9.2 line number is 1270.)
```
WKUserScript *script = [[WKUserScript alloc] initWithSource:html5HistoryAPIShimSource injectionTime:WKUserScriptInjectionTimeAtDocumentStart
forMainFrameOnly:YES];
// [wkWebViewConfig.userContentController addUserScript:script]; // this line that inject "html5HistoryAPIShimSource" on start
```
- You can use [patch-package](https://www.npmjs.com/package/patch-package) to easily persist this change## Contributing
Pull requests are highly appreciated! For major changes, please open an issue first to discuss what you would like to change.### Related Projects
- Stripe's identity verification for react native: [react-native-stripe-identity](https://github.com/A-Tokyo/react-native-stripe-identity)### Roadmap
- Add eslint
- Config prettier
- Add typescript