Get the api key
After you have created your account for Stripe, you will need to get the API key. You can do this by going to the API page in the dashboard. Here you will find theSecret key
and the Publishable key
. You will need the Secret key
for the integration to work.
Add environment variables
To use the Stripe integration, you need to define the following environment variables to your.env.local
as well as your production environment:
.env.local
In-App Purchases
You can use Stripe anywhere in your app by importing thestripe
object like so:
page.tsx
Creating a Checkout Session
We need to create a checkout session to charge your users , and we have implemented this whole process for you. It consists of two parts:- Client
/api/stripe/checkout
endpoint.
apps/web/[locale]/components/payment/price-form.tsx
- Server
apps/web/api/stripe/checkout/route.ts
This is out of the box, and if you want to customize, you can modify both files.
Stripe allows using the product’s Buy Link without creating a checkout session through the API. But create the checkout session with API is recommended for better tracking and analytics.
Webhooks
Create a webhook
To configure a new webhook, go to the Webhooks page in the Stripe dashboard. Click the Add endpoint button and select at least the following events: For subscriptions:customer.subscription.created
customer.subscription.updated
customer.subscription.deleted
checkout.session.completed
Webhook Handler
Stripe webhooks are handled in thePOST /webhooks/stripe
route in the apps/api
app. This route constructs the event and then switches on the event type to determine how to process the event.
We have implemented the basic structure of the
Webhook
handler for you. You can modify it as needed.Local Development
To test webhooks locally, we’ve configured the Stripe CLI inapps/api
app to forward webhooks to your local server. This will start automatically when you run pnpm dev
.
Anti-Fraud
As your app grows, you will inevitably encounter credit card fraud. Stripe Radar is enabled by default if you integrate payments using their SDK as described above. This provides a set of tools to help you detect and prevent fraud. Stripe Radar supports more advanced anti-fraud features if you embed the Stripe JS script in every page load. This is not enabled by default in ShipThing, but you can add it as follows:1
Edit the layout
Edit
apps/app/app/layout.tsx
and add <Script src="https://js.stripe.com/v3/" />
after the opening <html>
tag and before the opening <body>
tag. You will also need to add import Script from 'next/script'
2
Add script to the website
Add the same script to the website in
apps/web/app/layout.tsx
.3
Prevent common fraud patterns with Arcjet
Prevent common fraud patterns by using Arcjet IP address analysis to block requests from VPNs and proxies. These are commonly used by fraudsters to hide their location, but have legitimate uses as well so are not blocked by default. You could simply block these users, or you could adjust the checkout process to require approval before processing their payment.
apps/app/app/(authenticated)/layout.tsx
you could add this after the call to aj.protect()
: