详解用vue.js和laravel实现微信支付

2020-06-12 21:10:21易采站长站整理

'notify_url' => $_ENV['APP_URL'].'/api/wx_notify', // 支付结果通知网址,如果不设置则会使用配置里的默认地址
'openid' => $openid, // trade_type=JSAPI,此参数必传,用户在商户appid下的唯一标识,
// ...
];

$order = new Order($attributes);
$result = $this->wechat->payment->prepare($order);
if ($result->return_code == 'SUCCESS' && $result->result_code == 'SUCCESS'){
//创建预订单
$param = [
'out_trade_no'=>$out_trade_no,
'user_id'=>$user_id,
'broadcast_id'=>$broadcast_id,
'speaker_id'=>$speaker_id,
'body'=>$body,
'detail'=>$detail,
'paid_at'=>$paid_at,
'amount'=>$amount,
'flag'=>$flag,
'status'=>$status,
'num'=>$num
];
$this->bill->store($param);
//返回
$prepayId = $result->prepay_id;
$config = $this->wechat->payment->configForPayment($prepayId,false);

return response()->json($config);
}
}

}

4.在相应的控制器里添加回调wxnotify方法


/**
* 这是我自己项目的内部代码示例,具体根据自己的业务逻辑调整,切不可直接拷贝!
*/
public function wxnotify()
{
$response = $this->wechat->payment->handleNotify(function($notify, $successful){
$order = $this->bill->getBillByOrderno($notify->out_trade_no);//查询订单($notify->out_trade_no);
if (!$order) { // 如果订单不存在
return 'Order not exist.'; // 告诉微信,我已经处理完了,订单没找到,别再通知我了
}
// 如果订单存在
// 检查订单是否已经更新过支付状态
if ($order->paid_at) { // 假设订单字段“支付时间”不为空代表已经支付
return true; // 已经支付成功了就不再更新了
}
// 用户是否支付成功
if ($successful) {
// 不是已经支付状态则修改为已经支付状态
$order->paid_at = Carbon::now(); // 更新支付时间为当前时间
$order->status = 'paid';
} else { // 用户支付失败
$order->status = 'paid_fail';
}
$order->save(); // 保存订单
return true; // 返回处理完成
});
return $response;
}

5.vue.js中methods的方法代码参考


wechatpay(){
var param = {
'user_id':this.getStorage(),
'broadcast_id':this.id,
'num':1,
'flag':'buy',