1.2 确定支付相关的配置参数已经配置好
true, /** * 账号基本信息,请从微信公众平台/开放平台获取 */ 'app_id' => '', // AppID 'secret' => '', // AppSecret 'token' => '', // Token 'aes_key' => '', // EncodingAESKey,安全模式下请一定要填写!!! /** * 日志配置 * * level: 日志级别, 可选为: * debug/info/notice/warning/error/critical/alert/emergency * permission:日志文件权限(可选),默认为null(若为null值,monolog会取0644) * file:日志文件位置(绝对路径!!!),要求可写权限 */ 'log' => [ 'level' => 'debug', 'permission' => 0777, 'file' => LOG_PATH.'easywechat.log', ], /** * OAuth 配置 * * scopes:公众平台(snsapi_userinfo / snsapi_base),开放平台:snsapi_login * callback:OAuth授权完成后的回调页地址 */ 'oauth' => [ 'scopes' => ['snsapi_userinfo'], 'callback' => 'home/oauthallback', ], /** * 微信支付 */ 'payment' => [ 'merchant_id' => '', // 商户号 'key' => '', 'cert_path' => '', // XXX: 绝对路径!!!!(前往微信公众平台上下载) 'key_path' => '', // 同上 // 'device_info' => '', // 'sub_app_id' => '', // 'sub_merchant_id' => '', // ... ], /** * Guzzle 全局设置 * * 更多请参考: http://docs.guzzlephp.org/en/latest/request-options.html */ 'guzzle' => [ 'timeout' => 3.0, // 超时时间(秒) 'verify' => true ] ];
2. 支付操作
2.1 添加订单
wechat1.php
payment; $attributes = [ 'body' => '***-充值', 'out_trade_no' => $data['order_sn'], 'total_fee' => $data['money']*100, #'spbill_create_ip' => '***', 'notify_url' => url('**/notify'), // 回调地址 'trade_type' => 'JSAPI', 'openid' => $openid, ]; $order = new Order($attributes); $result = $payment->prepare($order); if ($result->return_code == 'SUCCESS' && $result->result_code == 'SUCCESS') { $prepayId = $result->prepay_id; $json = $payment->configForPayment($prepayId); return ['status' => 200, 'msg' => '', 'data' => $json]; } else { return ['status' => 400, 'msg' => '调起支付失败,请稍后尝试']; } } }
2.2 添加订单后调起支付
towxpay.php
namespace app\home\controller; use app\home\logic\Wechat1; use think\Session; use think\Db; public function towxpay() { $uid = Session::get('user_auth')['uid']; $money = input('recharge_money', ''); $wechat = new Wechat1(); $order_sn = MakeOrderSn(); // 设置订单号 # 添加订单 $data = array( // ...yourdata ); $order_id = db('order')->insertGetId($data); if (!$order_id) { $this->error('订单添加失败'); } $openid = db('member')->where('uid', $uid)->value('openid'); $check = $wechat->WeixinPrePay($data, $openid); if ($check['status'] == 400) { $this->error($check['msg']); } session::set('zejc_order_id', $order_id); $this->assign('jsonData', $check['data']); return $this->fetch(); }
2.3 调取支付页面
towxpay.html
发起支付-支付 支付中...
注: 统一下单后返回prepay_id后要调用小程序支付函数,有最重要的一步,是需要再次签名的,用统一下单返回的sign(签名)是没有用的。
================== 至此,微信支付结束 ===============