Invoicing in crypto via API
In the last article, we walked through the integration process for simple payment buttons using React framework and plain HTML. If you want to integrate with SMARTy Pay on your backend, you can set up a webhook endpoint and receive notifications upon successful payments.
Step 1. Webhook URL setup
First, we have to configure the webhook URL in the SMARTy Pay Merchant’s Admin Console:
- Navigate to “Settings”.
- Add your URL in the “Webhooks” section at the bottom of the page.
Tip:
If you don’t have a backend URL at the moment, you can test this feature via a free webhook debug service, like webhook.site or many others available on the Internet.
Step 2. Creating an invoice
Previously we were creating invoices via the payment button on the browser side, but today we will use SMARTy Pay backend SDK.
Doing it on a backend side have the advantage that you can pass some additional information along with the invoice, for example, your internal invoice id or some correlation id. These ids can be later used to match the internal invoice with SMARTy invoice in your webhook handling logic.
Here is an example of how to create an invoice using our Node SDK:
import {SmartyPayAPI} from 'smartypay-node-sdk';
const invoice = await SmartyPayAPI.createInvoice({
expiresAt: new Date(Date.now() + 1000 * 60 * 60),
amount: '1.99',
token: 'bUSDT',
metadata: 'YOUR_INTERNAL_INVOICE_ID' // optional
}, {
secretKey: 'YOUR_SECRET_KEY',
publicKey: 'YOUR_API_KEY',
});
const invoiceId = invoice.id;
After you created an invoice on the backend, you should redirect your customer to the SMARTy Pay checkout page:
https://checkout.smartypay.io/invoice?invoice-id=<created-invoice-id>
Step 3. Handling the webhook event
After your customer finishes the payment, SMARTy Pay will make a POST request to the configured webhook URL.
Here is a sample payload:
{
"invoiceId": "39a0eb43-4517-451f-a578-55244f9f40e5",
"paymentAddress": "0x8b727a8b121e9f5c95b852c18035661865e8a0a5",
"amount": "0.1 btBUSD",
"status": "Paid",
"createdAt": "2022-06-01T14:21:12.494322Z",
"expiresAt": "2022-06-01T15:21:11.191Z",
"paidAt": "2022-06-01T14:23:13.268108Z",
"paidAmount": "0.1 btBUSD",
"errorCode": null,
"metadata": "YOUR_PURCHASE_ID"
}
You can use this payload to match SMARTy invoices with your internal data and make corresponding state transitions in your e-commerce system.
To learn more about webhook notifications, please refer to our documentation.