前端:比较简单,在对应的支付事件上进行网络请求就好:
- view_moneysure:function(){
- var code = this.data.code;
- console.log('code是' +code)
- wx.request({
- url: 'https://...com/pay.php',//这个链接是后端写的
- header: {
- 'Content-Type': 'application/x-www-form-urlencoded'
- },
- data: {
- code: code,
- },
- method: 'POST',
- success: function (response) {
- console.log( response.data);
- // 发起支付
- wx.requestPayment({
- 'appId': response.data.appId,
- 'timeStamp': response.data.timeStamp,
- 'nonceStr': response.data.nonceStr,
- 'package': response.data.package,
- 'signType': 'MD5',
- 'paySign': response.data.paySign,
- 'success': function (res) {
- wx.showToast({
- title: '支付成功'
- });
- console.log(res);
- },
- 'fail': function (res) {
- console.log(res)
- }
- });
- },
- fail: function (res) {
- console.log(res)
- }
- })
- },
1. pay.php //小程序请求的后端地址
- <?php
- /**
- * Created by PhpStorm.
- * User: UFO
- * Date: 17/7/18
- * Time: 下午5:31
- */
- require_once ('WxPay.Api.php');
- class WXPay {
- function index() {
- // 初始化值对象
- $input = new WxPayUnifiedOrder();
- // 文档提及的参数规范:商家名称-销售商品类目
- $input->SetBody("testceshi");
- // 订单号应该是由小程序端传给服务端的,在用户下单时即生成,demo中取值是一个生成的时间戳
- $input->SetOut_trade_no(time().'');
- // 费用应该是由小程序端传给服务端的,在用户下单时告知服务端应付金额,demo中取值是1,即1分钱
- $input->SetTotal_fee("1");
- $input->SetNotify_url("https://...com/notify.php");//需要自己写的notify.php
- $input->SetTrade_type("JSAPI");
- // 由小程序端传给后端或者后端自己获取,写自己获取到的,
- $input->SetOpenid('UdhncondJcnkJnjknkcssdcAbckn');
- //$input->SetOpenid($this->getSession()->openid);
- // 向微信统一下单,并返回order,它是一个array数组
- $order = WxPayApi::unifiedOrder($input);
- // json化返回给小程序端
- header("Content-Type: application/json");
- echo $this->getJsApiParameters($order);
- }
- private function getJsApiParameters($UnifiedOrderResult)
- { //判断是否统一下单返回了prepay_id
- if(!array_key_exists("appid", $UnifiedOrderResult)
- || !array_key_exists("prepay_id", $UnifiedOrderResult)
- || $UnifiedOrderResult['prepay_id'] == "")
- {
- throw new WxPayException("参数错误");
- }
- $jsapi = new WxPayJsApiPay();
- $jsapi->SetAppid($UnifiedOrderResult["appid"]);
- $timeStamp = time();
- $jsapi->SetTimeStamp("$timeStamp");
- $jsapi->SetNonceStr(WxPayApi::getNonceStr());
- $jsapi->SetPackage("prepay_id=" . $UnifiedOrderResult['prepay_id']);
- $jsapi->SetSignType("MD5");
- $jsapi->SetPaySign($jsapi->MakeSign());
- $parameters = json_encode($jsapi->GetValues());
- return $parameters;
- }
- //这里是服务器端获取openid的函数
- // private function getSession() {
- // $code = $this->input->post('code');
- // $url = 'https://api.weixin.qq.com/sns/jscode2session?appid='.WxPayConfig::APPID.'&secret='.WxPayConfig::APPSECRET.'&js_code='.$code.'&grant_type=authorization_code';
- // $response = json_decode(file_get_contents($url));
- // return $response;
- // }
- }
- $WxPay = new WXPay();
- $WxPay->index();
2. 微信SDK 下载链接: https://pay.weixin.qq.com/wiki/doc/api/download/WxpayAPI_php_v3.zip
解压在lib文件夹下可以看到:
放到服务端可访问的目录下。
在WxPayConfig.php里面配置账号信息:
- class WxPayConfig
- {
- //=======【基本信息设置】=====================================
- //
- /**
- * TODO: 修改这里配置为您自己申请的商户信息
- * 微信公众号信息配置
- *
- * APPID:绑定支付的APPID(必须配置,开户邮件中可查看)
- *
- * MCHID:商户号(必须配置,开户邮件中可查看)
- *
- * KEY:商户支付密钥,参考开户邮件设置(必须配置,登录商户平台自行设置)
- * 设置地址:https://pay.weixin.qq.com/index.php/account/api_cert
- *
- * APPSECRET:公众帐号secert(仅JSAPI支付的时候需要配置, 登录公众平台,进入开发者中心可设置),
- * 获取地址:https://mp.weixin.qq.com/advanced/advanced?action=dev&t=advanced/dev&token=2005451881&lang=zh_CN
- * @var string
- */
- const APPID = 'wx123456789...';//这里填上自己的对应信息
- const MCHID = '14151666888';
- const KEY = '11223344556677889900';
- const APPSECRET = '828bfsdibfsiubfikdbfik';
- const NOTIFY_URL='https://...com/notify.php';
注:
期间遇到一个签名错误,一直不好,使用微信支付接口签名校验工具校验也没有错,像网上说的漏写错写参数都查了,就是一直返回<return_code><![CDATA[FAIL]]></return_code>
<return_msg><![CDATA[签名错误]]></return_msg>
这样的信息,最后解决办法是:重置了KEY (商户支付密钥),重置的和之前的一模一样,但竟然就可以了...
问题主要都是报签名错误,仔细检查就好,比如XML格式不对,MD5加密后的位数,字典排序没排好,缺少参数等...
https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=9_1&index=1
3.最后附上notify.php
- <?php
- /**
- * Created by PhpStorm.
- * User: UFO
- * Date: 17/7/13
- * Time: 下午6:42
- */
- require_once ('WxPay.Api.php');
- require_once ('WxPay.Notify.php');
- class PayNotifyCallBack extends WxPayNotify
- {
- //查询订单
- public function Queryorder($transaction_id)
- {
- $input = new WxPayOrderQuery();
- $input->SetTransaction_id($transaction_id);
- $result = WxPayApi::orderQuery($input);
- if(array_key_exists("return_code", $result)
- && array_key_exists("result_code", $result)
- && $result["return_code"] == "SUCCESS"
- && $result["result_code"] == "SUCCESS")
- {
- return true;
- }
- return false;
- }
- //重写回调处理函数
- public function NotifyProcess($data, &$msg)
- {
- $notfiyOutput = array();
- if(!array_key_exists("transaction_id", $data)){
- $msg = "输入参数不正确";
- return false;
- }
- //查询订单,判断订单真实性
- if(!$this->Queryorder($data["transaction_id"])){
- $msg = "订单查询失败";
- return false;
- }
- return true;
- }
- }
- $notify = new PayNotifyCallBack();
- $notify->Handle(false);
欢迎留言交流指正!