PHP code example of ponponpay / php-sdk

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

    

ponponpay / php-sdk example snippets




use PolyPay\PolyPay;

$polypay = new PolyPay('your-api-key');

$polypay = new PolyPay('sk_sandbox_your_key');

$checkoutUrl = $polypay->createCheckoutUrl([
    'mch_order_id' => 'ORDER_001',
    'amount'       => 10.00,
    'notify_url'   => 'https://your-site.com/webhook.php',
    'redirect_url' => 'https://your-site.com/success',
    'locale'       => 'en',
]);

header('Location: ' . $checkoutUrl);
exit;

$checkoutUrl = $polypay->createCheckoutUrl([
    'mch_order_id' => 'ORDER_001',
    'amount'       => 10.00,
    'notify_url'   => 'https://your-site.com/webhook.php',
    'redirect_url' => 'https://your-site.com/success',
    'locale'       => 'en',
    'currency'     => 'USDT',
    'network'      => 'Tron',
]);

$checkoutUrl = PolyPay::buildCheckoutUrl([
    'public_key'   => 'pub_your_public_key',
    'amount'       => 10.00,
    'order_id'     => 'ORDER_001',
    'notify_url'   => 'https://your-site.com/webhook.php',
    'redirect_url' => 'https://your-site.com/success',
    'currency'     => 'USDT',
    'network'      => 'Tron',
]);

$order = $polypay->createOrder([
    'mch_order_id' => 'ORDER_001',
    'currency'     => 'USDT',
    'network'      => 'tron',
    'amount'       => 10.00,
    'notify_url'   => 'https://your-site.com/webhook.php',
    'redirect_url' => 'https://your-site.com/success',
]);

echo $order->paymentUrl;  // Redirect user to this URL
echo $order->tradeId;     // PolyPay trade ID
echo $order->address;     // Payment address

// By trade ID
$order = $polypay->getOrderByTradeId('T20240101120000123456');

// By merchant order ID
$order = $polypay->getOrderByMchOrderId('ORDER_001');

echo $order->status;   // paid, pending, expired, cancelled
echo $order->txHash;   // Blockchain transaction hash

try {
    $data = $polypay->webhook()->handle();
    $status = WebhookHandler::resolveStatus($data);

    if ($status === 'paid') {
        // Payment successful!
        // Update your order status here
    }

    http_response_code(200);
    echo 'OK';
} catch (\PolyPay\Exception\SignatureException $e) {
    http_response_code($e->getHttpStatus());
    echo $e->getMessage();
}

$x402 = $polypay->x402([
    'resource' => [
        'payTo' => '0xYourMerchantSettlementWallet',
        'resource' => 'https://api.example.com/premium-data',
        'method' => 'GET',
        'price' => '$0.01',
        'maxAmountRequired' => '10000',
        'network' => 'eip155:8453',
        'asset' => 'USDC',
        'description' => 'Premium market data',
    ],
]);

$result = $x402->verifyAndSettle();

if (!$result['paid']) {
    $x402->sendRequirementAndExit();
}

header('Content-Type: application/json');
echo json_encode(['data' => 'premium payload']);

$polypay = new PolyPay('your-api-key', [
    'api_url'        => 'https://api.polypay.ai',  // API base URL
    'timeout'        => 30,                            // Request timeout (seconds)
    'debug'          => false,                         // Enable debug logging
    'debug_log_file' => '/tmp/polypay-debug.log',    // Debug log file path
]);

use PolyPay\Nonce\NonceStorageInterface;

class RedisNonceStorage implements NonceStorageInterface
{
    private $redis;

    public function __construct(\Redis $redis)
    {
        $this->redis = $redis;
    }

    public function consume(string $nonce, int $ttl = 600): bool
    {
        // SET NX returns true only if key doesn't exist
        return $this->redis->set('polypay_nonce:' . $nonce, '1', ['NX', 'EX' => $ttl]);
    }
}

// Usage
$handler = $polypay->webhook(new RedisNonceStorage($redis));

use PolyPay\Exception\ConfigException;
use PolyPay\Exception\ApiException;
use PolyPay\Exception\SignatureException;

try {
    $order = $polypay->createOrder([...]);
} catch (ConfigException $e) {
    // API Key not configured
} catch (ApiException $e) {
    echo $e->getMessage();       // Error message
    echo $e->getHttpCode();      // HTTP status code
    echo $e->getApiCode();       // Business error code
    echo $e->getResponseBody();  // Raw response body
}



use PolyPay\PolyPay;
use PolyPay\WebhookHandler;

// 初始化
$polypay = new PolyPay('你的API Key');

// 使用 API Key 模式跳转到 PolyPay 托管收银台,由 PolyPay 统一选择支付方式
$checkoutUrl = $polypay->createCheckoutUrl([
    'mch_order_id' => 'ORDER_001',
    'amount'       => 10.00,
    'notify_url'   => 'https://your-site.com/webhook.php',
    'redirect_url' => 'https://your-site.com/success',
    'locale'       => 'zh',
]);

header('Location: ' . $checkoutUrl);
exit;

// 默认跳转页面:https://checkout.polypay.ai/en/checkout
// 中文收银台:locale = zh,对应页面:https://checkout.polypay.ai/zh/checkout

// 如果商户已经明确支付方式,也可以使用 API Key 模式直接创建订单
$order = $polypay->createOrder([
    'mch_order_id' => 'ORDER_001',
    'currency'     => 'USDT',
    'network'      => 'tron',
    'amount'       => 10.00,
    'notify_url'   => 'https://your-site.com/webhook.php',
]);

// 处理回调(自动共享 API Key)
$data = $polypay->webhook()->handle();
if (WebhookHandler::resolveStatus($data) === 'paid') {
    // 支付成功,更新订单状态
}

// x402 Agent 支付保护接口
$x402 = $polypay->x402([
    'resource' => [
        'payTo' => '0x你的EVM收款钱包',
        'resource' => 'https://api.example.com/premium-data',
        'method' => 'GET',
        'price' => '$0.01',
        'maxAmountRequired' => '10000',
        'network' => 'eip155:8453',
        'asset' => 'USDC',
        'description' => '高级数据接口',
    ],
]);

$result = $x402->verifyAndSettle();
if (!$result['paid']) {
    $x402->sendRequirementAndExit();
}
bash
composer 
bash
composer