PHP支付宝当面付接入示例:生成支付二维码
以下是一个简单的PHP代码,用于对接支付宝当面付:
<?php
// 配置信息
$appId = '你的应用ID';
$merchantPrivateKey = '你的商户私钥';
$alipayPublicKey = '支付宝公钥';
$gatewayUrl = 'https://openapi.alipay.com/gateway.do';
$charset = 'UTF-8';
$format = 'json';
// 生成订单号
$outTradeNo = date('YmdHis') . mt_rand(1000, 9999);
// 订单金额,单位为元
$totalAmount = '0.01';
// 订单标题
$subject = '测试订单';
// 支付宝客户端IP
$clientIp = $_SERVER['REMOTE_ADDR'];
// 请求参数
$requestParams = [
'app_id' => $appId,
'method' => 'alipay.trade.precreate',
'charset' => $charset,
'format' => $format,
'sign_type' => 'RSA2',
'timestamp' => date('Y-m-d H:i:s'),
'version' => '1.0',
'biz_content' => json_encode([
'out_trade_no' => $outTradeNo,
'total_amount' => $totalAmount,
'subject' => $subject,
'store_id' => 'test_store_id',
'timeout_express' => '30m',
'qr_code_timeout_express' => '5m',
'disable_pay_channels' => 'credit_group',
'enable_pay_channels' => 'balance,moneyFund,debitCardExpress',
'terminal_id' => 'test_terminal_id',
'extend_params' => [
'sys_service_provider_id' => '2088511833207846',
],
'seller_id' => 'test_seller_id',
'goods_detail' => [
[
'goods_id' => 'test_goods_id',
'goods_name' => '测试商品',
'quantity' => 1,
'price' => '0.01',
'goods_category' => 'test_goods_category',
'body' => '测试商品描述',
]
]
]),
];
// 生成签名
ksort($requestParams);
$requestUrl = http_build_query($requestParams);
$sign = rtrim(strtr(base64_encode(openssl_sign($requestUrl, $merchantPrivateKey, OPENSSL_ALGO_SHA256)), '+/', '-_'), '=');
// 发送请求
$requestParams['sign'] = $sign;
$requestUrl = $gatewayUrl . '?' . http_build_query($requestParams);
$response = file_get_contents($requestUrl);
// 解析响应
$responseData = json_decode($response, true);
// 处理响应
if ($responseData['alipay_trade_precreate_response']['code'] == '10000') {
$qrCodeUrl = $responseData['alipay_trade_precreate_response']['qr_code'];
// 显示二维码
echo '<img src='' . $qrCodeUrl . ''>';
} else {
echo '生成订单失败:' . $responseData['alipay_trade_precreate_response']['msg'];
}
请注意,上述代码仅为示例,您需要根据实际情况进行修改和调整。同时,为了确保安全性,请勿将您的密钥信息(如应用ID、商户私钥、支付宝公钥等)硬编码在代码中,建议使用配置文件或环境变量等方式进行管理。
原文地址: https://www.cveoy.top/t/topic/l5nx 著作权归作者所有。请勿转载和采集!