django_program.sponsors.services¶
Bulk purchase service for sponsor voucher checkout flows.
Orchestrates the lifecycle of a sponsor’s bulk voucher purchase: creating the purchase record, initiating a Stripe Checkout Session, fulfilling the order with generated voucher codes after payment, and handling the webhook callback.
Classes
Stateless service for sponsor bulk voucher purchase operations. |
Exceptions
Raised when a bulk purchase operation fails. |
- exception django_program.sponsors.services.BulkPurchaseError[source]¶
Bases:
ExceptionRaised when a bulk purchase operation fails.
- class django_program.sponsors.services.BulkPurchaseService[source]¶
Bases:
objectStateless service for sponsor bulk voucher purchase operations.
Manages the full purchase lifecycle from creation through Stripe Checkout to voucher code generation upon successful payment.
- static create_checkout_session(bulk_purchase, success_url, cancel_url)[source]¶
Create a Stripe Checkout Session for the bulk purchase total.
Uses the conference’s Stripe credentials to create a Checkout Session in
paymentmode. Stores the session ID on theBulkPurchaserecord and transitions the status to PROCESSING.- Parameters:
bulk_purchase (
BulkPurchase) – The bulk purchase to create a session for.success_url (
str) – URL Stripe redirects to after successful payment.cancel_url (
str) – URL Stripe redirects to if the user cancels.
- Return type:
- Returns:
The Stripe Checkout Session URL for redirecting the user.
- Raises:
BulkPurchaseError – If the purchase is not in PENDING state or has a zero total.
ValueError – If the conference has no Stripe secret key.
- static fulfill_bulk_purchase(bulk_purchase)[source]¶
Generate voucher codes for a paid bulk purchase.
Idempotent: returns an empty list if the purchase is already fulfilled (status is PAID and vouchers have been generated). Generates codes using the voucher service’s
generate_voucher_codes()and createsBulkPurchaseVoucherlinks.- Parameters:
bulk_purchase (
BulkPurchase) – The bulk purchase to fulfill.- Return type:
- Returns:
List of newly created
Voucherinstances, or an empty list if already fulfilled.- Raises:
BulkPurchaseError – If the purchase is in a state that cannot be fulfilled (e.g. PENDING, FAILED, REFUNDED).
- static handle_checkout_webhook(session_id)[source]¶
Handle a Stripe
checkout.session.completedevent for bulk purchases.Looks up the
BulkPurchaseby its stored checkout session ID, extracts the payment intent ID from the session data, and fulfills the purchase by generating voucher codes.- Parameters:
session_id (
str) – The Stripe Checkout Session ID from the webhook event.- Return type:
- Returns:
The fulfilled
BulkPurchase, orNoneif no matching purchase was found (the event may belong to a regular registration checkout).
- static refund_bulk_purchase(bulk_purchase)[source]¶
Mark a bulk purchase as REFUNDED and deactivate all linked vouchers.
Called when a
charge.refundedwebhook identifies a payment intent belonging to a bulk purchase. Transitions the purchase to REFUNDED and setsis_active=Falseon every voucher generated for it.- Parameters:
bulk_purchase (
BulkPurchase) – The bulk purchase to refund.- Return type:
- static mark_failed(bulk_purchase)[source]¶
Transition a bulk purchase to FAILED state.
Used when a checkout session expires or the payment is declined. Only transitions from PENDING or PROCESSING to avoid overwriting a purchase that has already been fulfilled (PAID) or refunded.
- Parameters:
bulk_purchase (
BulkPurchase) – The bulk purchase to mark as failed.- Return type: