///
import { Agent } from 'https';
import { WeChatPaymentAPIConfig, WeChatPaymentConfig } from './config';
import Store from './store/Store';
import { Options } from 'got';
import { WeChatOptions } from './WeChatOptions';
interface PaymentSignatureObject {
params: Record;
paySign: string;
}
interface ChoosePaymentData {
timestamp: string;
nonceStr: string;
package: string;
signType: string;
paySign: string;
}
interface PaymentRequestResult {
return_code?: string;
return_msg?: string;
result_code?: string;
err_code?: number;
err_code_des?: string;
out_trade_no?: string;
sandbox_signkey?: string;
}
interface SimpleRequestResult {
requestData: Record;
responseData: PaymentRequestResult;
}
/**
* Wechat Payment class
* @constructor
* @param {WeChatOptions} options
* @return {Payment} Payment instance
*/
declare class Payment {
options: WeChatOptions;
paymentConfig: WeChatPaymentConfig;
paymentAPI: WeChatPaymentAPIConfig;
notifyUrl: string;
store: Store;
paymentAgent: Agent;
constructor(options?: WeChatOptions);
/**
* 1000fen -> 10RMB
* @param value
* @return {number}
*/
static fenToYuan(value: number | string): number;
/**
* 10RMB -> 1000fen, 10.123RMB -> 1012fen
* @param value
* @return {number}
*/
static yuanToFen(value: number | string): number;
/**
* RMB yuan, 10.123 => 10.12, 10 => 10.00, 10.456 => 10.45
* @param value
* @return {string} formatted currency
*/
static formatCurrency(value: number | string): string;
static get DOWNLOAD_BILL_TYPE(): Record;
static get TRADE_TYPE(): Record;
static get TRADE_STATE(): Record;
static get REFUND_STATUS(): Record;
static get FUND_ACCOUNT_TYPE(): Record;
static get SIGN_TYPE(): Record;
static get PAYMENT_TYPE(): Record;
static get COUPON_TYPE(): Record;
/**
* Generate payment signature
* @param {object} params
* @param {string=} signType
* @param {Boolean=} sandbox gen sign for retrieve sandbox sign key
* @return {object} signature object
*/
generateSignature(params: Record, signType?: string, sandbox?: boolean): Record;
/**
* Generate paySign info for jssdk to invoke wechat payment
* @param {string} prepayId received from unifiedOrder()
* @param {string=} signType MD5 or SHA1, default MD5
*/
generateChooseWXPayInfo(prepayId: string, signType?: string): Promise;
/**
* General payment sign generator
* @param params - data used to gen payment sign
* @param signType
* @param sandbox - if gen the sign to get sandbox api key
*/
generateGeneralPaymentSignature(params: Record, signType: string, sandbox?: boolean): PaymentSignatureObject;
/**
* Get extra options when pfx needed
* @return {object}
*/
getPaymentAgent(): Agent;
/**
* Generate simple trade id
* @return {string}
*/
simpleTradeNo(): string;
/**
* Generate unified order from wechat
* @param {object} orderInfo
* @return {Promise}
*/
unifiedOrder(orderInfo: Record): Promise;
/**
* Query specific order status from wechat
* @param {object} queryInfo
* @return {Promise}
*/
queryOrder(queryInfo: Record): Promise;
/**
* Close order from wechat
* @param {string} orderId wechat out_trade_no
* @return {Promise}
*/
closeOrder(orderId: string): Promise;
/**
* Request refund from wechat
* @param {object} info:
* {
* transaction_id: '123',
* out_trade_no: '3210', //only one of 'transaction_id' or 'out_trade_no' is required
* out_refund_no: '1234', //required, merchant order refund id, similar with 'out_trade_no'
* total_fee: '100', //required
* refund_fee: '100', //required
* refund_fee_type: 'CNY', //optional
* refund_desc: '', //optional
* refund_account: '', //optional, one of ['REFUND_SOURCE_UNSETTLED_FUNDS', 'REFUND_SOURCE_RECHARGE_FUNDS']
* notify_url: '', //optional
* }
* @return {Promise}
*/
refund(info: Record): Promise>;
/**
* Query refund status from wechat
* @param {object} info:
* {
* transaction_id: '',
* out_trade_no: '',
* out_refund_no: '',
* refund_id: '', //only one of four above is required
* offset: 15, //optional, start from number 16
* }
* @return {Promise}
*/
queryRefund(info: Record): Promise;
/**
* Download bill from wechat
* @param {string} billDate e.g: 20180603
* @param {string} billType e.g: FUND_ACCOUNT_TYPE.BASIC
* @param {boolean=} noGzip if download stream is gziped
* @return {Promise}
*/
downloadBill(billDate: string, billType: string, noGzip?: boolean): Promise>;
/**
* Download fund flow
* @param {string} billDate e.g: 20180603
* @param {string} accountType e.g: BILL_TYPE.ALL
* @param {boolean=} noGzip if download stream is gziped
* @return {Promise}
*/
downloadFundFlow(billDate: string, accountType: string, noGzip?: boolean): Promise>;
/**
* Download functionality
* @param {object} data request data
* @param {object=} requestOptions options send to request
* @param url to wechat api endpoint
* @return {Promise} resolve or reject with:
* {
* //an error object, rejected
* error: new Error(),
* //error msg, , rejected
* msg: '',
* //resolved with download stream,
* //can be piped to other writable stream, e.g: result.data.pipe(fs.createWritableStream('./bill.txt'))
* data: Stream,
* //sign info from response header
* digest: 'SHA=ec45d7c24492dcd62d92472b0f2816c8d9a2d773',
* }
*/
download(url: string, data: Record, requestOptions?: {
[key: string]: string | number | boolean;
} | Options): Promise>;
/**
* Report wechat services status to wechat
* @param {object} info
* @return {Promise}
*/
reportToWechat(info: Record): Promise;
getSandboxSignKey(): Promise;
/**
* Simplified request wrapper
* @param {string} apiUrl
* @param {object} info
* @param {Number=} attempts, sandbox key error retry count
*/
simpleRequest(apiUrl: string, info: Record, attempts?: number): Promise;
/**
* Merge custom params with default params
* @param customParams
* @param signType
* @return {object}
*/
mergeParams(customParams: Record, signType?: string): Record;
/**
* Parse xml data notified by wechat server
* @param data
* @return {Promise}
*/
parseNotifyData(data: string): Promise>;
/**
* Get xml reply data based on success or fail
* @param {boolean} isSuccess
* @return {Promise}
*/
replyData(isSuccess: boolean): Promise;
/**
* Format download bill date to format like: 20170101
* @param {string|Date} date
* @return {string}
*/
getDownloadBillDate(date: string | Date): string;
/**
* Get api key based on env
* @param {Boolean=} getSandboxKey the sandbox api key should also use the original payment api key
*/
getAPISignKey(getSandboxKey?: boolean): string;
/**
* Decrypt wechat refund notify result
* @see https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_16&index=10
* @param {string} xmlResult notify xml data
*/
decryptRefundNotifyResult(xmlResult: string): Promise<{
parsedXMLData: Record;
decryptedData: Record;
}>;
/**
* Batch query user comments
* @param {string} beginTime in format 'YYYYMMDDHHmmss'
* @param {string} endTime same as beginTime
* @param {number=} offset integer
* @param {number=} limit integer
* @return {Promise}
*/
queryComments(beginTime: string, endTime: string, offset?: number, limit?: number): Promise;
/**
* International merchant only
* Retrieve foreign currency settlements within the specified date range
* @param {object} query
* @see https://pay.weixin.qq.com/wiki/doc/api/external/jsapi.php?chapter=9_14&index=9
* @return {Promise}
*/
querySettlement(query: Record): Promise;
/**
* International merchant only
* Retrieve exchange rate for given foreign currency and date
* @param {object} query
* @see https://pay.weixin.qq.com/wiki/doc/api/external/jsapi.php?chapter=9_15&index=10
* @return {Promise}
*/
queryExchangeRate(query: Record): Promise;
}
export default Payment;