0.0.2 • Published 3 years ago

@i-orz/wechat-pay v0.0.2

Weekly downloads
-
License
MIT
Repository
-
Last release
3 years ago

node-wechat

Wechat Pay v3 Node.js SDK.

Install

yarn add @i-orz/wechat-pay

API

For more usage please issue.

constructor(config = {})

params

configrequireddescription
appIdyeswechat pay appid
mchIdyeswechat pay mchid
mchCertyeswehcat pay merchant certificate
mchCertKeyyeswehcat pay merchant certificate private key
mchCertSnyeswechat pay merchant certificate serial number
apiV3Keyyeswechat pay api v3 secret key
notifyUrlyesWechat Pay Notify callback url
notifySuccessWechat Pay Notify need response { code: 'SUCCESS' } event types
platformCertwehcat pay platform certificate
currencydefault is CNY
appTypemp = miniprogram
httpHTTP Client, default is built-in axios instance
errorclass of Error, default is WechatPayError
loggerfunction of logger, default is console
cachedefault is {}, it is reserved

return

Object of WechatPay Node.js SDK instance.

http(config)

http client for request wechat pay API.

params

paramrequireddescription
configyesconfig of axios

return

Promise of wechat pay result as axios response.

nonceStr()

return

String of nonce_str, random string.

timeStamp()

return

String of timestamp, unix timestamp second.

payRequest(payPackage, signType = 'RSA')

params

paramrequireddescription
payPackageyespackage of wechat pay, like prepay_id=***
signTypesignType of wechat pay, default is RSA

return

Object of wechat pay payment request

propdescription
appIdwehcat pay appid
timeStamptimestamp, seconds
nonceStrnonce string
packagepackage of wechat pay, like prepay_id=***
paySignbase64 signature
signTypesignType of wechat pay, like RSA

notify(data)

params

paramrequireddescription
datayesWechat Pay Notify request body

return

Object of Wechat Pay Notify decrypt resource with response suggestion.

propdescription
resourcedecrytp resource
responseresponse suggestion. like { code: 'SUCCESS' }

Example

const fs = require('fs');
const WechatPay = require('@axolo/node-wechat-pay');

const wechatPay = new WechatPay({
  appType: 'mp',
  appId: 'wechat_pay_appid',
  mchId: 'wechat_pay_mchid',
  mchCertSn: 'wechat_pay_mch_cert_serial_no',
  mchCertKey: fs.readFileSync('wehcat_pay_mch_cert_private_key.pem'),
  mchCert: fs.readFileSync('wehcat_pay_mch_cert.pem'),
  apiV3Key: 'wechat_pay_api_v3_secret',
  notifyUrl: 'https://url-of-wechat-pay-notify',
});

wechatPay.http.get('/v3/certificates').then(res => {
  wechatPay.logger.log(res.data);
}).catch(err => {
  wechatPay.logger.error(err);
});

Test

yarn test

TODO

  • support upload
  • test

Yueming Fang