@jayfong/x-server 2.5.0 → 2.6.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.
@@ -94,20 +94,52 @@ class PayService {
94
94
 
95
95
  verifyNotifyData(data) {
96
96
  try {
97
- var _this$alipaySdk;
97
+ if (!data || typeof data !== 'object') {
98
+ return false;
99
+ } // 支付宝
100
+ // https://opendocs.alipay.com/open/270/105902
98
101
 
99
- return !!data && typeof data === 'object' && ( // 支付宝
100
- data.passback_params === 'alipay' ? !!((_this$alipaySdk = this.alipaySdk) != null && _this$alipaySdk.checkNotifySign(data)) : // 微信支付
101
- data.resource ? JSON.parse(this.wepayDecrypt(data.resource)).attach === 'wepay' : false);
102
+
103
+ if (data.passback_params === 'alipay') {
104
+ var _this$alipaySdk;
105
+
106
+ if (!((_this$alipaySdk = this.alipaySdk) != null && _this$alipaySdk.checkNotifySign(data))) {
107
+ return false;
108
+ }
109
+
110
+ return {
111
+ channelOrderNo: data.trade_no
112
+ };
113
+ } // 微信支付
114
+ // https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_5.shtml
115
+
116
+
117
+ if (data.resource) {
118
+ const params = JSON.parse(this.wepayDecrypt(data.resource));
119
+
120
+ if (params.attach === 'wepay') {
121
+ return {
122
+ channelOrderNo: params.transaction_id
123
+ };
124
+ }
125
+
126
+ return false;
127
+ }
128
+
129
+ return false;
102
130
  } catch {
103
131
  return false;
104
132
  }
105
133
  }
106
134
 
107
135
  verifyNotifyDataOrFail(data, message) {
108
- if (!this.verifyNotifyData(data)) {
136
+ const res = this.verifyNotifyData(data);
137
+
138
+ if (!res) {
109
139
  throw new _http_error.HttpError.BadRequest(message);
110
140
  }
141
+
142
+ return res;
111
143
  }
112
144
 
113
145
  wepaySign(data) {
@@ -64,6 +64,15 @@ export interface PayServicePrepareWepayResult {
64
64
  paySign: string;
65
65
  };
66
66
  }
67
+ export interface PayServiceVerifyNotifyDataResult {
68
+ /**
69
+ * 渠道订单号。
70
+ *
71
+ * - 支付宝最长 `64` 位;
72
+ * - 微信支付最长 `32` 位。
73
+ */
74
+ channelOrderNo: string;
75
+ }
67
76
  export declare class PayService implements BaseService {
68
77
  private options;
69
78
  serviceName: string;
@@ -71,8 +80,8 @@ export declare class PayService implements BaseService {
71
80
  constructor(options: PayServiceOptions);
72
81
  prepareAlipay(options: PayServicePrepareAlipayOptions): Promise<PayServicePrepareAlipayResult>;
73
82
  prepareWepay(options: PayServicePrepareWepayOptions): Promise<PayServicePrepareWepayResult>;
74
- verifyNotifyData(data: any): boolean;
75
- verifyNotifyDataOrFail(data: any, message?: string): void;
83
+ verifyNotifyData(data: any): false | PayServiceVerifyNotifyDataResult;
84
+ verifyNotifyDataOrFail(data: any, message?: string): PayServiceVerifyNotifyDataResult;
76
85
  private wepaySign;
77
86
  private wepayDecrypt;
78
87
  private wepayRequest;
@@ -82,20 +82,52 @@ export class PayService {
82
82
 
83
83
  verifyNotifyData(data) {
84
84
  try {
85
- var _this$alipaySdk;
85
+ if (!data || typeof data !== 'object') {
86
+ return false;
87
+ } // 支付宝
88
+ // https://opendocs.alipay.com/open/270/105902
86
89
 
87
- return !!data && typeof data === 'object' && ( // 支付宝
88
- data.passback_params === 'alipay' ? !!((_this$alipaySdk = this.alipaySdk) != null && _this$alipaySdk.checkNotifySign(data)) : // 微信支付
89
- data.resource ? JSON.parse(this.wepayDecrypt(data.resource)).attach === 'wepay' : false);
90
+
91
+ if (data.passback_params === 'alipay') {
92
+ var _this$alipaySdk;
93
+
94
+ if (!((_this$alipaySdk = this.alipaySdk) != null && _this$alipaySdk.checkNotifySign(data))) {
95
+ return false;
96
+ }
97
+
98
+ return {
99
+ channelOrderNo: data.trade_no
100
+ };
101
+ } // 微信支付
102
+ // https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_5.shtml
103
+
104
+
105
+ if (data.resource) {
106
+ const params = JSON.parse(this.wepayDecrypt(data.resource));
107
+
108
+ if (params.attach === 'wepay') {
109
+ return {
110
+ channelOrderNo: params.transaction_id
111
+ };
112
+ }
113
+
114
+ return false;
115
+ }
116
+
117
+ return false;
90
118
  } catch {
91
119
  return false;
92
120
  }
93
121
  }
94
122
 
95
123
  verifyNotifyDataOrFail(data, message) {
96
- if (!this.verifyNotifyData(data)) {
124
+ const res = this.verifyNotifyData(data);
125
+
126
+ if (!res) {
97
127
  throw new HttpError.BadRequest(message);
98
128
  }
129
+
130
+ return res;
99
131
  }
100
132
 
101
133
  wepaySign(data) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jayfong/x-server",
3
- "version": "2.5.0",
3
+ "version": "2.6.0",
4
4
  "license": "ISC",
5
5
  "sideEffects": false,
6
6
  "main": "lib/_cjs/index.js",