{"_id":"@apps-sdk/wechat-pay","name":"@apps-sdk/wechat-pay","dist-tags":{"latest":"0.1.0"},"versions":{"0.1.0":{"name":"@apps-sdk/wechat-pay","version":"0.1.0","description":"微信支付","repository":{"type":"git","url":"git+ssh://git@github.com/apps-sdk/wechat.git"},"license":"MIT","type":"module","sideEffects":false,"types":"dist/index.d.ts","exports":{".":{"types":"./dist/index.d.ts","import":"./dist/index.js"},"./package.json":"./package.json"},"publishConfig":{"access":"public"},"engines":{"node":">=18"},"dependencies":{"dayjs":"^1.11.12","foca-axios":"^4.1.1","nanoid":"^5.0.7","raw-body":"^3.0.0"},"scripts":{},"_id":"@apps-sdk/wechat-pay@0.1.0","bugs":{"url":"https://github.com/apps-sdk/wechat/issues"},"homepage":"https://github.com/apps-sdk/wechat#readme","_integrity":"sha512-fyJnTG/ZgIMwRR7S2LYGKkJSIS3Kxq2QaqRxsSszCeR/1a/9rVVbhfvkDDkWDEzWkqEZQPfRSqSbh9zWFIkeig==","_resolved":"/tmp/26f54dbba4d1858c3f63f55fb521f156/apps-sdk-wechat-pay-0.1.0.tgz","_from":"file:apps-sdk-wechat-pay-0.1.0.tgz","_nodeVersion":"20.16.0","_npmVersion":"10.8.1","dist":{"integrity":"sha512-fyJnTG/ZgIMwRR7S2LYGKkJSIS3Kxq2QaqRxsSszCeR/1a/9rVVbhfvkDDkWDEzWkqEZQPfRSqSbh9zWFIkeig==","shasum":"558acf9ea178a951b06a627ae2b4eac46d741727","tarball":"https://registry.npmjs.org/@apps-sdk/wechat-pay/-/wechat-pay-0.1.0.tgz","fileCount":7,"unpackedSize":29459,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCvFdgFH1GwqnH5CTTdrlGsZpYwmZ3g1ApjB5RZnp9OXQIgFPBpFHS6Wmpr6i435m/RDKMGg93c+aB2rt0sTemYKoo="}]},"_npmUser":{"name":"geekact","email":"geekacting@gmail.com"},"directories":{},"maintainers":[{"name":"geekact","email":"geekacting@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/wechat-pay_0.1.0_1723771715110_0.48450328664085673"},"_hasShrinkwrap":false}},"time":{"created":"2024-08-16T01:28:35.035Z","0.1.0":"2024-08-16T01:28:35.280Z","modified":"2024-08-16T01:28:35.545Z"},"maintainers":[{"name":"geekact","email":"geekacting@gmail.com"}],"description":"微信支付","homepage":"https://github.com/apps-sdk/wechat#readme","repository":{"type":"git","url":"git+ssh://git@github.com/apps-sdk/wechat.git"},"bugs":{"url":"https://github.com/apps-sdk/wechat/issues"},"license":"MIT","readme":"# @apps-sdk/wechat-pay\n\n微信支付v3接口。\n\n已实现平台：\n\n- 小程序\n\n已实现功能\n\n- 支付\n- 回调通知\n\n# 安装\n\n```bash\npnpm add @apps-sdk/wechat-pay\n```\n\n# 小程序支付\n\n## 1. 初始化\n\n```typescript\nimport { MiniAppWeChatPay } from '@apps-sdk/wechat-pay';\n\nconst wepay = new MiniAppWeChatPay<Attach>({\n  appid: '',\n  mchid: '',\n  cert: {\n    file: '',\n    seriesNo: '',\n    secret: '',\n  },\n});\n\ntype Attach =\n  | {\n      type: 'business1';\n      data1: {};\n    }\n  | {\n      type: 'business2';\n      data2: {};\n    };\n```\n\n泛型为字段`attach`的类型约束，付款成功后可根据attach数据进行业务判断\n\n## 2. 生成预定单\n\n用户发起支付后，需要在服务端生成预定单，然后返回给前端\n\n```typescript\nrouter.post('/wepay/prepare', (request, response) => {\n  const args = await wepay.generatePaymentArgs({\n    description: '',\n    notify_url: '',\n    money: 100,\n    openid: '',\n    attach: {},\n  });\n\n  response.end(JSON.stringify(args));\n});\n```\n\n客户端可根据args直接发起支付，并判断结果\n\n```typescript\nwx.request({\n  url: 'http://api.com/wepay/prepare',\n  method: 'POST',\n  success: ({ data: args }) => {\n    const result = await wx.requestPayment(args);\n    if (result.errMsg.includes('ok')) {\n      console.log('付款成功');\n    }\n  },\n});\n```\n\n## 3. 支付回调\n\n用户付款后，微信支付平台会以`POST`的方式发送异步回调，地址为生成预定单提供的`notify_url`。业务系统需要判断通知的真实性，然后再做业务处理\n\n```typescript\nrouter.post('/wepay/notify', async (request, response) => {\n  const result = await wepay.verify(request);\n\n  // 验证失败\n  if (result === false) {\n    response.statusCode = 403;\n    response.end(JSON.stringify({ code: 'FAIL', message: '验证失败' }));\n    return;\n  }\n\n  // 注意：通知可能多次发送，务必使用 transaction_id 或者 out_trade_no 对业务做去重判断\n\n  if (result.trade_state === 'SUCCESS') {\n    // 在这里处理支付成功后的业务，attach的类型为最开始介绍的Attach泛型\n    console.log(result.attach);\n  }\n\n  // 必须响应，否则微信支付平台会再次发送通知\n  response.statusCode = 204;\n});\n```\n","readmeFilename":"README.md"}