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

wechat-pay-nodejs

v0.2.3

Published

微信支付 V3 NodeJS SDK

Downloads

27

Readme

微信支付 APIv3 NodeJS SDK

微信支付 APIv3 非官方 Node.js 语言开发库。 支持 直连商户服务商接口

安装

npm install wechat-pay-nodejs

使用示例

对象实例化 - 直连商户

import { WechatPay } from "wechat-pay-nodejs";

const wechatPay = new WechatPay({
    appid: '{appid}', // 应用ID
    mchid: '{mchid}', // 直连商户号
    cert_private_content: Buffer.from('{apiclient_key.pem}'), // 商户API私钥内容
    cert_public_content: Buffer.from('{apiclient_cert.pem}'), // 商户API证书内容
    user_agent: '{User-Agent}', // 选填
})

对象实例化 - 服务商

import { WechatPayPartner } from "wechat-pay-nodejs";

const wechatPayPartner = new WechatPayPartner({
    appid: '{sp_appid}', // 服务商应用ID
    mchid: '{sp_mchid}', // 服务商户号
    cert_private_content: Buffer.from('{apiclient_key.pem}'), // 服务商API私钥内容
    cert_public_content: Buffer.from('{apiclient_cert.pem}'), // 服务商API证书内容
    user_agent: '{User-Agent}', // 选填
})

接口调用 - 服务商JSAPI下单

import { PrepayJsApiPartnerParams } from "wechat-pay-nodejs/typings";

const params : PrepayJsApiPartnerParams = {
    sub_mchid: '{sub_mchid}', // 子商户号
    description: '{description}', // 商品描述
    out_trade_no: '{out_trade_no}', // 商户订单号
    notify_url: '{notify_url}', // 通知地址
    amount: {
    total: 100, // 订单金额
    },
    payer: {
    sp_openid: '{sp_openid}', // 用户服务标识
    }
};

const result = await wechatPayPartner.prepayJsapi(params);

签名验证

import { VerifyParams } from "wechat-pay-nodejs/typings";

const headers = '{HTTP Headers}';
const timestamp = headers['wechatpay-timestamp'];
const nonce = headers['wechatpay-nonce'];
const serialNumber = headers['wechatpay-serial'];
const signature = headers['wechatpay-signature'];
const certificates = '{certificates}'; // 微信平台证书列表 (调用getCertificates接口获取,建议缓存10小时)
const verifyParams : VerifyParams = {
    timestamp: timestamp, // HTTP头Wechatpay-Timestamp 中的应答时间戳
    nonce: nonce, // HTTP头Wechatpay-Nonce 中的应答随机串
    body: '{body}', // 应答主体(response Body),需要按照接口返回的顺序进行验签,错误的顺序将导致验签失败
    serialNumber: serialNumber, // HTTP头Wechatpay-Serial 证书序列号
    signature: signature, // HTTP头Wechatpay-Signature 签名
    apiSecret: '{api_v3_key}', // APIv3密钥
    certificates: certificates, // 微信证书列表
}
const result = wechatPay.verify(verifyParams);

回调报文解密

const decryptedDataText = wechatPay.decryptAesGcm({
    ciphertext: '{resource.ciphertext}', // 密文 
    associatedData: '{resource.associated_data}', // 附加数据
    nonce: '{resource.nonce}', // 随机串
    apiV3Key: '{api_v3_ke}', // APIv3密钥
})

目前已实现的接口

直连商户

| 分类 | 接口名称 | 函数名 | | ---- | ----------------- | ------------------------ | | 基础支付 | JSAPI下单 | prepayJsapi | | 基础支付 | H5下单API | prepayH5 | | 基础支付 | APP下单API | prepayApp | | 基础支付 | Native下单API | prepayNative | | 基础支付 | 微信支付订单号查询订单 | queryOrderById | | 基础支付 | 商户订单号查询订单 | queryOrderByOutTradeNo | | 基础支付 | 关闭订单API | closeOrder | | 基础支付 | 申请退款API | createRefund | | 基础支付 | 查询单笔退款API | queryRefundByOutRefundNo | | 基础支付 | 申请交易账单API | tradeBill | | 基础支付 | 申请资金账单API | fundFlowBill | | 基础支付 | 下载账单API | downloadBill | | 其他能力 | 微信支付平台证书 - 获取平台证书 | getCertificates |

服务商

| 分类 | 接口名称 | 函数名 | | ---- | ----------------- | ------------------------ | | 基础支付 | JSAPI下单 | prepayJsapi | | 基础支付 | H5下单API | prepayH5 | | 基础支付 | APP下单API | prepayApp | | 基础支付 | Native下单API | prepayNative | | 基础支付 | 微信支付订单号查询订单 | queryOrderById | | 基础支付 | 商户订单号查询订单 | queryOrderByOutTradeNo | | 基础支付 | 关闭订单API | closeOrder | | 基础支付 | 申请退款API | createRefund | | 基础支付 | 查询单笔退款API | queryRefundByOutRefundNo | | 基础支付 | 申请交易账单API | tradeBill | | 基础支付 | 申请资金账单API | fundFlowBill | | 基础支付 | 下载账单API | downloadBill | | 基础支付 | 申请单个子商户资金账单 | subMerchantFundFlowBill | | 其他能力 | 微信支付平台证书 - 获取平台证书 | getCertificates |

未实现的接口

  • 部分未实现的接口,可以调用SDK中的callApi方法来实现。具体参考以下示例
import { objToQueryString } from "wechat-pay-nodejs/lib/utils";

/**
 * 分账结果
 */
type QueryProfitSharingOrderResult = {
    transaction_id: string, // 微信订单号
    out_order_no: string, // 商户分账单号
    order_id: string, // 微信分账单号
    state: string, // 分账单状态
    receivers: {
        amount: number, // 分账金额
        description: string, // 分账描述
        type: string, // 分账接收方类型
        account: string, // 分账接收方账号
        result: string, // 分账结果
        fail_reason: string, // 分账失败原因
        detail_id: string, // 分账明细单号
        create_time: string, // 分账创建时间
        finish_time: string, // 分账完成时间
    }[], // 分账接收方列表
}

/**
 * 查询分账结果API
 * @param transactionId 微信订单号
 * @param outOrderNo 商户分账单号
 */
async queryProfitSharingOrder(transactionId: string, outOrderNo: string) : Promise<BaseResponse<QueryProfitSharingOrderResult>>{
    let apiUrl = `/v3/profitsharing/orders/${outOrderNo}`;

    const params = {
        transaction_id: transactionId
    }

    apiUrl = apiUrl + objToQueryString(params)

    // 接口请求
    const result = await wechatPay.callApi<QueryProfitSharingOrderResult>({ 
        method: HttpMethod.GET, 
        apiUrl
    })
}
  • 欢迎大家通过Pull Request一起完善接口

修改日志

查看修改日志