npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

egg-wechat-pay

v2.2.0

Published

Wechat pay plugin for egg.

Downloads

195

Readme

Build Status Coverage

egg-wechat-pay

Wechat pay plugin for egg.

Install

$ npm i egg-wechat-pay

Configurations

config/plugin.js

exports.wechatPay = {
  enable: true,
  package: 'egg-wechat-pay'
}

config/config.default.js

exports.wechatPay = {
  client: {
    // Optional,
    bodyPrefix: '麦当劳',
    appId,
    merchantId,
    secret,
    notifyUrl,
    pfx: fs.readFileSync(thePathToPFX)
  }
}

Then:

...
  async doSomething () {
    const params = await this.app.wechatPay.requestPayment(order)
    // {
    //   "appId": "wx...",
    //   "timeStamp": "1515043618",
    //   "nonceStr": "V0UGYV...",
    //   "signType": "MD5",
    //   "package": "prepay_id=wx2018...",
    //   "paySign": "54AD...",
    //   "timestamp": "1515043618"
    // }
  }
...

config.wechatPay.client

  • bodyPrefix ?String 商品描述的前缀,避免每次都需要写商品描述,格式见
  • appId String 应用的 appId,注意,该应用(小程序,服务号)需要开通微信支付功能,否则会报 商户号mch_id与appid不匹配 的错误
  • merchantId String 微信商户号,即 mch_id
  • secret String 微信支付的 API 密钥,请到 "微信支付|商户平台 -> API安全" 页面获取
  • notifyUrl URL 接收微信支付异步通知回调地址,通知url必须为直接可访问的url,不能携带参数
  • pfx Buffer | String 微信支付 API 证书(p12证书)

await wechatPay.requestPayment(order)

  • order Object 统一下单接口的参数
    • ip String 它是 spbill_create_ip 参数的简写
    • 其他参数

返回再次签名的回调结果,该结果可以直接被小程序,JSBridge,或客户端调用。

const payment = await wechatPay.requestPayment(order)

// https://mp.weixin.qq.com/debug/wxadoc/dev/api/api-pay.html#wxrequestpaymentobject
wx.requestPayment({
  ...payment,
  success (res) {

  },
  fail (res) {

  }
})

wechatPay.newApp(appId)

创建另一个应用的微信支付实例,可创建与同一个微信支付商户绑定的多个应用的实例

const params = this.app.wechatPay.newApp('wx2...')
.requestPayment(order)

await wechatPay.parse(xml)

将微信支付通知中的 return_msg(XML) 转换为 JavaScript 对象。XML 通知的结构见 这个文档

这个方法在转换的过程中,还会结合 config.wechatPay.client 校验 XML 是否有效,否则会 reject。

返回 Object

wechatPay.stringify(object)

将 JavaScript 转换为 XML 文本

返回 String

wechatPay.success(ctx)

  • ctx EggContext

向微信返回成功响应

wechatPay.success(ctx, message)

  • ctx EggContext
  • message String 作为 return_msg 的值

向微信返回失败的响应

License

MIT