Stripe Setup & Integration Guide
The stripe provider integrates Stripe Checkout, Stripe Elements, Subscriptions & Invoicing, Customer Billing Portal, Saved Payment Methods, and Dispute Management into the Kirak payments runtime.
1. Prerequisites & Installation
Section titled “1. Prerequisites & Installation”No extra install: the provider calls Stripe’s REST API over Kirak’s own async HTTP client, so
pip install kirak is all it needs.
2. Credentials & Configuration
Section titled “2. Credentials & Configuration”Step 1: Obtain API Keys
Section titled “Step 1: Obtain API Keys”In the Stripe Dashboard:
- Navigate to Developers $\rightarrow$ API keys.
- Copy your Secret key (
sk_test_...in test mode,sk_live_...in production).
Step 2: Configure kirak.json
Section titled “Step 2: Configure kirak.json”Declare the Stripe instance under payments.providers:
{ "payments": { "default_provider": "stripe", "providers": { "stripe": { "type": "stripe", "success_url": "https://your-domain.com/payment/success", "cancel_url": "https://your-domain.com/payment/cancel", "timeout": 15 } } }}| Setting | Type | Default | Description |
|---|---|---|---|
type |
string | "stripe" |
Provider type identifier |
success_url |
string | "/payment/success" |
Buyer redirect URL after payment completion (Kirak appends ?tid=<transaction_id>). Must be absolute |
cancel_url |
string | "/payment/cancel" |
Buyer redirect URL if checkout is cancelled. Must be absolute |
timeout |
number | 15 |
Maximum seconds allowed for Stripe API calls |
Stripe sends the buyer’s browser to success_url and cancel_url from its own site, so both must be
absolute (https://...). The defaults above are relative and Stripe refuses them: set both, or
payments.success_url and payments.cancel_url for every provider, before taking a payment. A
relative one is refused with 400 STRIPE_ERROR naming the setting, in details.config_key.
Step 3: Set Environment Variables
Section titled “Step 3: Set Environment Variables”Set credentials in your environment. Secrets are never declared in kirak.json:
KIRAK_PAYMENT_STRIPE_SECRET_KEY=sk_test_51...KIRAK_PAYMENT_STRIPE_WEBHOOK_SECRET=whsec_... # Obtained in Step 4[!NOTE] If you name your instance differently in
kirak.json(e.g."stripe_us"), the environment variables becomeKIRAK_PAYMENT_STRIPE_US_SECRET_KEYandKIRAK_PAYMENT_STRIPE_US_WEBHOOK_SECRET.
3. Configuring Webhooks
Section titled “3. Configuring Webhooks”In the Stripe Dashboard, navigate to Developers $\rightarrow$ Webhooks $\rightarrow$ Add endpoint:
- Endpoint URL:
https://your-domain.com/payments/webhook/stripe - Listen to: Events on your account.
- Select at minimum the following events:
- Checkout & One-Time:
checkout.session.completed,checkout.session.async_payment_succeeded,checkout.session.async_payment_failed - Subscriptions & Invoices:
invoice.payment_succeeded,invoice.payment_failed,customer.subscription.updated,customer.subscription.deleted - Refunds:
charge.refunded - Saved Methods & Off-Session:
payment_method.detached,payment_method.updated,payment_method.automatically_updated,customer.updated,payment_intent.succeeded,payment_intent.payment_failed - Disputes:
charge.dispute.created,charge.dispute.closed
- Checkout & One-Time:
- Copy the Signing secret (
whsec_...) and export it asKIRAK_PAYMENT_STRIPE_WEBHOOK_SECRET.
[!WARNING] If
KIRAK_PAYMENT_STRIPE_WEBHOOK_SECRETis unset, all incoming webhooks are rejected with500 WEBHOOK_SECRET_NOT_CONFIGUREDto prevent spoofing.
4. Multi-Database Isolation
Section titled “4. Multi-Database Isolation”If you run multiple Kirak applications or environments (e.g. Staging and Production) against the same Stripe account in test mode, Stripe broadcasts events to all endpoints.
Kirak safely isolates events using the following protections:
- Checkout Sessions: A checkout webhook only updates the database record that originally stored the Stripe Checkout
session_id. - Off-Session Charges: Off-session PaymentIntents store a random identifier (
kirak_ref) in Stripe metadata; events lacking a matching reference inpayment_metaare acknowledged (HTTP 200) and safely skipped. - Setup Sessions: Cards are vaulted only for the customer ID registered by that database instance.
5. Usage Examples
Section titled “5. Usage Examples”5.1 One-Time Checkout
Section titled “5.1 One-Time Checkout”result = await kirak.payments.initiate_payment({ "provider": "stripe", "user_id": "usr_100", "amount": 2999, # $29.99 in cents "currency": "USD", "name": "Standard License", "type": "one_time", "idempotency_key": "order-100-std",})
checkout_url = result["data"]["url"]5.2 Recurring Subscription
Section titled “5.2 Recurring Subscription”result = await kirak.payments.initiate_payment({ "provider": "stripe", "user_id": "usr_100", "amount": 0, # Charged directly from Stripe Price ID "currency": "USD", "name": "Pro Monthly Plan", "type": "subscription", "subscription_plan_id": "price_1NXXXXXXXXXXXXXX",})5.3 Creating the Price That Subscription Charges
Section titled “5.3 Creating the Price That Subscription Charges”The subscription_plan_id above is a Stripe Price, which ensure_plan creates so the plans can live in the app’s code rather than in the dashboard. Set "plan_catalog": true under payments in kirak.json first; it is off by default.
answer = await kirak.payments.ensure_plan({ "provider": "stripe", "key": "pro_monthly_2900", # becomes the Price's lookup_key "product_key": "pro", # the Product the Prices hang off "name": "Pro Monthly Plan", # the Product's name, shown at checkout "amount": 2900, "currency": "usd", "interval": "month", "interval_count": 1, "metadata": {"tier": "pro"},})price_id = answer["data"]["plan_id"] # price_1NXXXXXXXXXXXXXXStripe splits a plan into a Product (what is sold) and a Price (how much, how often), and a Price is immutable. ensure_plan maps onto that: product_key is the Product’s id, created if missing, and key is the Price’s lookup_key, which it searches before creating anything. Because the lookup_key lives on the Price for good, a re-run finds the same Price and reports created: false - unlike an idempotency key, which Stripe forgets after 24 hours. A changed amount needs a new key; the old Price keeps billing its existing subscribers until they are moved with update_subscription.
5.4 Customer Billing Portal
Section titled “5.4 Customer Billing Portal”portal = await kirak.payments.get_billing_portal({ "user_id": "usr_100", "return_url": "https://app.example.com/account", "provider": "stripe",})portal_url = portal["data"]["portal_url"]5.5 Saved Cards & Off-Session Charges
Section titled “5.5 Saved Cards & Off-Session Charges”# Save card during checkout:await kirak.payments.initiate_payment({ "provider": "stripe", "user_id": "usr_100", "amount": 1000, "currency": "USD", "name": "Verification Hold", "type": "one_time", "save_payment_method": True, "consent": {"text_version": "2026-v1"},})
# Charge saved card headless (Admin / System only):charge = await kirak.payments.charge_off_session({ "provider": "stripe", "user_id": "usr_100", "amount": 4500, # $45.00 "currency": "USD", "name": "Usage Overage", "idempotency_key": "overage-usr100-oct",})6. Webhook Event Mapping
Section titled “6. Webhook Event Mapping”Kirak normalizes Stripe webhook events into standard after_webhook event names:
Normalized event |
Gateway Events | Description |
|---|---|---|
payment_completed |
checkout.session.completed (when paid), checkout.session.async_payment_succeeded, payment_intent.succeeded (off-session) |
Payment confirmed and settled |
payment_failed |
invoice.payment_failed, checkout.session.async_payment_failed, payment_intent.payment_failed (declined) |
Payment rejected |
subscription_renewed |
invoice.payment_succeeded |
Recurring subscription billed |
subscription_updated |
customer.subscription.updated |
Subscription terms altered |
subscription_past_due |
customer.subscription.updated (past_due, unpaid) |
Recurring charge failed |
subscription_cancelled |
customer.subscription.deleted |
Subscription canceled |
refund_completed |
charge.refunded |
Refund issued and settled |
dispute_created |
charge.dispute.created |
Customer bank opened chargeback |
dispute_updated |
charge.dispute.closed |
Dispute resolved or closed |
payment_method_saved |
payment_method.updated, customer.updated |
Card details vaulted |
payment_method_removed |
payment_method.detached |
Card removed at Stripe |