Implementing Buy Now, Pay Later (BNPL) on Your E-commerce Platform: Tools, APIs, and Code Snippets
In an article that I published a while back, I talked about different platforms that you can use for Buy Now, Pay Later (BNPL). In this article I have selected five top BNPL platforms and solutions and expalined each tool with a code samples.
Introduction
The “Buy Now, Pay Later” (BNPL) payment model has emerged as a powerful tool for boosting conversion rates in e-commerce. By allowing customers to split payments into installments, BNPL increases purchasing power and enhances the overall customer experience. In this article, we explore the leading BNPL tools and APIs available for integration, complete with code snippets to get you started.
Top BNPL Tools and APIs with Code Snippets
1. Klarna
Klarna is one of the most widely adopted BNPL providers, offering various options such as installment payments, pay later in 30 days, and direct payment methods.
- Features:
- Pay in 3 or 4 installments.
- Pay later in 30 days.
- Financing options (6 to 36 months).
- API Integration: Klarna’s API supports order management, payment capture, refunds, and customer identification. Visit Klarna Documentation
const axios = require('axios');
const createOrder = async () => {
const orderData = {
purchase_country: 'US',
purchase_currency: 'USD',
locale: 'en-US',
order_amount: 10000,
order_tax_amount: 2000,
order_lines: [
{
type: 'physical',
name: 'T-shirt',
quantity: 1,
unit_price: 10000,
tax_rate: 2000,
},
],
};
try {
const response = await axios.post(
'https://api.playground.klarna.com/checkout/v3/orders',
orderData,
{
headers: {
Authorization: `Basic ${Buffer.from('username:password').toString('base64')}`,
'Content-Type': 'application/json',
},
}
);
console.log(response.data);
} catch (error) {
console.error(error);
}
};
createOrder();
2. Afterpay/Clearpay
Afterpay (Clearpay in the UK) enables customers to divide purchases into four equal interest-free installments.
- Features:
- Four interest-free payments.
- Easy integration with popular e-commerce platforms like Shopify, WooCommerce, and Magento.
- API Integration: Provides RESTful APIs for managing transactions, customer data, and reporting. Visit Afterpay Documentation
import requests
url = "https://api.us.afterpay.com/v2/payments"
headers = {
"Authorization": "Bearer YOUR_ACCESS_TOKEN",
"Content-Type": "application/json"
}
payload = {
"amount": {
"amount": "100.00",
"currency": "USD"
},
"merchantReference": "ORDER12345",
"consumer": {
"email": "customer@example.com",
"givenNames": "John",
"surname": "Doe"
},
"billing": {
"name": "John Doe",
"line1": "123 Street",
"postcode": "12345",
"countryCode": "US"
}
}
response = requests.post(url, json=payload, headers=headers)
print(response.json())
3. Affirm
Affirm offers a flexible pay-over-time model, letting users choose from 3, 6, or 12-month plans. It integrates seamlessly with major platforms like Shopify and WooCommerce.
- Features: Pay-over-time options with instant credit decision at checkout.
- API Integration: Allows developers to handle checkout, authorization, refunds, and payment capture. Visit Affirm Documentation
const affirmConfig = {
public_api_key: 'YOUR_PUBLIC_KEY',
amount: 10000, // amount in cents
shipping: 500,
items: [
{
display_name: 'Sample Product',
sku: '12345',
unit_price: 10000,
quantity: 1,
},
],
};
affirm.checkout(affirmConfig);
affirm.checkout.open();
4. Sezzle
Sezzle is another popular BNPL provider that lets shoppers pay in four interest-free installments over six weeks.
- Features:
- Interest-free installments.
- Easy integration with major e-commerce platforms.
- API Integration: Offers REST APIs for handling orders, payments, and user authentication. Visit Sezzle Documentation
$url = "https://sandbox.api.sezzle.com/v2/checkouts";
$data = [
"amount" => ["amount_in_cents" => 5000, "currency" => "USD"],
"order" => ["reference_id" => "ORDER123"],
"items" => [["name" => "Product", "quantity" => 1, "price_in_cents" => 5000]]
];
$options = [
'http' => [
'header' => "Content-Type: application/json\r\nAuthorization: Bearer YOUR_TOKEN\r\n",
'method' => 'POST',
'content' => json_encode($data)
]
];
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
echo $result;
5. PayPal Pay Later
PayPal’s BNPL solution includes “Pay in 4” and PayPal Credit, making it an excellent choice for businesses already using PayPal as a payment gateway.
- Features:
- Pay in 4 interest-free payments.
- PayPal Credit for longer-term financing.
- API Integration: PayPal Checkout APIs support BNPL options alongside other payment methods. Visit PayPal Documentation
const paypal = require('@paypal/checkout-server-sdk');
const environment = new paypal.core.SandboxEnvironment('clientId', 'clientSecret');
const client = new paypal.core.PayPalHttpClient(environment);
async function createOrder() {
const request = new paypal.orders.OrdersCreateRequest();
request.prefer("return=representation");
request.requestBody({
intent: 'CAPTURE',
purchase_units: [{
amount: {
currency_code: 'USD',
value: '100.00'
}
}]
});
try {
const order = await client.execute(request);
console.log(order.result);
} catch (err) {
console.error(err);
}
}
createOrder();
Conclusion
Integrating BNPL solutions into your e-commerce platform is a strategic move that can enhance customer experience and boost sales. Each BNPL provider offers unique features and supports various programming languages, making integration simple and effective. By following the provided code snippets, developers can easily get started with BNPL integration and customize it to meet their platform’s specific needs.
Note: Always ensure you handle sensitive information, like API keys and customer data, securely according to industry standards.