Skip to content

wechatpay.js

James ZHANG edited this page Jan 19, 2022 · 6 revisions

Wechatpay

A WeChatPay OpenAPI v2&v3's amazing client.

Kind: global class

new Wechatpay(config)

Constructor of the magic APIv2&v3's chain.

Param Type Description
config object configuration
config.mchid string The merchant ID
config.serial string The serial number of the merchant certificate
config.privateKey string | Buffer The merchant private key certificate
config.certs object The wechatpay provider size configuration, {serial: publicKey} pair
[config.secret] string The merchant secret key string
[config.merchant] object The merchant certificates, more @see {import('tls').createSecureContext}
[config.merchant.cert] string | Buffer The merchant cert chains in PEM format
[config.merchant.key] string | Buffer The merchant private keys in PEM format
[config.merchant.pfx] string | Buffer The merchant PFX or PKCS12 encoded private key and certificate chain.
[config.merchant.passphrase] string | Buffer The merchant shared passphrase used for a single private key and/or a PFX.

Example

const {Wechatpay} = require('wechatpay-axios-plugin');
const wxpay = new Wechatpay({
  mchid,
  serial,
  privateKey: '-----BEGIN PRIVATE KEY-----\n-FULL-OF-THE-FILE-CONTENT-\n-----END PRIVATE KEY-----',
  certs: {
    'serial_number': '-----BEGIN CERTIFICATE-----\n-FULL-OF-THE-FILE-CONTENT-\n-----END CERTIFICATE-----',
  },
  secret,
  merchant: {
    cert,
    key,
    // pfx,
    // passphase,
  }
});

wxpay.v2.pay.micropay({}).then(console.info).catch(console.error);

wxpay.v2.secapi.pay.refund.post({}).then(console.info).catch(console.error);

wxpay.v3.marketing.busifavor.stocks.post({})
  .then(({data}) => console.info(data))
  .catch(({response: {data}}) => console.error(data));

wxpay.v3.pay.transactions.native.post({})
  .then(({data: {code_url}}) => console.info(code_url))
  .catch(({ response: {data}}) => console.error(data));

(async () => {
  try {
    const {data: detail} = await wxpay.v3.pay.transactions.id.$transaction_id$
      .get({params: {mchid: '1230000109'}, transaction_id: '1217752501201407033233368018'});
    // or simple like this
    // const {data: detail} = await wxpay.v3.pay.transactions.id['{transaction_id}']
    //   .get({params: {mchid: '1230000109'}, transaction_id: '1217752501201407033233368018'});
    // or simple like this
    // const {data: detail} = await wxpay.v3.pay.transactions.id['1217752501201407033233368018']
    //   .get({params: {mchid: '1230000109'}});
    console.info(detail);
  } catch({response: {status, statusText, data}}) {
    console.error(status, statusText, data);
  }
})();

Wechatpay.client

Kind: static property of Wechatpay
Properties

Name Type Description
client Decorator The Decorator instance

Wechatpay.handler

Kind: static property of Wechatpay
Properties

Name Type Description
handler object A Getter handler object

handler.get(target, property) ⇒ object

Object's getter handler

Kind: instance method of handler
Returns: object - - An object or object's property

Param Type Description
target object The object
property string The property

Wechatpay.normalize(str) ⇒ string

Normalize the str by the rules: PascalCase -> camelCase & camelCase -> camel-case & $dynamic$ -> {dynamic}

Kind: static method of Wechatpay
Returns: string - - The transformed string

Param Type Description
str string The string waiting for normalization

Wechatpay.compose([prefix], [suffix]) ⇒ Proxy

Compose a named function with prefix and suffix whose joined by a slash(/)

Kind: static method of Wechatpay
Returns: Proxy - - With a special Getter Function.

Param Type Description
[prefix] string The prefix string.
[suffix] string The suffix string.

Wechatpay.chain(pathname) ⇒ function

Chain the input pathname with several HTTP verbs onto a Function object. The standardized verbs(methods) are defined in all-uppercase US-ASCII letters.

Kind: static method of Wechatpay
Returns: function - - Named as given pathname function
See: https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods

Param Type Description
pathname string The pathname string.