一、文章介绍
两个文件实现PHP接入支付宝当面付功能,包括发起支付和支付回调应答
二、准备资料
- appid
- 私钥
- 公钥
- 在支付宝官方开启IP白名单
三、支付代码
1.index.php
<?php
//支付配置
$appid = ''; //APPID
$rsaPrivateKey= ''; //私钥
//默认配置
$notifyUrl = 'https://***/notify.php'; //付款成功后的异步回调地址
$outTradeNo = date("YmdHis").uniqid(); //商品订单号
//取商品名称和金额
$payAmount = ''; //付款金额,单位:元
$orderName = ''; //订单标题
//发起支付
$aliPay = new IndexService();
$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'];
if($result['code']=='10000'){
$url = 'https://wenhairu.com/static/api/qr/?size=300&text='.$result['qr_code'];
echo "<img src='{$url}' style='width:300px;'><br>";
echo '二维码内容:'.$result['qr_code'];
}
//IndexService类
class IndexService
{
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;
}
/**
* 发起订单