🎩 You're Invited:Meet the Socket team at Black Hat in Las Vegas, August 3-6.RSVP
Sign In

@humwing/wxpay

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@humwing/wxpay - npm Package Compare versions

Comparing version
0.0.2
to
0.0.3
+191
-61
dist/index.cjs

@@ -275,2 +275,79 @@ 'use strict';

/**
* 微信支付H5卡券领取方法
*
* @param {string} stock_id - 微信支付商家券批次.
* @param {string} send_coupon_merchant - 微信支付服务商id.
* @param {string} open_id - 需要发放的用户openid,此openid需要是与商户号关联的公众号openid或者小程序的openid.
* @return {string} 返回领取链接
*/
getH5CouponUrl(stock_id, send_coupon_merchant, open_id) {
const params = {
stock_id,
out_request_no: Date.now(),
send_coupon_merchant,
open_id
};
const sign = this.signHMACSHA256(params);
const url = `https://action.weixin.qq.com/busifavor/getcouponinfo?stock_id=${params.stock_id}&out_request_no=${params.out_request_no}&sign=${sign}&send_coupon_merchant=${params.send_coupon_merchant}&open_id=${open_id}#wechat_redirect`;
return url;
}
/**
* 微信支付卡券领取参数生成
*
* @param {Array<{stock_id: string, out_request_no: string, coupon_code?: string,customize_send_time?:string}>} stockList - 微信卡券批次信息
* @param {string|number} send_coupon_merchant 商户id,服务商请填写服务商id,直连商户填直连商户id
* @return {{miniPluginParams,miniApiParams,jssdkParams}} an object containing three parameters: miniPluginParams, miniApiParams, and jssdkParams
*/
getWXPayCouponApiCouponInfo(stockList, send_coupon_merchant) {
const signParams = stockList.reduce((pval, item, index) => {
const itemVal = Object.keys(item).reduce((val, key) => {
val[`${key}${index}`] = item[key];
return val;
}, {});
return {
...pval,
...itemVal
};
}, {});
signParams["send_coupon_merchant"] = send_coupon_merchant;
const sign = this.signHMACSHA256(signParams);
const miniPluginParams = {
// send_coupon_params这里还是直接传:stock_id、out_request_no,不需要带商户id
send_coupon_params: stockList,
send_coupon_merchant,
sign
};
const miniApiParams = {
cardList: stockList.map((item, index) => {
if (index === 0) {
return {
cardId: item.stock_id,
//对应商家券批次号
cardExt: JSON.stringify({
sign,
send_coupon_merchant,
coupon_code: item.coupon_code,
customize_send_time: item.customize_send_time,
out_request_no: item.out_request_no
})
};
}
return {
cardId: item.stock_id,
//对应商家券批次号
cardExt: JSON.stringify({
coupon_code: item.coupon_code,
customize_send_time: item.customize_send_time,
out_request_no: item.out_request_no
})
};
})
};
return {
miniPluginParams,
miniApiParams,
jssdkParams: miniApiParams
};
}
/**
* 把json转成xml数据

@@ -788,65 +865,118 @@ * @param json json数据

const PayUtil = {
/**
* 获取RSA-SHA256签名,返回base64格式
* @param message 需要被签名的信息
* @param privateKey 私钥,PRIVATE KEY
* @returns 签名信息
*/
sign(message, privateKey) {
const sign = crypto__default.createSign("RSA-SHA256");
sign.update(message);
const signature = sign.sign(privateKey, "base64");
return signature;
},
/**
* aes-256-gcm加密数据
* @param data 待加密的字符串
* @param aad 额外的鉴权信息
* @param iv 初始向量
* @param secret 密钥
* @returns 加密数据
*/
encrypt(data, aad, iv, secret) {
let cipher = crypto__default.createCipheriv("aes-256-gcm", secret, iv);
cipher.setAAD(Buffer.from(aad, "utf-8"));
let ciphertext = cipher.update(Buffer.from(data, "utf-8"));
let encrypted = Buffer.concat([
ciphertext,
cipher.final(),
cipher.getAuthTag()
]);
function signRSASHA256(message, privateKey) {
const sign = crypto__default.createSign("RSA-SHA256");
sign.update(message);
const signature = sign.sign(privateKey, "base64");
return signature;
}
function getH5CouponUrl(stock_id, send_coupon_merchant, open_id, secret) {
const params = {
stock_id,
out_request_no: Date.now(),
send_coupon_merchant,
open_id
};
const sign = signHMACSHA256(params, secret);
const url = `https://action.weixin.qq.com/busifavor/getcouponinfo?stock_id=${params.stock_id}&out_request_no=${params.out_request_no}&sign=${sign}&send_coupon_merchant=${params.send_coupon_merchant}&open_id=${open_id}#wechat_redirect`;
return url;
}
function getWXPayCouponApiCouponInfo(stockList, send_coupon_merchant, secret) {
const signParams = stockList.reduce((pval, item, index) => {
const itemVal = Object.keys(item).reduce((val, key) => {
val[`${key}${index}`] = item[key];
return val;
}, {});
return {
ciphertext: encrypted.toString("base64"),
associated_data: aad,
nonce: iv
...pval,
...itemVal
};
},
/**
* aes-256-gcm解密数据
* @param ciphertext 密文
* @param aad 鉴权数据
* @param iv 初始向量
* @param secret 密钥
* @returns 界面的数据
*/
decipher(ciphertext, aad, iv, secret) {
const _ciphertext = Buffer.from(ciphertext, "base64");
const authTag = _ciphertext.slice(_ciphertext.length - 16);
const data = _ciphertext.slice(0, _ciphertext.length - 16);
const decipher = crypto__default.createDecipheriv("aes-256-gcm", secret, iv);
decipher.setAuthTag(authTag);
decipher.setAAD(Buffer.from(aad));
const decoded = decipher.update(data, void 0, "utf8");
decipher.final();
return decoded;
},
/**
* 构建签名的信息
* @param messages 信息数组
* @returns 返回构建好的字符串
*/
buildMessage(messages) {
return messages.concat("").join("\n");
}
}, {});
signParams["send_coupon_merchant"] = send_coupon_merchant;
const sign = this.signHMACSHA256(signParams, secret);
const miniPluginParams = {
// send_coupon_params这里还是直接传:stock_id、out_request_no,不需要带商户id
send_coupon_params: stockList,
send_coupon_merchant,
sign
};
const miniApiParams = {
cardList: stockList.map((item, index) => {
if (index === 0) {
return {
cardId: item.stock_id,
//对应商家券批次号
cardExt: JSON.stringify({
sign,
send_coupon_merchant,
coupon_code: item.coupon_code,
customize_send_time: item.customize_send_time,
out_request_no: item.out_request_no
})
};
}
return {
cardId: item.stock_id,
//对应商家券批次号
cardExt: JSON.stringify({
coupon_code: item.coupon_code,
customize_send_time: item.customize_send_time,
out_request_no: item.out_request_no
})
};
})
};
return {
miniPluginParams,
miniApiParams,
jssdkParams: miniApiParams
};
}
function signHMACSHA256(param, secret) {
let querystring = Object.keys(param).sort().filter((key) => {
return param[key] != null && param[key] !== "";
}).filter((key) => {
return ["sign", "key"].indexOf(key) < 0;
}).map((key) => {
return key + "=" + param[key];
}).join("&");
const hash = crypto__default.createHmac("sha256", secret).update(querystring + "&key=" + secret).digest("hex");
return hash.toUpperCase();
}
function encrypt(data, aad, iv, secret) {
let cipher = crypto__default.createCipheriv("aes-256-gcm", secret, iv);
cipher.setAAD(Buffer.from(aad, "utf-8"));
let ciphertext = cipher.update(Buffer.from(data, "utf-8"));
let encrypted = Buffer.concat([
ciphertext,
cipher.final(),
cipher.getAuthTag()
]);
return {
ciphertext: encrypted.toString("base64"),
associated_data: aad,
nonce: iv
};
}
function decipher(ciphertext, aad, iv, secret) {
const _ciphertext = Buffer.from(ciphertext, "base64");
const authTag = _ciphertext.slice(_ciphertext.length - 16);
const data = _ciphertext.slice(0, _ciphertext.length - 16);
const decipher2 = crypto__default.createDecipheriv("aes-256-gcm", secret, iv);
decipher2.setAuthTag(authTag);
decipher2.setAAD(Buffer.from(aad));
const decoded = decipher2.update(data, void 0, "utf8");
decipher2.final();
return decoded;
}
function buildMessage(messages) {
return messages.concat("").join("\n");
}
const PayUtil = {
signRSASHA256,
getH5CouponUrl,
getWXPayCouponApiCouponInfo,
signHMACSHA256,
encrypt,
decipher,
buildMessage
};

@@ -853,0 +983,0 @@

@@ -7,2 +7,76 @@ import * as axios from 'axios';

/**
* 获取RSA-SHA256签名,返回base64格式
* @param message 需要被签名的信息
* @param privateKey 私钥,PRIVATE KEY
* @returns 签名信息
*/
declare function signRSASHA256(message: string, privateKey: string): string;
/**
* 微信支付H5卡券领取方法
*
* @param {string} stock_id - 微信支付商家券批次.
* @param {string} send_coupon_merchant - 微信支付服务商id.
* @param {string} open_id - 需要发放的用户openid,此openid需要是与商户号关联的公众号openid或者小程序的openid.
* @return {string} 返回领取链接
*/
declare function getH5CouponUrl(stock_id: string, send_coupon_merchant: string, open_id: string, secret: string): string;
/**
* 微信支付卡券领取参数生成
*
* @param {Array<{stock_id: string, out_request_no: string, coupon_code?: string,customize_send_time?:string}>} stockList - 微信卡券批次信息
* @param {string|number} send_coupon_merchant 商户id,服务商请填写服务商id,直连商户填直连商户id
* @return {{miniPluginParams,miniApiParams,jssdkParams}} an object containing three parameters: miniPluginParams, miniApiParams, and jssdkParams
*/
declare function getWXPayCouponApiCouponInfo(stockList: any, send_coupon_merchant: any, secret: string): {
miniPluginParams: {
send_coupon_params: any;
send_coupon_merchant: any;
sign: any;
};
miniApiParams: {
cardList: any;
};
jssdkParams: {
cardList: any;
};
};
/**
* 微信体系模式的Hmac sha256签名
* @param param 任意的参数形式
* @param secret 签名秘钥
* @returns sha256签名
*/
declare function signHMACSHA256(param: {
[key: string]: any;
}, secret: string): string;
/**
* aes-256-gcm加密数据
* @param data 待加密的字符串
* @param aad 额外的鉴权信息
* @param iv 初始向量
* @param secret 密钥
* @returns 加密数据
*/
declare function encrypt(data: string, aad: string, iv: string, secret: string): {
ciphertext: string;
associated_data: string;
nonce: string;
};
/**
* aes-256-gcm解密数据
* @param ciphertext 密文
* @param aad 鉴权数据
* @param iv 初始向量
* @param secret 密钥
* @returns 界面的数据
*/
declare function decipher(ciphertext: string, aad: string, iv: string, secret: string): string;
/**
* 构建签名的信息
* @param messages 信息数组
* @returns 返回构建好的字符串
*/
declare function buildMessage(messages: string[]): string;
type WXPayOptions = {

@@ -59,2 +133,31 @@ /** 商户号 */

/**
* 微信支付H5卡券领取方法
*
* @param {string} stock_id - 微信支付商家券批次.
* @param {string} send_coupon_merchant - 微信支付服务商id.
* @param {string} open_id - 需要发放的用户openid,此openid需要是与商户号关联的公众号openid或者小程序的openid.
* @return {string} 返回领取链接
*/
getH5CouponUrl(stock_id: string, send_coupon_merchant: string, open_id: string): string;
/**
* 微信支付卡券领取参数生成
*
* @param {Array<{stock_id: string, out_request_no: string, coupon_code?: string,customize_send_time?:string}>} stockList - 微信卡券批次信息
* @param {string|number} send_coupon_merchant 商户id,服务商请填写服务商id,直连商户填直连商户id
* @return {{miniPluginParams,miniApiParams,jssdkParams}} an object containing three parameters: miniPluginParams, miniApiParams, and jssdkParams
*/
getWXPayCouponApiCouponInfo(stockList: any, send_coupon_merchant: any): {
miniPluginParams: {
send_coupon_params: any;
send_coupon_merchant: any;
sign: string;
};
miniApiParams: {
cardList: any;
};
jssdkParams: {
cardList: any;
};
};
/**
* 把json转成xml数据

@@ -94,9 +197,14 @@ * @param json json数据

type Options = {
/**商户id*/
mchid: string;
serialNo?: string;
privateKey?: string | crypto.KeyObject;
/**证书序列号*/
serialNo: string;
/**V3商户秘钥*/
apiV3Secret: string;
/**证书私钥*/
privateKey: string | crypto.KeyObject;
privateKeyStr?: string;
publicKey?: string | crypto.KeyObject;
/**证书公钥*/
publicKey: string | crypto.KeyObject;
publicKeyStr?: string;
apiV3Secret?: string;
};

@@ -361,10 +469,9 @@ type WXPayPlatformCert = {

payUtils: {
sign(message: string, privateKey: string): string;
encrypt(data: string, aad: string, iv: string, secret: string): {
ciphertext: string;
associated_data: string;
nonce: string;
};
decipher(ciphertext: string, aad: string, iv: string, secret: string): string;
buildMessage(messages: string[]): string;
signRSASHA256: typeof signRSASHA256;
getH5CouponUrl: typeof getH5CouponUrl;
getWXPayCouponApiCouponInfo: typeof getWXPayCouponApiCouponInfo;
signHMACSHA256: typeof signHMACSHA256;
encrypt: typeof encrypt;
decipher: typeof decipher;
buildMessage: typeof buildMessage;
};

@@ -371,0 +478,0 @@ };

@@ -7,2 +7,76 @@ import * as axios from 'axios';

/**
* 获取RSA-SHA256签名,返回base64格式
* @param message 需要被签名的信息
* @param privateKey 私钥,PRIVATE KEY
* @returns 签名信息
*/
declare function signRSASHA256(message: string, privateKey: string): string;
/**
* 微信支付H5卡券领取方法
*
* @param {string} stock_id - 微信支付商家券批次.
* @param {string} send_coupon_merchant - 微信支付服务商id.
* @param {string} open_id - 需要发放的用户openid,此openid需要是与商户号关联的公众号openid或者小程序的openid.
* @return {string} 返回领取链接
*/
declare function getH5CouponUrl(stock_id: string, send_coupon_merchant: string, open_id: string, secret: string): string;
/**
* 微信支付卡券领取参数生成
*
* @param {Array<{stock_id: string, out_request_no: string, coupon_code?: string,customize_send_time?:string}>} stockList - 微信卡券批次信息
* @param {string|number} send_coupon_merchant 商户id,服务商请填写服务商id,直连商户填直连商户id
* @return {{miniPluginParams,miniApiParams,jssdkParams}} an object containing three parameters: miniPluginParams, miniApiParams, and jssdkParams
*/
declare function getWXPayCouponApiCouponInfo(stockList: any, send_coupon_merchant: any, secret: string): {
miniPluginParams: {
send_coupon_params: any;
send_coupon_merchant: any;
sign: any;
};
miniApiParams: {
cardList: any;
};
jssdkParams: {
cardList: any;
};
};
/**
* 微信体系模式的Hmac sha256签名
* @param param 任意的参数形式
* @param secret 签名秘钥
* @returns sha256签名
*/
declare function signHMACSHA256(param: {
[key: string]: any;
}, secret: string): string;
/**
* aes-256-gcm加密数据
* @param data 待加密的字符串
* @param aad 额外的鉴权信息
* @param iv 初始向量
* @param secret 密钥
* @returns 加密数据
*/
declare function encrypt(data: string, aad: string, iv: string, secret: string): {
ciphertext: string;
associated_data: string;
nonce: string;
};
/**
* aes-256-gcm解密数据
* @param ciphertext 密文
* @param aad 鉴权数据
* @param iv 初始向量
* @param secret 密钥
* @returns 界面的数据
*/
declare function decipher(ciphertext: string, aad: string, iv: string, secret: string): string;
/**
* 构建签名的信息
* @param messages 信息数组
* @returns 返回构建好的字符串
*/
declare function buildMessage(messages: string[]): string;
type WXPayOptions = {

@@ -59,2 +133,31 @@ /** 商户号 */

/**
* 微信支付H5卡券领取方法
*
* @param {string} stock_id - 微信支付商家券批次.
* @param {string} send_coupon_merchant - 微信支付服务商id.
* @param {string} open_id - 需要发放的用户openid,此openid需要是与商户号关联的公众号openid或者小程序的openid.
* @return {string} 返回领取链接
*/
getH5CouponUrl(stock_id: string, send_coupon_merchant: string, open_id: string): string;
/**
* 微信支付卡券领取参数生成
*
* @param {Array<{stock_id: string, out_request_no: string, coupon_code?: string,customize_send_time?:string}>} stockList - 微信卡券批次信息
* @param {string|number} send_coupon_merchant 商户id,服务商请填写服务商id,直连商户填直连商户id
* @return {{miniPluginParams,miniApiParams,jssdkParams}} an object containing three parameters: miniPluginParams, miniApiParams, and jssdkParams
*/
getWXPayCouponApiCouponInfo(stockList: any, send_coupon_merchant: any): {
miniPluginParams: {
send_coupon_params: any;
send_coupon_merchant: any;
sign: string;
};
miniApiParams: {
cardList: any;
};
jssdkParams: {
cardList: any;
};
};
/**
* 把json转成xml数据

@@ -94,9 +197,14 @@ * @param json json数据

type Options = {
/**商户id*/
mchid: string;
serialNo?: string;
privateKey?: string | crypto.KeyObject;
/**证书序列号*/
serialNo: string;
/**V3商户秘钥*/
apiV3Secret: string;
/**证书私钥*/
privateKey: string | crypto.KeyObject;
privateKeyStr?: string;
publicKey?: string | crypto.KeyObject;
/**证书公钥*/
publicKey: string | crypto.KeyObject;
publicKeyStr?: string;
apiV3Secret?: string;
};

@@ -361,10 +469,9 @@ type WXPayPlatformCert = {

payUtils: {
sign(message: string, privateKey: string): string;
encrypt(data: string, aad: string, iv: string, secret: string): {
ciphertext: string;
associated_data: string;
nonce: string;
};
decipher(ciphertext: string, aad: string, iv: string, secret: string): string;
buildMessage(messages: string[]): string;
signRSASHA256: typeof signRSASHA256;
getH5CouponUrl: typeof getH5CouponUrl;
getWXPayCouponApiCouponInfo: typeof getWXPayCouponApiCouponInfo;
signHMACSHA256: typeof signHMACSHA256;
encrypt: typeof encrypt;
decipher: typeof decipher;
buildMessage: typeof buildMessage;
};

@@ -371,0 +478,0 @@ };

@@ -7,2 +7,76 @@ import * as axios from 'axios';

/**
* 获取RSA-SHA256签名,返回base64格式
* @param message 需要被签名的信息
* @param privateKey 私钥,PRIVATE KEY
* @returns 签名信息
*/
declare function signRSASHA256(message: string, privateKey: string): string;
/**
* 微信支付H5卡券领取方法
*
* @param {string} stock_id - 微信支付商家券批次.
* @param {string} send_coupon_merchant - 微信支付服务商id.
* @param {string} open_id - 需要发放的用户openid,此openid需要是与商户号关联的公众号openid或者小程序的openid.
* @return {string} 返回领取链接
*/
declare function getH5CouponUrl(stock_id: string, send_coupon_merchant: string, open_id: string, secret: string): string;
/**
* 微信支付卡券领取参数生成
*
* @param {Array<{stock_id: string, out_request_no: string, coupon_code?: string,customize_send_time?:string}>} stockList - 微信卡券批次信息
* @param {string|number} send_coupon_merchant 商户id,服务商请填写服务商id,直连商户填直连商户id
* @return {{miniPluginParams,miniApiParams,jssdkParams}} an object containing three parameters: miniPluginParams, miniApiParams, and jssdkParams
*/
declare function getWXPayCouponApiCouponInfo(stockList: any, send_coupon_merchant: any, secret: string): {
miniPluginParams: {
send_coupon_params: any;
send_coupon_merchant: any;
sign: any;
};
miniApiParams: {
cardList: any;
};
jssdkParams: {
cardList: any;
};
};
/**
* 微信体系模式的Hmac sha256签名
* @param param 任意的参数形式
* @param secret 签名秘钥
* @returns sha256签名
*/
declare function signHMACSHA256(param: {
[key: string]: any;
}, secret: string): string;
/**
* aes-256-gcm加密数据
* @param data 待加密的字符串
* @param aad 额外的鉴权信息
* @param iv 初始向量
* @param secret 密钥
* @returns 加密数据
*/
declare function encrypt(data: string, aad: string, iv: string, secret: string): {
ciphertext: string;
associated_data: string;
nonce: string;
};
/**
* aes-256-gcm解密数据
* @param ciphertext 密文
* @param aad 鉴权数据
* @param iv 初始向量
* @param secret 密钥
* @returns 界面的数据
*/
declare function decipher(ciphertext: string, aad: string, iv: string, secret: string): string;
/**
* 构建签名的信息
* @param messages 信息数组
* @returns 返回构建好的字符串
*/
declare function buildMessage(messages: string[]): string;
type WXPayOptions = {

@@ -59,2 +133,31 @@ /** 商户号 */

/**
* 微信支付H5卡券领取方法
*
* @param {string} stock_id - 微信支付商家券批次.
* @param {string} send_coupon_merchant - 微信支付服务商id.
* @param {string} open_id - 需要发放的用户openid,此openid需要是与商户号关联的公众号openid或者小程序的openid.
* @return {string} 返回领取链接
*/
getH5CouponUrl(stock_id: string, send_coupon_merchant: string, open_id: string): string;
/**
* 微信支付卡券领取参数生成
*
* @param {Array<{stock_id: string, out_request_no: string, coupon_code?: string,customize_send_time?:string}>} stockList - 微信卡券批次信息
* @param {string|number} send_coupon_merchant 商户id,服务商请填写服务商id,直连商户填直连商户id
* @return {{miniPluginParams,miniApiParams,jssdkParams}} an object containing three parameters: miniPluginParams, miniApiParams, and jssdkParams
*/
getWXPayCouponApiCouponInfo(stockList: any, send_coupon_merchant: any): {
miniPluginParams: {
send_coupon_params: any;
send_coupon_merchant: any;
sign: string;
};
miniApiParams: {
cardList: any;
};
jssdkParams: {
cardList: any;
};
};
/**
* 把json转成xml数据

@@ -94,9 +197,14 @@ * @param json json数据

type Options = {
/**商户id*/
mchid: string;
serialNo?: string;
privateKey?: string | crypto.KeyObject;
/**证书序列号*/
serialNo: string;
/**V3商户秘钥*/
apiV3Secret: string;
/**证书私钥*/
privateKey: string | crypto.KeyObject;
privateKeyStr?: string;
publicKey?: string | crypto.KeyObject;
/**证书公钥*/
publicKey: string | crypto.KeyObject;
publicKeyStr?: string;
apiV3Secret?: string;
};

@@ -361,10 +469,9 @@ type WXPayPlatformCert = {

payUtils: {
sign(message: string, privateKey: string): string;
encrypt(data: string, aad: string, iv: string, secret: string): {
ciphertext: string;
associated_data: string;
nonce: string;
};
decipher(ciphertext: string, aad: string, iv: string, secret: string): string;
buildMessage(messages: string[]): string;
signRSASHA256: typeof signRSASHA256;
getH5CouponUrl: typeof getH5CouponUrl;
getWXPayCouponApiCouponInfo: typeof getWXPayCouponApiCouponInfo;
signHMACSHA256: typeof signHMACSHA256;
encrypt: typeof encrypt;
decipher: typeof decipher;
buildMessage: typeof buildMessage;
};

@@ -371,0 +478,0 @@ };

@@ -263,2 +263,79 @@ import xml2js from 'xml2js';

/**
* 微信支付H5卡券领取方法
*
* @param {string} stock_id - 微信支付商家券批次.
* @param {string} send_coupon_merchant - 微信支付服务商id.
* @param {string} open_id - 需要发放的用户openid,此openid需要是与商户号关联的公众号openid或者小程序的openid.
* @return {string} 返回领取链接
*/
getH5CouponUrl(stock_id, send_coupon_merchant, open_id) {
const params = {
stock_id,
out_request_no: Date.now(),
send_coupon_merchant,
open_id
};
const sign = this.signHMACSHA256(params);
const url = `https://action.weixin.qq.com/busifavor/getcouponinfo?stock_id=${params.stock_id}&out_request_no=${params.out_request_no}&sign=${sign}&send_coupon_merchant=${params.send_coupon_merchant}&open_id=${open_id}#wechat_redirect`;
return url;
}
/**
* 微信支付卡券领取参数生成
*
* @param {Array<{stock_id: string, out_request_no: string, coupon_code?: string,customize_send_time?:string}>} stockList - 微信卡券批次信息
* @param {string|number} send_coupon_merchant 商户id,服务商请填写服务商id,直连商户填直连商户id
* @return {{miniPluginParams,miniApiParams,jssdkParams}} an object containing three parameters: miniPluginParams, miniApiParams, and jssdkParams
*/
getWXPayCouponApiCouponInfo(stockList, send_coupon_merchant) {
const signParams = stockList.reduce((pval, item, index) => {
const itemVal = Object.keys(item).reduce((val, key) => {
val[`${key}${index}`] = item[key];
return val;
}, {});
return {
...pval,
...itemVal
};
}, {});
signParams["send_coupon_merchant"] = send_coupon_merchant;
const sign = this.signHMACSHA256(signParams);
const miniPluginParams = {
// send_coupon_params这里还是直接传:stock_id、out_request_no,不需要带商户id
send_coupon_params: stockList,
send_coupon_merchant,
sign
};
const miniApiParams = {
cardList: stockList.map((item, index) => {
if (index === 0) {
return {
cardId: item.stock_id,
//对应商家券批次号
cardExt: JSON.stringify({
sign,
send_coupon_merchant,
coupon_code: item.coupon_code,
customize_send_time: item.customize_send_time,
out_request_no: item.out_request_no
})
};
}
return {
cardId: item.stock_id,
//对应商家券批次号
cardExt: JSON.stringify({
coupon_code: item.coupon_code,
customize_send_time: item.customize_send_time,
out_request_no: item.out_request_no
})
};
})
};
return {
miniPluginParams,
miniApiParams,
jssdkParams: miniApiParams
};
}
/**
* 把json转成xml数据

@@ -776,65 +853,118 @@ * @param json json数据

const PayUtil = {
/**
* 获取RSA-SHA256签名,返回base64格式
* @param message 需要被签名的信息
* @param privateKey 私钥,PRIVATE KEY
* @returns 签名信息
*/
sign(message, privateKey) {
const sign = crypto.createSign("RSA-SHA256");
sign.update(message);
const signature = sign.sign(privateKey, "base64");
return signature;
},
/**
* aes-256-gcm加密数据
* @param data 待加密的字符串
* @param aad 额外的鉴权信息
* @param iv 初始向量
* @param secret 密钥
* @returns 加密数据
*/
encrypt(data, aad, iv, secret) {
let cipher = crypto.createCipheriv("aes-256-gcm", secret, iv);
cipher.setAAD(Buffer.from(aad, "utf-8"));
let ciphertext = cipher.update(Buffer.from(data, "utf-8"));
let encrypted = Buffer.concat([
ciphertext,
cipher.final(),
cipher.getAuthTag()
]);
function signRSASHA256(message, privateKey) {
const sign = crypto.createSign("RSA-SHA256");
sign.update(message);
const signature = sign.sign(privateKey, "base64");
return signature;
}
function getH5CouponUrl(stock_id, send_coupon_merchant, open_id, secret) {
const params = {
stock_id,
out_request_no: Date.now(),
send_coupon_merchant,
open_id
};
const sign = signHMACSHA256(params, secret);
const url = `https://action.weixin.qq.com/busifavor/getcouponinfo?stock_id=${params.stock_id}&out_request_no=${params.out_request_no}&sign=${sign}&send_coupon_merchant=${params.send_coupon_merchant}&open_id=${open_id}#wechat_redirect`;
return url;
}
function getWXPayCouponApiCouponInfo(stockList, send_coupon_merchant, secret) {
const signParams = stockList.reduce((pval, item, index) => {
const itemVal = Object.keys(item).reduce((val, key) => {
val[`${key}${index}`] = item[key];
return val;
}, {});
return {
ciphertext: encrypted.toString("base64"),
associated_data: aad,
nonce: iv
...pval,
...itemVal
};
},
/**
* aes-256-gcm解密数据
* @param ciphertext 密文
* @param aad 鉴权数据
* @param iv 初始向量
* @param secret 密钥
* @returns 界面的数据
*/
decipher(ciphertext, aad, iv, secret) {
const _ciphertext = Buffer.from(ciphertext, "base64");
const authTag = _ciphertext.slice(_ciphertext.length - 16);
const data = _ciphertext.slice(0, _ciphertext.length - 16);
const decipher = crypto.createDecipheriv("aes-256-gcm", secret, iv);
decipher.setAuthTag(authTag);
decipher.setAAD(Buffer.from(aad));
const decoded = decipher.update(data, void 0, "utf8");
decipher.final();
return decoded;
},
/**
* 构建签名的信息
* @param messages 信息数组
* @returns 返回构建好的字符串
*/
buildMessage(messages) {
return messages.concat("").join("\n");
}
}, {});
signParams["send_coupon_merchant"] = send_coupon_merchant;
const sign = this.signHMACSHA256(signParams, secret);
const miniPluginParams = {
// send_coupon_params这里还是直接传:stock_id、out_request_no,不需要带商户id
send_coupon_params: stockList,
send_coupon_merchant,
sign
};
const miniApiParams = {
cardList: stockList.map((item, index) => {
if (index === 0) {
return {
cardId: item.stock_id,
//对应商家券批次号
cardExt: JSON.stringify({
sign,
send_coupon_merchant,
coupon_code: item.coupon_code,
customize_send_time: item.customize_send_time,
out_request_no: item.out_request_no
})
};
}
return {
cardId: item.stock_id,
//对应商家券批次号
cardExt: JSON.stringify({
coupon_code: item.coupon_code,
customize_send_time: item.customize_send_time,
out_request_no: item.out_request_no
})
};
})
};
return {
miniPluginParams,
miniApiParams,
jssdkParams: miniApiParams
};
}
function signHMACSHA256(param, secret) {
let querystring = Object.keys(param).sort().filter((key) => {
return param[key] != null && param[key] !== "";
}).filter((key) => {
return ["sign", "key"].indexOf(key) < 0;
}).map((key) => {
return key + "=" + param[key];
}).join("&");
const hash = crypto.createHmac("sha256", secret).update(querystring + "&key=" + secret).digest("hex");
return hash.toUpperCase();
}
function encrypt(data, aad, iv, secret) {
let cipher = crypto.createCipheriv("aes-256-gcm", secret, iv);
cipher.setAAD(Buffer.from(aad, "utf-8"));
let ciphertext = cipher.update(Buffer.from(data, "utf-8"));
let encrypted = Buffer.concat([
ciphertext,
cipher.final(),
cipher.getAuthTag()
]);
return {
ciphertext: encrypted.toString("base64"),
associated_data: aad,
nonce: iv
};
}
function decipher(ciphertext, aad, iv, secret) {
const _ciphertext = Buffer.from(ciphertext, "base64");
const authTag = _ciphertext.slice(_ciphertext.length - 16);
const data = _ciphertext.slice(0, _ciphertext.length - 16);
const decipher2 = crypto.createDecipheriv("aes-256-gcm", secret, iv);
decipher2.setAuthTag(authTag);
decipher2.setAAD(Buffer.from(aad));
const decoded = decipher2.update(data, void 0, "utf8");
decipher2.final();
return decoded;
}
function buildMessage(messages) {
return messages.concat("").join("\n");
}
const PayUtil = {
signRSASHA256,
getH5CouponUrl,
getWXPayCouponApiCouponInfo,
signHMACSHA256,
encrypt,
decipher,
buildMessage
};

@@ -841,0 +971,0 @@

{
"name": "@humwing/wxpay",
"version": "0.0.2",
"version": "0.0.3",
"description": "wechat pay",
"keywords": [
"微信支付",
"微信支付V3接口"
"微信支付V3接口",
"微信支付发券"
],

@@ -9,0 +10,0 @@ "license": "MIT",