• Stars
    star
    113
  • Rank 310,115 (Top 7 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 6 years ago
  • Updated over 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

微信支付 SDK,支持刷卡支付、公众号支付、扫码支付、APP支付、H5支付,以及优惠券,红包,企业付款,微信代扣

微信支付 node.js

Build Status Codacy Badge dependencies Status Known Vulnerabilities

微信支付 SDK,支持刷卡支付、公众号支付、扫码支付、APP 支付、H5 支付,以及优惠券,红包,企业付款,微信代扣

特性

  • 使用 Typescript 编写,拥有更安全的类型和更好的代码提示
  • 支持所有类型微信支付
  • 支持微信代扣
  • 提供调试模式

开始使用

const path = require("path");
const fs = require("fs");
const {
  PubPay,
  RequestError,
  CommunicationError,
  utils: { getXMLBody }
} = require("@sigodenjs/wechatpay");

const pay = new PubPay({
  appId: "wxb80e5bddb2d804f3",
  key: "6Q9VX4N3WTBM9G9XBL7H1L9PB9ANHLY7",
  mchId: "1434712502",
  pfx: fs.readFileSync(path.resolve(__dirname, "cert.p12"))
});

// 调用统一下单接口
pay
  .unifiedOrder({
    body: "腾讯充值中心-QQ会员充值",
    out_trade_no: "1217752501201407033233368018",
    total_fee: 888,
    spbill_create_ip: "8.8.8.8",
    notify_url: "https://example.com/wechatpay/notify",
    trade_type: "JSAPI",
    openid: "oUpF8uMuAJO_M2pxb1Q9zNjWeS6o"
  })
  .then(res => {
    if (!pay.verifySign(res)) {
      // 签名校验失败
      throw new Error("签名校验失败");
    }
    if (res.result_code === "FAIL") {
      console.log(res.err_code, res.err_code_des);
    } else {
      console.log(res.prepay_id);
    }
  })
  .catch(err => {
    if (err instanceof RequestError) {
      // 请求错误
    } else if (err instanceof CommunicationError) {
      // return_code = FAIL
    }
  });

// 支付结果通知
router.post("/wechatpay/notify", (req, res) => {
  const options = {
    length: req.headers["content-length"],
    limit: "1mb",
    encoding: "utf8"
  };
  getXMLBody(req, options).then(data => {
    pay
      .payNotify(data, async parsedData => {
        if (!pay.verifySign(parsedData)) {
          // 签名校验失败
        }
        if (parsedData.result_code === "FAIL") {
          // 业务逻辑失败
        }
        // ...
        return {
          return_code: "SUCCESS",
          return_msg: "OK"
        };
      })
      .then(returnData => {
        res.set("Content-Type", "application/xml; charset=utf-8");
        res.end(returnData);
      });
  });
});

类和接口

业务类

综合类

  • Pay: 组合所有支付类,包含所有支付相关接口
  • Entrust: 组合所有代扣类,包含所有代扣相关接扣

如果你只使用了某一项支付,请使用业务类。但如果涉及多种支付,使用综合类更便捷。

支付相关接口

代扣相关接口

许可证

Copyright (c) 2018 sigoden

Licensed under the MIT license.

More Repositories

1

dufs

A file server that supports static serving, uploading, searching, accessing control, webdav...
Rust
5,158
star
2

aichat

All-in-one AI CLI tool that integrates 20+ AI platforms, including OpenAI, Azure-OpenAI, Gemini, Claude, Mistral, Cohere, VertexAI, Bedrock, Ollama, Ernie, Qianwen, Deepseek...
Rust
3,111
star
3

argc

A Bash CLI framework, also a Bash-based command runner.
Rust
665
star
4

window-switcher

Easily switch between windows of the same app with Alt+` (Backtick), also switch between apps with Alt+Tab.
Rust
415
star
5

upt

Universal Package-management Tool for any OS.
Rust
391
star
6

proxyfor

A lightweight proxy for capturing HTTP(S) and WS(S) traffic.
Rust
258
star
7

argc-completions

{bash,zsh,fish,powershell,nushell}-completions for 1000+ commands.
Shell
188
star
8

netease-music-crx

浏览器插件版网易云音乐
JavaScript
156
star
9

apitest

Apitest is declarative api testing tool with JSON-like DSL.
TypeScript
110
star
10

projclean

Project dependencies & build artifacts cleanup tool.
Rust
92
star
11

htte

Document Driven API Test Framework
JavaScript
73
star
12

clii

Easily build a cli app.
TypeScript
40
star
13

dynimgen

A dynamic image generator.
Rust
30
star
14

opscan

A open port scanner.
Rust
30
star
15

chrome-extensions-manager

A snapshot based chrome extensions manager
JavaScript
27
star
16

wechat-devtools

使用 Linux, Docker 运行微信 web 开发者工具
Shell
21
star
17

llm-functions

Extend LLM with functions written in bash/js/python.
Shell
17
star
18

runme

[Deprecatd] A shell-script based task runner.
Rust
13
star
19

tomato-timer

A terminal tomato timer with notification
Rust
11
star
20

vmprotect-keygen

vmprotect keygen for nodejs
TypeScript
9
star
21

wasm-pkg-build

Effortlessly create npm packages from Rust wasm crates.
TypeScript
8
star
22

webhook

webhook-cli is a lightweight configurable tool written in NodeJS, that allows you to easily create HTTP endpoints (hooks) on your server, which you can use to execute configured commands
JavaScript
6
star
23

a-captcha

A Lightweight Pure JavaScript Captcha for Node.js
TypeScript
5
star
24

subexpo

Block explorer for Substrate based chain
JavaScript
4
star
25

xf

File-aware dynamic command runner.
Rust
3
star
26

node-fisheye

A opencv fisheye camera model bindings for Node.js.
C++
3
star
27

chatgpt-wechat-browser-extension

ChatGPT For Wechat FileHelper
JavaScript
2
star
28

glob-convert-encoding

Convert encoding of files that match glob
JavaScript
2
star
29

sequelize-modelgen

Generate sequelize models from sql
JavaScript
2
star
30

node-imagediff

Diff image to check whether objects have changed.
C++
2
star
31

gatsby-plugin-baidu-tongji

添加百度统计到 Gatsby 站点
JavaScript
2
star
32

deepin-wine-baidupan-arch

在Archlinux及衍生发行版上运行百度盘
Shell
2
star
33

conditions-lang

Boolean language for conditional builds, stages, jobs
TypeScript
2
star
34

fakepty

Run a command in a fake pty.
Rust
2
star
35

mynotes

My persional notebook.
2
star
36

orgdo

Command-line tool to manage the Todo lists
TypeScript
1
star
37

iredismodule

Create redis module with rust
Rust
1
star
38

trisue

Trisue是一款 REST API 调试及异常报告工具
JavaScript
1
star
39

dee

document-driven web framework, powered by express and openapi
TypeScript
1
star
40

use-services-packages

TypeScript
1
star
41

solid-color-page

Solid color page
HTML
1
star
42

fswebcam

wrap linux tool fswebcam to manipulate cameras
JavaScript
1
star
43

wasm-toml-js

TOML format for Node.js
Rust
1
star
44

alipay

蚂蚁金服开放平台 node sdk
TypeScript
1
star
45

myblog

Shell
1
star
46

toy-ipfs

Rust
1
star
47

change-case

Transform a string between camelCase, PascalCase, Capital Case, snake_case, param-case, CONSTANT_CASE and others.
Rust
1
star
48

mycrypt

Encrypt/decrypt your file
Rust
1
star
49

terminal-fonts

Big fonts for terminal display. Each character is a block formed by many dots.
Rust
1
star
50

calc-rs

A simple arithmetic calculator written in rust
Rust
1
star
51

install-gh-release

A website to automatically generate bash script to install binary published on github releases.
HTML
1
star
52

fp-course

Functional Programming Course
Haskell
1
star
53

dee-swaggerize

swagger document driven route builder for dee framework
JavaScript
1
star
54

install-binary

GitHub Action to install a binary from a GitHub Release
TypeScript
1
star