Example: Setting Up Inline Payment
html
Calling the ModemPayCheckout() Function
Let’s examine the parameters provided when invokingModemPayCheckout() in detail:
| Field | Description | Example |
|---|---|---|
amount | The amount to be charged. | 450 (for GMD 450.00) |
currency | The transaction currency (default: GMD). | "GMD" |
payment_methods | Available payment methods (card, wallet, bank). | "card,wallet,bank" |
title | A short title describing the transaction. | "Course Payment" |
description | Detailed description of the transaction. | "Access to Premium Content" |
customer | A unique identifier for the customer. | "7e1908bb-bd62-4d20-9af7-3463a150fd98" |
customer_email | Customer’s email address. | "customer@example.com" |
customer_name | Full name of the customer. | "John Doe" |
customer_phone | Customer’s phone number. | "7000000" |
metadata | A JSON object for additional payment information. | { "order_id": "98765" } |
return_url | URL to redirect to after successful payment. | "https://yourwebsite.com/success" |
cancel_url | URL to redirect to if the payment is canceled. | "https://yourwebsite.com/cancel" |
callback | Function to handle post-payment actions. | (transaction) => { ... } |
onClose | Function to handle when the modal is closed or payment is canceled. | (cancelled) => { ... } |
Public KeyKeep in mind that this is client-side code, so it utilizes your public key rather than your secret key.
How Callbacks and onClose Work
Here’s an example that demonstrates how to use both thecallback and onClose functions. The callback function sends a verification request to the backend in the background. Meanwhile, the onClose function handles the modal closure by displaying a “verifying” message if the background request is still in progress, or a thank-you message if the verification succeeds. If the verification fails, the user is prompted to retry or contact support.
View: https://codepen.io/mercury-sms/embed/EaYNGOv
callback: Handling Successful Payments
The callback function allows you to perform actions after a successful payment. For instance, you can verify the transaction, send a confirmation email, or update your database. The transaction parameter contains essential information about the payment.
The
redirect_url takes priority over the callback. If both are provided, the redirect_url will be used.javascript
Always verify the payment on your server after the transaction, even if a callback was used.
Keep in mind that the
callback function is triggered after the payment is completed, even while the modal remains open. For an enhanced user experience, you can combine the callback with the onClose handler.onClose: Handling Payment Cancellations
The onClose function is invoked when the customer cancels the payment by closing the modal. This is particularly useful for logging or notifying users of incomplete transactions.
javascript
Precedence Between callback, return_url, and onClose
-
return_urlvscallback:
If both are provided, thereturn_urlwill take precedence, and the user will be redirected to the specified URL after the payment instead of executing thecallbackfunction. -
cancel_urlvsonClose:
If acancel_urlis provided, it overrides theonClosefunction, and the user will be redirected to the specified URL upon cancellation.
Webhooks
If you have configured webhook notifications for your account, we will send a webhook when the payment is completed, as well as for any failed payment attempts. Here’s an example of the webhook payload:Why Use Inline Payments?
Inline payment integrations provide a frictionless experience by letting users complete payments without leaving your page. This approach minimizes drop-offs during checkout and enhances customer satisfaction. Perfect for:- E-commerce Sites: Add quick payment buttons on product pages.
- Subscription Services: Charge for recurring services effortlessly.
- Event Ticketing: Allow customers to book and pay on the same page.
callback and onClose, you gain full control over the payment flow and can provide a seamless experience tailored to your users’ needs.