PHP code example of coinpay / php-integration

1. Go to this page and download the library: Download coinpay/php-integration 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/ */

    

coinpay / php-integration example snippets


use Coinpay\Finance\CoinPayGateway;
use Coinpay\Finance\Exceptions\CoinPayException;

$gateway = new CoinPayGateway('###COINPAY_API_KEY###');

try {
    $response = $gateway->createPayment(
        1.0,                                                            // The amount to be paid (in Dollar - $).
        'https://your-callback.url',                                    // The URL the user will be redirected to after payment.
        'ref123',                                                       // A unique reference ID for tracking the transaction.
        '[email protected]',                                            // The identity of the payer (email or phone number).
        'Alimo',                                                        // Full name of the payer.
        'Test Payment',                                                 // Description of the payment (e.g. "Payment for order #123").
        '1234567890'                                                    // National identification code of the payer.
    );

    echo "Payment URL: " . $response->url . PHP_EOL;                    // Redirect the user to this URL
    echo "Transaction ID: " . $response->transactionId . PHP_EOL;       // Store this ID if you need it later
} catch (CoinPayException $e) {
    // $e->getHttpStatusCode() — HTTP status code, or 0 if the request never got a response
    // $e->getResponseBody()   — decoded (array) or raw (string) response body, if any
    // $e->isNetworkError()    — true for connection/timeout failures rather than API rejections
    echo "Error: " . $e->getMessage() . PHP_EOL;
}

use Coinpay\Finance\CoinPayGateway;
use Coinpay\Finance\Exceptions\CoinPayException;

$gateway = new CoinPayGateway('###COINPAY_API_KEY###');

try {
    $status = $gateway->checkStatus('transaction-id-from-createPayment');

    echo $status->status . PHP_EOL;          // e.g. "completed", "pending", "failed"
    echo $status->amount . PHP_EOL;
    echo $status->transactionId . PHP_EOL;
    echo $status->reason . PHP_EOL;
    echo $status->transactionHash . PHP_EOL;
    echo $status->network . PHP_EOL;
} catch (CoinPayException $e) {
    echo "Error: " . $e->getMessage() . PHP_EOL;
}

use Coinpay\Finance\CoinPayWebhookVerifier;

$rawBody    = file_get_contents('php://input');
$timestamp  = $_SERVER['HTTP_X_COINPAY_TIMESTAMP'] ?? '';
$deliveryId = $_SERVER['HTTP_X_COINPAY_DELIVERY'] ?? '';
$signature  = $_SERVER['HTTP_X_COINPAY_SIGNATURE'] ?? null;
$secret     = '###YOUR_COINPAY_WEBHOOK_SECRET###';

if (!CoinPayWebhookVerifier::verify($rawBody, $timestamp, $deliveryId, $secret, $signature)) {
    http_response_code(403);
    exit;
}

$payload = json_decode($rawBody, true);

// $payload['status'], $payload['reason'], $payload['transaction_id'],
// $payload['amount'], $payload['transaction_hash']

http_response_code(200);

use Coinpay\Finance\CoinPayWebhookVerifier;

$rawBody = file_get_contents('php://input');
$secret  = '###YOUR_COINPAY_WEBHOOK_SECRET###';

$signature = $_SERVER['HTTP_X_COINPAY_SIGNATURE'] ?? null;

$verified = $signature !== null
    ? CoinPayWebhookVerifier::verify(
        $rawBody,
        $_SERVER['HTTP_X_COINPAY_TIMESTAMP'] ?? '',
        $_SERVER['HTTP_X_COINPAY_DELIVERY'] ?? '',
        $secret,
        $signature
    )
    : CoinPayWebhookVerifier::verifyLegacy($secret, $_SERVER['HTTP_SECRET'] ?? null);

if (!$verified) {
    http_response_code(403);
    exit;
}
bash
composer