Stripe Payment Setup
This guide covers setting up Stripe for payment processing in your web and desktop applications.
Prerequisites
- Stripe account (Create one here)
- Access to Stripe Dashboard
- Your backend server URL for webhooks
Step 1: Create a Stripe Account
- Go to Stripe Dashboard
- Create an account with your business email
- Verify your email address
- Complete business verification (required for live payments)
Step 2: Get API Keys
- Log in to Stripe Dashboard
- Navigate to Developers > API keys
- You'll see two environments:
Test Mode Keys
| Key Type | Format | Usage |
|---|---|---|
| Publishable Key | pktest... |
Frontend, safe to expose |
| Secret Key | sktest... |
Backend only, keep secret |
Live Mode Keys
| Key Type | Format | Usage |
|---|---|---|
| Publishable Key | pklive... |
Production frontend |
| Secret Key | sklive... |
Production backend |
Warning: Never expose Secret Keys in frontend code or version control.
Step 3: Create Products and Prices
Using Stripe Dashboard
- Go to Products in the sidebar
- Click Add Product
- Fill in product details:
- Name: Product name
- Description: Product description
- Image: Product image (optional)
- Add pricing:
One-time Payments
Price: $9.99
Type: One-time
Subscription Pricing
Price: $9.99
Type: Recurring
Billing period: Monthly
Using Stripe API
// Create a product
const product = await stripe.products.create({
name: 'Pro Plan',
description: 'Access to all premium features',
});
// Create a price for subscription
const price = await stripe.prices.create({
product: product.id,
unit_amount: 999, // $9.99 in cents
currency: 'usd',
recurring: {
interval: 'month',
},
});
- Note down the Price ID (starts with
price_)
Step 4: Configure Webhooks
Webhooks notify your server of payment events.
- Go to Developers > Webhooks
- Click Add endpoint
- Enter your endpoint URL:
https://yourdomain.com/v1/payment/webhook/stripe
- Select events to listen to:
checkout.session.completedpayment_intent.succeededpaymentintent.paymentfailedcustomer.subscription.createdcustomer.subscription.updatedcustomer.subscription.deletedinvoice.paidinvoice.payment_failed
- Click Add endpoint
- Note down the Webhook Signing Secret (starts with
whsec_)
Step 5: Configure in OpenDev
- Log in to OpenDev Platform
- Navigate to your application settings
- Go to Payment Configuration
- Add Stripe configuration:
{
"platform": "stripe",
"enabled": true,
"config": {
"publishableKey": "pk_live_xxx",
"secretKey": "sk_live_xxx",
"webhookSecret": "whsec_xxx"
}
}
Configuration Fields
| Field | Required | Description |
|---|---|---|
| Publishable Key | Yes | Frontend API key |
| Secret Key | Yes | Backend API key |
| Webhook Secret | Yes | Webhook signing secret |
Step 6: Configure Product Tiers
Link Stripe Price IDs to your product tiers:
- Go to Product Tiers in OpenDev
- For each tier, add the Stripe Price ID:
{
"productId": "pro_monthly",
"name": "Pro Monthly",
"platformProductIds": {
"stripe": "price_1234567890"
}
}
Step 7: Implement Checkout
Using Stripe Checkout (Recommended)
The easiest way to accept payments:
// Frontend - Create checkout session
const response = await fetch('/api/create-checkout-session', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
priceId: 'price_xxx',
successUrl: 'https://yourdomain.com/success',
cancelUrl: 'https://yourdomain.com/cancel',
}),
});
const { sessionId } = await response.json();
// Redirect to Stripe Checkout
const stripe = Stripe('pk_xxx');
stripe.redirectToCheckout({ sessionId });
// Backend - Create session
const session = await stripe.checkout.sessions.create({
payment_method_types: ['card'],
line_items: [{
price: 'price_xxx',
quantity: 1,
}],
mode: 'subscription', // or 'payment' for one-time
success_url: 'https://yourdomain.com/success?session_id={CHECKOUT_SESSION_ID}',
cancel_url: 'https://yourdomain.com/cancel',
});
Using Payment Elements
For custom UI integration:
<form id="payment-form">
<div id="payment-element"></div>
<button type="submit">Pay</button>
</form>
<script>
const stripe = Stripe('pk_xxx');
const elements = stripe.elements({
clientSecret: 'pi_xxx_secret_xxx',
});
const paymentElement = elements.create('payment');
paymentElement.mount('#payment-element');
</script>
Step 8: Handle Webhooks
// Backend webhook handler
app.post('/webhook/stripe', express.raw({ type: 'application/json' }), async (req, res) => {
const sig = req.headers['stripe-signature'];
let event;
try {
event = stripe.webhooks.constructEvent(req.body, sig, webhookSecret);
} catch (err) {
return res.status(400).send(`Webhook Error: ${err.message}`);
}
switch (event.type) {
case 'checkout.session.completed':
const session = event.data.object;
// Fulfill the purchase
await handleCheckoutComplete(session);
break;
case 'invoice.paid':
// Subscription renewal
await handleSubscriptionRenewal(event.data.object);
break;
case 'customer.subscription.deleted':
// Subscription canceled
await handleSubscriptionCanceled(event.data.object);
break;
}
res.json({ received: true });
});
Step 9: Test the Integration
Test Cards
| Card Number | Scenario |
|---|---|
| 4242 4242 4242 4242 | Successful payment |
| 4000 0000 0000 9995 | Declined (insufficient funds) |
| 4000 0000 0000 3220 | 3D Secure required |
Use any future expiry date and any 3-digit CVC.
Testing Webhooks Locally
Use Stripe CLI for local webhook testing:
# Install Stripe CLI
brew install stripe/stripe-cli/stripe
# Login
stripe login
# Forward webhooks to local server
stripe listen --forward-to localhost:3000/webhook/stripe
Troubleshooting
Error: No such price
Solution: Verify the Price ID is correct and exists in your Stripe dashboard.
Error: Invalid API Key
Solution: Check you're using the correct key for your environment (test vs live).
Webhook Signature Verification Failed
Solutions:
- Ensure webhook secret is correct
- Use raw body parser for webhook endpoint
- Check webhook endpoint is receiving POST requests
Payment Declined
Solutions:
- In test mode, use test card numbers
- In live mode, verify card details are correct
- Check for fraud prevention blocks
Security Best Practices
- Never Log Full Card Numbers - PCI compliance
- Use Webhook Signing - Verify all webhook events
- Implement Idempotency - Handle duplicate events
- Secure API Keys - Use environment variables
- Enable 3D Secure - Reduce fraud and chargebacks