• Stars
    star
    408
  • Rank 105,946 (Top 3 %)
  • Language
    TypeScript
  • License
    Other
  • Created over 6 years ago
  • Updated 3 months ago

Reviews

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

Repository Details

支付宝开放平台 Alipay SDK for Node.js

Alipay SDK

简介

Alipay SDK for Node.js 用于给 Node 服务器提供调用支付宝开放平台的能力。包括向支付宝服务器发起 OpenAPI 请求、订单信息生成,以及配套的证书、加签和验签能力。

环境要求

  • 需要 Node.js 8 以上版本
  • 安装依赖

npm install alipay-sdk --save

平台配置

  • 先前往支付宝开发平台-开发者中心完成开发者接入的一些准备工作,包括创建应用、为应用添加功能包、设置应用的接口加签方式等。
    • 可以使用 支付宝开放平台秘钥工具 获取所需的公私钥,并在平台上上传公钥。
    • 本 SDK 默认采用 PKCS1 的格式解析密钥,与密钥工具的默认生成格式不一致。请使用密钥工具【格式转换】功能转为 PKCS1,或在本 SDK 初始化时显式指定 keyType: 'PKCS8'
  • 在设置加签方式结束之后,记录必要信息用于初始化 SDK。
    • 公钥证书模式(推荐): appId应用私钥应用公钥证书文件支付宝公钥证书文件支付宝根证书文件
    • 公钥模式:appId应用私钥应用公钥支付宝公钥

初始化 SDK

代码示例中的路径和文件名仅做示范,请根据项目实际读取文件所在的位置 请保存好私钥文件,避免信息泄露

普通公钥模式

const AlipaySdk = require('alipay-sdk');
// TypeScript,可以使用 import AlipaySdk from 'alipay-sdk';
// 普通公钥模式
const alipaySdk = new AlipaySdk({
  appId: '2016123456789012',
  // keyType: 'PKCS1', // 默认值。请与生成的密钥格式保持一致,参考平台配置一节
  privateKey: fs.readFileSync('private-key.pem', 'ascii'),
  alipayPublicKey: fs.readFileSync('alipay-public-key.pem', 'ascii'),
});

证书模式

const AlipaySdk = require('alipay-sdk');

const alipaySdk = new AlipaySdk({
  appId: '2016123456789012',
  privateKey: fs.readFileSync('private-key.pem', 'ascii'),
  // 传入支付宝根证书、支付宝公钥证书和应用公钥证书。
  alipayRootCertPath: path.join(__dirname, 'alipayRootCert.crt'),
  alipayPublicCertPath: path.join(__dirname, 'alipayCertPublicKey_RSA2.crt'),
  appCertPath: path.join(__dirname, 'appCertPublicKey.crt'),
});

验证配置

可以使用如下基础接口请求服务端,以验证配置正确。具体的接口定义可以在开放平台文档站获取。

// 小程序:生成二维码
const result = await alipaySdk.exec('alipay.open.public.qrcode.create');

// 生活号:基础信息查询
const result = await alipaySdk.exec('alipay.open.public.info.query');

// 第三方应用:查询应用授权信息。需要先给第三方应用授权:https://opendocs.alipay.com/isv/04h3ue
const result = await alipaySdk.exec('alipay.open.auth.token.app.query', {
  bizContent: { app_auth_token: 'token 请在开放平台上查询' }
});

如返回 JSON 格式内容,即说明配置成功。

{
  code: '10000',
  msg: 'Success',
  // 其他字段省略
}
// 如果未挂载对应功能包,可能会报以下错误,也说明服务通了:
{
  code: '20002',
  msg: '授权权限不足',
}

其余情况,如代码报错或者返回 html 代码,则说明未配置成功。

快速使用

exec 示例接口

用于向支付宝服务器发起请求。与具体接口相关的业务参数,需要放在 bizContent 中。

const result = await alipay.exec('alipay.trade.pay', {
  notify_url: 'http://www.notify.com/notify', // 通知回调地址
  bizContent: {
    out_trade_no: '商家的交易码,需保持唯一性',
    total_amount: '0.1',
    subject: '测试订单',
  }
});

部分接口,如 alipay.system.oauth.token,其请求参数不在 bizContent 中。具体可参考官网各接口定义。

使用 AlipayFormData 配置表单

部分接口需要上传文件。SDK 内部封装了一个 Form 对象,用以在发起 multipart/form-data 请求时使用。以 上传门店照片和视频接口 为例:

import AlipayFormData from 'alipay-sdk/lib/form';

const formData = new AlipayFormData();

formData.addField('imageType', 'jpg');
formData.addField('imageName', '图片.jpg');
formData.addFile('imageContent', '图片.jpg', path.join(__dirname, './test.jpg'));


const result = await alipaySdk.exec(
  'alipay.offline.material.image.upload',
  // 文件上传类接口 params 需要设置为 {}
  {},
  {
    // 通过 formData 设置请求参数
    formData: formData,
  },
);

pageExec 示例接口

pageExec 方法主要是用于网站支付接口请求链接生成,传入前台访问输入密码完成支付,如电脑网站支付(alipay.trade.page.pay )等接口。

表单示例:

const bizContent = {
  out_trade_no: "ALIPfdf1211sdfsd12gfddsgs3",
  product_code: "FAST_INSTANT_TRADE_PAY",
  subject: "abc",
  body: "234",
  total_amount: "0.01"
},

// 支付页面接口,返回 html 代码片段,内容为 Form 表单
const result = sdk.pageExec('alipay.trade.page.pay', {
  method: 'POST',
  bizContent,
  returnUrl: 'https://www.taobao.com'
});
<form action="https://openapi.alipay.com/gateway.do?method=alipay.trade.app.pay&app_id=2021002182632749&charset=utf-8&version=1.0&sign_type=RSA2&timestamp=2023-02-28%2011%3A48%3A28&app_auth_token=202302BBbcfad868001a4df3bbfa99e8a6913F10&sign=j9DjDGgxLt3jbOQZy7q7Qu8baKWTl4hZlxOHa%2B46hC1djmFx%2FIyBqzQntPMurzz3f8efXJsalZz3nqZ9ClowCCxBfBvqE0cdzCDAeQ1GMgjd7dbWgjfNNcqKgmJPsIkLaHnP5vTvj%2BA27SqkeZCMbeVfv%2B4nYurXaFB9dNBtA%3D%3D" method="post" name="alipaySDKSubmit1677556108819" id="alipaySDKSubmit1677556108819">
    <input type="hidden" name="alipay_sdk" value="alipay-sdk-nodejs-3.3.0" /><input type="hidden" name="biz_content" value="{&quot;out_trade_no&quot;:&quot;ziheng-test-eeee&quot;,&quot;product_code&quot;:&quot;QUICK_MSECURITY_PAY&quot;,&quot;subject&quot;:&quot;订单标题&quot;,&quot;total_amount&quot;:&quot;0.01&quot;,&quot;body&quot;:&quot;订单描述&quot;}" />
  </form>
<script>document.forms["alipaySDKSubmit1677556108819"].submit();</script>

支付链接示例:

// 支付页面接口,返回支付链接,交由用户打开,会跳转至支付宝网站
const result = sdk.pageExec('alipay.trade.page.pay', {
  method: 'GET',
  bizContent,
  returnUrl: 'https://www.taobao.com'
});

// 返回示例:https://openapi.alipay.com/gateway.do?method=alipay.trade.app.pay&app_id=2021002182632749&charset=utf-8&version=1.0&sign_type=RSA2&timestamp=2023-02-28%2011%3A46%3A35&app_auth_token=202302BBbcfaf3bbfa99e8a6913F10&sign=TPi33NcaKLRBLJDofon84D8itMoBkVAdJsfmIiQDScEw4NHAklXvcvn148A2t47YxDSK0urBnhS0%2BEV%2BVR6h6aKgp931%2FfFbG1I3SAguMjMbr23gnbS68d4spcQ%3D%3D&alipay_sdk=alipay-sdk-nodejs-3.3.0&biz_content=blabla

sdkExec 示例接口

sdkExec 方法主要是服务端生成请求字符串使用的,不会直接支付扣款,需传值到客户端进行调用收银台输入密码完成支付,如 App 支付接口 alipay.trade.app.pay

// App 支付接口,生成请求字符串,
const orderStr = sdk.sdkExec('alipay.trade.app.pay', {
  bizContent: {
    out_trade_no: "ALIPfdf1211sdfsd12gfddsgs3",
    product_code: "FAST_INSTANT_TRADE_PAY",
    subject: "abc",
    body: "234",
    total_amount: "0.01"
},
  returnUrl: 'https://www.taobao.com'
})

// 返回支付宝客户端之后,在【小程序中】通过 my.tradePay 进行调用。
// 详见:https://opendocs.alipay.com/mini/api/openapi-pay
my.tradePay({
  // 服务端生成的字符串,即上面的 result
  orderStr: 'method=alipay.trade.app.pay&app_id=2021002182632749&charset=utf-8&version=1.0&sign_type=RSA2&timestamp=2023-02-24%2016%3A20%3A28&app_auth_token=202302BBbcfad868001a4df3bbfa99e8a6913F10&sign=M%2B2sTNATtUk3i8cOhHGtqjVDHIHSpPReZgjfLfIgbQD4AvI%2Fh%2B%2FS2lkqfJVnI%2Bu0IQ2z7auE1AYQ0wd7yPC4%2B2m5WnN21Q6uQhCCHOsg30mXdnkdB3rgXIiFOSuURRwnaiBmKNKdhaXel51fxYZOTOApV47K6ZUsOlPxc%2FVJWUnC7Hrl64%2BAKqtbv%2BcaefzapYsJwGDzMAGccHGfxevSoZ2Ev7S0FsrDe4LBx4m%2BCWSIFASWFyWYxJq%2BJg7LH1HJqBdBk1jjh5JJ3bNlEqJk8MEFU7sNRae2ErdEPOwCchWkQOaVGOGpFlEHuTSvxnAKnjRkFerE14v%2BVm6weC1Tbw%3D%3D&alipay_sdk=alipay-sdk-nodejs-3.2.0&biz_content=%7B%22out_trade_no%22%3A%22ziheng-test-eeee%22%2C%22product_code%22%3A%22QUICK_MSECURITY_PAY%22%2C%22subject%22%3A%22%E8%AE%A2%E5%8D%95%E6%A0%87%E9%A2%98%22%2C%22total_amount%22%3A%220.01%22%2C%22body%22%3A%22%E8%AE%A2%E5%8D%95%E6%8F%8F%E8%BF%B0%22%7D',
  success: (res) => {
    my.alert({
      content: JSON.stringify(res),
    });
  },
  fail: (res) => {
    my.alert({
      content: JSON.stringify(res),
    });
  }
});

通知验签

部分接口会设置回调地址,用于支付宝服务器向业务服务器通知业务情况(如交易成功)等。此时业务服务应该验证该回调的来源安全性,确保其确实由支付宝官方发起。SDK 提供了对应的通知验签能力。

// 获取 queryObj,如 ctx.query, router.query
// 如服务器未将 queryString 转化为 object,需要手动转化
const queryObj = { sign_type: 'RSA2', sign: 'QfTb8tqE1BMhS5qAn.....', gmt_create: '2019-08-15 15:56:22', other_biz_field: '....' }

// true | false
const signRes = sdk.checkNotifySign(queryObj);

问题反馈

如您在使用 Alipay SDK for Node.js 过程中遇到问题,欢迎前往 支付宝开放社区 发帖与支付宝工作人员和其他开发者一起交流,或联系 支付宝开放平台客服 协助解决。

API

new AlipaySdk(config)

Param Type Description
config AlipaySdkConfig 初始化 SDK 配置

AlipaySdkConfig

参数 说明 类型 必须
appId 应用ID string
privateKey 应用私钥字符串。RSA签名验签工具:
https://docs.open.alipay.com/291/106097
string
signType 签名种类 "RSA2" | "RSA"
alipayPublicKey 支付宝公钥(需要对返回值做验签时候必填) string
gateway 网关 string
timeout 网关超时时间(单位毫秒,默认 5s) number
camelcase 是否把网关返回的下划线 key 转换为驼峰写法 boolean
keyType 指定private key类型, 默认: PKCS1, PKCS8: PRIVATE KEY, PKCS1: RSA PRIVATE KEY "PKCS1" | "PKCS8"
appCertPath 应用公钥证书文件路径 string
appCertContent 应用公钥证书文件内容 string Buffer
appCertSn 应用公钥证书sn string
alipayRootCertPath 支付宝根证书文件路径 string
alipayRootCertContent 支付宝根证书文件内容 string | Buffer
alipayRootCertSn 支付宝根证书sn string
alipayPublicCertPath 支付宝公钥证书文件路径 string
alipayPublicCertContent 支付宝公钥证书文件内容 string | Buffer
alipayCertSn 支付宝公钥证书sn string
encryptKey AES密钥,调用AES加解密相关接口时需要 string
wsServiceUrl 服务器地址 string

alipaySdk.sdkExec(method, params) ⇒ string

生成请求字符串,用于客户端进行调用

Returns: string - 请求字符串

Param Type Description
method string 方法名
params IRequestParams 请求参数
params.bizContent object 业务请求参数

alipaySdk.pageExec(method, params) ⇒ string

生成网站接口请求链接或表单

Returns: string - 请求链接或表单 HTML

Param Type Description
method string 方法名
params IRequestParams 请求参数
params.bizContent object 业务请求参数
params.method string 后续进行请求的方法。如为 GET,即返回 http 链接;如为 POST,则生成表单 html

alipaySdk.exec(method, params, option) ⇒ Promise.<(AlipaySdkCommonResult|string)>

执行请求,调用支付宝服务端

Returns: Promise.<(AlipaySdkCommonResult|string)> - 请求执行结果

Param Type Description
method string 调用接口方法名,比如 alipay.ebpp.bill.add
params IRequestParams 请求参数
params.bizContent object 业务请求参数
option IRequestOption 选项
option.validateSign Boolean 是否验签
args.log object 可选日志记录对象

AlipaySdkCommonResult

响应结果

参数 说明 类型 必须
code 响应码。10000 表示成功,其余详见 https://opendocs.alipay.com/common/02km9f string
msg 响应讯息。Success 表示成功。 string
sub_code 错误代号 string
sub_msg 错误辅助信息 string

IRequestParams

请求参数

参数 说明 类型 必须
bizContent 业务请求参数 object
needEncrypt 自动AES加解密 boolean

alipaySdk.checkNotifySign(postData, raw)

通知验签
Returns: Boolean - 是否验签成功

Param Type Description
postData JSON 服务端的消息内容
raw Boolean 是否使用 raw 内容而非 decode 内容验签

More Repositories

1

SoloPi

SoloPi 自动化测试工具
Java
5,736
star
2

alipay-easysdk

Alipay Easy SDK for multi-language(java、c#、php、ts etc.) allows you to enjoy a minimalist programming experience and quickly access the various high-frequency capabilities of the Alipay Open Platform.
Java
1,099
star
3

agentUniverse

agentUniverse is a LLM multi-agent framework that allows developers to easily build multi-agent applications.
Python
799
star
4

alipay-sdk-java-all

支付宝开放平台 Alipay SDK for Java
Java
521
star
5

mPaaS

mPaaS Demo 合集,mPaaS 是源自于支付宝的移动开发平台。The collection of demos for mPaaS components. mPaaS is the Mobile Development Platform which oriented from Alipay.
C
323
star
6

ant-application-security-testing-benchmark

xAST评价体系,让安全工具不再“黑盒”. The xAST evaluation benchmark makes security tools no longer a "black box".
Java
301
star
7

PainlessInferenceAcceleration

Python
283
star
8

alipay-sdk-python-all

支付宝开放平台 Alipay SDK for Python
Python
268
star
9

Owfuzz

Owfuzz: a WiFi protocol fuzzing tool
C
216
star
10

alipay-sdk-net-all

支付宝开放平台 Alipay SDK for .NET
C#
200
star
11

cvpr2020-plant-pathology

Python
170
star
12

antcloud-node-stack

蚂蚁金融科技官方 Node 技术栈脚本
JavaScript
159
star
13

financial_evaluation_dataset

Python
154
star
14

rdf-file

Rdf-File是一个处理结构化文本文件的工具组件
Java
149
star
15

alipay-sdk-php-all

支付宝开放平台 Alipay SDK for PHP
PHP
146
star
16

SOFAStack

SOFAStack™ (Scalable Open Financial Architecture Stack) is a collection of cloud native middleware components, which are designed to build distributed systems with high performance and reliability, and have been fully validated by mission-critical financial business scenarios.
139
star
17

Ant-Multi-Modal-Framework

Research Code for Multimodal-Cognition Team in Ant Group
Python
117
star
18

vsag

vsag is a vector indexing library used for similarity search.
C++
115
star
19

Pyraformer

Python
100
star
20

container-observability-service

Simplify Kubernetes applications operation with one-stop observability services, including resource delivery SLO,root cause diagnoses and container lifecycle tracing and more.
Go
88
star
21

ios-malicious-bithunter

iOS Malicious Bit Hunter is a malicious plug-in detection engine for iOS applications. It can analyze the head of the macho file of the injected dylib dynamic library based on runtime. If you are interested in other programs of the author, please visit https://github.com/SecurityLife
C
83
star
22

goldfish

A development framework for Alipay Mini Program.
TypeScript
80
star
23

SQLFlow

SQLFlow is a bridge that connects a SQL engine, e.g. MySQL, Hive, SparkSQL or SQL Server, with TensorFlow and other machine learning toolkits. SQLFlow extends the SQL language to enable model training, prediction and inference.
73
star
24

KnowledgeGraphEmbeddingsViaPairedRelationVectors_PairRE

Python
61
star
25

Antchain-MPC

Antchain-MPC is a library of MPC (Multi-Parties Computation)
Terra
57
star
26

VCSL

Video Copy Segment Localization (VCSL) dataset and benchmark [CVPR2022]
Python
49
star
27

StructuredLM_RTDT

A library for building hierarchical text representation and corresponding downstream applications.
Python
48
star
28

RJU_Ant_QA

The RJUA-QA (RenJi hospital department of Urology and Antgroup collaborative Question and Answer dataset) is an innovative medical urology specialty QA inference dataset.
47
star
29

Z-RareCharacterSolution

TypeScript
45
star
30

quic-lb

nginx-quic-lb is an implementation of ietf-quic-lb, based on nginx-release-1.18.0, you can see the detailed code in this pull request
C
41
star
31

jpmml-sparkml-lightgbm

JPMML-SparkML plugin for converting LightGBM-Spark models to PMML
Java
41
star
32

PASE

C
41
star
33

global-open-sdk-java

Ant global gateway SDK
Java
35
star
34

container-auto-tune

Container Auto Tune is an intelligent parameter tuning product that helps developers, operators automatically adjust the application, analyzes JVM reasonable configuration parameters through intelligent algorithms.Please visit the official site for the quick start guide and documentation.
Java
32
star
35

promo-mini-component

支付宝营销玩法小程序组件库
JavaScript
31
star
36

private_llm

Python
28
star
37

tls13-sm-spec

IETF Internet-Draft (I-D) of Chinese cipher suites in TLSv1.3 and related documentation.
Makefile
27
star
38

microservice_system_twin_graph_based_anomaly_detection

Python
26
star
39

mobile-agent

Python
26
star
40

alipay-intellij-plugin

Intellij IDEA Plugin
20
star
41

character-js

TypeScript
19
star
42

global-open-sdk-php

Ant global gateway SDK
PHP
17
star
43

ams-java-sdk

AMS Java binding
Java
13
star
44

antchain-openapi-prod-sdk

PHP
10
star
45

Pattern-Based-Compression

High-Ratio Compression for Machine-Generated Data
C
10
star
46

global-open-sdk-python

Ant global gateway SDK
Python
10
star
47

PC2-NoiseofWeb

Noise of Web (NoW) is a challenging noisy correspondence learning (NCL) benchmark containing 100K image-text pairs for robust image-text matching/retrieval models.
Python
9
star
48

YiJian-Community

YiJian-Comunity: a full-process automated large model safety evaluation tool designed for academic research
Python
9
star
49

AOP-Based-Runtime-Security-Analysis-Toolkit

TypeScript
8
star
50

ant-application-security-testing-benchmark-nodejs

JavaScript
8
star
51

agentUniverse-Guides

8
star
52

RGSL

Python
8
star
53

POA

Python
7
star
54

payment-code-widget

A lightweight library provides UI widgets to display payment code in mobile applications. The dimension of the payment code is optimal and scanner-friendly.
Java
6
star
55

TDEER

Code For TDEER: An Efficient Translating Decoding Schema for Joint Extraction of Entities and Relations (EMNLP 2021)
Python
5
star
56

Finite_State_Autoregressive_Entropy_Coding

Python
5
star
57

ComBERT

4
star
58

Parameter_Inference_Efficient_PIE

Python
4
star
59

NMCDR

Python
4
star
60

A2-efficient-automated-attacker-for-boosting-adversarial-training

Python
4
star
61

global-open-sdk-dotnet

C#
3
star
62

tldk

This is a fork of FDio/tldk.
C
3
star
63

DUPLEX

Python
3
star
64

antchain-openapi-util-sdk

C#
3
star
65

Automatic_AI_Model_Greenness_Track_Toolkit

JavaScript
3
star
66

style-tokenizer

Python
3
star
67

Timestep-aware-SentenceEmbedding-and-AcmeCoverage

Python
2
star
68

ATTEMPT_Pre-training_with_Aspect-Content_Text_Mutual_Prediction

Python
2
star
69

hypro_tpp

Python
1
star
70

BehaviorAugmentedRelevanceModel

Implementation and data of the paper "Beyond Semantics: Learning a Behavior Augmented Relevance Model with Self-supervised Learning" in CIKM'23.
Python
1
star
71

A-Knowledge-augmented-Method-DiKGRS

Python
1
star
72

Analogic-Reasoning-Augmented-Large-Language-Model

Python
1
star
73

MMDL-based-Data-Augmentation-with-Domain-Knowledge-for-Time-Series-Classification

This repository contains the official implementation for the paper: MMDL-based Data Augmentation with Domain Knowledge for Time Series Classification.
Python
1
star