使用easywechat对接微信支付流程的记录

一、相关设置

  1. 拿到微信支付的商户id,mch_id类似:157395****和商户key,自己设置的32位字符。
  2. 在微信商户平台绑定需要微信支付的公众号app_id:wxf84df92f1236****。
  3. 对于公众号还要设置支付目录。比如我的支付页面地址是http://xuxuxu.ittun.com/naruto/vip/buy/page,我在后台设置的支付目录为:http://xuxuxu.ittun.com/naruto/vip/buy/
  4. 公众号的授权域名、js安全域名都要设置。

二、调用流程

  • 引入jssdk

<script src="https://res2.wx.qq.com/open/js/jweixin-1.4.0.js"></script>
  •  前端页面js
function pay() {
        if (!$('#tick').prop('checked'))
        {
            alert('请勾选阅读并同意协议');
            return false;
        }
        $.ajax({
            "type":"post",
            "url":"/naruto/wechat/prepay",
            "dataType":"json",
            "success":function(res){
                if (res.code === 1){
                    WeixinJSBridge.invoke(
                        'getBrandWCPayRequest', $.parseJSON(res.data),
                        function(res){
                            if(res.err_msg === "get_brand_wcpay_request:ok" ) {
                                alert('支付成功');
                                location.href = "{{env('APP_URL')}}"+'/naruto/vip/pay-success/page?order_code='+res.message;
                            }
                        }
                    );
                }
            },
            "error":function(err){
                alert(err.responseJSON.message)
            }
        });
    }
  • 大概流程:点击支付按钮请求后台接口--后台生成一笔待支付订单--通过订单数据请求微信统一下单接口--返回prepay_id--通过预支付id生成前端所需的jsApiParameters,用于调起微信支付--前端支付成功跳转到支付成功页面。  具体是否支付成功通过微信的支付回调的数据来判断。

调用统一下单接口,一直报签名错误,先用签名校验工具校验,如果校验成功,果断重置商户key,结果OK。

//将微信的回调写入文件,方便调试
$xml = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : file_get_contents("php://input");
file_put_contents('wx_notify.txt', date('Y-m-d H:i:s',time()).$xml, FILE_APPEND);
//接受微信支付回调的方法
public function vipNotify()
    {
        $config = config('wechat.payment.default');
        $app = Factory::payment($config);
        BaseClient::setDefaultOptions([
            'curl' => [
                CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4,
            ],
            'verify' => false
        ]);
        $response = $app->handlePaidNotify(function ($message, $fail) {
            $orderCode = $message['out_trade_no'];
            $order = $this->service->getVipOrderBy($orderCode);
            if (!$order || $order->paid_at) {
                return true;
            }
            DB::beginTransaction();
            if ($message['return_code'] === 'SUCCESS' && array_get($message, 'result_code') === 'SUCCESS') {
                if ($order->getAttribute('pay_status')!= VipOrder::UN_PAY || $order->getAttribute('money')*100 != $message['total_fee']) {
                    DB::rollBack();
                    return true;
                }
                $order->paid_at = date("Y-m-d H:i:s"); // 更新支付时间为当前时间
                $order->pay_status = VipOrder::PAID;
                $order->transaction_id = $message['transaction_id'];
                $order->saveOrFail();
                User::query()->find($order->getAttribute('user_id'))->update([
                    'vip_invalid_time' => date('Y-m-d H:i:s', strtotime('+1year'))
                ]);
                DB::commit();
                return true;
            } else {
                DB::rollBack();
                return true;
            }
        });
        return $response;
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值