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

@apps-sdk/wechat-pay

v0.1.0

Published

微信支付

Downloads

2

Readme

@apps-sdk/wechat-pay

微信支付v3接口。

已实现平台:

  • 小程序

已实现功能

  • 支付
  • 回调通知

安装

pnpm add @apps-sdk/wechat-pay

小程序支付

1. 初始化

import { MiniAppWeChatPay } from '@apps-sdk/wechat-pay';

const wepay = new MiniAppWeChatPay<Attach>({
  appid: '',
  mchid: '',
  cert: {
    file: '',
    seriesNo: '',
    secret: '',
  },
});

type Attach =
  | {
      type: 'business1';
      data1: {};
    }
  | {
      type: 'business2';
      data2: {};
    };

泛型为字段attach的类型约束,付款成功后可根据attach数据进行业务判断

2. 生成预定单

用户发起支付后,需要在服务端生成预定单,然后返回给前端

router.post('/wepay/prepare', (request, response) => {
  const args = await wepay.generatePaymentArgs({
    description: '',
    notify_url: '',
    money: 100,
    openid: '',
    attach: {},
  });

  response.end(JSON.stringify(args));
});

客户端可根据args直接发起支付,并判断结果

wx.request({
  url: 'http://api.com/wepay/prepare',
  method: 'POST',
  success: ({ data: args }) => {
    const result = await wx.requestPayment(args);
    if (result.errMsg.includes('ok')) {
      console.log('付款成功');
    }
  },
});

3. 支付回调

用户付款后,微信支付平台会以POST的方式发送异步回调,地址为生成预定单提供的notify_url。业务系统需要判断通知的真实性,然后再做业务处理

router.post('/wepay/notify', async (request, response) => {
  const result = await wepay.verify(request);

  // 验证失败
  if (result === false) {
    response.statusCode = 403;
    response.end(JSON.stringify({ code: 'FAIL', message: '验证失败' }));
    return;
  }

  // 注意:通知可能多次发送,务必使用 transaction_id 或者 out_trade_no 对业务做去重判断

  if (result.trade_state === 'SUCCESS') {
    // 在这里处理支付成功后的业务,attach的类型为最开始介绍的Attach泛型
    console.log(result.attach);
  }

  // 必须响应,否则微信支付平台会再次发送通知
  response.statusCode = 204;
});