Prompt
How do I accept payments on my website with an API?
Latest observation
To accept payments on your website with an API, the most common approach is to use a payment processor like Stripe, PayPal, Square, or Adyen. These providers handle card data securely so you don’t need to store sensitive payment details yourself.
Typical flow
- Customer enters payment details on your website.
- Your frontend sends the details to the payment provider using their client-side SDK or hosted fields.
- Your backend creates a payment intent / order / transaction via the provider’s API.
- The payment provider confirms the payment.
- Your backend receives a webhook confirming success or failure.
- You mark the order as paid in your database.
Recommended setup
1. Choose a payment provider
Pick one based on your needs:
- Stripe: easiest for APIs and subscriptions
- PayPal: familiar to many customers
- Square: good for online + in-person
- Adyen: enterprise/global
2. Create an account and get API keys
You’ll usually get:
- Publishable key for frontend
- Secret key for backend
Never expose secret keys in frontend code.
3. Use hosted payment UI or payment elements
This is the safest approach because the provider handles PCI-sensitive data. Examples:
- Stripe Checkout
- Stripe Elements
- PayPal buttons
4. Create a backend endpoint to start payment
Your backend:
- calculates amount
- creates payment session/intent through the API
- returns a client token or checkout URL to the frontend
5. Confirm payment using webhooks
Do not rely only on the frontend redirect/success page. Use webhooks to confirm:
- payment succeeded
- payment failed
- chargeback/refund events
Example: Stripe-style flow
Backend (Node.js example)
import express from "express";
import Stripe from "stripe";
const app = express();
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY);
app.use(express.json());
app.post("/create-payment-intent", async (req, res) => {
const { amount } = req.body;
const paymentIntent = await stripe.paymentIntents.create({
amount,
currency: "usd",
});
res.json({ clientSecret: paymentIntent.client_secret });
});
app.listen(3000);
Frontend
Use Stripe Elements or Checkout to collect card details and confirm payment with the clientSecret.
Webhook example
Your server should listen for events like payment_intent.succeeded and then update your order status.
Important security tips
- Use HTTPS
- Don’t store raw card numbers yourself
- Validate amounts on the server
- Use webhooks for final payment confirmation
- Protect endpoints with authentication if needed
- Consider fraud protection and 3D Secure
If you want the easiest path
Use Stripe Checkout:
- minimal code
- hosted payment page
- handles much of PCI compliance
- easy to integrate with APIs
If you want, I can give you a step-by-step implementation for:
- Stripe
- PayPal
- Node.js / Python / PHP
- One-time payments or subscriptions