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

cl-alipay

v1.1.0

Published

支付宝支付 sdk node版

Downloads

4

Readme

cl-alipay

介绍

支付宝支付 sdk node版

安装

npm install cl-alipay

配置

const Alipay = require("cl-alipay");

// 初始化 sdk 配置
const alipaySdk = Alipay.config({
  appId: "沙箱里的APPID", //支付宝沙箱appId
  signType: "RSA2", //默认的加密算法  
  gateway: "https://openapi.alipaydev.com/gateway.do", //默认的支付宝网关  
  alipayPublicKey:"支付宝公钥",//支付宝公钥
  privateKey:"应用私钥" // 应用私钥
});

支付宝沙箱网址:https://open.alipay.com/develop/sandbox/app

输入图片说明

在首页里 点击启动公钥模式,再点击查看支付宝给我们提供的支付宝公钥应用私钥

输入图片说明

输入图片说明

内置函数

Alipay.config( Object : config )

该函数的作用是配置sdk的基础值,用于与支付宝通信

config:{
	appId,    //支付宝沙箱appId
	signType,   //加密算法  默认值为 RSA2
	gateway,   //支付宝网关  默认值为 https://openapi.alipaydev.com/gateway.do
 	alipayPublicKey,  //支付宝公钥
 	privateKey   // 应用私钥
}

返回值 alipaySdk 对象

示例:
const Alipay = require("cl-alipay");

// 初始化 sdk 配置
const alipaySdk = Alipay.config(
  {
    appId: "你的沙箱里的APPID", 
    alipayPublicKey:"你的支付宝公钥",
    privateKey:"你的应用私钥" 
  }
);

Alipay.pay( alipaySdk,bizContent,returnUrl [,notifyUrl])

该函数的作用是与支付宝进行通信,接收传递过来的商品数据并传入到支付宝后台,在用户完成支付后跳转到指定网址

alipaySdk:Alipay.config 函数的返回值作为 alipaySdk

bizContent:商品对象,包括如下属性

const bizContent = {
  outTradeNo: "商品订单编号",
  productCode: "商品代码",
  totalAmount: "商品价格",
  subject: "商品名称",
  body: "商品详情",
}

returnUrl:支付完成后同步跳转的网址,前端路由,该返回地址上携带商品信息以及支付宝订单信息

notifyUrl:可选值,支付完成后由支付宝发起的异步支付回调地址,公网后端服务器地址,一般用于支付成功后,接收到支付宝的回调进行数据库的操作

**返回值是一个 promise ** ,promise的结果中包含要跳转的支付网址 URL:跳转支付宝支付的网址

示例:
const Alipay = require("cl-alipay");

// 初始化 sdk 配置
const alipaySdk = Alipay.config(
  {
    appId: "你的沙箱里的APPID", 
    alipayPublicKey:"你的支付宝公钥",
    privateKey:"你的应用私钥" 
  }
);


Alipay.pay(alipaySdk, bizContent, "http://localhost:8080/pay-success")
  .then( url=> console.log(`该地址给前端返回${url},用于支付页面的跳转`))

Alipay.verifyPay(alipaySdk,bizContent)

该函数是作用是查询用户订单是否支付成功

alipaySdk:Alipay.config 函数的返回值作为 alipaySdk

bizContent:订单号对象,里边包含两个属性,一个是out_trade_no,是商品订单号,另一个是trade_no,支付宝订单号

返回值 promise对象 结果为查询订单后的结果集

示例:

 // 获取前端传递过来的商品订单号和支付宝订单号
 // 需要注意的是,用户支付完成后才能获取支付宝订单号
  const { out_trade_no, trade_no } = ctx.request.body;
  
  Alipay.verifyPay(alipaySdk, {
    out_trade_no: "商品id",
    trade_no: "2023030922001476421000663465",
  }).then(result=>{
    console.log(result)
  })

完整使用流程

1.先清楚是由前端用户点击购买按钮后,前端发起一个请求到后端获取商品订单号,根据这个订单号,展示用户要购买的商品。

2.当用户点击支付按钮时,前端将该商品信息传入服务器后台,服务器与支付宝进行通信完成支付。

3.当支付完成后,会直接跳转到提前设置好的页面中,显示支付情况。

4.也可以设置异步回调,由支付宝通知服务器完成支付,进行后台数据库的更新操作。

假设当前处于后端服务器支付宝支付的路由中:

// 1.引入sdk
const Alipay = require("cl-alipay");

// 2.配置 alipay sdk
const alipaySdk = Alipay.config(
     {
      appId: "你的沙箱里的APPID", 
      alipayPublicKey:"你的支付宝公钥",
      privateKey:"你的应用私钥" 
    }
  );

// 3.获取前端传过来的商品数据,
// 需要注意的是该对象的属性名不能随意更改,只能按照下边的来传值,也就是说商品名属性为subject,而不能改为productname
const bizContent = {
  outTradeNo: "2022030123943749",  //商品订单号
  productCode: "FAST_INSTANT_TRADE_PAY", //产品码
  totalAmount: "9999.99",  // 价格
  subject: "iphone13 Pro Max", //商品名字
  body: "512G 远峰蓝",  // 商品详情
};

// 4、Alipay.pay 发起支付,返回一个promise,res里的值是支付宝的支付网址
Alipay.pay(
  alipaySdk, //传入配置好的sdk
  bizContent,  //商品信息
  "http://localhost:8080/pay-success", //支付完成后跳转的路由,该路由为前端路由,而不是后端路由!!
  "http://t64ehj.natappfree.cc/notice"
).then(async (res) => {
  // 5.给前端返回该网址 res
  console.log(res);

  // 假设前端打开了该网站,并且用户在支付宝页完成了支付,支付宝会自动跳转到 returnUrl 中(前端路由),
  // 需要注意的是该路由不能跟任何参数,因为支付宝会在路由上跟上订单的参数
  // 里边包括了 商品订单号 out_trade_no 和 支付宝订单号 trade_no
  // 此时前端可以通过 axios 向后端某个接口发起请求 查询订单是否支付成功,

  // 假设已经发起请求到后端,且后端取得了这两个参数
  
  // 6.后端拿到请求 订单号对象 { out_trade_no trade_no },向支付宝发起验证是否支付成功
  // 返回一个订单对象
  const a = await Alipay.verifyPay( 
    alipaySdk, //sdk 对象
    { 
      out_trade_no: "cccc",
      trade_no: "2023030922001476421000663465",
    }
  );

  console.log(a);  
  // 订单对象属性如下
  //   {
  //   code: '10000',  //查询成功
  //   msg: 'Success',
  //   buyer_logon_id: 'kxn***@sandbox.com',
  //   buyer_pay_amount: '0.00',
  //   buyer_user_id: '2038222008273422',    
  //   buyer_user_type: 'PRIVATE',
  //   invoice_amount: '0.00',
  //   out_trade_no: '2023030128397', //商品订单号
  //   point_amount: '0.00',
  //   receipt_amount: '0.00',
  //   send_pay_date: '2023-03-09 22:28:02', // 时间
  //   total_amount: '9999.99',  // 总价格
  //   trade_no: '2023030922001476421000663465',  //支付宝订单号
  //   trade_status: 'TRADE_SUCCESS'  //已付款
  // }
  
  // 到此如果返回值中包含 trade_status: 'TRADE_SUCCESS' 则说明支付成功,此时可以进行数据库的更新操作!
});