From 8672f526a4b74ca947525dd6282cfc176b021e93 Mon Sep 17 00:00:00 2001 From: liujun Date: Fri, 6 Jan 2017 12:30:51 +0800 Subject: [PATCH] add redpack method --- lib/wxpay.js | 21 ++++++++++++++++++++- package.json | 3 ++- 2 files changed, 22 insertions(+), 2 deletions(-) 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": {