Skip to content

wechatpay.js

James ZHANG edited this page Mar 20, 2021 · 6 revisions

Classes

Wechatpay

A Wechatpay APIv2&v3's amazing client.

const {Wechatpay} = require('wechatpay-axios-plugin')
const wxpay = new Wechatpay({
  mchid,
  serial,
  privateKey: '-----BEGIN PRIVATE KEY-----' + '...' + '-----END PRIVATE KEY-----',
  certs: {
    'serial_number': '-----BEGIN CERTIFICATE-----' + '...' + '-----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) } })()

Functions

get(target, property)object

Wechatpay

A Wechatpay APIv2&v3's amazing client.

const {Wechatpay} = require('wechatpay-axios-plugin')
const wxpay = new Wechatpay({
  mchid,
  serial,
  privateKey: '-----BEGIN PRIVATE KEY-----' + '...' + '-----END PRIVATE KEY-----',
  certs: {
    'serial_number': '-----BEGIN CERTIFICATE-----' + '...' + '-----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)
  }
})()

Kind: global class

new Wechatpay(config)

Constructor of the magic APIv2&v3's chain.

Param Type Description
config object configuration
config.mchid string | number 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: publicCert} 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.

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

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.

get(target, property) ⇒ object

Kind: global function
Returns: object - - An object or object's property

Param Type Description
target object The object
property string The property

Properties

Name Type Description
get function Object's getter handler
Clone this wiki locally