一、微信支付后台服务器部署

服务器采用ubuntu16.04 + php7.0 + apache2.0。

微信支付后台服务使用了curl 和 samplexml ,因此php.ini配置中必须开启这两项的扩展。

查看是否开启这两项扩展:在网站根目录下(www)下新建index.php文件,文件代码写入:phpinfo() 保存退出,然后访问index.php.如果在网页中找到,这说明已经开启,反之没有开启。

如果没有开启这两项扩展,微信小程序支付调试会提示:内部错误(500)

1、开启curl扩展的方法:

sudo apt-get install curl libcurl3 libcurl-dev php7.0-curl

命令成功安装curl后,重启apache服务

sudo /etc/init.d/apache2 restart

如果仍有问题,尝试编辑php.ini配置文件(文件路径:/etc/php/7.0/apache2/php.ini)

找到“extentsion = php_curl.dll”把前面的“#”号去掉,保存重启apache服务。

2、开启simplexml方法:

sudo apt-get install php7.0-xml

命令成功安装samplexml 重启apache服务

如果仍有问题,尝试编辑php.ini配置文件

找到“extentsion = php_samplexml.dll” 去掉前面的“#”,保存重启apache服务。

二、微信支付服务程序搭建

去官网下载sdk和demo文件。我们这里是下载php版本。

下载地址:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=11_1

sdk与demo的文件结构如下:

下载文件后把文件拷贝到服务器上,服务的入口文件是 wxpay/jsapi.php

 

 1 <?php
 2   /**
 3   *微信小程序支付后台交易程序
 4   **/
 5   require_once "../lib/WxPay.Api.php";
 6   require_once "WxPay.Config.php";
 7   require_once 'log.php';
 8   header('Access-Control-Allow-Origin:*');//注意!跨域要加这个头
 9   header("Access-Control-Allow-Method:POST,GET");
10  /*
11   * 初始化日志
12   * 
13  */
14  $logHandler= new CLogFileHandler("../logs/".date('Y-m-d').'.log');
15  $log = Log::Init($logHandler);
16  
17  if($_SERVER['REQUEST_METHOD'] != 'POST'){
18          return_err('error request method');
19  }
20  log::info("*****交易开始*****");
21  //$info = json_encode($_POST);
22  //log::info($info);
23  try{
24       $openid = $_POST['openid'];//openid 微信唯一识别码
25       $body = $_POST['body'];//设置商品或支付单简要描述
26       $order_sn = $_POST['order_sn'];//订单号
27       $total_fee = $_POST['total_fee'];//付款金额
28       log::info('*****获取post值开始*****');
29       log::info($openid);
30       log::info($body);
31       log::info($order_sn);
32       log::info($total_fee);
33       log::info('*****获取post值结束*****');
34       /*统一下单*/
35       $input = new WxPayUnifiedOrder();
36       $input->SetBody($body);//设置商品或支付单简要描述
37       $input->SetAttach($body);//设置附加数据,在查询API和支付通知中原样返回,该字段主要用于商户携带订单的自定义数据
38       $input->SetOut_trade_no($order_sn);//设置商户系统内部的订单号,32个字符内、可包含字母, 其他说明见商户订单号
39       $input->SetTotal_fee($total_fee);//设置订单总金额,只能为整数,详见支付金额
40       $input->SetTime_start(date("YmdHis"));
41       $input->SetTime_expire(date("YmdHis", time() + 600));
42       //$input->SetGoods_tag("test");//设置商品标记,代金券或立减优惠功能的参数,说明详见代金券或立减优惠
43       $input->SetNotify_url( 'https://'.$_SERVER['HTTP_HOST'].'/notify.php');
44       $input->SetTrade_type("JSAPI");
45       $input->SetOpenid($openid);
46       $config = new WxPayConfig();
47       $order = WxPayApi::unifiedOrder($config, $input);
48       return_data($order);
49  
50  } catch(Exception $e) {
51     log::info('*****异常错误开始*****');
52  }
53 /**
54  * 错误返回提示
55  * @param string $errMsg 错误信息
56  * @param string $status 错误码
57  * @return  json的数据
58  */
59 function return_err($errMsg='error',$status=0){
60    $ret_str = json_encode(array('status'=>$status,'result'=>'fail','errmsg'=>$errMsg));
61     Log::ERROR("error request method");
62    exit($ret_str);
63 }
64 /**
65  * 正确返回
66  * @param  array $data 要返回的数组
67  * @return  json的数据
68  */
69 function return_data($data=array()){
70    $ret_str = json_encode(array('status'=>1,'result'=>'success','data'=>$data));
71     log::INFO($ret_str);
72    exit($ret_str);
73 }

 

在配置文件中配置相应的常量,必须注意一点的是商户号 mchid 与 appid 一定要匹配,,如果调试提示未匹配在,进入 微信支付后台(pay.weixin.qq.com) 中的产品中心下的APPID授权管理中加入,加入后需要,登入小程序的管理后台审核通过。

经过上面的注意事项后,程序支付调用成功,则返回 

 

根据返回的内容,发起微信支付请求,下面是支付函数封装

/**
* 微信支付
* 参数说明:
* @body 商品或支付单简要描述
* @order_no 订单号
* @totalprice 支付金额 (微信支付是以分为单位的,要转换成元为单位。)
*function wxpay(body, order_sn,totalpri  var that = this  wx.request({
    url: app.globalData.urlWxpayPath,
    method: "POST",
    header: {
      'content-type': 'application/x-www-form-urlencoded'
    },
    data: {
      "openid": app.globalData.openid,
      "body": body,
      "order_sn": order_sn,
      "total_fee": Number(totalprice)*100 //微信支付是以分为单位的,要转换成元为单位。
    },
    success: function (res) {
      //console.log(res);
      //console.log(new Date().bv ().toString());
      var appid = res.data.data.appid;
      var mch_id = res.data.data.mch_id;
      var prepay_id = res.data.data.prepay_id;
      var sign = res.data.data.sign;
      var trade_type = res.data.data.trade_type;
      var timeStamp = new Date().getTime().toString();//时间戳
      var nonceStr = res.data.data.nonce_str;
      var signType = 'MD5';
      const key = app.globalData.key;//key为商户的支付密钥,微信后台页面可以查询到。
      var pg = 'prepay_id=' + res.data.data.prepay_id;
//md5加密
var paySign = utils.hexMD5('appId=' + appid + '&nonceStr=' + nonceStr + '&package=' + pg + '&signType=' + signType + '&timeStamp=' + timeStamp + '&key=' + key); //console.log(paySign.toUpperCase()); wx.requestPayment({ timeStamp: timeStamp, nonceStr: nonceStr, package: pg, signType: 'MD5', paySign: paySign.toUpperCase(), success: function (res) { //接口调用成功的回调函数 //console.log(res) wx.showToast({ title: '支付成功', icon: 'none', duration: 2000 }) }, fail: function (res) { //接口调用失败的回调函数 wx.showToast({ title: '支付失败', icon: 'none', duration: 2000 }) }, complete: function (res) { //接口调用结束的回调函数(调用成功、失败都会执行) console.log(res); } }) } }) }

支付请求中paySign涉及到一个md5 加密算法

/* 
 * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message 
 * Digest Algorithm, as defined in RFC 1321. 
 * Version 1.1 Copyright (C) Paul Johnston 1999 - 2002. 
 * Code also contributed by Greg Holt 
 * See http://pajhome.org.uk/site/legal.html for details. 
 */

/* 
 * Add integers, wrapping at 2^32. This uses 16-bit operations internally 
 * to work around bugs in some JS interpreters. 
 */
function safe_add(x, y) {
  var lsw = (x & 0xFFFF) + (y & 0xFFFF)
  var msw = (x >> 16) + (y >> 16) + (lsw >> 16)
  return (msw << 16) | (lsw & 0xFFFF)
}

/* 
 * Bitwise rotate a 32-bit number to the left. 
 */
function rol(num, cnt) {
  return (num << cnt) | (num >>> (32 - cnt))
}

/* 
 * These functions implement the four basic operations the algorithm uses. 
 */
function cmn(q, a, b, x, s, t) {
  return safe_add(rol(safe_add(safe_add(a, q), safe_add(x, t)), s), b)
}
function ff(a, b, c, d, x, s, t) {
  return cmn((b & c) | ((~b) & d), a, b, x, s, t)
}
function gg(a, b, c, d, x, s, t) {
  return cmn((b & d) | (c & (~d)), a, b, x, s, t)
}
function hh(a, b, c, d, x, s, t) {
  return cmn(b ^ c ^ d, a, b, x, s, t)
}
function ii(a, b, c, d, x, s, t) {
  return cmn(c ^ (b | (~d)), a, b, x, s, t)
}

/* 
 * Calculate the MD5 of an array of little-endian words, producing an array 
 * of little-endian words. 
 */
function coreMD5(x) {
  var a = 1732584193
  var b = -271733879
  var c = -1732584194
  var d = 271733878

  for (var i = 0; i < x.length; i += 16) {
    var olda = a
    var oldb = b
    var oldc = c
    var oldd = d

    a = ff(a, b, c, d, x[i + 0], 7, -680876936)
    d = ff(d, a, b, c, x[i + 1], 12, -389564586)
    c = ff(c, d, a, b, x[i + 2], 17, 606105819)
    b = ff(b, c, d, a, x[i + 3], 22, -1044525330)
    a = ff(a, b, c, d, x[i + 4], 7, -176418897)
    d = ff(d, a, b, c, x[i + 5], 12, 1200080426)
    c = ff(c, d, a, b, x[i + 6], 17, -1473231341)
    b = ff(b, c, d, a, x[i + 7], 22, -45705983)
    a = ff(a, b, c, d, x[i + 8], 7, 1770035416)
    d = ff(d, a, b, c, x[i + 9], 12, -1958414417)
    c = ff(c, d, a, b, x[i + 10], 17, -42063)
    b = ff(b, c, d, a, x[i + 11], 22, -1990404162)
    a = ff(a, b, c, d, x[i + 12], 7, 1804603682)
    d = ff(d, a, b, c, x[i + 13], 12, -40341101)
    c = ff(c, d, a, b, x[i + 14], 17, -1502002290)
    b = ff(b, c, d, a, x[i + 15], 22, 1236535329)

    a = gg(a, b, c, d, x[i + 1], 5, -165796510)
    d = gg(d, a, b, c, x[i + 6], 9, -1069501632)
    c = gg(c, d, a, b, x[i + 11], 14, 643717713)
    b = gg(b, c, d, a, x[i + 0], 20, -373897302)
    a = gg(a, b, c, d, x[i + 5], 5, -701558691)
    d = gg(d, a, b, c, x[i + 10], 9, 38016083)
    c = gg(c, d, a, b, x[i + 15], 14, -660478335)
    b = gg(b, c, d, a, x[i + 4], 20, -405537848)
    a = gg(a, b, c, d, x[i + 9], 5, 568446438)
    d = gg(d, a, b, c, x[i + 14], 9, -1019803690)
    c = gg(c, d, a, b, x[i + 3], 14, -187363961)
    b = gg(b, c, d, a, x[i + 8], 20, 1163531501)
    a = gg(a, b, c, d, x[i + 13], 5, -1444681467)
    d = gg(d, a, b, c, x[i + 2], 9, -51403784)
    c = gg(c, d, a, b, x[i + 7], 14, 1735328473)
    b = gg(b, c, d, a, x[i + 12], 20, -1926607734)

    a = hh(a, b, c, d, x[i + 5], 4, -378558)
    d = hh(d, a, b, c, x[i + 8], 11, -2022574463)
    c = hh(c, d, a, b, x[i + 11], 16, 1839030562)
    b = hh(b, c, d, a, x[i + 14], 23, -35309556)
    a = hh(a, b, c, d, x[i + 1], 4, -1530992060)
    d = hh(d, a, b, c, x[i + 4], 11, 1272893353)
    c = hh(c, d, a, b, x[i + 7], 16, -155497632)
    b = hh(b, c, d, a, x[i + 10], 23, -1094730640)
    a = hh(a, b, c, d, x[i + 13], 4, 681279174)
    d = hh(d, a, b, c, x[i + 0], 11, -358537222)
    c = hh(c, d, a, b, x[i + 3], 16, -722521979)
    b = hh(b, c, d, a, x[i + 6], 23, 76029189)
    a = hh(a, b, c, d, x[i + 9], 4, -640364487)
    d = hh(d, a, b, c, x[i + 12], 11, -421815835)
    c = hh(c, d, a, b, x[i + 15], 16, 530742520)
    b = hh(b, c, d, a, x[i + 2], 23, -995338651)

    a = ii(a, b, c, d, x[i + 0], 6, -198630844)
    d = ii(d, a, b, c, x[i + 7], 10, 1126891415)
    c = ii(c, d, a, b, x[i + 14], 15, -1416354905)
    b = ii(b, c, d, a, x[i + 5], 21, -57434055)
    a = ii(a, b, c, d, x[i + 12], 6, 1700485571)
    d = ii(d, a, b, c, x[i + 3], 10, -1894986606)
    c = ii(c, d, a, b, x[i + 10], 15, -1051523)
    b = ii(b, c, d, a, x[i + 1], 21, -2054922799)
    a = ii(a, b, c, d, x[i + 8], 6, 1873313359)
    d = ii(d, a, b, c, x[i + 15], 10, -30611744)
    c = ii(c, d, a, b, x[i + 6], 15, -1560198380)
    b = ii(b, c, d, a, x[i + 13], 21, 1309151649)
    a = ii(a, b, c, d, x[i + 4], 6, -145523070)
    d = ii(d, a, b, c, x[i + 11], 10, -1120210379)
    c = ii(c, d, a, b, x[i + 2], 15, 718787259)
    b = ii(b, c, d, a, x[i + 9], 21, -343485551)

    a = safe_add(a, olda)
    b = safe_add(b, oldb)
    c = safe_add(c, oldc)
    d = safe_add(d, oldd)
  }
  return [a, b, c, d]
}

/* 
 * Convert an array of little-endian words to a hex string. 
 */
function binl2hex(binarray) {
  var hex_tab = "0123456789abcdef"
  var str = ""
  for (var i = 0; i < binarray.length * 4; i++) {
    str += hex_tab.charAt((binarray[i >> 2] >> ((i % 4) * 8 + 4)) & 0xF) +
      hex_tab.charAt((binarray[i >> 2] >> ((i % 4) * 8)) & 0xF)
  }
  return str
}

/* 
 * Convert an array of little-endian words to a base64 encoded string. 
 */
function binl2b64(binarray) {
  var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
  var str = ""
  for (var i = 0; i < binarray.length * 32; i += 6) {
    str += tab.charAt(((binarray[i >> 5] << (i % 32)) & 0x3F) |
      ((binarray[i >> 5 + 1] >> (32 - i % 32)) & 0x3F))
  }
  return str
}

/* 
 * Convert an 8-bit character string to a sequence of 16-word blocks, stored 
 * as an array, and append appropriate padding for MD4/5 calculation. 
 * If any of the characters are >255, the high byte is silently ignored. 
 */
function str2binl(str) {
  var nblk = ((str.length + 8) >> 6) + 1 // number of 16-word blocks  
  var blks = new Array(nblk * 16)
  for (var i = 0; i < nblk * 16; i++) blks[i] = 0
  for (var i = 0; i < str.length; i++)
    blks[i >> 2] |= (str.charCodeAt(i) & 0xFF) << ((i % 4) * 8)
  blks[i >> 2] |= 0x80 << ((i % 4) * 8)
  blks[nblk * 16 - 2] = str.length * 8
  return blks
}

/* 
 * Convert a wide-character string to a sequence of 16-word blocks, stored as 
 * an array, and append appropriate padding for MD4/5 calculation. 
 */
function strw2binl(str) {
  var nblk = ((str.length + 4) >> 5) + 1 // number of 16-word blocks  
  var blks = new Array(nblk * 16)
  for (var i = 0; i < nblk * 16; i++) blks[i] = 0
  for (var i = 0; i < str.length; i++)
    blks[i >> 1] |= str.charCodeAt(i) << ((i % 2) * 16)
  blks[i >> 1] |= 0x80 << ((i % 2) * 16)
  blks[nblk * 16 - 2] = str.length * 16
  return blks
}

/* 
 * External interface 
 */
function hexMD5(str) { return binl2hex(coreMD5(str2binl(str))) }
function hexMD5w(str) { return binl2hex(coreMD5(strw2binl(str))) }
function b64MD5(str) { return binl2b64(coreMD5(str2binl(str))) }
function b64MD5w(str) { return binl2b64(coreMD5(strw2binl(str))) }
/* Backward compatibility */
function calcMD5(str) { return binl2hex(coreMD5(str2binl(str))) }
module.exports = {
  hexMD5: hexMD5
}

这样程序就会成功接入微信支付了。

需要注意的一点是:支付需要传入的订单号不能重复使用,比如,第一次支付取消了,然后去订单页面去重新支付,会提示 total_fee 是必须传入选项,这时需要重新再生成一个新的订单号才行。 

内容来源于网络如有侵权请私信删除
你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!