如果是个人测试时只需要更改alipay/config.php内的商户私钥,同步跳转,app_id,支付宝网关和支付宝公钥。
注:public下加入第三方文件
更改alipay/aop/AopClient.php 的第413行为:
foreach ($para_temp as $key => $val){
if (false === $this->checkEmpty($val)) {
//$val = $this->characet($val, $this->postCharset);
$val = str_replace("'","'",$val);
//$val = str_replace("\"",""",$val);
$sHtml.= "<input type='hidden' name='".$key."' value='".$val."'/>";
}
}
在控制器内设置支付功能
//支付
Route::any('pay','base/pay');
public function pay(Request $request)
{
// 获取订单号
$data = $request->post();
$order_sn = $data['WIDout_trade_no'];
// 获取订单名称
$name = $data['WIDsubject'];
// 查询订单价格
$totalPrice = OrderBusiness::getCarData()['totalPrice'];
// 判断订单号是否被篡改
$res = \app\home\model\Order::where('user_id', session('userinfo.id'))->where('order_sn', $order_sn)->find();
if (!$res) {
return errorX('订单号异常');
}
$pay_type = 'alipay';
switch ($pay_type) {
case 'alipay':
{
echo "<form id='alipayment' action='./alipay/pagepay/pagepay.php' method='post'>
<input id='WIDout_trade_no' name='WIDout_trade_no' value='" . $order_sn . "'/>
<input id='WIDsubject' name='WIDsubject' value='" . $name . "'/>
<input id='WIDtotal_amount' name='WIDtotal_amount' value='" . $totalPrice . "'/>
<input id='WIDbody' name='WIDbody' />
</form><script>document.getElementById('alipayment').submit();</script>";
}
}
}
如果考虑支付过程中断网或者网络异常,只需要用try{}catch(){}包裹住控制器内的支付模块,捕获异常即可
支付回调
//支付回调
Route::any('back','base/alipayRollBack');
public function alipayRollBack()
{
$param = input();
require_once("./alipay/config.php");
require_once ("./alipay/pagepay/service/AlipayTradeService.php");
$alipaySevice = new \AlipayTradeService($config);
$result = $alipaySevice->check($param);
if ($result) {//验证成功
echo '验证成功';
} else {
echo "验证失败";
}
}
回调验证成功即为支付成功否则支付失败
|