@celljs/alipay 0.0.0-stage → 4.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +109 -2
- package/cell.schema.json +58 -0
- package/cell.yml +10 -0
- package/esm/node/alipay-client.d.ts +58 -0
- package/esm/node/alipay-client.js +234 -0
- package/esm/node/alipay-client.js.map +1 -0
- package/esm/node/alipay-error.d.ts +28 -0
- package/esm/node/alipay-error.js +20 -0
- package/esm/node/alipay-error.js.map +1 -0
- package/esm/node/alipay-notification-middleware.d.ts +23 -0
- package/esm/node/alipay-notification-middleware.js +102 -0
- package/esm/node/alipay-notification-middleware.js.map +1 -0
- package/esm/node/alipay-protocol.d.ts +200 -0
- package/esm/node/alipay-protocol.js +15 -0
- package/esm/node/alipay-protocol.js.map +1 -0
- package/esm/node/alipay-util.d.ts +35 -0
- package/esm/node/alipay-util.js +191 -0
- package/esm/node/alipay-util.js.map +1 -0
- package/esm/node/index.d.ts +5 -0
- package/esm/node/index.js +6 -0
- package/esm/node/index.js.map +1 -0
- package/esm/node/module.d.ts +3 -0
- package/esm/node/module.js +4 -0
- package/esm/node/module.js.map +1 -0
- package/esm/node/test/fixtures.d.ts +10 -0
- package/esm/node/test/fixtures.js +266 -0
- package/esm/node/test/fixtures.js.map +1 -0
- package/esm/package.json +3 -0
- package/lib/node/alipay-client.d.ts +59 -0
- package/lib/node/alipay-client.d.ts.map +1 -0
- package/lib/node/alipay-client.js +237 -0
- package/lib/node/alipay-client.js.map +1 -0
- package/lib/node/alipay-error.d.ts +29 -0
- package/lib/node/alipay-error.d.ts.map +1 -0
- package/lib/node/alipay-error.js +24 -0
- package/lib/node/alipay-error.js.map +1 -0
- package/lib/node/alipay-notification-middleware.d.ts +24 -0
- package/lib/node/alipay-notification-middleware.d.ts.map +1 -0
- package/lib/node/alipay-notification-middleware.js +105 -0
- package/lib/node/alipay-notification-middleware.js.map +1 -0
- package/lib/node/alipay-protocol.d.ts +201 -0
- package/lib/node/alipay-protocol.d.ts.map +1 -0
- package/lib/node/alipay-protocol.js +19 -0
- package/lib/node/alipay-protocol.js.map +1 -0
- package/lib/node/alipay-util.d.ts +36 -0
- package/lib/node/alipay-util.d.ts.map +1 -0
- package/lib/node/alipay-util.js +194 -0
- package/lib/node/alipay-util.js.map +1 -0
- package/lib/node/alipay-util.spec.d.ts +2 -0
- package/lib/node/alipay-util.spec.d.ts.map +1 -0
- package/lib/node/alipay-util.spec.js +49 -0
- package/lib/node/alipay-util.spec.js.map +1 -0
- package/lib/node/alipay.spec.d.ts +8 -0
- package/lib/node/alipay.spec.d.ts.map +1 -0
- package/lib/node/alipay.spec.js +314 -0
- package/lib/node/alipay.spec.js.map +1 -0
- package/lib/node/index.d.ts +6 -0
- package/lib/node/index.d.ts.map +1 -0
- package/lib/node/index.js +22 -0
- package/lib/node/index.js.map +1 -0
- package/lib/node/module.d.ts +4 -0
- package/lib/node/module.d.ts.map +1 -0
- package/lib/node/module.js +6 -0
- package/lib/node/module.js.map +1 -0
- package/lib/node/test/fixtures.d.ts +11 -0
- package/lib/node/test/fixtures.d.ts.map +1 -0
- package/lib/node/test/fixtures.js +269 -0
- package/lib/node/test/fixtures.js.map +1 -0
- package/lib/package.spec.d.ts +2 -0
- package/lib/package.spec.d.ts.map +1 -0
- package/lib/package.spec.js +7 -0
- package/lib/package.spec.js.map +1 -0
- package/package.json +70 -4
- package/src/node/alipay-client.ts +266 -0
- package/src/node/alipay-error.ts +31 -0
- package/src/node/alipay-notification-middleware.ts +90 -0
- package/src/node/alipay-protocol.ts +210 -0
- package/src/node/alipay-util.spec.ts +56 -0
- package/src/node/alipay-util.ts +183 -0
- package/src/node/alipay.spec.ts +331 -0
- package/src/node/index.ts +5 -0
- package/src/node/module.ts +4 -0
- package/src/node/test/fixtures.ts +272 -0
- package/src/package.spec.ts +5 -0
package/README.md
CHANGED
|
@@ -1,3 +1,110 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Cell - Alipay Component
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Add `@celljs/alipay` to the dependencies to take payments with [Alipay](https://opendocs.alipay.com/open/00a0ut) (支付宝): an injectable `AlipayClient` signs gateway requests (RSA2), verifies the signatures of the answers and builds the page, WAP and App payments, and a notification endpoint verifies asynchronous notifications and hands them to your handlers. Public key mode and certificate mode are supported. It uses Node's `crypto` and `fetch`, with no third-party dependency.
|
|
4
|
+
|
|
5
|
+
```yaml
|
|
6
|
+
# cell.yml: configuration is server-only unless a schema marks it public; take credentials from the environment
|
|
7
|
+
alipay:
|
|
8
|
+
appId: ${env.ALIPAY_APP_ID}
|
|
9
|
+
privateKey: ${env.ALIPAY_PRIVATE_KEY} # 应用私钥: PEM, base64 (PKCS#8 or PKCS#1) or a file path
|
|
10
|
+
alipayPublicKey: ${env.ALIPAY_PUBLIC_KEY} # 支付宝公钥 (public key mode)
|
|
11
|
+
notifyUrl: https://shop.example.com/alipay/notify
|
|
12
|
+
# gateway: https://openapi-sandbox.dl.alipaydev.com/gateway.do # the sandbox
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { Autowired } from '@celljs/core';
|
|
17
|
+
import { Body, Controller, Post } from '@celljs/mvc/lib/node';
|
|
18
|
+
import { AlipayClient, AlipayNotification, AlipayNotificationHandler } from '@celljs/alipay';
|
|
19
|
+
|
|
20
|
+
@Controller('orders')
|
|
21
|
+
export class OrderController {
|
|
22
|
+
@Autowired(AlipayClient)
|
|
23
|
+
protected readonly alipay: AlipayClient;
|
|
24
|
+
|
|
25
|
+
@Post('page') // 电脑网站支付: redirect the browser to the URL
|
|
26
|
+
page(@Body() order: { id: string }) {
|
|
27
|
+
return { url: this.alipay.pagePay({ out_trade_no: order.id, total_amount: '88.88', subject: 'Cell T-shirt' }, { return_url: 'https://shop.example.com/paid' }) };
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
@Post('qr') // 当面付扫码: show qr_code as a QR code
|
|
31
|
+
async qr(@Body() order: { id: string }) {
|
|
32
|
+
const { qr_code } = await this.alipay.precreate({ out_trade_no: order.id, total_amount: '88.88', subject: 'Cell T-shirt' });
|
|
33
|
+
return { qr_code };
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
@AlipayNotificationHandler(['TRADE_SUCCESS', 'TRADE_FINISHED'])
|
|
38
|
+
export class PaidHandler implements AlipayNotificationHandler {
|
|
39
|
+
async handle(notification: AlipayNotification) {
|
|
40
|
+
// out_trade_no, trade_no, total_amount... as Alipay sent them, signature verified.
|
|
41
|
+
// Mark the order paid once: the same notification can come again.
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Client
|
|
47
|
+
|
|
48
|
+
`biz_content` uses the field names of Alipay's documentation (snake_case); amounts are strings in yuan (`'88.88'`). Payment methods default `notify_url` to `alipay.notifyUrl`; the second argument sets other common parameters (`return_url`, `app_auth_token`...).
|
|
49
|
+
|
|
50
|
+
| Method | API |
|
|
51
|
+
| --- | --- |
|
|
52
|
+
| `pagePay(request, { httpMethod?, return_url? })` | alipay.trade.page.pay (电脑网站支付): a URL to redirect to, or with `httpMethod: 'POST'` an HTML form that submits itself |
|
|
53
|
+
| `wapPay(request, options?)` | alipay.trade.wap.pay (手机网站支付), same results |
|
|
54
|
+
| `appPay(request)` | alipay.trade.app.pay (App 支付): the order string for the App SDK |
|
|
55
|
+
| `precreate(request)` → `{ qr_code }` | alipay.trade.precreate (当面付扫码) |
|
|
56
|
+
| `create(request)` → `{ trade_no }` | alipay.trade.create (JSAPI / 小程序, `buyer_id` or `buyer_open_id`); the page calls `my.tradePay({ tradeNO })` |
|
|
57
|
+
| `query(query)` / `close(query)` | alipay.trade.query / alipay.trade.close |
|
|
58
|
+
| `refund(request)` / `queryRefund(query)` | alipay.trade.refund / alipay.trade.fastpay.refund.query; give `out_request_no` for partial refunds |
|
|
59
|
+
| `billDownloadUrl({ bill_type, bill_date })` | alipay.data.dataservice.bill.downloadurl.query |
|
|
60
|
+
| `execute(method, biz?, options?)` | Any other API: resolves with `<method>_response` |
|
|
61
|
+
| `pageExecute(method, biz?, options?)` / `sdkExecute(method, biz?, options?)` | Any other browser or App SDK API |
|
|
62
|
+
| `verifyNotification(params)` | Verifies a notification received on a route of your own |
|
|
63
|
+
|
|
64
|
+
Answers whose `code` is not `10000` reject with `CELL_ALIPAY_API_ERROR`; `detail` is the answer (`sub_code: 'ACQ.TRADE_NOT_EXIST'`...). The signature of an answer is checked over its JSON text as Alipay sent it.
|
|
65
|
+
|
|
66
|
+
## Notifications
|
|
67
|
+
|
|
68
|
+
The endpoint is `POST /alipay/notify` (`alipay.notification.url`, under `server.path`); set its absolute URL as `notifyUrl`. It runs before the body parser and authentication, verifies the signature of the form and that `app_id` is this application's, then runs the `@AlipayNotificationHandler` components of its `trade_status` (`WAIT_BUYER_PAY`, `TRADE_SUCCESS`, `TRADE_FINISHED`, `TRADE_CLOSED`; the `notify_type` for notifications without one), then those declared without types, one after the other. It answers `success` when they all succeed and `fail` otherwise (400 for a bad signature, 500 when a handler throws), so Alipay sends the notification again later: handlers must be idempotent. A partial refund comes as a notification with `refund_fee` and `gmt_refund`.
|
|
69
|
+
|
|
70
|
+
Notifications only say what happened: check `total_amount` against the order before shipping it.
|
|
71
|
+
|
|
72
|
+
## Certificate mode
|
|
73
|
+
|
|
74
|
+
Applications created with certificates (公钥证书) give the three certificates instead of `alipayPublicKey` (PEM text or file paths):
|
|
75
|
+
|
|
76
|
+
```yaml
|
|
77
|
+
alipay:
|
|
78
|
+
appId: ${env.ALIPAY_APP_ID}
|
|
79
|
+
privateKey: ${env.ALIPAY_PRIVATE_KEY}
|
|
80
|
+
appCert: certs/appCertPublicKey_2021000000000001.crt
|
|
81
|
+
alipayPublicCert: certs/alipayCertPublicKey_RSA2.crt
|
|
82
|
+
alipayRootCert: certs/alipayRootCert.crt
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
Requests carry `app_cert_sn` and `alipay_root_cert_sn` computed as Alipay's SDKs do; an answer signed with another Alipay certificate is refused, so replace `alipayPublicCert` when Alipay renews it.
|
|
86
|
+
|
|
87
|
+
## Configuration
|
|
88
|
+
|
|
89
|
+
```yaml
|
|
90
|
+
alipay:
|
|
91
|
+
gateway: https://openapi.alipay.com/gateway.do
|
|
92
|
+
timeout: 10000 # milliseconds per request
|
|
93
|
+
notification:
|
|
94
|
+
enabled: true
|
|
95
|
+
url: /alipay/notify
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
## Errors
|
|
99
|
+
|
|
100
|
+
Failures are `AlipayError`s with a stable `code` and a `hint`:
|
|
101
|
+
|
|
102
|
+
| Code | When |
|
|
103
|
+
| --- | --- |
|
|
104
|
+
| `CELL_ALIPAY_NOT_CONFIGURED` | `appId` or `privateKey` is missing, or neither `alipayPublicKey` nor all three certificates are set |
|
|
105
|
+
| `CELL_ALIPAY_API_ERROR` | The answer's `code` is not `10000`: `detail.sub_code` says why |
|
|
106
|
+
| `CELL_ALIPAY_HTTP_ERROR` | The gateway answered with an HTTP error or not with JSON |
|
|
107
|
+
| `CELL_ALIPAY_SIGNATURE_INVALID` | An answer or a notification is not signed by Alipay |
|
|
108
|
+
| `CELL_ALIPAY_APP_ID_MISMATCH` | A notification is for another application |
|
|
109
|
+
|
|
110
|
+
Content encryption (`encrypt_key`, AES) is not supported.
|
package/cell.schema.json
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "http://json-schema.org/draft-07/schema#",
|
|
3
|
+
"properties": {
|
|
4
|
+
"alipay": {
|
|
5
|
+
"type": "object",
|
|
6
|
+
"description": "Alipay.",
|
|
7
|
+
"properties": {
|
|
8
|
+
"appId": {
|
|
9
|
+
"type": "string"
|
|
10
|
+
},
|
|
11
|
+
"privateKey": {
|
|
12
|
+
"type": "string",
|
|
13
|
+
"description": "From the environment: ${env.ALIPAY_PRIVATE_KEY}; PEM, base64 or a file path."
|
|
14
|
+
},
|
|
15
|
+
"alipayPublicKey": {
|
|
16
|
+
"type": "string",
|
|
17
|
+
"description": "Public key mode."
|
|
18
|
+
},
|
|
19
|
+
"appCert": {
|
|
20
|
+
"type": "string",
|
|
21
|
+
"description": "Certificate mode."
|
|
22
|
+
},
|
|
23
|
+
"alipayPublicCert": {
|
|
24
|
+
"type": "string",
|
|
25
|
+
"description": "Certificate mode."
|
|
26
|
+
},
|
|
27
|
+
"alipayRootCert": {
|
|
28
|
+
"type": "string",
|
|
29
|
+
"description": "Certificate mode."
|
|
30
|
+
},
|
|
31
|
+
"gateway": {
|
|
32
|
+
"type": "string",
|
|
33
|
+
"description": "Sandbox: https://openapi-sandbox.dl.alipaydev.com/gateway.do"
|
|
34
|
+
},
|
|
35
|
+
"notifyUrl": {
|
|
36
|
+
"type": "string",
|
|
37
|
+
"description": "Absolute URL of the notification endpoint."
|
|
38
|
+
},
|
|
39
|
+
"timeout": {
|
|
40
|
+
"type": "integer",
|
|
41
|
+
"description": "Milliseconds a request may take."
|
|
42
|
+
},
|
|
43
|
+
"notification": {
|
|
44
|
+
"type": "object",
|
|
45
|
+
"properties": {
|
|
46
|
+
"enabled": {
|
|
47
|
+
"type": "boolean"
|
|
48
|
+
},
|
|
49
|
+
"url": {
|
|
50
|
+
"type": "string",
|
|
51
|
+
"description": "POST endpoint that receives the notifications."
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
package/cell.yml
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
alipay:
|
|
2
|
+
# Credentials (appId, privateKey, alipayPublicKey or appCert / alipayPublicCert / alipayRootCert) go here too,
|
|
3
|
+
# from the environment: privateKey: ${env.ALIPAY_PRIVATE_KEY}
|
|
4
|
+
# Sandbox: https://openapi-sandbox.dl.alipaydev.com/gateway.do
|
|
5
|
+
gateway: https://openapi.alipay.com/gateway.do
|
|
6
|
+
timeout: 10000 # ms
|
|
7
|
+
notification:
|
|
8
|
+
# POST endpoint that receives the asynchronous notifications; give its absolute URL as notifyUrl.
|
|
9
|
+
enabled: true
|
|
10
|
+
url: /alipay/notify
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { type KeyObject } from 'node:crypto';
|
|
2
|
+
import { AlipayError } from './alipay-error.js';
|
|
3
|
+
import { AlipayClient, type AlipayCreateRequest, type AlipayNotification, type AlipayOptions, type AlipayPageOptions, type AlipayPagePayRequest, type AlipayRefund, type AlipayRefundQuery, type AlipayRefundRequest, type AlipayRequestOptions, type AlipayResponse, type AlipayTrade, type AlipayTradeQuery, type AlipayTradeRequest, type AlipayWapPayRequest } from './alipay-protocol.js';
|
|
4
|
+
interface Keys {
|
|
5
|
+
privateKey: KeyObject;
|
|
6
|
+
alipayPublicKey: KeyObject;
|
|
7
|
+
/** Certificate mode only. */
|
|
8
|
+
appCertSn?: string;
|
|
9
|
+
alipayCertSn?: string;
|
|
10
|
+
alipayRootCertSn?: string;
|
|
11
|
+
}
|
|
12
|
+
export declare class AlipayClientImpl implements AlipayClient {
|
|
13
|
+
protected readonly options?: AlipayOptions;
|
|
14
|
+
protected loaded?: Keys;
|
|
15
|
+
execute<T extends AlipayResponse = AlipayResponse>(method: string, biz?: object, options?: AlipayRequestOptions): Promise<T>;
|
|
16
|
+
pageExecute(method: string, biz?: object, options?: AlipayPageOptions): string;
|
|
17
|
+
sdkExecute(method: string, biz?: object, options?: AlipayRequestOptions): string;
|
|
18
|
+
pagePay(request: AlipayPagePayRequest, options?: AlipayPageOptions): string;
|
|
19
|
+
wapPay(request: AlipayWapPayRequest, options?: AlipayPageOptions): string;
|
|
20
|
+
appPay(request: AlipayTradeRequest, options?: AlipayRequestOptions): string;
|
|
21
|
+
precreate(request: AlipayTradeRequest, options?: AlipayRequestOptions): Promise<AlipayResponse & {
|
|
22
|
+
out_trade_no: string;
|
|
23
|
+
qr_code: string;
|
|
24
|
+
}>;
|
|
25
|
+
create(request: AlipayCreateRequest, options?: AlipayRequestOptions): Promise<AlipayResponse & {
|
|
26
|
+
out_trade_no: string;
|
|
27
|
+
trade_no: string;
|
|
28
|
+
}>;
|
|
29
|
+
query(query: AlipayTradeQuery, options?: AlipayRequestOptions): Promise<AlipayTrade>;
|
|
30
|
+
close(query: {
|
|
31
|
+
out_trade_no?: string;
|
|
32
|
+
trade_no?: string;
|
|
33
|
+
operator_id?: string;
|
|
34
|
+
}, options?: AlipayRequestOptions): Promise<AlipayResponse>;
|
|
35
|
+
refund(request: AlipayRefundRequest, options?: AlipayRequestOptions): Promise<AlipayRefund>;
|
|
36
|
+
queryRefund(query: AlipayRefundQuery, options?: AlipayRequestOptions): Promise<AlipayResponse & {
|
|
37
|
+
refund_status?: 'REFUND_SUCCESS';
|
|
38
|
+
refund_amount?: string;
|
|
39
|
+
}>;
|
|
40
|
+
billDownloadUrl(query: {
|
|
41
|
+
bill_type: 'trade' | 'signcustomer';
|
|
42
|
+
bill_date: string;
|
|
43
|
+
}): Promise<AlipayResponse & {
|
|
44
|
+
bill_download_url: string;
|
|
45
|
+
}>;
|
|
46
|
+
verifyNotification(params: Record<string, string>): AlipayNotification;
|
|
47
|
+
/** The configuration; throws when the application is not configured. */
|
|
48
|
+
protected config(): AlipayOptions;
|
|
49
|
+
protected keys(): Keys;
|
|
50
|
+
protected gateway(): string;
|
|
51
|
+
protected notify<O extends AlipayRequestOptions>(options?: O): O;
|
|
52
|
+
/** The common parameters, `biz_content` and `sign`. */
|
|
53
|
+
protected params(method: string, biz?: object, options?: AlipayRequestOptions): Record<string, string>;
|
|
54
|
+
/** The `<method>_response` of a gateway answer, after checking its signature and its code. */
|
|
55
|
+
protected answer<T>(method: string, text: string): T;
|
|
56
|
+
protected invalidSignature(message: string): AlipayError;
|
|
57
|
+
}
|
|
58
|
+
export {};
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
8
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
|
+
};
|
|
10
|
+
import { X509Certificate } from 'node:crypto';
|
|
11
|
+
import { Component, Value } from '@celljs/core';
|
|
12
|
+
import { AlipayError } from './alipay-error.js';
|
|
13
|
+
import { AlipayClient, } from './alipay-protocol.js';
|
|
14
|
+
import { AlipayUtil } from './alipay-util.js';
|
|
15
|
+
const SUCCESS = '10000';
|
|
16
|
+
let AlipayClientImpl = class AlipayClientImpl {
|
|
17
|
+
async execute(method, biz, options) {
|
|
18
|
+
var _a;
|
|
19
|
+
const config = this.config();
|
|
20
|
+
const { biz_content, ...common } = this.params(method, biz, options);
|
|
21
|
+
const response = await fetch(`${this.gateway()}?${encode(common)}`, {
|
|
22
|
+
method: 'POST',
|
|
23
|
+
headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8', Accept: 'application/json' },
|
|
24
|
+
body: biz_content === undefined ? undefined : encode({ biz_content }),
|
|
25
|
+
signal: AbortSignal.timeout((_a = config.timeout) !== null && _a !== void 0 ? _a : 10000),
|
|
26
|
+
});
|
|
27
|
+
const text = await response.text();
|
|
28
|
+
if (!response.ok) {
|
|
29
|
+
throw new AlipayError({
|
|
30
|
+
code: 'CELL_ALIPAY_HTTP_ERROR',
|
|
31
|
+
message: `The Alipay gateway answered ${response.status} to ${method}.`,
|
|
32
|
+
hint: 'Check alipay.gateway (production https://openapi.alipay.com/gateway.do, sandbox https://openapi-sandbox.dl.alipaydev.com/gateway.do).',
|
|
33
|
+
detail: { body: text.slice(0, 500) },
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
return this.answer(method, text);
|
|
37
|
+
}
|
|
38
|
+
pageExecute(method, biz, options = {}) {
|
|
39
|
+
const { httpMethod, ...rest } = options;
|
|
40
|
+
const params = this.params(method, biz, rest);
|
|
41
|
+
if (httpMethod === 'POST') {
|
|
42
|
+
const inputs = Object.entries(params)
|
|
43
|
+
.map(([name, value]) => `<input type="hidden" name="${escapeHtml(name)}" value="${escapeHtml(value)}">`)
|
|
44
|
+
.join('');
|
|
45
|
+
return `<form id="alipay" action="${escapeHtml(this.gateway())}?charset=utf-8" method="post">${inputs}</form><script>document.getElementById('alipay').submit();</script>`;
|
|
46
|
+
}
|
|
47
|
+
return `${this.gateway()}?${encode(params)}`;
|
|
48
|
+
}
|
|
49
|
+
sdkExecute(method, biz, options) {
|
|
50
|
+
return encode(this.params(method, biz, options));
|
|
51
|
+
}
|
|
52
|
+
pagePay(request, options) {
|
|
53
|
+
return this.pageExecute('alipay.trade.page.pay', { product_code: 'FAST_INSTANT_TRADE_PAY', ...request }, this.notify(options));
|
|
54
|
+
}
|
|
55
|
+
wapPay(request, options) {
|
|
56
|
+
return this.pageExecute('alipay.trade.wap.pay', { product_code: 'QUICK_WAP_WAY', ...request }, this.notify(options));
|
|
57
|
+
}
|
|
58
|
+
appPay(request, options) {
|
|
59
|
+
return this.sdkExecute('alipay.trade.app.pay', { product_code: 'QUICK_MSECURITY_PAY', ...request }, this.notify(options));
|
|
60
|
+
}
|
|
61
|
+
precreate(request, options) {
|
|
62
|
+
return this.execute('alipay.trade.precreate', request, this.notify(options));
|
|
63
|
+
}
|
|
64
|
+
create(request, options) {
|
|
65
|
+
return this.execute('alipay.trade.create', { product_code: 'JSAPI_PAY', ...request }, this.notify(options));
|
|
66
|
+
}
|
|
67
|
+
query(query, options) {
|
|
68
|
+
return this.execute('alipay.trade.query', query, options);
|
|
69
|
+
}
|
|
70
|
+
close(query, options) {
|
|
71
|
+
return this.execute('alipay.trade.close', query, options);
|
|
72
|
+
}
|
|
73
|
+
refund(request, options) {
|
|
74
|
+
return this.execute('alipay.trade.refund', request, options);
|
|
75
|
+
}
|
|
76
|
+
queryRefund(query, options) {
|
|
77
|
+
return this.execute('alipay.trade.fastpay.refund.query', query, options);
|
|
78
|
+
}
|
|
79
|
+
billDownloadUrl(query) {
|
|
80
|
+
return this.execute('alipay.data.dataservice.bill.downloadurl.query', query);
|
|
81
|
+
}
|
|
82
|
+
verifyNotification(params) {
|
|
83
|
+
const keys = this.keys();
|
|
84
|
+
if (!params.sign || !AlipayUtil.verifyText(AlipayUtil.signContent(params, ['sign_type']), params.sign, keys.alipayPublicKey)) {
|
|
85
|
+
throw this.invalidSignature('The signature of the notification does not match.');
|
|
86
|
+
}
|
|
87
|
+
if (params.app_id !== this.config().appId) {
|
|
88
|
+
throw new AlipayError({
|
|
89
|
+
code: 'CELL_ALIPAY_APP_ID_MISMATCH',
|
|
90
|
+
message: `The notification is for the application ${params.app_id}, not ${this.config().appId}.`,
|
|
91
|
+
hint: 'Give each application its own notify_url, or check alipay.appId.',
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
return params;
|
|
95
|
+
}
|
|
96
|
+
/** The configuration; throws when the application is not configured. */
|
|
97
|
+
config() {
|
|
98
|
+
const options = this.options;
|
|
99
|
+
const missing = ['appId', 'privateKey'].filter(key => !(options === null || options === void 0 ? void 0 : options[key]));
|
|
100
|
+
const certificates = [options === null || options === void 0 ? void 0 : options.appCert, options === null || options === void 0 ? void 0 : options.alipayPublicCert, options === null || options === void 0 ? void 0 : options.alipayRootCert].filter(Boolean).length;
|
|
101
|
+
if (missing.length || (certificates > 0 && certificates < 3) || (!certificates && !(options === null || options === void 0 ? void 0 : options.alipayPublicKey))) {
|
|
102
|
+
throw new AlipayError({
|
|
103
|
+
code: 'CELL_ALIPAY_NOT_CONFIGURED',
|
|
104
|
+
message: missing.length
|
|
105
|
+
? `alipay is missing ${missing.join(', ')}.`
|
|
106
|
+
: 'alipay needs alipayPublicKey (public key mode) or all of appCert, alipayPublicCert and alipayRootCert (certificate mode).',
|
|
107
|
+
hint: 'Set them under alipay in cell.yml, from the environment: privateKey: ${env.ALIPAY_PRIVATE_KEY}; a key or certificate can also be the path of its file.',
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
return options;
|
|
111
|
+
}
|
|
112
|
+
keys() {
|
|
113
|
+
if (!this.loaded) {
|
|
114
|
+
const { privateKey, alipayPublicKey, appCert, alipayPublicCert, alipayRootCert } = this.config();
|
|
115
|
+
if (appCert && alipayPublicCert && alipayRootCert) {
|
|
116
|
+
const alipayCert = new X509Certificate(AlipayUtil.content(alipayPublicCert));
|
|
117
|
+
this.loaded = {
|
|
118
|
+
privateKey: AlipayUtil.privateKey(privateKey),
|
|
119
|
+
alipayPublicKey: alipayCert.publicKey,
|
|
120
|
+
appCertSn: AlipayUtil.certSn(appCert),
|
|
121
|
+
alipayCertSn: AlipayUtil.certSn(alipayCert),
|
|
122
|
+
alipayRootCertSn: AlipayUtil.rootCertSn(alipayRootCert),
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
this.loaded = { privateKey: AlipayUtil.privateKey(privateKey), alipayPublicKey: AlipayUtil.publicKey(alipayPublicKey) };
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return this.loaded;
|
|
130
|
+
}
|
|
131
|
+
gateway() {
|
|
132
|
+
var _a;
|
|
133
|
+
return (_a = this.config().gateway) !== null && _a !== void 0 ? _a : 'https://openapi.alipay.com/gateway.do';
|
|
134
|
+
}
|
|
135
|
+
notify(options) {
|
|
136
|
+
var _a;
|
|
137
|
+
var _b;
|
|
138
|
+
const notifyUrl = (_b = options === null || options === void 0 ? void 0 : options.notify_url) !== null && _b !== void 0 ? _b : (_a = this.options) === null || _a === void 0 ? void 0 : _a.notifyUrl;
|
|
139
|
+
return { ...options, notify_url: notifyUrl };
|
|
140
|
+
}
|
|
141
|
+
/** The common parameters, `biz_content` and `sign`. */
|
|
142
|
+
params(method, biz, options) {
|
|
143
|
+
const config = this.config();
|
|
144
|
+
const keys = this.keys();
|
|
145
|
+
const params = {
|
|
146
|
+
app_id: config.appId,
|
|
147
|
+
method,
|
|
148
|
+
format: 'JSON',
|
|
149
|
+
charset: 'utf-8',
|
|
150
|
+
sign_type: 'RSA2',
|
|
151
|
+
timestamp: AlipayUtil.timestamp(),
|
|
152
|
+
version: '1.0',
|
|
153
|
+
app_cert_sn: keys.appCertSn,
|
|
154
|
+
alipay_root_cert_sn: keys.alipayRootCertSn,
|
|
155
|
+
...options,
|
|
156
|
+
biz_content: biz === undefined ? undefined : JSON.stringify(biz),
|
|
157
|
+
};
|
|
158
|
+
const result = {};
|
|
159
|
+
for (const [key, value] of Object.entries(params)) {
|
|
160
|
+
if (value !== undefined && value !== '') {
|
|
161
|
+
result[key] = value;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
result.sign = AlipayUtil.signText(AlipayUtil.signContent(result), keys.privateKey);
|
|
165
|
+
return result;
|
|
166
|
+
}
|
|
167
|
+
/** The `<method>_response` of a gateway answer, after checking its signature and its code. */
|
|
168
|
+
answer(method, text) {
|
|
169
|
+
var _a, _b;
|
|
170
|
+
const key = `${method.replace(/\./g, '_')}_response`;
|
|
171
|
+
let json;
|
|
172
|
+
try {
|
|
173
|
+
json = JSON.parse(text);
|
|
174
|
+
}
|
|
175
|
+
catch (error) {
|
|
176
|
+
throw new AlipayError({
|
|
177
|
+
code: 'CELL_ALIPAY_HTTP_ERROR',
|
|
178
|
+
message: `The answer of the Alipay gateway to ${method} is not JSON.`,
|
|
179
|
+
hint: 'Check alipay.gateway: it must be the gateway.do URL of Alipay.',
|
|
180
|
+
detail: { body: text.slice(0, 500) },
|
|
181
|
+
cause: error,
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
const name = json[key] ? key : 'error_response';
|
|
185
|
+
const node = json[name];
|
|
186
|
+
const keys = this.keys();
|
|
187
|
+
if (json.sign) {
|
|
188
|
+
const raw = AlipayUtil.rawJson(text, name);
|
|
189
|
+
if (!raw || !AlipayUtil.verifyText(raw, json.sign, keys.alipayPublicKey)) {
|
|
190
|
+
throw this.invalidSignature(`The signature of the answer to ${method} does not match.`);
|
|
191
|
+
}
|
|
192
|
+
if (json.alipay_cert_sn && keys.alipayCertSn && json.alipay_cert_sn !== keys.alipayCertSn) {
|
|
193
|
+
throw this.invalidSignature(`The answer to ${method} is signed with the Alipay certificate ${json.alipay_cert_sn}, not the configured one.`);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
else if ((node === null || node === void 0 ? void 0 : node.code) === SUCCESS) {
|
|
197
|
+
throw this.invalidSignature(`The answer to ${method} is not signed.`);
|
|
198
|
+
}
|
|
199
|
+
if ((node === null || node === void 0 ? void 0 : node.code) !== SUCCESS) {
|
|
200
|
+
throw new AlipayError({
|
|
201
|
+
code: 'CELL_ALIPAY_API_ERROR',
|
|
202
|
+
message: `Alipay answered ${method} with ${node === null || node === void 0 ? void 0 : node.code} ${node === null || node === void 0 ? void 0 : node.msg}: ${(_a = node === null || node === void 0 ? void 0 : node.sub_code) !== null && _a !== void 0 ? _a : ''} ${(_b = node === null || node === void 0 ? void 0 : node.sub_msg) !== null && _b !== void 0 ? _b : ''}`.trim(),
|
|
203
|
+
hint: "detail.sub_code is Alipay's error code; look it up in the documentation of the API (opendocs.alipay.com).",
|
|
204
|
+
detail: node,
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
return node;
|
|
208
|
+
}
|
|
209
|
+
invalidSignature(message) {
|
|
210
|
+
return new AlipayError({
|
|
211
|
+
code: 'CELL_ALIPAY_SIGNATURE_INVALID',
|
|
212
|
+
message,
|
|
213
|
+
hint: "Set alipay.alipayPublicKey to Alipay's public key (not the application's), or update alipayPublicCert when Alipay changed its certificate.",
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
__decorate([
|
|
218
|
+
Value('alipay'),
|
|
219
|
+
__metadata("design:type", Object)
|
|
220
|
+
], AlipayClientImpl.prototype, "options", void 0);
|
|
221
|
+
AlipayClientImpl = __decorate([
|
|
222
|
+
Component(AlipayClient)
|
|
223
|
+
], AlipayClientImpl);
|
|
224
|
+
export { AlipayClientImpl };
|
|
225
|
+
/** `a=1&b=x%20y`: spaces as %20, as the gateway expects. */
|
|
226
|
+
function encode(params) {
|
|
227
|
+
return Object.entries(params)
|
|
228
|
+
.map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`)
|
|
229
|
+
.join('&');
|
|
230
|
+
}
|
|
231
|
+
function escapeHtml(value) {
|
|
232
|
+
return value.replace(/&/g, '&').replace(/"/g, '"').replace(/</g, '<').replace(/>/g, '>');
|
|
233
|
+
}
|
|
234
|
+
//# sourceMappingURL=alipay-client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"alipay-client.js","sourceRoot":"","sources":["../../src/node/alipay-client.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAAkB,eAAe,EAAE,MAAM,aAAa,CAAC;AAC9D,OAAO,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AAChD,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAC7C,OAAO,EACH,YAAY,GAef,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAE3C,MAAM,OAAO,GAAG,OAAO,CAAC;AAYjB,IAAM,gBAAgB,GAAtB,MAAM,gBAAgB;IAMzB,KAAK,CAAC,OAAO,CAA4C,MAAc,EAAE,GAAY,EAAE,OAA8B;;QACjH,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC;QAC7B,MAAM,EAAE,WAAW,EAAE,GAAG,MAAM,EAAE,GAAG,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE,GAAG,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K,CAAC,EAAE,CAAC;SAChF,IAAI,CAAC,GAAG,CAAC,CAAC;AACnB,CAAC;AAED,SAAS,UAAU,CAAC,KAAa;IAC7B,OAAO,KAAK,CAAC,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;AAC5G,CAAC"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A failure of Alipay or of its configuration. `code` is stable and `hint` says how to fix it in one sentence
|
|
3
|
+
* (docs/design/agent-first.md R5).
|
|
4
|
+
*/
|
|
5
|
+
export declare class AlipayError extends Error {
|
|
6
|
+
readonly name = "AlipayError";
|
|
7
|
+
readonly code: AlipayError.Code;
|
|
8
|
+
readonly hint: string;
|
|
9
|
+
/** Alipay's answer, for `CELL_ALIPAY_API_ERROR`: `{ code: '40004', msg, sub_code: 'ACQ.TRADE_NOT_EXIST', sub_msg }`. */
|
|
10
|
+
readonly detail?: {
|
|
11
|
+
code?: string;
|
|
12
|
+
msg?: string;
|
|
13
|
+
sub_code?: string;
|
|
14
|
+
sub_msg?: string;
|
|
15
|
+
[key: string]: any;
|
|
16
|
+
};
|
|
17
|
+
constructor(options: {
|
|
18
|
+
code: AlipayError.Code;
|
|
19
|
+
message: string;
|
|
20
|
+
hint: string;
|
|
21
|
+
detail?: AlipayError['detail'];
|
|
22
|
+
cause?: unknown;
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
export declare namespace AlipayError {
|
|
26
|
+
type Code = 'CELL_ALIPAY_NOT_CONFIGURED' | 'CELL_ALIPAY_API_ERROR' | 'CELL_ALIPAY_HTTP_ERROR' | 'CELL_ALIPAY_SIGNATURE_INVALID' | 'CELL_ALIPAY_APP_ID_MISMATCH';
|
|
27
|
+
function is(error: unknown, code?: Code): error is AlipayError;
|
|
28
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A failure of Alipay or of its configuration. `code` is stable and `hint` says how to fix it in one sentence
|
|
3
|
+
* (docs/design/agent-first.md R5).
|
|
4
|
+
*/
|
|
5
|
+
export class AlipayError extends Error {
|
|
6
|
+
constructor(options) {
|
|
7
|
+
super(options.message, options.cause === undefined ? undefined : { cause: options.cause });
|
|
8
|
+
this.name = 'AlipayError';
|
|
9
|
+
this.code = options.code;
|
|
10
|
+
this.hint = options.hint;
|
|
11
|
+
this.detail = options.detail;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
(function (AlipayError) {
|
|
15
|
+
function is(error, code) {
|
|
16
|
+
return error instanceof AlipayError && (code === undefined || error.code === code);
|
|
17
|
+
}
|
|
18
|
+
AlipayError.is = is;
|
|
19
|
+
})(AlipayError || (AlipayError = {}));
|
|
20
|
+
//# sourceMappingURL=alipay-error.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"alipay-error.js","sourceRoot":"","sources":["../../src/node/alipay-error.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,OAAO,WAAY,SAAQ,KAAK;IAOlC,YAAY,OAAmH;QAC3H,KAAK,CAAC,OAAO,CAAC,OAAO,EAAE,OAAO,CAAC,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,OAAO,CAAC,KAAK,EAAE,CAAC,CAAC;QAP7E,SAAI,GAAG,aAAa,CAAC;QAQnC,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;QACzB,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;QACzB,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IACjC,CAAC;CACJ;AAED,WAAiB,WAAW;IAQxB,SAAgB,EAAE,CAAC,KAAc,EAAE,IAAW;QAC1C,OAAO,KAAK,YAAY,WAAW,IAAI,CAAC,IAAI,KAAK,SAAS,IAAI,KAAK,CAAC,IAAI,KAAK,IAAI,CAAC,CAAC;IACvF,CAAC;IAFe,YAAA,EAAE,KAEjB,CAAA;AACL,CAAC,EAXgB,WAAW,KAAX,WAAW,QAW3B"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Logger, type Provider } from '@celljs/core';
|
|
2
|
+
import { PathResolver } from '@celljs/web';
|
|
3
|
+
import { type BodyOptions, BodyParser, type Context, Middleware, RequestMatcher } from '@celljs/web/lib/node';
|
|
4
|
+
import { AlipayClient, type AlipayNotification, type AlipayNotificationHandler, type AlipayOptions } from './alipay-protocol.js';
|
|
5
|
+
/**
|
|
6
|
+
* The notification endpoint (`POST alipay.notification.url`): verifies the signature of the form and runs the
|
|
7
|
+
* `@AlipayNotificationHandler` components of its `trade_status`. It answers `success` when they succeed and `fail`
|
|
8
|
+
* otherwise (400 for a bad signature, 500 when a handler throws), so that Alipay sends the notification again.
|
|
9
|
+
*/
|
|
10
|
+
export declare class AlipayNotificationMiddleware implements Middleware {
|
|
11
|
+
protected readonly client: AlipayClient;
|
|
12
|
+
protected readonly handlerProvider: Provider<AlipayNotificationHandler>;
|
|
13
|
+
protected readonly requestMatcher: RequestMatcher;
|
|
14
|
+
protected readonly pathResolver: PathResolver;
|
|
15
|
+
protected readonly logger: Logger;
|
|
16
|
+
protected readonly options?: AlipayOptions;
|
|
17
|
+
protected readonly bodyOptions?: BodyOptions;
|
|
18
|
+
readonly priority: number;
|
|
19
|
+
protected handlers?: Map<string, AlipayNotificationHandler[]>;
|
|
20
|
+
protected parser?: BodyParser;
|
|
21
|
+
handle(ctx: Context, next: () => Promise<void>): Promise<void>;
|
|
22
|
+
protected dispatch(notification: AlipayNotification): Promise<void>;
|
|
23
|
+
}
|