StripePlugin
StripePlugin
Plugin to enable payments through Stripe via the Payment Intents API.
Requirements
-
You will need to create a Stripe account and get your secret key in the dashboard.
-
Create a webhook endpoint in the Stripe dashboard (Developers -> Webhooks, “Add an endpoint”) which listens to the
payment_intent.succeeded
andpayment_intent.payment_failed
events. The URL should behttps://my-server.com/payments/stripe
, wheremy-server.com
is the host of your Vendure server. Note: for local development, you’ll need to use the Stripe CLI to test your webhook locally. See the local development section below. -
Get the signing secret for the newly created webhook.
-
Install the Payments plugin and the Stripe Node library:
yarn add @vendure/payments-plugin stripe
or
npm install @vendure/payments-plugin stripe
Setup
- Add the plugin to your VendureConfig
plugins
array:import { StripePlugin } from '@vendure/payments-plugin/package/stripe'; // ... plugins: [ StripePlugin.init({ // This prevents different customers from using the same PaymentIntent storeCustomersInStripe: true, }), ]
- Create a new PaymentMethod in the Admin UI, and select “Stripe payments” as the handler.
- Set the webhook secret and API key in the PaymentMethod form.
Storefront usage
The plugin is designed to work with the Custom payment flow. In this flow, Stripe provides libraries which handle the payment UI and confirmation for you. You can install it in your storefront project with:
yarn add @stripe/stripe-js
# or
npm install @stripe/stripe-js
If you are using React, you should also consider installing @stripe/react-stripe-js
, which is a wrapper around Stripe Elements.
The high-level workflow is:
- Create a “payment intent” on the server by executing the
createStripePaymentIntent
mutation which is exposed by this plugin. - Use the returned client secret to instantiate the Stripe Payment Element:
import { Elements } from '@stripe/react-stripe-js'; import { loadStripe, Stripe } from '@stripe/stripe-js'; import { CheckoutForm } from './CheckoutForm'; const stripePromise = getStripe('pk_test_....wr83u'); type StripePaymentsProps = { clientSecret: string; orderCode: string; } export function StripePayments({ clientSecret, orderCode }: StripePaymentsProps) { const options = { // passing the client secret obtained from the server clientSecret, } return ( <Elements stripe={stripePromise} options={options}> <CheckoutForm orderCode={orderCode} /> </Elements> ); }
// CheckoutForm.tsx import { useStripe, useElements, PaymentElement } from '@stripe/react-stripe-js'; import { FormEvent } from 'react'; export const CheckoutForm = ({ orderCode }: { orderCode: string }) => { const stripe = useStripe(); const elements = useElements(); const handleSubmit = async (event: FormEvent) => { // We don't want to let default form submission happen here, // which would refresh the page. event.preventDefault(); if (!stripe || !elements) { // Stripe.js has not yet loaded. // Make sure to disable form submission until Stripe.js has loaded. return; } const result = await stripe.confirmPayment({ //`Elements` instance that was used to create the Payment Element elements, confirmParams: { return_url: location.origin + `/checkout/confirmation/${orderCode}`, }, }); if (result.error) { // Show error to your customer (for example, payment details incomplete) console.log(result.error.message); } else { // Your customer will be redirected to your `return_url`. For some payment // methods like iDEAL, your customer will be redirected to an intermediate // site first to authorize the payment, then redirected to the `return_url`. } }; return ( <form onSubmit={handleSubmit}> <PaymentElement /> <button disabled={!stripe}>Submit</button> </form> ); };
- Once the form is submitted and Stripe processes the payment, the webhook takes care of updating the order without additional action
in the storefront. As in the code above, the customer will be redirected to
/checkout/confirmation/${orderCode}
.
A full working storefront example of the Stripe integration can be found in the Remix Starter repo
Local development
- Download & install the Stripe CLI: https://stripe.com/docs/stripe-cli
- From your Stripe dashboard, go to Developers -> Webhooks and click “Add an endpoint” and follow the instructions under “Test in a local environment”.
- The Stripe CLI command will look like
stripe listen --forward-to localhost:3000/payments/stripe
- The Stripe CLI will create a webhook signing secret you can then use in your config of the StripePlugin.
Signature
class StripePlugin {
static static options: StripePluginOptions;
static init(options: StripePluginOptions) => Type<StripePlugin>;
}
Members
options
StripePluginOptions
init
(options: StripePluginOptions) => Type<StripePlugin>
StripePluginOptions
Configuration options for the Stripe payments plugin.
Signature
interface StripePluginOptions {
storeCustomersInStripe?: boolean;
metadata?: (
injector: Injector,
ctx: RequestContext,
order: Order,
) => Stripe.MetadataParam | Promise<Stripe.MetadataParam>;
}
Members
storeCustomersInStripe
boolean
false
true
, a Customer object will be created in Stripe - if
it doesn’t already exist - for authenticated users, which prevents payment methods attached to other Customers
to be used with the same PaymentIntent. This is done by adding a custom field to the Customer entity to store
the Stripe customer ID, so switching this on will require a database migration / synchronization.
metadata
( injector: Injector, ctx: RequestContext, order: Order, ) => Stripe.MetadataParam | Promise<Stripe.MetadataParam>