-- 创造无限可能

thinkphp6使用EasyWeChat在uniapp小程序微信支付

2023-11-21 17:53:55
1487 人浏览 0 人点赞
有用,点赞支持一下

1、第一步,安装EasyWeChat版本,本章文档使用的是4.*版本

使用 composer:

composer require overtrue/wechat:~4.0 -vvv

2、配置

use EasyWeChat\Factory; //引入sdk

$config = [
// 必要配置
'app_id'             => 'xxxxxx', //微信小程序appid
'mch_id'             => 'xxxxxx', //微信支付商户号
'key'                => 'xxxxxx', //微信支付API v2 密钥 (注意: 是v2密钥 是v2密钥 是v2密钥)
// 如需使用敏感接口(如退款、发送红包等)需要配置 API 证书路径(登录商户平台下载 API 证书)
'cert_path'          => 'path/to/your/cert.pem', // XXX: 绝对路径!!!!
'key_path'           => 'path/to/your/key',      // XXX: 绝对路径!!!!
'notify_url'         => $this->request->domain() . '/index/order/payCallback',// 支付回调地址
];

$app = Factory::payment($config);

3、下单 (本章文档使用的是JSAPI)

$result = $app->order->unify([
'body' => '充值积分',
'out_trade_no' => "xxx", //订单号
'total_fee' => 1.00, //支付金额
'spbill_create_ip' => '', // 可选,如不传该参数,SDK 将会自动获取相应 IP 地址
'notify_url' => $this->request->domain() . '/index/order/payCallback', // 支付回调地址
'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
'openid' => $user['openid'], //用户的openid
]);

4、获取签名,并返回给前端

if ($result['return_code'] === 'SUCCESS' && $result['result_code'] === 'SUCCESS') {

$package = "prepay_id=" . $result['prepay_id']; 

//需要进行二次签名(重要!重要!重要!)
//$result = $app->jssdk->bridgeConfig($result['prepay_id'], false);
$result = $app->jssdk->appConfig($result['prepay_id']);

$time = time();
$result['timeStamp'] = "$time";
$result['signType'] = "MD5"; //默认加密方式
$result['package'] = $package;

return $this->success('成功', $result);
}

5、前端uniapp的uni.requestPayment是一个统一各平台的客户端支付API

    //拉起支付
    pay(){
        http.post('/order/payment', {order_number:"xxxx"}).then((res) => {
            let payParams = res.data
            uni.requestPayment({
                provider: 'wxpay',
                timeStamp: payParams.timeStamp,
                nonceStr: payParams.nonceStr,
                package: payParams.package,
                signType: payParams.signType,
                paySign: payParams.paySign,
                success: function (res) {
                    console.log('支付成功');
                    console.log(res);
                    uni.showToast({
                        title: '支付成功',
                        icon: "success"
                    })
                },
                fail: function (res) {
                    console.log(res);
                    console.log('支付失败');
                    uni.showToast({
                        title: '支付失败',
                        icon: "error"
                    })
                }
            });
        })
    },

6、支付回调



    //支付回调
    public function payCallback()
    {
        try{
//            $myfile = fopen(public_path()."/pay_log.txt", "w+") or die("Unable to open file!");


            $xml = file_get_contents('php://input');
            $data = $this->xmltoarrayx($xml);

//            fwrite($myfile, $xml);
//            fclose($myfile);

            if ($data == null) {
                $data = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : '';
            }

            if (empty($data) || $data == null || $data == '') {
                //阻止微信接口反复回调接口  文档地址 https://pay.weixin.qq.com/wiki/doc/api/H5.php?chapter=9_7&index=7,下面这句非常重要!!!
                //$str = '';
                //echo $str;
				$return_msg = 'OK';
				$return_code = 'SUCCESS';
				return json_encode([
					"return_msg" => $return_msg,
					"return_code" => $return_code
				]);
                exit('Notify 非法回调');
            }

            Log::info('支付回调数据 -> ' . json_encode($data));

            $out_trade_no = isset($data['out_trade_no']) && !empty($data['out_trade_no']) ? $data['out_trade_no'] : 0;

            if ($data['return_code'] == 'SUCCESS' && $data['result_code'] == 'SUCCESS') {
                // 查询订单号
                $order = OrderModel::where('uuid', $out_trade_no)->find();
                if($order){
                    //更改订单支付状态
                    $order->pay_state = 1;
                    $order->save();
					
					//更新金额等操作
			

                }
            }
			
			//回调给微信,告知接收成功
			//
            //$str = ``;
            //echo $str;
			$return_msg = 'OK';
			$return_code = 'SUCCESS';
			return json_encode([
				"return_msg" => $return_msg,
				"return_code" => $return_code
			]);
        }catch (\Exception $e){
//            $myfile = fopen(public_path()."/pay_log_error.txt", "w+") or die("Unable to open file!");
//            fwrite($myfile, $e->getMessage());
//            fclose($myfile);
            Log::error('支付回调异常 -> ' . $e->getMessage());
        }
    }
	
	
	
    // xml 转数组
    protected function xmlToArrayx($xml)
    {
        if (!$xml) {
            return false;
        }
        libxml_disable_entity_loader(true);
        $arr = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        return $arr;
    }

附:有什么参错上不懂的可以看官方文档
微信支付JSAPI调起支付官方文档:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_7&index=6
微信支付统一下单官方文档:https://pay.weixin.qq.com/wiki/doc/api/H5.php?chapter=9_20&index=1
开发接入文档:https://pay.weixin.qq.com/wiki/doc/api/index.html
微信支付接口签名校验工具: https://pay.weixin.qq.com/wiki/doc/api/micropay.php?chapter=20_1