PHP code example of mercanpay / mercanpay-php

1. Go to this page and download the library: Download mercanpay/mercanpay-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mercanpay / mercanpay-php example snippets


use MercanPay\MercanPay;
use MercanPay\MercanPayException;

$zp = new MercanPay('https://mercanpay.com', getenv('MERCANPAY_API_KEY'));

// Invoice priced in USD — the customer picks the coin on the payment page
$invoice = $zp->createUsdInvoice('25', ['order_id' => 'order-1000']);

// Checkout in a fixed coin: create an invoice and redirect the customer to the hosted payment page
$invoice = $zp->createInvoice('USDT', '49.90', [
    'order_id'     => 'order-1001',              // idempotent: retries return the same invoice
    'callback_url' => 'https://shop.example/mercanpay-webhook.php',
    'metadata'     => ['customer_id' => 42],
]);
header('Location: ' . $invoice['payment_url']);

$body = file_get_contents('php://input');
if (!MercanPay::verifyWebhook(getenv('MERCANPAY_WEBHOOK_SECRET'), $body, $_SERVER['HTTP_X_MERCANPAY_SIGNATURE'] ?? '')) {
    http_response_code(401);
    exit;
}
$event = json_decode($body, true);
if ($event['type'] === 'invoice.paid' || $event['type'] === 'invoice.overpaid') {
    $orderId = $event['data']['invoice']['order_id'];
    // mark order as paid — make this idempotent: the same event id can be delivered more than once
}
http_response_code(200);

$w = $zp->createWithdrawal('USDT', '10', 'T...', 'payout-2024-001');
echo $w['status']; // queued -> processing -> completed
bash
composer