diff --git a/lib/wxpay.js b/lib/wxpay.js index 969ac13..217963e 100644 --- a/lib/wxpay.js +++ b/lib/wxpay.js @@ -2,7 +2,7 @@ var util = require('./util'); var request = require('request'); var md5 = require('MD5'); - +var moment = require("moment"); exports = module.exports = WXPay; function WXPay() { @@ -190,3 +190,22 @@ WXPay.mix('refund',function(order, fn){ }); }); +WXPay.mix('redpack', function (opts, fn) { + opts.mch_billno = `${this.options.mch_id}${moment().format("YYYYMMDD")}${Date.now().toString().substr(2, 10)}` + opts.wxappid = this.wxpayID.appid + opts.mch_id = this.wxpayID.mch_id + opts.nonce_str = opts.nonce_str || util.generateNonceString(); + opts.sign = this.sign(opts); + console.log(this.wxpayID, opts) + request({ + url: "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack", + method: "POST", + body: util.buildXML({xml: opts}), + agentOptions: { + pfx: this.options.pfx, + passphrase: this.options.mch_id + } + }, function(err, response, body){ + util.parseXML(body, fn); + }); +}) diff --git a/package.json b/package.json index 47dc3bb..06910d8 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,8 @@ "dependencies": { "request": "^2.54.0", "MD5": "^1.2.1", - "xml2js": "^0.4.6" + "xml2js": "^0.4.6", + "moment": "^2.17.1" }, "devDependencies": {}, "scripts": {