react-native-google-pay
v3.0.0
Published
React Native Turbo Module for Google Pay
Maintainers
Readme
react-native-google-pay
Accept payments with Google Pay in React Native apps. The Android implementation is a Turbo Module written in Kotlin.
Requirements
- React Native
>= 0.76with the New Architecture enabled. React Native 0.82 and newer are New Architecture only. minSdkVersionfrom your React Native app. Google Pay requires 23 or higher; React Native 0.88 uses 24.- Google Play services on the device.
Google Pay is Android-only. On other platforms setEnvironment, isReadyToPay, and requestPayment throw Google Pay is available on Android only. ENVIRONMENT_TEST is 3 and ENVIRONMENT_PRODUCTION is 1 on every platform.
Installation
yarn add react-native-google-payAutolinking picks up the Android Turbo Module. iOS is intentionally not linked.
Add the Google Pay API meta-data element to the <application> element of your app's AndroidManifest.xml:
<meta-data
android:name="com.google.android.gms.wallet.api.enabled"
android:value="true" />Usage
import { GooglePay } from 'react-native-google-pay';
const request = {
...GooglePay.BaseRequest,
allowedPaymentMethods: [
{
type: 'CARD',
parameters: {
allowedAuthMethods: ['PAN_ONLY', 'CRYPTOGRAM_3DS'],
allowedCardNetworks: ['VISA', 'MASTERCARD'],
},
tokenizationSpecification: {
type: 'PAYMENT_GATEWAY',
parameters: {
gateway: 'example',
gatewayMerchantId: 'exampleGatewayMerchantId',
},
},
},
],
merchantInfo: {
merchantId: 'Example Merchant',
merchantName: 'Example Merchant',
},
transactionInfo: {
totalPrice: '9.99',
totalPriceStatus: 'FINAL',
currencyCode: 'USD',
},
};
GooglePay.setEnvironment(GooglePay.ENVIRONMENT_TEST);
GooglePay.isReadyToPay({
...GooglePay.BaseRequest,
allowedPaymentMethods: request.allowedPaymentMethods,
}).then(ready => {
if (ready) {
GooglePay.requestPayment(request)
.then(paymentData => {
const token = paymentData.paymentMethodData.tokenizationData.token;
// Send the token to your payment gateway.
})
.catch(error => console.log(error.code, error.message));
}
});Requests use the Google Pay API objects. TypeScript types come from @types/googlepay, which this package depends on. GooglePay.BaseRequest is { apiVersion: 2, apiVersionMinor: 0 }.
requestPayment resolves with a PaymentData object. The payment token is paymentMethodData.tokenizationData.token. The promise rejects when the payer closes the sheet (PAYMENT_RESULT_CANCELED) or when Google Pay returns an error (PAYMENT_RESULT_ERROR). isReadyToPay resolves true or false. It rejects NO_ACTIVITY when the Android activity is missing, NO_PAYMENTS_CLIENT when setEnvironment has not run, and IS_READY_TO_PAY_REQUEST when the request JSON is invalid.
For a gateway integration, set tokenizationSpecification.parameters.gateway and gatewayMerchantId. Stripe uses the same PAYMENT_GATEWAY type with string parameters stripe:publishableKey and stripe:version. For type: 'DIRECT', set protocolVersion to ECv2 and publicKey. The Example app calls all three.
Example
The Example app is a React Native 0.88 project:
npm install
npm --prefix Example install
npm --prefix Example run androidThe demo uses GooglePay.ENVIRONMENT_TEST.
