Get paid with Stripe
Goal: for the user to get paid in production with the least custom code and without ever storing card data. Stripe provides the payment page, the customer portal, and the invoices. You only connect the pieces.
Before writing code, ask
- One-time payment or subscription? If subscription: monthly, annual, or both? Is there a free trial? How many days?
- Do you sell to individuals, to businesses, or both? It changes the VAT and the invoice.
- Only Spain, or also the rest of the EU or outside it? It changes the VAT and whether Stripe Tax is needed.
- What happens when someone pays? (grant access, send an email, create a user...). That is what the webhook will do.
- What stack does the project use? Look at the code before proposing anything. If there is no project, propose Next.js or Node + Express and explain why in two sentences.
If the user does not know how to answer 2 or 3, tell them to discuss it with their tax advisor (gestor) before charging the first euro. Do not guess the tax regime.
Architecture you always propose
- Stripe Checkout (payment page hosted by Stripe). No custom card forms.
- Stripe Customer Portal so the customer can change card, change plan, or cancel on their own. It saves support.
- A single webhook endpoint that receives Stripe events and updates your database.
- Products and prices created in the Stripe dashboard, not by hand in code. The code only uses the
price_id. - Invoices generated by Stripe: enable
invoice_creationin Checkout for one-time payments; for subscriptions they are generated automatically.
Step by step
- Account and keys. Do not ask them to paste the test secret key (
sk_test_...) to you: they should copy it themselves from the dashboard into their.env(variableSTRIPE_SECRET_KEY), never in the code, the chat, or the repo. Check that.envis in.gitignore. - Products. Explain how to create the product and the price in the Stripe dashboard (test mode). Ask for the
price_id(price_...). - Checkout. Create a route that opens a Checkout session with that
price_id,modepaymentorsubscription,success_url, andcancel_url. For businesses, enabletax_id_collectionto ask for the tax ID (NIF). Enablecustomer_creationso that a customer always exists in Stripe. - Portal. Create a route that generates a link to the Customer Portal for the logged-in customer.
- Webhook. An endpoint that verifies the signature with
STRIPE_WEBHOOK_SECRET, stores theevent.idso the same event is not processed twice, and responds 200 in under 5 seconds. Minimum events:checkout.session.completed,invoice.paid,invoice.payment_failed,customer.subscription.updated,customer.subscription.deleted. Incheckout.session.completed, grant access only ifpayment_status == 'paid'(with deferred payment methods the event arrives before the money: wait forcheckout.session.async_payment_succeeded). To set it up and test it use the/webhook-stripecommand. - Database. Store per user:
stripe_customer_id,stripe_subscription_id,estado(status: active, unpaid, canceled), andfin_periodo_actual(end of the current period). Access to the product is decided withestado, never with the result of the success page. - VAT (IVA). If you only sell in Spain to individuals: price with VAT included (
tax_behavior: inclusive) and a 21% tax rate created in the dashboard. If you sell to the EU or to businesses: propose Stripe Tax and warn that it has a per-transaction cost [check the current rate]. - Invoices. In the dashboard: sequential numbering at the account level, the user's tax details in the header, and the customer's tax ID (NIF) on the invoice. Before accepting the first invoice as good, run it through the
revisor-facturassubagent. - Moving to production. Swap the test keys for the real ones, create the production webhook in the dashboard with the public URL, and put the new
whsec_in the hosting's environment variable (never in the code or the chat). Make a real payment of 1 € and refund it.
What to return
- List of files created or modified, with one sentence per file saying what it does.
- The
.envvariables it needs, with the exact name and where each one is obtained in the Stripe dashboard. - An 8-point checklist for moving from test to production.
- The webhook events that are listened to and what each one does in the database, in a table.
Risks that are told to the user, not kept quiet
- Stripe charges a fee per transaction. Tell them to look at it on Stripe's website before setting prices. Do not make up the percentage.
- A Stripe invoice is not valid in Spain if data is missing (sequential number, tax ID of issuer and recipient, base, rate, and amount of VAT). Configuring it correctly is the user's obligation, not Stripe's.
- Spain's Verifactu regulation imposes requirements on invoicing software in Spain from [date depending on the type of taxpayer]. They should confirm with their tax advisor (gestor) whether it applies to them and whether their Stripe flow complies.
- If they store card data on their server, they fall under PCI DSS. With Checkout it is not necessary and should not be done.
What NOT to do
- Do not build custom card forms or use Elements unless the user expressly asks and knows what it implies.
- Do not grant access to the product at the
success_url. That URL can be reached without having paid. Access is granted by the webhook. - Do not trust a webhook without verifying the signature. Anyone can send a fake POST to your endpoint.
- Do not process the same
event.idtwice. Stripe retries for days if it does not receive a 200. - Do not put keys in the code, in the frontend, or in the repository. The publishable key (
pk_) is the only one that can go to the browser. - Do not calculate VAT by hand in code. It is defined in Stripe (tax rate or Stripe Tax).
- Do not go to production without having tested a failed payment with the test card
4000 0000 0000 0341. - Do not write code for stacks the user does not use. Look at the project first.