node-wxpay3
Version:
212 lines (211 loc) • 10.2 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.WxPay2 = void 0;
var md5_1 = __importDefault(require("md5"));
var crypto_1 = __importDefault(require("crypto"));
var xml2js_1 = __importDefault(require("xml2js"));
var superagent_1 = __importDefault(require("superagent"));
var util_1 = require("./utils/util");
var builder = new xml2js_1.default.Builder({
headless: true,
allowSurrogateChars: true,
rootName: 'xml',
cdata: true,
});
var urls = {
unifiedorder: 'https://api.mch.weixin.qq.com/pay/unifiedorder',
orderquery: 'https://api.mch.weixin.qq.com/pay/orderquery',
closeorder: 'https://api.mch.weixin.qq.com/pay/closeorder',
refund: 'https://api.mch.weixin.qq.com/secapi/pay/refund',
refundquery: 'https://api.mch.weixin.qq.com/pay/refundquery',
downloadbill: 'https://api.mch.weixin.qq.com/pay/downloadbill',
downloadfundflow: 'https://api.mch.weixin.qq.com/pay/downloadfundflow',
report: 'https://api.mch.weixin.qq.com/payitil/report',
batchquerycomment: 'https://api.mch.weixin.qq.com/billcommentsp/batchquerycomment',
reverse: 'https://api.mch.weixin.qq.com/secapi/pay/reverse',
micropay: 'https://api.mch.weixin.qq.com/pay/micropay',
authcodetoopenid: 'https://api.mch.weixin.qq.com/tools/authcodetoopenid',
sendredpack: 'https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack',
sendgroupredpack: 'https://api.mch.weixin.qq.com/mmpaymkttransfers/sendgroupredpack',
gethbinfo: 'https://api.mch.weixin.qq.com/mmpaymkttransfers/gethbinfo',
sendminiprogramhb: 'https://api.mch.weixin.qq.com/mmpaymkttransfers/sendminiprogramhb',
transfers: 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers',
gettransferinfo: 'https://api.mch.weixin.qq.com/mmpaymkttransfers/gettransferinfo',
pay_bank: 'https://api.mch.weixin.qq.com/mmpaysptrans/pay_bank',
query_bank: 'https://api.mch.weixin.qq.com/mmpaysptrans/query_bank',
getpublickey: 'https://fraud.mch.weixin.qq.com/risk/getpublickey',
send_coupon: 'https://api.mch.weixin.qq.com/mmpaymkttransfers/send_coupon',
query_coupon_stock: 'https://api.mch.weixin.qq.com/mmpaymkttransfers/query_coupon_stock',
querycouponsinfo: 'https://api.mch.weixin.qq.com/mmpaymkttransfers/querycouponsinfo',
};
var WxPay2 = (function () {
function WxPay2(obj) {
this._params = {};
this.appid = obj.appid;
this.mch_id = obj.mch_id;
this.key = obj.key;
this.pfx = obj.pfx;
}
WxPay2.prototype._joinotherParams = function (params) {
this._params = __assign({ nonce_str: (0, util_1.getNonceStr)() }, params);
};
WxPay2.prototype._checkParams = function (properties) {
var _this = this;
properties.forEach(function (item) {
if (_this._params[item] === undefined || _this._params[item] === null)
throw new Error('缺少' + item);
});
};
WxPay2.prototype._md5 = function (params) {
var querystring = Object.keys(params)
.filter(function (key) {
return (params[key] !== undefined &&
params[key] !== '' &&
!['pfx', 'sign', 'key', 'redirect_url'].includes(key));
})
.sort()
.map(function (key) {
return key + '=' + params[key];
})
.join('&') +
'&key=' +
this.key;
return (0, md5_1.default)(querystring).toUpperCase();
};
WxPay2.prototype._hmac = function (params) {
var querystring = Object.keys(params)
.filter(function (key) {
return (params[key] !== undefined &&
params[key] !== '' &&
!['pfx', 'sign', 'key', 'redirect_url'].includes(key));
})
.sort()
.map(function (key) {
return key + '=' + params[key];
})
.join('&') +
'&key=' +
this.key;
var hash = crypto_1.default.createHmac('sha256', this.key).update(querystring).digest('hex');
return hash.toUpperCase();
};
WxPay2.prototype._jsontoxml = function (params) {
delete params['pfx'];
delete params['key'];
delete params['redirect_url'];
var xmlOption = builder.buildObject(params);
return xmlOption;
};
WxPay2.prototype._xmltojson = function (params) {
var body = {};
xml2js_1.default.parseString(params, { trim: true, explicitArray: false }, function (err, result) {
if (err) {
console.error(err);
body = {};
}
else {
body = result.xml;
}
});
return body;
};
WxPay2.prototype._request = function (name, xml) {
return __awaiter(this, void 0, void 0, function () {
var url, result;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
url = urls[name];
return [4, superagent_1.default
.post(url)
.send(xml)
.pfx(__assign({}, (this.pfx && {
pfx: this.pfx,
passphrase: this.mch_id,
})))
.type('xml')];
case 1:
result = _a.sent();
if (result.type === 'text/plain' || result.type === 'text/html' || result.type === 'text/xml') {
if (result.text.indexOf('<xml>') !== -1) {
return [2, this._xmltojson(result.text)];
}
return [2, {
data: result.text,
return_code: 'SUCCESS',
result_code: 'SUCCESS',
return_msg: 'OK',
}];
}
else if (result.type === 'application/x-gzip') {
return [2, {
data: result.body,
return_code: 'SUCCESS',
result_code: 'SUCCESS',
return_msg: 'OK',
}];
}
else if (result.type === 'application/xml') {
return [2, this._xmltojson(result.body.toString())];
}
else {
return [2, result];
}
return [2];
}
});
});
};
return WxPay2;
}());
exports.WxPay2 = WxPay2;