1.传入订单id
public function pay(Request $request)
{
include_once app_path() . '/Libs/alipay/wappay/service/AlipayTradeService.php';
include_once app_path() . '/Libs/alipay/wappay/buildermodel/AlipayTradeWapPayContentBuilder.php';
include_once app_path() . '/Libs/alipay/config.php';
$id=$request->get('id');
$orderInfo = Order::where('id', $id)->first();
$body =$orderInfo['price'] . '元';
$subject =$orderInfo['book_id'];
$out_trade_no =$orderInfo['order_code'];
$total_amount =$orderInfo['price'];
$timeout_express = '5m'; //5分钟
$payRequestBuilder = new \AlipayTradeWapPayContentBuilder();
$payRequestBuilder->setBody($body);
$payRequestBuilder->setSubject($subject);
$payRequestBuilder->setOutTradeNo($out_trade_no);
$payRequestBuilder->setTotalAmount($total_amount);
$payRequestBuilder->setTimeExpress($timeout_express);
$payResponse = new \AlipayTradeService($config);
$result = $payResponse->wapPay($payRequestBuilder, $config['return_url'], $config['notify_url']);
return $result;
}
2.异步回调
//异步
public function notify(Request $request)
{
include_once app_path() . '/Libs/alipay/wappay/service/AlipayTradeService.php';
include_once app_path() . '/Libs/alipay/wappay/buildermodel/AlipayTradeWapPayContentBuilder.php';
include_once app_path() . '/Libs/alipay/config.php';
$arr=$request->post();
$alipaySevice = new \AlipayTradeService($config);
$alipaySevice->writeLog(var_export($arr,true));
$result = $alipaySevice->check($arr);
/* 实际验证过程建议商户添加以下校验。
1、商户需要验证该通知数据中的out_trade_no是否为商户系统中创建的订单号,
2、判断total_amount是否确实为该订单的实际金额(即商户订单创建时的金额),
3、校验通知中的seller_id(或者seller_email) 是否为out_trade_no这笔单据的对应的操作方(有的时候,一个商户可能有多个seller_id/seller_email)
4、验证app_id是否为该商户本身。
*/
if($result) {//验证成功
//商户订单号
$out_trade_no = $arr['out_trade_no'];
//支付宝交易号
$trade_no = $arr['trade_no'];
//交易状态
$trade_status = $arr['trade_status'];
if ($arr['trade_status'] == 'TRADE_SUCCESS') {
file_put_contents(public_path().'/test.txt',$out_trade_no);
Order::where('order_code',$out_trade_no)->update(['status'=>1]);
}
//——请根据您的业务逻辑来编写程序(以上代码仅作参考)——
echo "success"; //请不要修改或删除
}else {
//验证失败
echo "fail"; //请不要修改或删除
}
}
3.同步
//同步
public function async()
{
echo 'ok';
}
|