Stripe Payments
Integrate Stripe for card payments, checkout sessions, and subscriptions.
Configuration
pay({
stripe: {
secretKey: process.env.STRIPE_SECRET_KEY,
webhookSecret: process.env.STRIPE_WEBHOOK_SECRET,
currency: "usd", // Default currency
},
});
Checkout Sessions
Programmatic
const session = await app.pay.stripe.createCheckout({
items: [
{ name: "Pro Plan", amount: 2999, currency: "usd", quantity: 1 },
],
successUrl: "https://myapp.com/success?session_id={CHECKOUT_SESSION_ID}",
cancelUrl: "https://myapp.com/cancel",
metadata: { userId: "user-123" },
});
console.log(session.url); // Redirect URL
console.log(session.sessionId); // Stripe session ID
REST API
curl -X POST http://localhost:3000/pay/stripe/checkout \
-H "Content-Type: application/json" \
-H "Authorization: Bearer eyJhbGci..." \
-d '{
"items": [
{ "name": "Pro Plan", "amount": 2999, "currency": "usd" }
],
"successUrl": "https://myapp.com/success",
"cancelUrl": "https://myapp.com/cancel"
}'
Response (200):
{
"sessionId": "cs_test_abc123",
"url": "https://checkout.stripe.com/pay/cs_test_abc123"
}
Subscriptions
// Create a subscription checkout
const session = await app.pay.stripe.createSubscription({
priceId: "price_abc123", // Stripe Price ID
customerId: "cus_abc123", // Or use email to auto-create
trialDays: 14,
successUrl: "https://myapp.com/success",
cancelUrl: "https://myapp.com/cancel",
});
// Cancel a subscription
await app.pay.stripe.cancelSubscription("sub_abc123");
Webhooks
FluxKit automatically handles Stripe webhooks at POST /pay/stripe/webhook. Configure the webhook URL in your Stripe dashboard to point to:
https://myapp.com/pay/stripe/webhook
Listening for Events
app.pay.stripe.on("payment_intent.succeeded", async (event) => {
const paymentIntent = event.data.object;
console.log(`Payment received: ${paymentIntent.amount}`);
});
app.pay.stripe.on("customer.subscription.deleted", async (event) => {
const subscription = event.data.object;
// Handle subscription cancellation
});
Refunds
// Full refund
const refund = await app.pay.stripe.refund(transactionId);
// Partial refund
const refund = await app.pay.stripe.refund(transactionId, {
amount: 1000, // Refund $10.00
});
curl -X POST http://localhost:3000/pay/stripe/refund \
-H "Content-Type: application/json" \
-H "Authorization: Bearer eyJhbGci..." \
-d '{
"transactionId": "65a1b2c3d4e5f6a7b8c9d0e1",
"amount": 1000
}'
Retrieving Transactions
const transactions = await app.pay.getTransactions({
provider: "stripe",
status: "completed",
limit: 20,
});