Guides

Coming from Stripe in Python

Move code that uses the official Stripe library for Python to Monetize.Pay: the base URL and key, your own Idempotency-Key, webhook verification, the tested versions and the known limits.

This page moves an application that already uses the official stripe library for Python to Monetize.Pay with the fewest changes. Follow it top to bottom. Coming from Stripe lists, call by call, what works, what is refused and what to use instead. Monetize.Pay is not affiliated with or endorsed by Stripe, Inc. Stripe is a trademark of Stripe, Inc. Billing runs in test mode in this release.

1. Point the library at Monetize.Pay

This page covers the global style: the base URL and key set on the stripe module, and calls such as stripe.Customer.create. Code that uses StripeClient needs the same base URL and key set in its own way, and that style is not covered by Monetize.Pay's test run.

Python
stripe.api_base = "https://api.zeropoint.example"
stripe.api_key = os.environ["MONETIZEPAY_KEY"]

Use your Monetize.Pay host from the console in place of api.zeropoint.example, and your Monetize.Pay key (zp_test_...). Change nothing else, and keep the library's default retries. With more than one merchant in a mode, name the merchant on each call with stripe_account='mch_...' (it sends the Stripe-Account header); with one merchant, leave it out. An acct_... id is refused.

2. Settings this library needs

Pass your own Idempotency-Key

Python
key = str(uuid.uuid4())
first = stripe.Customer.create(email="py-idem@example.test", idempotency_key=key)
second = stripe.Customer.create(email="py-idem@example.test", idempotency_key=key)

The same call sent again with the same key returns the first answer and runs nothing twice: second is the customer first created. Pass idempotency_key the same way on every call that charges, refunds or changes a plan, for example a subscription create:

Python
sub = stripe.Subscription.create(
    # customer, items and default_payment_method, as your code sends them today
    idempotency_key=str(uuid.uuid4()),
)
  • Make one key per order or per change, store it and keep it with the order before you make the call, and send that stored key again when you retry. After a timeout, a 5xx or a dropped connection the outcome is unknown, and only the same request with the same key is safe: it replays the first answer or finishes the same payment, never a second one. The test code above makes each key inline only because each of its calls is a new order.
  • Do not rely on a key the library generates. stripe-python adds a random key to a POST when you pass none, but that key is new on every call and your code never sees it, so a retry your code makes carries a new key and is a new request, run again.
  • These requests require a key and answer a request without one 400 idempotency_key_required, with nothing run or charged (the message names idempotency_key): POST /v1/subscriptions, POST /v1/subscriptions/{subscription_id}, POST /v1/invoices/{invoice_id}/pay, POST /v1/refunds, POST /v1/payments, POST /v1/payments/{payment_id}/cancel, POST /v1/payments/{payment_id}/refund, POST /v1/payments/{payment_id}/void, POST /v1/payment_methods, POST /v1/webhooks, POST /v1/webhooks/{webhook_id}/rotate-secret, POST /v1/billing_settings, POST /v1/merchants, POST /v1/merchants/{merchant_id}/terminals and, in test mode, POST /v1/test_helpers/checkout/sessions/{session_id}/confirm and POST /v1/test_helpers/merchants/{merchant_id}/settle (it settles the merchant's test sales now, so a refund can follow). On customer, catalog and checkout session creates a key is optional; with one, a retry replays the first answer.

Read error codes from e.error.code

Branch on Monetize.Pay's code, never on the HTTP status. On a 409, 429 or 5xx answer stripe-python leaves e.code empty: read the code from e.error.code.

3. Receive webhooks

Register each URL once with a plain HTTP request (the library's webhook endpoint calls are not served):

POST /v1/webhooks
Authorization: Bearer zp_test_...
Idempotency-Key: <a new unique value>
Content-Type: application/json

{"url":"https://api.acme.test/zeropoint/stripe","event_types":["checkout.session.completed","invoice.paid","invoice.payment_failed","customer.subscription.created","customer.subscription.updated","customer.subscription.deleted"],"stripe_signature_header":true,"stripe_compatible_payload":true}

Send no Stripe-Account header on it. List every billing type you handle: billing events are opt-in, and an empty list gets payment events only. Keep the whsec_ secret from the answer: it is shown once. Leave api_version out: stripe-python does not check an event's version.

Then verify each delivery with the library's own verifier:

Python
event = stripe.Webhook.construct_event(body, signature, secret)
got = event["data"]["object"]["id"]

body is the raw request body as received (bytes, never parsed and serialized again), signature is the Stripe-Signature header and secret is the whsec_ secret. A body changed by even one byte raises SignatureVerificationError. Every event to this endpoint carries request (id and idempotency_key, both null), as Stripe's events do. What each event holds: Billing webhooks.

4. Versions Monetize.Pay's test run covers

Monetize.Pay's own test-mode run checks each version below with only the base URL and key changed and an explicit Idempotency-Key on the subscription create. Each passes all 15 checks: customers, paging, errors, idempotent replay, prices, subscriptions, invoices, checkout sessions and webhook verification.

  • 2.76.0: it sends no Stripe-Version by default.
  • 5.5.1: it sends no Stripe-Version by default.
  • 7.14.1
  • 11.6.1
  • 14.4.2
  • 17.0.0

Other versions are untested.

5. Known limits

  • After a connection cut mid-answer or a client timeout, the library can raise an error for a call that succeeded. Read the object again, or retry with the same key, which replays the first answer and never charges again.
  • If your code reads an invoice line's price as an object, the request must carry a Stripe-Version dated before 2025-03-31, and the key must be allowed to read prices (catalog:read); without that header the line holds the price id. Set stripe.api_version to the API version your code was written for. Section 4 marks the tested versions that send no Stripe-Version by default. An answer larger than 8 MiB (8,388,608 bytes) is sent with the price ids, not objects: a caller on an older API version whose subscriptions carry very large metadata should list invoices with a smaller limit (an ordinary page of 100 invoices is under a tenth of that bound).
  • Your Stripe ids (cus_, price_, sub_) do not exist here, and saved cards cannot be moved: see Coming from Stripe.