Payment Flow
Available Payment Methods
ToffeePay supports the following payment methods:- card: Traditional credit/debit card payments
- tokenized_card: Saved card tokens for returning customers
- apple_pay: Apple Pay for iOS and Safari users
- google_pay: Google Pay for Android and Chrome users
- paypal: PayPal wallet payments
- paylater: PayPal’s buy now, pay later options
- venmo: Venmo wallet payments
Create a Payment Session
Send a server-side request to create a payment session.game_id: Your unique game identifieruser_id: Unique identifier for the playeritem: The item being purchasedtitle: Display name of the itemprice: Price in cents (e.g., 499 = $4.99)currency: Three-letter currency code (USD, EUR, etc.)image: Base64-encoded image or URL
return_url: Deep link to return to after paymentwallet_enabled: Boolean flag that specifies whether Toffee Wallet should be enabled or disabled for the session
Payment Statuses
Session Statuses
Payment sessions can have the following statuses:pending: Payment session created but not yet paidpaid: Payment successfully completedfailed: Payment attempt failedcancelled: Payment session was cancelledexpired: Payment session has expired
Payment Statuses
Individual payment objects (created when processing a session) have these statuses:processing: Payment is being processed by the payment providerauthorized: Payment has been authorized but not yet capturedsucceeded: Payment completed successfullycancelled: Authorized payment was cancelled before capturefailed: Payment processing failed
-
Session starts as
pending -
When payment begins, a Payment object is created with status
processing -
Payment can transition to:
authorized: Payment authorized but not capturedsucceeded: Payment completed successfully (either automatically or via Complete Payment)cancelled: Authorized payment was cancelled via Cancel Paymentfailed: Payment processing failed
authorizedtosucceededor require manual completion via Complete Payment is configured during game setup. -
Session status updates based on final payment status:
paid(if payment succeeded)cancelled(if session was cancelled manually via Cancel Session or after 3 cancelled payment attempts)failed(after 3 failed payment attempts)
Timestamp Fields
Both sessions and payments include relevant timestamp fields: Session timestamps:created_at: When the session was createdpaid_at: When the session was successfully paid (if applicable)failed_at: When the session failed (if applicable)expired_at: When the session expired (if applicable)cancelled_at: When the session was cancelled (if applicable)
created_at: When payment processing beganauthorized_at: When payment was authorized (if applicable)succeeded_at: When payment completed successfully (if applicable)cancelled_at: When payment was cancelled (if applicable)failed_at: When payment failed (if applicable)
Webhook Events
ToffeePay sends webhooks for the following payment-related events:Session Events
session.created: When a payment session is createdsession.paid: When a session is successfully paidsession.failed: When payment attempt related to this session failssession.expired: When a session expires without paymentsession.cancelled: When session is cancelled by user
Payment Events
payment.created: When a payment is created and processing beginspayment.authorized: When payment is authorized but not yet capturedpayment.succeeded: When payment completes successfullypayment.cancelled: When an authorized payment is cancelledpayment.failed: When payment attempt fails
Open the Payment Page
Open the returned paymenturl in the browser or webview.
The user will:
- View the item details and total price
- Pay using Apple Pay, Google Pay, or other supported methods
- Be redirected back to your game via the
return_url
Internationalization
You can customize the payment page language by adding alocale query parameter to the payment URL:
Handle Payment Notifications
ToffeePay notifies you of payment events through two methods:Webhooks
When a payment is completed, ToffeePay sends a signed webhook to your backend. See the Webhooks page for detailed implementation. Sample Payload:Return URL
After payment, the user is redirected to your specifiedreturn_url. This is typically a custom deep link that your game handles.
Example:
Frontend Events
The payment page emits events to the parent window viapostMessage. This is useful for client-side integrations (e.g., if using an iframe or popup).
Event Types:
Payment Events (payload includes payment id):
toffeepay-payment-unreachedtoffeepay-payment-successtoffeepay-payment-failed
id):
toffeepay-session-paidtoffeepay-session-failedtoffeepay-session-expiredtoffeepay-session-cancelledtoffeepay-session-authorized
Cancel a Payment Session
You can cancel a payment session that is still inpending status:
- The session status changes to
cancelled - The
cancelled_attimestamp is set - A
session.cancelledwebhook event is sent - The session cannot be paid and the payment URL becomes invalid
Check Session Status
To confirm session status (especially when handling return URLs), you can use either endpoint:Get Session (Full Details)
Get Session Status (Status Only)
- If
statusispaid, grant the items and show success - If
statusispending, show a waiting screen - If
statusisfailed,cancelledorexpired, inform the user and offer to retry
Complete Payment
When your game wants to control when an authorized payment is captured, use Complete Payment. This is useful for scenarios where you want to authorize payment but only capture it after fulfilling the order (e.g., confirming item availability).Cancel Payment
Cancel an authorized payment before it’s captured. This releases the hold on the customer’s payment method.Get Payment Details
Retrieve details of a payment:List Sessions
Retrieve historical sessions for audit and analytics:List Payments
Retrieve historical payments (successful transactions only):Tax Handling
ToffeePay automatically calculates and applies taxes based on the customer’s location. Tax information is included in session responses when the session is completed.Tax Structure
The tax object in session responses contains:amount: Tax amount in cents (same currency as the item price)
Example
For a \0.50 tax:Image Requirements
Item images must meet these requirements:- Format: PNG, JPG, WebP
- Size: ≤ 300 KB recommended
- Encoding: Base64 with MIME prefix or direct URL
Best Practices
- Authentication: See Authentication for access token management
- Idempotency: Use Idempotency-Key header to prevent duplicate operations
- Webhook verification: Always verify webhook signatures (see Webhooks)
- Error handling: Implement proper error handling for all payment statuses
- User experience: Show loading states and clear success/failure messages
- Security: Use HTTPS for all webhook endpoints and return URLs
- Testing: Use sandbox mode for development and testing
Error Handling
Common payment errors and how to handle them:invalid_game_id: Check your game registration and access token (see Authentication)invalid_currency: Ensure currency code is supported (USD, EUR, etc.)invalid_price: Price must be a positive integer in centsinvalid_image: Check image format and size requirementssession_expired: Payment session has expired, create a new oneinsufficient_funds: User’s payment method was declined
Testing
Use ToffeePay’s sandbox environment for testing:- Use sandbox access tokens (different from production)
- Test with various payment methods and failure scenarios
- Verify webhook delivery and signature verification
- Test return URL handling in your game client
Test Codes
We don’t send real SMS messages for validating OTP in the sandbox environments. We have a static value: 466577 that can be used for getting access.Test Cards
Use these test card numbers in sandbox mode for testing payments: Mastercard- Number:
5100 0600 0000 0002 - Expiry:
12/2029 - CVC:
737
- Number:
4400 0000 0000 0008 - Expiry:
03/2030 - CVC:
737