easywechat之微信支付--在thinkPHP5中的使用
1. 准备工作
1.1 easywechat 安装完成
未安装移步至 -> http://www.cnblogs.com/flyphper/p/8484600.html
1.2 确定支付相关的配置参数已经配置好
<?php return [ /** * Debug 模式,bool 值:true/false * * 当值为 false 时,所有的日志都不会记录 */ 'debug' => 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
<?php /** * ====================================== * * Created by Super Demon W. * Author: \/Lin\/ 764582547@qq.com * Date: 2018/2/22 * Time: 17:09 * * ======================================= */ namespace app\home\logic; use think\Model; use EasyWeChat\Foundation\Application; use EasyWeChat\payment\Order; use think\Config; class Wechat1 extends Model { /** * 支付准备 * @param $data * @param $openid * @return array */ public function WeixinPrePay($data, $openid) { $options = Config::get('wechat'); $app = new Application($options); $payment = $app->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
<html> <head> <meta http-equiv="content-type" content="text/html;charset=utf-8"/> <meta name="viewport" content="width=device-width, initial-scale=1"/> <title>发起支付-支付</title> <script type="text/javascript"> //调用微信JS api 支付 function jsApiCall() { WeixinJSBridge.invoke( 'getBrandWCPayRequest',{$jsonData}, function(res){ if (res.err_msg == "get_brand_wcpay_request:ok") { // 支付成功 location.href = '***?type=success'; } if(res.err_msg == "get_brand_wcpay_request:fail") { // 支付失败 location.href = '***?type=fail'; } if (res.err_msg == "get_brand_wcpay_request:cancel") { // 取消支付 location.href = '***?type=cancel'; } } ); } function callpay() { if (typeof WeixinJSBridge == "undefined"){ if( document.addEventListener ){ document.addEventListener('WeixinJSBridgeReady', jsApiCall, false); }else if (document.attachEvent){ document.attachEvent('WeixinJSBridgeReady', jsApiCall); document.attachEvent('onWeixinJSBridgeReady', jsApiCall); } }else{ jsApiCall(); } } callpay(); </script> </head> <body> <div align="center" style="color: #00a157; margin-top: 20%"> 支付中... </div> </body> </html>
注: 统一下单后返回prepay_id后要调用小程序支付函数,有最重要的一步,是需要再次签名的,用统一下单返回的sign(签名)是没有用的。
================== 至此,微信支付结束 ===============