Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

wechatpay-nodesdk

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

wechatpay-nodesdk

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

  • 1.1.8
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
7
increased by16.67%
Maintainers
1
Weekly downloads
 
Created
Source

微信支付 node.js

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

##Fork版本

此版本是fork版本,在实际生产应用的时候发现了许多BUG,此项目为原有项目的部分bug修复版本。

特性

  • 使用 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);
      });
  });
});

类和接口

业务类

  • AppPay: APP 支付
  • Bank: 企业付款
  • Coupon: 代金券
  • LitePay: 小程序支付
  • PubPay: 公众号支付
  • PubQrPay: 扫码支付
  • PubScanPay: 刷卡支付
  • RedPack: 现金红包
  • WapPay: H5 支付
  • AppEntrust: APP 代扣
  • LiteEntrust: 小程序代扣
  • PubEntrsut: 公众号代扣
  • WapEntrust: H5 代扣

综合类

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

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

支付相关接口

代扣相关接口

许可证

Copyright (c) 2018 sigoden

Licensed under the MIT license.

FAQs

Package last updated on 06 May 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc