0.2.1 • Published 5 years ago

@panz3r/react-native-stripe v0.2.1

Weekly downloads
-
License
MIT
Repository
github
Last release
5 years ago

React Native Stripe

React Native bindings for the Stripe SDK


Installation

yarn add @panz3r/react-native-stripe

or

npm install @panz3r/react-native-stripe

iOS Setup

Install required pods using

npx pod-install --quiet

Usage

import { ReactNativeStripe } from '@panz3r/react-native-stripe';

// Init ReactNativeStripe as soon as your app is ready
await ReactNativeStripe.init({
  publishableKey,
})

// Init a CustomerContext passing an EphemeralKeyProvider function when you are ready to link your user to a Stripe customer
await ReactNativeStripe.initCustomerContext(ephemeralKeyProviderFn);

const ephemeralKeyProviderFn: RNStripeEphemeralKeyProviderFn = async (options) => {
  console.log('ephemeralKeyProviderFn called', options);
  const res = await fetch('<STRIPE_SERVER_URL>/ephemeral_keys', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify(options),
  });
  return await res.json();
};

// Init PaymentContext when you need start the checkout flow
await ReactNativeStripe.initPaymentContext({
  // Listener to receive paymentContext updates (optional)
  paymentContextUpdateListener,
  // Function to validate the shipping address and return the shipping methods (iOS only)
  shippingAddressValidator,
  // PaymentContext options
  paymentContextOptions: {
    requiredBillingAddressFields: 'none',
    requiredShippingAddressFields: [
      'name',
      'postalAddress',
    ],
  },
});

// Present Stripe PaymentOptions view
ReactNativeStripe.presentPaymentOptionsView();
// or
ReactNativeStripe.pushPaymentOptionsView();


// Present the Shipping view
ReactNativeStripe.presentShippingView();
// or
ReactNativeStripe.pushShippingView();

// Start the payment process passing a Stripe clientSecret generated on your backend
const completed = await ReactNativeStripe.requestPayment(clientSecret);
// Check if the payment process has been completed -> 'completed' does not mean that the payment was successfull, you should handle this check on your backend using Stripe webhooks

Methods

init

Initialize ReactNativeStripe using the provided initOptions

async init(initOptions: RNStripeManagerInitOptions): Promise<boolean>

RNStripeManagerInitOptions

NameDescriptionTypeRequired
publishableKeySet this to your Stripe publishable API key, obtained from Stripe dashboardstringtrue
appleMerchantIdThe Apple Merchant Identifier to use during Apple Pay transactions (iOS only). To create one of these, see the guide at Stripe ApplePay docsstringfalse
themeTheme object can be used to visually style Stripe-provided UI (iOS only)RNStripeThemefalse

RNStripeTheme

Theme object can be used to visually style Stripe-provided UI (iOS only).

See Stripe iOS docs for more information.

NameDescriptionTypeRequired
primaryForegroundColorThe primary foreground color of this themestringfalse
secondaryForegroundColorThe secondary foreground color of this themestringfalse
primaryBackgroundColorThe primary background color of the themestringfalse
secondaryBackgroundColorThe secondary background color of this themestringfalse
accentColorThe accent color of this themestringfalse
errorColorThe error color of this themestringfalse

initCustomerContext

Initializes a new Stripe CustomerContext with the specified key provider.

Upon initialization, a CustomerContext will fetch a new ephemeral key from your backend and use it to prefetch the customer object specified in the key. Subsequent customer and payment method retrievals will return the prefetched customer and attached payment methods immediately if its age does not exceed 60 seconds.

async initCustomerContext(ephemeralKeyProviderFn: RNStripeEphemeralKeyProviderFn): Promise<boolean>

RNStripeEphemeralKeyProviderFn

A function to retrieve a Stripe EphemeralKey for the current user from your backend.

See Stripe docs for more details on how to implement it.

type RNStripeEphemeralKeyProviderFn = (options: RNStripeEphemeralKeyProviderFnOptions) => Promise<StripeCustomerKeyObject>

initPaymentContext

Initializes a new PaymentContext with the active CustomerContext and Theme, using the provided configuration.

async initPaymentContext(paymentContextOptions: RNStripePaymentContextOptions): Promise<boolean>;

RNStripePaymentContextOptions

NameDescriptionTypeRequired
paymentContextOptionsAll the options you can set or change around a paymentRNStripePaymentConfigurationfalse
paymentContextUpdateListenerListener function called each time the PaymentContext changesRNStripePaymentContextListenertrue
shippingAddressValidatorThis function is called after the user enters a shipping address. Validate the returned address and determine the shipping methods available for that address. (iOS only)RNStripePaymentContextShippingAddressValidatorfalse
RNStripePaymentConfiguration
NameDescriptionTypeRequiredDefault
paymentAmountThe amount of money you’re requesting from the user, in the smallest currency unit for the selected currency. For example, to indicate $10 USD, use 1000 (i.e. 1000 cents)numberfalseundefined
paymentCurrencyThe three-letter currency code for the currency of the payment (i.e. USD, GBP, JPY, etc)numberfalse'USD'
paymentCountryThe two-letter country code for the country where the payment will be processednumberfalse'US'
requiredBillingAddressFieldsThe billing address fields the user must fill out when prompted for their payment details. Can be one of 'none', 'postalCode', 'full' or 'name'stringfalse'postalCode'
requiredShippingAddressFieldsThe shipping address fields the user must fill out when prompted for their shipping info. Leave undefined if shipping address is not requiredRNStripeContactField[]falseundefined
RNStripeContactField

Represent a shipping address field the user must fill out when prompted for their shipping info.

Note: 'name' and 'country' are always required on Android.

Note: 'emailAddress' is not available on Android and won't be returned as part of RNStripePaymentContextSnapshot shippingAddress.

type RNStripeContactField = 'name' | 'emailAddress' | 'phoneNumber' | 'postalAddress';

RNStripePaymentContextListener

Listener function called each time the PaymentContext changes.

type RNStripePaymentContextListener = (paymenyContext: RNStripePaymentContextSnapshot) => void;
RNStripePaymentContextSnapshot

A snapshot of the current PaymentContext.

NameDescriptionType
paymentMethodThe user’s currently selected payment option. May be undefinedRNStripePaymentOption
shippingAddressThe user’s shipping address. May be undefinedRNStripeAddress
shippingMethodThe user’s currently selected shipping method. May be undefinedRNStripeShippingMethod
isPaymentReadyToChargeWhether the payment data is ready for making a charge. Always true on iOSboolean
RNStripePaymentOption

Represent a user payment option

NameDescriptionType
labelA string describing the payment method, such as "Apple Pay" or "Visa 4242"string
imageA base64 encoded image with a small (32x20 points) logo representing the payment methodstring
templateImageA base64 encoded image with a small (32 x 20 points) logo representing the payment method that can be used as template for tinted iconsstring
isReusableDescribes whether this payment option may be used multiple time. If it is not reusable, the payment method must be discarded after useboolean
RNStripeAddress

Represent a user shipping address

NameDescriptionType
nameThe user’s full name (e.g. “Jane Doe”)string
phoneThe phone number of the address (e.g. “8885551212”)string
emailThe email of the address (e.g. “jane@doe.com”)string
line1The first line of the user’s street address (e.g. “123 Fake St”)string
line2The apartment, floor number, etc of the user’s street address (e.g. “Apartment 1A”)string
cityThe city in which the user resides (e.g. “San Francisco”)string
postalCodeThe postal code in which the user resides (e.g. “90210”)string
stateThe state in which the user resides (e.g. “CA”)string
countryThe ISO country code of the address (e.g. “US”)string
RNStripeShippingMethod

Represent a shipping method

NameDescriptionType
labelA short, localized description of the shipping method.string
amountThe shipping method cost as a string (e.g. "5.99").string
detailA user-readable description of the shipping method.string
identifierA unique identifier for the shipping method, used by the app.string

RNStripePaymentContextShippingAddressValidator

This function is called after the user enters a shipping address.

Validate the returned address and determine the shipping methods available for that address.

If the address is valid, resolve the promise with an array of RNStripeShippingMethod. If you don't need to collect a shipping method simply return null. If the address is invalid, reject the promise with an error message describing the issue with the address.

Note: Providing an error message is optional—if you omit it, the user will simply see an alert with the message "Invalid Shipping Address".

Note: if left undefined user inserted addresses will always be considered valid.

NOTE: Shipping address validation is currently supported only on iOS due to a limitation of the Stripe Android SDK.

type RNStripePaymentContextShippingAddressValidator = (address: RNStripeAddress) => Promise<RNStripeShippingMethod[] | null>;

presentPaymentOptionsView

This method creates, configures, and appropriately presents a Stripe PaymentOptionsView on top of the payment context.

It’ll be dismissed automatically when the user is done selecting their payment method.

Note: This method will do nothing if it is called while PaymentContext is already showing a view controller or in the middle of requesting a payment.

async presentPaymentOptionsView(): Promise<boolean>;

pushPaymentOptionsView

This creates, configures, and appropriately pushes an PaymentOptionsView onto the navigation stack of the context.

It’ll be popped automatically when the user is done selecting their payment method.

Note: This method will do nothing if it is called while PaymentContext is already showing a view controller or in the middle of requesting a payment.

async pushPaymentOptionsView(): Promise<boolean>;

presentShippingView

This method creates, configures, and appropriately presents a view controller for collecting shipping address and shipping method on top of the payment context.

It’ll be dismissed automatically when the user is done entering their shipping info.

Note: This method will do nothing if it is called while PaymentContext is already showing a view controller or in the middle of requesting a payment.

async presentShippingView(): Promise<boolean>;

pushShippingView

This method creates, configures, and appropriately pushes a view controller for collecting shipping address and shipping method on top of the payment context.

It’ll be dismissed automatically when the user is done entering their shipping info.

Note: This method will do nothing if it is called while PaymentContext is already showing a view controller or in the middle of requesting a payment.

async pushShippingView(): Promise<boolean>;

requestPayment

Request a payment using the active PaymentContext.

paymentIntentClientSecret represent the client secret of the PaymentIntent to be confirmed.

async requestPayment(paymentIntentClientSecret: RNStripePaymentIntentClientSecret): Promise<boolean>;

RNStripePaymentIntentClientSecret

The client secret of the PaymentIntent to be confirmed. It should be created on your backend and it's used to complete the payment on your frontend.

See Stripe Payments docs for more about how to accept a payment and learn about how client_secret should be handled.

WARNING: It should not be stored, logged, embedded in URLs, or exposed to anyone other than the customer.

type RNStripePaymentIntentClientSecret = string;

Contributing

See the contributing guide to learn how to contribute to the repository and the development workflow.

License

MIT


Made with :sparkles: & :heart: by Mattia Panzeri and contributors

If you found this project to be helpful, please consider buying me a coffee.

buy me a coffee ko-fi