QQ扫一扫联系
<?php
header('Content-type:text/html; Charset=utf-8');
/*** 请填写以下配置信息 ***/
$appid = 'xxxxx'; //https://open.alipay.com 账户中心->密钥管理->开放平台密钥,填写添加了电脑网站支付的应用的APPID
$notifyUrl = 'http://www.xxx.com/alipay/notify.php'; //付款成功后的异步回调地址
$outTradeNo = uniqid(); //你自己的商品订单号,不能重复
$payAmount = 0.01; //付款金额,单位:元
$orderName = '支付测试'; //订单标题
$signType = 'RSA2'; //签名算法类型,支持RSA2和RSA,推荐使用RSA2
$rsaPrivateKey='xxxxx'; //商户私钥,填写对应签名算法类型的私钥
/*** 配置结束 ***/
$aliPay = new AlipayService();
$aliPay->setAppid($appid);
$aliPay->setNotifyUrl($notifyUrl);
$aliPay->setRsaPrivateKey($rsaPrivateKey);
$aliPay->setTotalFee($payAmount);
$aliPay->setOutTradeNo($outTradeNo);
$aliPay->setOrderName($orderName);
$result = $aliPay->doPay();
$result = $result['alipay_trade_precreate_response'];
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>支付宝支付 - <?php echo $orderName; ?></title>
<style>
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: 'PingFang SC', 'Helvetica Neue', Arial, sans-serif;
background-color: #f5f5f5;
color: #333;
line-height: 1.6;
padding: 20px;
}
.payment-container {
max-width: 500px;
margin: 30px auto;
background: #fff;
border-radius: 12px;
padding: 25px;
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}
.payment-header {
text-align: center;
margin-bottom: 20px;
}
.payment-header h2 {
color: #1677FF;
font-weight: 600;
margin-bottom: 10px;
}
.payment-amount {
text-align: center;
margin: 25px 0;
}
.amount-number {
font-size: 36px;
font-weight: 700;
color: #FF5722;
}
.amount-unit {
font-size: 16px;
color: #666;
}
.qr-code-container {
text-align: center;
margin: 20px 0;
padding: 15px;
background: #f9f9f9;
border-radius: 8px;
}
.qr-code {
width: 220px;
height: 220px;
margin: 0 auto;
border: 1px solid #eee;
padding: 10px;
background: white;
}
.payment-details {
margin: 20px 0;
padding: 15px;
background: #f9f9f9;
border-radius: 8px;
font-size: 14px;
}
.detail-item {
display: flex;
justify-content: space-between;
margin-bottom: 10px;
padding-bottom: 10px;
border-bottom: 1px dashed #eee;
}
.detail-item:last-child {
border-bottom: none;
margin-bottom: 0;
padding-bottom: 0;
}
.detail-label {
color: #666;
}
.detail-value {
font-weight: 500;
text-align: right;
max-width: 60%;
word-break: break-all;
}
.payment-instruction {
margin-top: 20px;
padding: 12px;
background: #e8f4ff;
border-radius: 6px;
color: #1677FF;
font-size: 14px;
text-align: center;
}
.payment-footer {
text-align: center;
margin-top: 20px;
color: #999;
font-size: 13px;
}
.alipay-logo {
width: 50px;
height: 50px;
margin: 0 auto 15px;
display: block;
}
/* 电脑端特定样式 */
@media (min-width: 768px) {
.payment-container {
padding: 30px;
}
.qr-code {
width: 250px;
height: 250px;
}
}
/* 错误提示样式 */
.error-message {
color: #f44336;
text-align: center;
padding: 20px;
font-size: 16px;
}
</style>
</head>
<body>
<?php if($result['code'] && $result['code']=='10000'): ?>
<div>
<div>
<svg viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg">
<path d="M789.6 148.3c-18.5-4.6-37.9-7.2-57.9-7.2-84.6 0-160.4 35.4-214.2 92-53.8-56.6-129.6-92-214.2-92-20 0-39.4 2.6-57.9 7.2C169.4 183.4 96 280.7 96 392.1c0 137.2 110.8 248.4 247.5 248.4 49.9 0 96.2-15.2 134.9-41.1 3.2 20.1 12.9 38.6 27.6 53.3 50.5 50.5 132.5 50.5 183 0 50.5-50.5 50.5-132.5 0-183-14.7-14.7-33.2-24.4-53.3-27.6 25.9-38.7 41.1-85 41.1-134.9.1-111.4-73.3-208.7-175.5-243.8zM343.5 554.5c-89.7 0-162.4-72.7-162.4-162.4S253.8 229.7 343.5 229.7 505.9 302.4 505.9 392.1 433.2 554.5 343.5 554.5z" fill="#1677FF"/>
</svg>
<h2><?php echo $orderName; ?></h2>
<p>请使用支付宝扫码完成支付</p>
</div>
<div>
<span><?php echo $payAmount; ?></span>
<span>元</span>
</div>
<div>
<img src="https://api.2dcode.biz/v1/create-qr-code?data=<?php echo urlencode($result['qr_code']); ?>&size=250x250&format=png&error_correction=H&border=2" alt="支付宝支付二维码">
</div>
<div>
<div>
<span>订单编号</span>
<span><?php echo $outTradeNo; ?></span>
</div>
<div>
<span>支付方式</span>
<span>支付宝支付</span>
</div>
<div>
<span>创建时间</span>
<span><?php echo date('Y-m-d H:i:s'); ?></span>
</div>
</div>
<div>
打开支付宝扫一扫,扫描上方二维码完成支付
</div>
<div>
支付完成后,系统将自动处理您的订单
</div>
</div>
<?php else: ?>
<div>
<?php echo isset($result['msg']) ? $result['msg'] : '支付请求失败'; ?>
<?php echo isset($result['sub_msg']) ? ' : '.$result['sub_msg'] : ''; ?>
</div>
<?php endif; ?>
</body>
</html>
<?php
class AlipayService
{
protected $appId;
protected $notifyUrl;
protected $charset;
//私钥值
protected $rsaPrivateKey;
protected $totalFee;
protected $outTradeNo;
protected $orderName;
public function __construct()
{
$this->charset = 'utf-8';
}
public function setAppid($appid)
{
$this->appId = $appid;
}
public function setNotifyUrl($notifyUrl)
{
$this->notifyUrl = $notifyUrl;
}
public function setRsaPrivateKey($saPrivateKey)
{
$this->rsaPrivateKey = $saPrivateKey;
}
public function setTotalFee($payAmount)
{
$this->totalFee = $payAmount;
}
public function setOutTradeNo($outTradeNo)
{
$this->outTradeNo = $outTradeNo;
}
public function setOrderName($orderName)
{
$this->orderName = $orderName;
}
/**
* 发起订单
* @return array
*/
public function doPay()
{
//请求参数
$requestConfigs = array(
'out_trade_no'=>$this->outTradeNo,
'total_amount'=>$this->totalFee, //单位 元
'subject'=>$this->orderName, //订单标题
'timeout_express'=>'2h' //该笔订单允许的最晚付款时间
);
$commonConfigs = array(
//公共参数
'app_id' => $this->appId,
'method' => 'alipay.trade.precreate',
'format' => 'JSON',
'charset'=>$this->charset,
'sign_type'=>'RSA2',
'timestamp'=>date('Y-m-d H:i:s'),
'version'=>'1.0',
'notify_url' => $this->notifyUrl,
'biz_content'=>json_encode($requestConfigs),
);
$commonConfigs["sign"] = $this->generateSign($commonConfigs, $commonConfigs['sign_type']);
$result = $this->curlPost('https://openapi.alipay.com/gateway.do?charset='.$this->charset,$commonConfigs);
return json_decode($result,true);
}
public function generateSign($params, $signType = "RSA") {
return $this->sign($this->getSignContent($params), $signType);
}
protected function sign($data, $signType = "RSA") {
$priKey=$this->rsaPrivateKey;
$res = "-----BEGIN RSA PRIVATE KEY-----\n" .
wordwrap($priKey, 64, "\n", true) .
"\n-----END RSA PRIVATE KEY-----";
($res) or die('您使用的私钥格式错误,请检查RSA私钥配置');
if ("RSA2" == $signType) {
openssl_sign($data, $sign, $res, version_compare(PHP_VERSION,'5.4.0', '<') ? SHA256 : OPENSSL_ALGO_SHA256);
} else {
openssl_sign($data, $sign, $res);
}
$sign = base64_encode($sign);
return $sign;
}
protected function checkEmpty($value) {
if (!isset($value))
return true;
if ($value === null)
return true;
if (trim($value) === "")
return true;
return false;
}
public function getSignContent($params) {
ksort($params);
$stringToBeSigned = "";
$i = 0;
foreach ($params as $k => $v) {
if (false === $this->checkEmpty($v) && "@" != substr($v, 0, 1)) {
$v = $this->characet($v, $this->charset);
if ($i == 0) {
$stringToBeSigned .= "$k" . "=" . "$v";
} else {
$stringToBeSigned .= "&" . "$k" . "=" . "$v";
}
$i++;
}
}
unset ($k, $v);
return $stringToBeSigned;
}
function characet($data, $targetCharset) {
if (!empty($data)) {
$fileType = $this->charset;
if (strcasecmp($fileType, $targetCharset) != 0) {
$data = mb_convert_encoding($data, $targetCharset, $fileType);
}
}
return $data;
}
public function curlPost($url = '', $postData = '', $options = array())
{
if (is_array($postData)) {
$postData = http_build_query($postData);
}
$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, $postData);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
if (!empty($options)) {
curl_setopt_array($ch, $options);
}
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
}