public function index(Request $request){
$openid = $this->user_data['openid']; // 数据库获取当前登录者openid;
$order_sn = $request->param('order_sn'); //获取订单号
$ordersmsg = Db::name('order')->where(['order_sn'=>$order_sn])->field('order_amount,order_id')->find(); //订单金额
$goods_name = Db::name('order_goods')->where('order_id',$ordersmsg['order_id'])->value('goods_name');
// 金额
$order_amount = $ordersmsg['order_amount'];
//获取微信配置
$wxConf = config('wechat');
//实例化easyWeChat https://blog.csdn.net/flysnownet/article/details/90257076
// use EasyWeChat\Foundation\Application;
// use EasyWeChat\Payment\Order;
$wxApp = new Application($wxConf);
//支付订单参数
$attributes = [
'trade_type' => 'JSAPI', // JSAPI,NATIVE,APP...
'body' => $goods_name,
'detail' => $goods_name,
'out_trade_no' => $order_sn, //'自己生成自己站点的唯一单号',
'total_fee' => $order_amount*100, // 单位:分// 支付结果通知网址,如果不设置则会使用配置里的默认地址
'notify_url' => ' // 支付回调地址
'openid' => $openid //openid
];
//初始化订单
$order = new Order($attributes);
//实例化支付
$payment = $wxApp->payment;
//预支付
$result = $payment->prepare($order);
if ($result->return_code == 'SUCCESS' && $result->result_code == 'SUCCESS') {
$prepayId = $result->prepay_id;
$json = $payment->configForPayment($prepayId);
return json(['jsondata'=>$json,'code'=>2000]);
}else{
return json(['code'=>2001,'msg'=>$result]);
}
}
// 前端调用上面接口后处理逻辑
wx_pay: function () {
var _that = this;
var token = window.storeWithExpiration.get('token');
var order_sn = _that.order_ing.order_sn; // 进入页面生成的新订单号,从后端获取 保证唯一性
var url = "/jsapipay?token=" + token + "&order_sn=" + order_sn; // 调用上面的代码地址
_that.$axios.get(url).then(function (response) {
if (response.status == 200) {
_that.orderdetailpay(JSON.parse(response.data.jsondata)) // 发起支付
}
}).catch(function (error) {
});
},
// 发起支付的方法
orderdetailpay: function (config) {
//有支付
function jsApiCall(config) {
WeixinJSBridge.invoke('getBrandWCPayRequest', config, function (res) {
//WeixinJSBridge.log(res.err_msg);
if (res.err_msg == "get_brand_wcpay_request:ok") {
// self.RouterLink('personal')
_that.$dialog.alert({mes: '支付成功'})
}else if (res.err_msg == 'get_brand_wcpay_request:cancel') {
_that.$dialog.alert({mes: '已取消支付'})
} else if (res.err_msg == 'get_brand_wcpay_request:fail') {
_that.$dialog.alert({mes: '网络异常'})
}
});
}
if (typeof WeixinJSBridge == "undefined") {
if (document.addEventListener) {
document.addEventListener('WeixinJSBridgeReady', jsApiCall(config), false);
} else if (document.attachEvent) {
document.attachEvent('WeixinJSBridgeReady', jsApiCall(config));
document.attachEvent('onWeixinJSBridgeReady', jsApiCall(config));
}
} else {
jsApiCall(config);
}
},
//后端支付回调
//异步通知
public function notify(Request $request){
$wxConf = config('wechat');
$wxApp = new Application($wxConf);
try{
$response = $wxApp->payment->handleNotify(function ($notify,$successful) {
if($successful){
$out_trade_no = $notify->out_trade_no;
$total_fee = $notify->total_fee; //支付金额(单位:分)
$total_fee2 = $total_fee/100; //转化为单位:元
//变更订单状态 0:未支付;1已支付
$recharge = Db::name('order')->where(['order_sn' => $out_trade_no])->field('order_id,user_id,pay_status')->find();
if($recharge['pay_status'] == 0){
Db::name('order')->where(['order_sn' => $out_trade_no])->update([
'pay_status' => 1,
'pay_name'=>'微信支付',
'pay_time' => time()
]);
Db::name('account_log')->insert([
'user_id'=>$recharge['user_id'],
'change_money'=>$total_fee2,
'pay_points'=>$total_fee2,
'change_time'=>time(),
'desc'=>'购买商品区赠送积分
']);
}
//更新商品已被购买
$order_goods = Db::name('order_goods')->where('order_id',$recharge['order_id'])->find();
Db::name('goods')->where('goods_id',$order_goods['goods_id'])->update(['goods_status'=>2]);
# 支付成功通知
$this->ordermessage($recharge['order_id'],$order_goods['goods_name'],$out_trade_no,$total_fee2);
}
return true;
});
}catch(\EasyWeChat\Core\Exceptions\FaultException $e){
}
}
微信服务号 微信支付 前端加后端处理逻辑
最新推荐文章于 2023-10-13 14:52:18 发布