yehuisir

一、支付准备

1. 登录微信公众平台,到小程序后台获取小程序应用信息:APP_ID(应用ID)、APP_SECRET(应用秘钥)

2. 登录微信商户平台,获取商户信息:MCH_ID(商户ID)、MCH_KEY(商户公钥)

3. 在商户平台配置中设置回调网址授权。

二、小程序调用支付代码

use Config;
use EasyWeChat\Factory;
 
public function pay()
{
    $options = [
        \'app_id\' => Config.get(pay.app_id),
        \'mch_id\' => Config.get(pay.mch_id),
        \'key\' => Config.get(pay.mch_key),
        \'notify_url\' => \'https://example.com/notify\'
    ];
 
    $payment = Factory::payment($options);
    $jssdk = $payment->jssdk;
 
    $attributes = [
        \'trade_type\' => \'JSAPI\',              // 支付方式,小程序支付使用JSAPI
        \'body\' => \'这是一个测试订单\',            // 订单说明
        \'out_trade_no\' => \'wyevcweu2178cec\',  // 自定义订单号
        \'total_fee\' => 1 * 100,               // 单位:分
        \'openid\' => $openid                   // 当前用户的openId
    ];
 
    $result = $payment->order->unify($attributes);
 
    if ($result[\'return_code\'] == \'SUCCESS\' && $result[\'result_code\'] == \'SUCCESS\') {
        $prepayId = $result[\'prepay_id\'];
        $config = $jssdk->sdkConfig($prepayId);
        return response($config);
    }
 
    if ($result[\'return_code\'] == \'FAIL\' && array_key_exists(\'return_msg\', $result)) {
        return $this->responseError(-1, $result[\'return_msg\']);
    }
 
    return $this->responseError(-1, $result[\'err_code_des\']);
}

三、回调方法

    public function notify(Request $request)
    {
        $notify = $this->getNotify();
 
        $options = [
            \'app_id\' => $notify->appid,
            \'mch_id\' => Config.get(pay.mch_id),
            \'key\' => Config.get(pay.mch_key),
            \'notify_url\' => \'https://example.com/notify\'
        ];
 
        $payment = Factory::payment($options);
 
        $response = $payment->handlePaidNotify(function ($message, $fail)
        {
            // 根据返回的订单号查询订单数据
            $order = $this->order->findBy(\'order_num\', $message[\'out_trade_no\']);
            
            if (!$order) {
                $fail(\'Order not exist.\');
            }
            
            if ($order->pay_status  == \'已支付\') {
                return true;
            }
            
            // 支付成功后的业务逻辑
            if($message[\'result_code\'] === \'SUCCESS\')
            {
                ……
 
            }
            
            return true;
        });
 
        return $response;
    }
 
    private function getNotify(Request $request = null)
    {
        $request = Request::createFromGlobals();
    
        try {
            $xml = XML::parse(strval($request->getContent()));
        } catch (\Throwable $e) {
            throw new Exception(\'Invalid request XML: \' . $e->getMessage(), 400);
        }
    
        if (!is_array($xml) || empty($xml)) {
            throw new Exception(\'Invalid request XML.\', 400);
        }
 
        return new Collection($xml);
    }

 附:回调返回的XML值 

/*
 *    小程序支付回调参数
 */
 
{
    xml: {
        "appid": "小程序序应用id",
        "bank_type": "CFT",
        "cash_fee": "1",
        "fee_type": "CNY",
        "is_subscribe": "N",
        "mch_id": "微信商户应用id",
        "nonce_str": "5b8d273d5f7d9",
        "openid": "oEncxK5efj-NDZHlFB1uHDbc02Oxo",
        "out_trade_no": "db76c530af7311e8b58200163e0ee306",
        "result_code": "SUCCESS",
        "return_code": "SUCCESS",
        "sign": "4BEE4F02914454E692FEAF95859EA642",
        "time_end": "20180903202121",
        "total_fee": "1",
        "trade_type": "JSAPI",
        "transaction_id": "4200000172201809034664407865"
    }
}

 


版权声明:本文为CSDN博主「追风2019」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/createno_1/article/details/82377998

博主:https://blog.csdn.net/createno_1?t=1  (该博客大量原创laravel5.5文章)

分类:

技术点:

相关文章: