Laravel5.5+EasyWeChat_小程序支付(含回調)


一、支付准備

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文章)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM