Hi 你好,欢迎访问!登录
当前位置:首页 - 原创插件 - 正文 君子好学,自强不息!

PHP单文件实现支付宝当面付示例

2025-10-29原创插件祥技术支持12°c
A+ A-

app_id: 支付宝应用ID

merchant_private_key: 商户私钥

alipay_public_key: 支付宝公钥

notify_url和return_url: 通知和返回地址


<?php
header("Content-Type: text/html; charset=utf-8");
mb_internal_encoding("UTF-8");
ini_set('default_charset', 'utf-8');
// 配置参数
$config = [
    'app_id' => '',
    'merchant_private_key' => '',
    'alipay_public_key' => '',
    'notify_url' => '',
    'return_url' => '',
    'gateway_url' => 'https://openapi.alipay.com/gateway.do',
    'charset' => 'UTF-8',
    'sign_type' => 'RSA2',
    'format' => 'JSON'
];

// 处理支付请求
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['amount'])) {
    $amount = floatval($_POST['amount']);
    $out_trade_no = date('YmdHis') . mt_rand(1000, 9999);
    
    // 构造请求参数
    $biz_content = [
        'out_trade_no' => $out_trade_no,
        'total_amount' => $amount,
        'subject' => '商品支付',
        'store_id' => 'shop001',
        'timeout_express' => '15m'
    ];
    
    $params = [
        'app_id' => $config['app_id'],
        'method' => 'alipay.trade.precreate',
        'charset' => $config['charset'],
        'sign_type' => $config['sign_type'],
        'timestamp' => date('Y-m-d H:i:s'),
        'version' => '1.0',
        'biz_content' => json_encode($biz_content, JSON_UNESCAPED_UNICODE)
    ];
    
    // 生成签名
    $params['sign'] = generateSign($params, $config);
    
    // 发送请求
    $result = sendRequest($config['gateway_url'], $params);
    
    // 处理响应
    if ($result['alipay_trade_precreate_response']['code'] == '10000') {
        $qr_code = $result['alipay_trade_precreate_response']['qr_code'];
        showQrCode($qr_code, $out_trade_no, $amount);
        exit;
    } else {
        showError($result['alipay_trade_precreate_response']['sub_msg']);
    }
}

// 显示支付表单
showPaymentForm();

// 生成签名
function generateSign($params, $config) {
    ksort($params);
    $stringToBeSigned = "";
    foreach ($params as $k => $v) {
        if ($k != 'sign' && $v !== "" && !is_array($v)) {
            $stringToBeSigned .= "$k=$v&";
        }
    }
    $stringToBeSigned = rtrim($stringToBeSigned, '&');
    
    $merchantPrivateKey = "-----BEGIN RSA PRIVATE KEY-----\n" .
        wordwrap($config['merchant_private_key'], 64, "\n", true) .
        "\n-----END RSA PRIVATE KEY-----";
    
    openssl_sign($stringToBeSigned, $sign, $merchantPrivateKey, OPENSSL_ALGO_SHA256);
    return base64_encode($sign);
}

// 发送HTTP请求
function sendRequest($url, $params) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Content-Type: application/x-www-form-urlencoded; charset=UTF-8',
        'Accept: application/json; charset=UTF-8'
    ]);
    $response = curl_exec($ch);
    $response = mb_convert_encoding($response, 'UTF-8', 'auto');
    //print_r($response);
    curl_close($ch);
    return json_decode($response, true);
}

// 显示支付表单
function showPaymentForm() {
    echo <<<HTML
<!DOCTYPE html>
<html>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">
<head>
    <title>支付宝当面付</title>
    <style>
        body { font-family: Arial, sans-serif; max-width: 500px; margin: 0 auto; padding: 20px; }
        .form-group { margin-bottom: 15px; }
        label { display: block; margin-bottom: 5px; }
        input { width: 100%; padding: 8px; box-sizing: border-box; }
        button { background: #108ee9; color: white; border: none; padding: 10px 15px; cursor: pointer; }
        button:hover { background: #0e77ca; }
    </style>
</head>
<body>
    <h2>支付宝当面付</h2>
    <form method="post">
        <div class="form-group">
            <label for="amount">支付金额(元)</label>
            <input type="number" id="amount" name="amount" step="0.01" min="0.01" required>
        </div>
        <button type="submit">生成支付二维码</button>
    </form>
</body>
</html>
HTML;
}

// 显示二维码
function showQrCode($qrCode, $orderNo, $amount) {
    echo <<<HTML
<!DOCTYPE html>
<html>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">
<head>
    <title>支付宝支付</title>
    <style>
        body { font-family: Arial, sans-serif; text-align: center; max-width: 500px; margin: 0 auto; padding: 20px; }
        .info { margin: 20px 0; }
        .qr-code { margin: 20px auto; }
        .amount { font-size: 24px; color: #f44336; }
        a {color: #ffffff;text-decoration: none;background-color: #2a5afd;padding: 10px;border-radius: 6px;}
    </style>
</head>
<body>
    <h2>支付宝支付</h2>
    <div class="info">
        <p>订单号: {$orderNo}</p>
        <p>支付金额: <span class="amount">{$amount}</span> 元</p>
    </div>
    <div class="qr-code">
        <img src="https://api.rcku.cn/qr?data={$qrCode}" alt="支付二维码">
    </div>
    <p>请使用支付宝扫描二维码完成支付</p>
    <p><a href="{$qrCode}" target="_blank" rel="noopener noreferrer">或点击这里唤起支付宝</a></p>
</body>
</html>
HTML;
}

// 显示错误信息
function showError($message) {
    echo <<<HTML
<!DOCTYPE html>
<html>
<head>
    <title>错误</title>
    <style>
        body { font-family: Arial, sans-serif; max-width: 500px; margin: 0 auto; padding: 20px; }
        .error { color: #f44336; padding: 10px; background: #ffeeee; border: 1px solid #ffcccc; }
    </style>
</head>
<body>
    <h2>支付出错</h2>
    <div class="error">
        {$message}
    </div>
    <p><a href="index.php">返回</a></p>
</body>
</html>
HTML;
}
?>


123云盘分享

更新时间:2025年10月29日 21:03:19
      选择打赏方式
    微信赞助

    打赏

    QQ钱包

    打赏

    支付宝赞助

    打赏

    未定义标签

    发表评论

    取消回复

    选填

    必填

    必填

    选填

    请拖动滑块解锁
    >>


      用户登录