FlutterwaveCheckout() function when your customer clicks the payment button. We’ll handle the payment process and notify you once it’s complete.
An Example
Try it OutYou can try out the payment above. Use the card number
4187427415564246 with CVV 828 and expiry 09/32, or grab a test card or
bank account from our Testing page.- First, you include the Flutterwave Inline library with a script tag:
- Next is the payment button. This is the button the customer clicks after they’ve reviewed their order and are ready to pay you. You’ll attach an
onclickevent handler to this button and call themakePayment()function.
- Finally, in the
makePayment()function, you call theFlutterwaveCheckout()function with some custom parameters:
Calling the FlutterwaveCheckout() Function
Let’s take a closer look at the parameters we passed when calling FlutterwaveCheckout():
| Parameters | Functions |
|---|---|
tx_ref | A unique transaction reference you should generate for each transaction. |
amount | The amount the customer is to pay. In this case, we’re hardcoding it, but you could also dynamically get it from your server or client-side JavaScript. |
currency | The currency the amount is in. Flutterwave supports over a hundred different currencies, so be sure to specify the currency you mean. |
customer | This field holds the customer details. The email field is required (we’ll send the customer a payment receipt afterward unless you’ve disabled that). You can also pass in a name and phone_number. |
public_key | This is your public API key. Here’s how to get it. |
meta (optional) | This will hold any extra details you want to record with the transaction. |
redirect_url (optional) | This is the URL we’ll redirect to on your site after the payment is done. Alternatively, you can specify a callback function. See the redirects and callbacks section. |
customizations (optional) | This allows you to customize the look and feel of the payment modal. You can set a logo, the store name to be displayed (title), and a description for the payment. |
payment_options (optional) | This allows you to specify what payment methods you want to allow payments from. Learn more about this field and the available options here. |
Don’t forget that this is client-side code, so it uses your public key, not
your secret key.
We also recommend generating the transaction reference (
tx_ref) on the server,
so you can compare with previous transactions to be sure there are no
duplicates.FlutterwaveCheckout() returns a JavaScript object with a close() method that can be used to close the modal, which can be useful if you are using callbacks instead of redirects.
Redirects and Callbacks
After calling theFlutterwaveCheckout() function and the payment is done, you can do one of the following:
Option 1: Redirect
If you specify aredirect_url when calling FlutterwaveCheckout() (like in our example above), we’ll redirect the customer to that URL when the payment is made. We’ll append your transaction reference, our transaction ID, and status to the URL like this: tx_ref=ref&transaction_id=30490&status=successful.
On your server, you should handle the redirect and always verify the final state of the transaction.
Option 2: Callback
Alternatively, you can specify acallback instead. In this case, you’ll specify a JavaScript function that we’ll call when the payment is made, passing in the payment object. Here’s what that object looks like:
Note that the
callback will be called after the payment is completed, but
while the modal is still open. You can combine the callback with the
onclose handler for a better experience.callback and onclose function. The callback function sends a verification request to the backend in the background. Then, when the customer closes the modal, the onclose function displays a “verifying” message if the background request is not complete or a thank-you message if the verification was successful. If the verification fails, the customer will be asked to try again or contact support.
The
redirect_url has higher precedence than the callback, so if you
specify both, we’ll use the redirect.callback option to manually close the modal with the modal’s close() method (this won’t trigger the onclose handler):
What if the Payment Fails?
If the payment attempt fails (for instance, due to insufficient funds), you don’t need to do anything. We’ll keep the payment page open so the customer can try again until the payment succeeds or they choose to cancel. If you have webhooks enabled, we’ll send you a notification for each failed payment attempt. This is useful in case you want to later reach out to customers who had issues paying. See our webhooks guide for an example. If the customer closes the payment modal, you can handle this event using theonclose option, described in the next section.
Handling Cancelled Payments
If the customer closes the payment modal before making payment or clicks Cancel Payment, we’ll cancel the payment session, hide the modal, and you won’t be notified. You can specify anonclose function. We’ll call this function when the payment is cancelled, with a single boolean argument that’s true if the payment was not completed.
An example of when you might want to use this is to send such events to your analytics service or reach out to customers who didn’t complete their orders.
Handling Payment Retries and Timeout on Checkout
Flutterwave allows you to configure retries and timeout on checkout to further improve your customers’ experience. Timeouts help you limit the completion time for each payment. Once the timeout period is completed, the payment window is closed, and the user is redirected to the specified URL (redirect_url). Uncompleted transactions are cancelled and marked as failed.
The timeout can be set to a max value of 1440 minutes.
Additionally, you can limit how many attempts users make for failed transactions on checkout. By setting max_retry, the user is prevented from attempting transactions unnecessarily at checkout. When making a payment, the transaction would be cancelled and marked as failed if the user attempts to reach max retry.
Using these configurations can help you improve security on checkout by limiting malicious users’ payment attempts. For example, If timeout and retry for a transaction are set to 10 minutes and five (5) attempts, respectively. The transaction fails automatically if the user makes more than five attempts or spends longer than 10 minutes to complete the transaction.
Framework Integrations
If you’re using a frontend JS framework, we’ve got custom integrations to ensure the payment flow plays nice with your framework. Check out our frontend libraries page to see what’s available.Webhooks
If you’ve set up webhook notifications for your account, we’ll send you a webhook when the payment is completed, as well as for each failed payment attempt. Here’s what the webhook payload looks like:Next Steps
All done! Now you know how to collect payments with Flutterwave Inline. If the JS-based flow doesn’t work for you, there are other options:- HTML checkout lets you do the same thing, but entirely in HTML.
- Flutterwave Standard lets you control the payment flow from your server instead.