Skip to content

PHP 接入示例 ​

完整代码 ​

php
<?php
class XindaPay {
    private $apiUrl = 'https://usdt.686869.cloud';
    private $token;

    public function __construct($token) {
        $this->token = $token;
    }

    public function sign(array $params): string {
        unset($params['signature']);
        $params = array_filter($params, function($v) {
            return $v !== null && $v !== '';
        });
        ksort($params);

        $pairs = [];
        foreach ($params as $k => $v) {
            if (is_bool($v)) $v = $v ? 'true' : 'false';
            $pairs[] = $k . '=' . $v;
        }
        $signStr = implode('&', $pairs) . $this->token;

        return strtolower(md5($signStr));
    }

    public function createTransaction(array $params): array {
        $params['signature'] = $this->sign($params);

        $ch = curl_init($this->apiUrl . '/api/v1/order/create-transaction');
        curl_setopt_array($ch, [
            CURLOPT_POST => true,
            CURLOPT_POSTFIELDS => json_encode($params),
            CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_TIMEOUT => 30,
        ]);
        $response = curl_exec($ch);
        $error = curl_error($ch);
        curl_close($ch);

        if ($error) {
            throw new Exception('请求失败: ' . $error);
        }
        return json_decode($response, true);
    }

    public function verifyCallback(array $data): bool {
        $received = $data['signature'] ?? '';
        $expected = $this->sign($data);
        return hash_equals($expected, $received);
    }
}

// 使用
$pay = new XindaPay('your_api_token_here');

$result = $pay->createTransaction([
    'order_id' => 'ORDER_' . time(),
    'amount' => 28.88,
    'fiat' => 'CNY',
    'trade_type' => 'usdt.trc20',
    'name' => '测试商品',
    'notify_url' => 'https://your.site/notify.php',
    'redirect_url' => 'https://your.site/success.php',
]);

if ($result['status_code'] === 200) {
    header("Location: " . $result['data']['payment_url']);
}

XindaPayusdt · 加密货币收款网关