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

payjs-ts

v1.0.1

Published

The TypeScript SDK for PayJS payment service

Downloads

140

Readme

# PayJS TypeScript SDK

一个为 PayJS 支付服务开发的 TypeScript SDK,提供完整的类型支持和简单易用的 API。

## 特性

- 完整的 TypeScript 类型支持
- 支持扫码支付、付款码支付和收银台支付
- 简单清晰的 API 设计
- 内置签名算法
- 符合 PayJS 官方接口规范

## 安装

```bash
npm install payjs-ts

或者

yarn add payjs-ts

快速开始

import PayJS from 'payjs-ts';

// 初始化
const payjs = new PayJS('your-mchid', 'your-key');

// 扫码支付示例
async function nativePayment() {
  const result = await payjs.native({
    total_fee: 100,              // 金额(分)
    out_trade_no: 'order123',    // 订单号
    body: '商品标题'             // 商品标题
  });
  
  console.log(result.qrcode);    // 支付二维码链接
  console.log(result.payjs_order_id);  // PayJS 订单号
}

// 付款码支付示例
async function micropayment() {
  try {
    const result = await payjs.micropay({
      total_fee: 100,
      out_trade_no: 'order123',
      body: '商品标题',
      auth_code: '134567890123456789'  // 扫码支付授权码
    });
    
    console.log(result.payjs_order_id);
  } catch (error) {
    console.error('支付失败:', error);
  }
}

// 收银台支付示例
function cashierPayment() {
  const cashierUrl = payjs.cashier({
    total_fee: 100,
    out_trade_no: 'order123',
    body: '商品标题',
    callback_url: 'https://your-domain.com/callback',  // 支付成功后的跳转地址
    notify_url: 'https://your-domain.com/notify'       // 异步通知地址
  });
  
  // 跳转到收银台页面
  window.location.href = cashierUrl;
}

API 文档

初始化

const payjs = new PayJS(mchid: string, key: string);

扫码支付 (native)

interface PayJSNativeRequest {
  total_fee: number;      // 金额(分)
  out_trade_no: string;   // 用户端订单号
  body?: string;          // 订单标题
  attach?: string;        // 用户自定义数据
  notify_url?: string;    // 异步通知地址
}

const result = await payjs.native(params: PayJSNativeRequest);

付款码支付 (micropay)

interface PayJSMicropayRequest {
  total_fee: number;      // 金额(分)
  out_trade_no: string;   // 用户端订单号
  body?: string;          // 订单标题
  attach?: string;        // 用户自定义数据
  auth_code: string;      // 扫码支付授权码
  notify_url?: string;    // 异步通知地址
}

const result = await payjs.micropay(params: PayJSMicropayRequest);

收银台支付 (cashier)

interface PayJSCashierRequest {
  total_fee: number;      // 金额(分)
  out_trade_no: string;   // 用户端订单号
  body?: string;          // 订单标题
  attach?: string;        // 用户自定义数据
  notify_url?: string;    // 异步通知地址
  callback_url?: string;  // 用户支付成功后,前端跳转地址
  auto?: boolean;         // 自动提交
  hide?: boolean;         // 隐藏收银台背景
}

const url = payjs.cashier(params: PayJSCashierRequest);

错误处理

SDK 会在以下情况抛出错误:

  • 付款码格式不正确
  • 支付失败
  • 网络请求失败
  • 参数验证失败

建议使用 try-catch 进行错误处理:

try {
  const result = await payjs.micropay({
    // ...支付参数
  });
} catch (error) {
  console.error('支付失败:', error.message);
}

类型支持

该 SDK 导出了所有支付相关的类型定义:

import {
  PayJSNativeRequest,
  PayJSNativeResponse,
  PayJSMicropayRequest,
  PayJSMicropayResponse,
  PayJSCashierRequest,
  PayJSCashierErrorResponse
} from 'payjs-ts';

环境要求

  • Node.js >= 14.0.0
  • TypeScript >= 4.5.0 (如果在 TypeScript 项目中使用)

许可证

MIT

贡献指南

欢迎提交 Issue 和 Pull Request。