wxPayUtil.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. 'use strict';
  2. const request = require('request-promise');
  3. const md5 = require('md5'); // 引入md5加密模块
  4. const fs = require('fs');
  5. // const appId = 'wx1c015df104db7030'; // 小程序的appid
  6. const mchId = '1226156302'; // 商户号
  7. const mchKey = 'zaq12wsxcde34rfvbgt56yhnmjuioplk'; // 商户秘钥
  8. // 进行签名的参数
  9. function getSignParam(obj) {
  10. let keys = Object.keys(obj);
  11. keys = keys.sort();
  12. let _str = '';
  13. const _arr = [];
  14. keys.forEach(function(key) {
  15. _arr.push(key + '=' + obj[key]);
  16. });
  17. _str = _str + _arr.join('&') + '&key=' + mchKey;
  18. _str = md5(_str);
  19. const signValue = _str.toUpperCase();
  20. return signValue;
  21. }
  22. // 请求时的xml参数
  23. function getXmlParam(obj) {
  24. let _xml = '<xml>';
  25. for (const key in obj) {
  26. _xml += '<' + key + '>' + obj[key] + '</' + key + '>';
  27. }
  28. _xml = _xml + '</xml>';
  29. return _xml;
  30. }
  31. // 微信提现http请求
  32. async function WeApiPromotion(formData) {
  33. const options = {
  34. url: 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers', // 微信官方请求接口
  35. method: 'POST',
  36. headers: {
  37. 'content-type': 'application/json;charset=utf-8',
  38. },
  39. body: formData,
  40. agentOptions: {
  41. key: fs.readFileSync('./app/public/uploads/apiclient_key.pem'), // 将微信生成的证书放入 cert目录下
  42. cert: fs.readFileSync('./app/public/uploads/apiclient_cert.pem'),
  43. passphrase: mchId,
  44. },
  45. };
  46. const payData = await request(options);
  47. return payData;
  48. }
  49. // 产生一个随机字符串
  50. function getRandomStr(strLength) {
  51. let str = '';
  52. const arr = [ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' ];
  53. for (let i = 1; i <= strLength; i++) {
  54. const random = Math.floor(Math.random() * arr.length);
  55. str += arr[random];
  56. }
  57. return str;
  58. }
  59. // 构造订单号
  60. function getOrderID() {
  61. const currentDate = new Date();
  62. const year = currentDate.getFullYear();
  63. let month = currentDate.getMonth();
  64. month = month >= 9 ? month + 1 : '0' + (month + 1);
  65. let day = currentDate.getDate();
  66. day = this.MgetFixedNumber(day);
  67. // mch_id 是微信支付分配的商户号
  68. const orderid = mchId + year + month + day + this.MgetRandomNumber();
  69. return orderid;
  70. }
  71. // 返回给定宽度的数字字符串
  72. function MgetFixedNumber(number, width) {
  73. width = width ? width : 2;
  74. let fixedNumber = number.toString();
  75. for (; fixedNumber.length < width;) {
  76. fixedNumber = '0' + fixedNumber;
  77. }
  78. return fixedNumber;
  79. }
  80. // 返回随机数字字符串
  81. function MgetRandomNumber(length) {
  82. const numbers = '0123456789';
  83. length = length ? length : 10;
  84. const range = numbers.length;
  85. let result = '';
  86. let position = 0;
  87. for (let i = 0; i < length; i++) {
  88. position = Math.floor(Math.random() * range);
  89. result += numbers[position];
  90. }
  91. return result;
  92. }
  93. module.exports = {
  94. getRandomStr,
  95. getSignParam,
  96. getXmlParam,
  97. WeApiPromotion,
  98. MgetFixedNumber, MgetRandomNumber, getOrderID,
  99. };