easywechat php支付,easywechat之微信支付--在thinkPHP5中的使用

来源:csdn博客 分类: 文章浏览史 发布时间:2023-03-07 21:31:54 最后更新:2023-03-07 浏览:173
转载声明:
本文为摘录自“csdn博客”,版权归原作者所有。
温馨提示:
为了更好的体验,请点击原文链接进行浏览
摘录时间:
2023-03-07 21:31:54

1. 准备工作

1.1 easywechat 安装完成

未安装移步至 -> http://www.cnblogs.com/flyphper/p/8484600.html

1.2 确定支付相关的配置参数已经配置好

* 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

* ======================================

*

* Created by Super Demon W.

* Author: \/Lin\/ 764582547@qq.com

* Date: 2018/2/22

* Time: 17:09

*

* =======================================*/namespace app\home\logic;usethink\Model;useEasyWeChat\Foundation\Application;useEasyWeChat\payment\Order;usethink\Config;class Wechat1 extendsModel {/**

* 支付准备

* @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;useapp\home\logic\Wechat1;usethink\Session;usethink\Db;public functiontowxpay() {$uid = Session::get('user_auth')['uid'];$money = input('recharge_money', '');$wechat = newWechat1();$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

发起支付-支付

//调用微信JS api 支付

functionjsApiCall()

{

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';

}}

);

}functioncallpay()

{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();

支付中...

注: 统一下单后返回prepay_id后要调用小程序支付函数,有最重要的一步,是需要再次签名的,用统一下单返回的sign(签名)是没有用的。

================== 至此,微信支付结束 ===============

php技术微信