Simple Solana Payment Button Using PHP
In this tutorial we will see how to embed a crypto payment button in your website and process the payment within an HTTP webhook in PHP, in the most simple way
Register your webhook
By registering your webhook, you will get a webhook ID you can provide in the HTML payment form.
Nb : if you dont need a webhook, just specify 0 (zero) as a hook ID.
In order to register your webhook, you need to use the form available at Solana Payment Links
You will have to specify :
- your wallet address (receiving payments)
- the currency that you will use to provide amount (USD/EUR)
- an email for payment notifications (optional)
- the webhook URL (optional)
- the return URL (optional, redirection when the payment is complete or cancelled)
When the form is submitted, you will get a webhook ID, and a secret that will secure the webhook. You can then just copy and paste the HTML code you will get, that should look like this example :
<form action="https://p-link.io/1/4n9aiKrqNb2UJY3bABwfGNVkwkbYHBuugn4uJWUEh77v/1/USD/Pay/PARAM">
<input type="submit" value="Save">
</form>
Embedding a payment button into my HTML
You can see that the form is only made of a payment URL, complying to the following format :
https://p-link.io/Pay/[walletID]/[requestedAmount]/[webHookID]/[EUR/USD]/[optionalTitle]/[optionalParameter]/[optionalReturnURL]/
So you can just change the parameters :
- requestedAmount : provide the payment amount in USD or EUR
- optionalParameter : a parameter that will be transfered into the payment notification sent
- optionalTitle : a title that will be shown in the payment page
- optionalReturnURL : the URL the user will be redirected to after payment
Processing the payment notifications in PHP
The processing of the payment notification HTTP webhook is very simple, as well in PHP or in any programming language.
The data will be sent as a JSON encoded BODY of an HTTP request with the parameters :
{
myParam : 'the parameter you provided in the payment link or form',
sourcePayment : 'the wallet address of the payer',
paidAmountInToken: 'the amount paid in token',
paidAmountInTokenUi: 'the amount paid in token with decimals',
tokenUsedForPayment: 'the mint address of token used for payment',
EUR_Amount : 'the amount in euros',
USD_Amount : 'the amount in USD',
trx : 'the transaction ID',
secret : 'your secret key'
}
Here is the PHP code in order to process the webhook :
$inputData = file_get_contents("php://input");
$jsonData = json_decode($inputData);
$secret = $jsonData->secret;
if ($secret == "[YOURSECRET]") {
$paymentAmount = $jsonData->USD_Amount;
$payerWallet = $jsonData->sourcePayment;
$transactionID = $jsonData->trx;
$myOrderID = $jsonData->myParam;
// PROCESS THE PAYMENT HERE
}
Demo
You can view an example of a payment page here :
https://p-link.io/1/4n9aiKrqNb2UJY3bABwfGNVkwkbYHBuugn4uJWUEh77v/1/USD/Pay/PARAM
Links
Github page : https://github.com/paracetamol951/Solana-Token-Payment-Gateway
P-Link project page : Solana P-Link Payment gateway