UNPKG

node-easywechat

Version:

EasyWechat SDK for Node.js (NOT OFFICIAL)

46 lines (45 loc) 1.86 kB
'use strict'; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; const Utils_1 = require("../Core/Support/Utils"); const merge_1 = __importDefault(require("merge")); const RSA_1 = __importDefault(require("../Core/Support/RSA")); class Signature { constructor(merchant) { this.merchant = merchant; } /** * V3版本的签名计算 * @param method 请求方式 * @param url 请求地址 * @param payload 请求载荷 * @returns */ createHeader(method, url, payload) { let pathname = url; if (url.startsWith('https://') || url.startsWith('http://')) { let urlObj = new URL(url); let search = (0, Utils_1.buildQueryString)((0, merge_1.default)(true, urlObj.searchParams, payload.params)); pathname = urlObj.pathname + (search ? '?' + search : ''); } let nonce = (0, Utils_1.randomString)(); let timestamp = (0, Utils_1.getTimestamp)(); let body = ''; if (payload.data) { if (typeof payload.data === 'object') { body = JSON.stringify(payload.data); } else { body = payload.data; } } let signString = `${method.toUpperCase()}\n${pathname}\n${timestamp}\n${nonce}\n${body}`; let rsa = new RSA_1.default; rsa.setPublicKey(this.merchant.getCertificate().getValue()); rsa.setPrivateKey(this.merchant.getPrivateKey().getKey()); let sign = rsa.sign(signString); return `WECHATPAY2-SHA256-RSA2048 mchid="${this.merchant.getMerchantId()}",nonce_str="${nonce}",timestamp="${timestamp}",serial_no="${this.merchant.getCertificate().getSerialNo()}",signature="${sign}"`; } } module.exports = Signature;