oak-pay-business 2.2.7 → 2.2.9

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.
Files changed (40) hide show
  1. package/es/checkers/pay.js +1 -0
  2. package/es/checkers/refund.js +2 -2
  3. package/es/components/refund/list/index.js +2 -1
  4. package/es/components/refund/list/web.pc.d.ts +1 -0
  5. package/es/components/refund/list/web.pc.js +2 -2
  6. package/es/configuration/attrUpdateMatrix.js +7 -1
  7. package/es/entities/Refund.d.ts +1 -0
  8. package/es/entities/Refund.js +1 -0
  9. package/es/oak-app-domain/Refund/Schema.d.ts +4 -0
  10. package/es/oak-app-domain/Refund/Storage.js +3 -0
  11. package/es/oak-app-domain/Refund/locales/zh_CN.json +1 -1
  12. package/es/triggers/pay.js +13 -0
  13. package/es/triggers/refund.js +4 -1
  14. package/es/triggers/withdraw.d.ts +1 -1
  15. package/es/utils/pay.d.ts +8 -0
  16. package/es/utils/pay.js +65 -1
  17. package/es/utils/payClazz/WechatPay/WechatPay.js +13 -8
  18. package/es/watchers/pay.js +4 -45
  19. package/es/watchers/refund.js +3 -2
  20. package/lib/aspects/withdraw.d.ts +4 -4
  21. package/lib/checkers/pay.js +1 -0
  22. package/lib/checkers/refund.js +2 -2
  23. package/lib/configuration/attrUpdateMatrix.js +7 -1
  24. package/lib/context/BackendRuntimeContext.d.ts +1 -1
  25. package/lib/entities/Refund.d.ts +1 -0
  26. package/lib/entities/Refund.js +1 -0
  27. package/lib/oak-app-domain/Refund/Schema.d.ts +4 -0
  28. package/lib/oak-app-domain/Refund/Storage.js +3 -0
  29. package/lib/oak-app-domain/Refund/locales/zh_CN.json +1 -1
  30. package/lib/triggers/pay.js +13 -0
  31. package/lib/triggers/refund.js +4 -1
  32. package/lib/utils/application.d.ts +1 -1
  33. package/lib/utils/pay.d.ts +8 -0
  34. package/lib/utils/pay.js +67 -2
  35. package/lib/utils/payClazz/WechatPay/WechatPay.js +13 -8
  36. package/lib/utils/payClazz/WechatPay/index.d.ts +1 -1
  37. package/lib/watchers/pay.js +3 -45
  38. package/lib/watchers/refund.js +3 -2
  39. package/package.json +2 -2
  40. package/src/entities/Refund.ts +2 -0
@@ -12,6 +12,7 @@ const checkers = [
12
12
  data.paid = 0;
13
13
  data.applicationId = context.getApplicationId();
14
14
  data.creatorId = context.getCurrentUserId();
15
+ data.refundable = false;
15
16
  if (!data.meta) {
16
17
  data.meta = {};
17
18
  }
@@ -24,7 +24,7 @@ const checkers = [
24
24
  checker(operation, context) {
25
25
  const { data, filter } = operation;
26
26
  const { externalId } = data;
27
- // 必须有ExternalId,除非是account类型的支付
27
+ // 当offline退款成功时,必须有ExternalId
28
28
  if (!externalId) {
29
29
  return pipeline(() => context.select('refund', {
30
30
  data: {
@@ -39,7 +39,7 @@ const checkers = [
39
39
  }
40
40
  }, { dontCollect: true }), (refunds) => {
41
41
  const [refund] = refunds;
42
- if (refund.pay.entity !== 'account') {
42
+ if (refund.pay.entity === 'offlineAcount') {
43
43
  throw new OakAttrNotNullException('refund', ['externalId']);
44
44
  }
45
45
  });
@@ -55,7 +55,7 @@ export default OakComponent({
55
55
  }
56
56
  }
57
57
  ],
58
- formData({ data }) {
58
+ formData({ data, features }) {
59
59
  return {
60
60
  refunds: data?.map((ele) => {
61
61
  const { creator, withdrawId, pay, ...rest } = ele;
@@ -70,6 +70,7 @@ export default OakComponent({
70
70
  creatorMobile: creator?.mobile$user?.[0]?.mobile || '-',
71
71
  };
72
72
  }),
73
+ amIRoot: features.token.isRoot(),
73
74
  };
74
75
  }
75
76
  });
@@ -8,4 +8,5 @@ export default function Render(props: WebComponentProps<EntityDict, 'pay', false
8
8
  isWithdraw: boolean;
9
9
  payChannel: string;
10
10
  })[];
11
+ amIRoot?: boolean;
11
12
  }>): React.JSX.Element | null;
@@ -6,7 +6,7 @@ import { FilterPanel } from '../../../components/AbstractComponents';
6
6
  import { ToYuan, ThousandCont } from 'oak-domain/lib/utils/money';
7
7
  import assert from 'assert';
8
8
  export default function Render(props) {
9
- const { refunds, oakFullpath, oakExecutable } = props.data;
9
+ const { refunds, oakFullpath, oakExecutable, amIRoot } = props.data;
10
10
  const { t, updateItem, execute, clean, setMessage } = props.methods;
11
11
  const [upsertId, setUpsertId] = useState('');
12
12
  const [updateAction, setUpdateAction] = useState('');
@@ -100,7 +100,7 @@ export default function Render(props) {
100
100
  ]} onAction={(row, action) => {
101
101
  const { entity } = row.pay;
102
102
  assert(entity !== 'account');
103
- if (entity !== 'offlineAccount') {
103
+ if (entity !== 'offlineAccount' && !amIRoot) {
104
104
  setMessage({
105
105
  type: 'error',
106
106
  title: t('cantOperateOnline'),
@@ -84,7 +84,13 @@ const attrUpdateMatrix = {
84
84
  },
85
85
  reason: {
86
86
  actions: ['fail'],
87
- }
87
+ },
88
+ successAt: {
89
+ actions: ['succeed'],
90
+ },
91
+ externalMeta: {
92
+ actions: ['succeed', 'fail'],
93
+ },
88
94
  },
89
95
  withdraw: {
90
96
  dealLoss: {
@@ -10,6 +10,7 @@ export interface Schema extends EntityShape {
10
10
  loss: Price;
11
11
  pay: Pay;
12
12
  meta?: Object;
13
+ externalMeta?: Object;
13
14
  externalId?: String<128>;
14
15
  price: Price;
15
16
  creator: User;
@@ -16,6 +16,7 @@ export const entityDesc = {
16
16
  loss: '损耗',
17
17
  withdraw: '关联提现',
18
18
  meta: 'metadata',
19
+ externalMeta: '外部meta信息',
19
20
  externalId: '外部退款流水号',
20
21
  iState: '状态',
21
22
  creator: '创建者',
@@ -14,6 +14,7 @@ export type OpSchema = EntityShape & {
14
14
  loss: Price;
15
15
  payId: ForeignKey<"pay">;
16
16
  meta?: Object | null;
17
+ externalMeta?: Object | null;
17
18
  externalId?: String<128> | null;
18
19
  price: Price;
19
20
  creatorId: ForeignKey<"user">;
@@ -27,6 +28,7 @@ export type Schema = EntityShape & {
27
28
  loss: Price;
28
29
  payId: ForeignKey<"pay">;
29
30
  meta?: Object | null;
31
+ externalMeta?: Object | null;
30
32
  externalId?: String<128> | null;
31
33
  price: Price;
32
34
  creatorId: ForeignKey<"user">;
@@ -54,6 +56,7 @@ type AttrFilter = {
54
56
  payId: Q_StringValue;
55
57
  pay: Pay.Filter;
56
58
  meta: Object;
59
+ externalMeta: Object;
57
60
  externalId: Q_StringValue;
58
61
  price: Q_NumberValue;
59
62
  creatorId: Q_StringValue;
@@ -78,6 +81,7 @@ export type Projection = {
78
81
  payId?: number;
79
82
  pay?: Pay.Projection;
80
83
  meta?: number | Object;
84
+ externalMeta?: number | Object;
81
85
  externalId?: number;
82
86
  price?: number;
83
87
  creatorId?: number;
@@ -17,6 +17,9 @@ export const desc = {
17
17
  meta: {
18
18
  type: "object"
19
19
  },
20
+ externalMeta: {
21
+ type: "object"
22
+ },
20
23
  externalId: {
21
24
  type: "varchar",
22
25
  params: {
@@ -1 +1 @@
1
- { "name": "帐户", "attr": { "pay": "关联支付", "price": "价格", "loss": "损耗", "withdraw": "关联提现", "meta": "metadata", "externalId": "外部退款流水号", "iState": "状态", "creator": "创建者", "reason": "原因", "opers": "相关账户操作", "successAt": "退款成功时间" }, "action": { "succeed": "退款成功", "fail": "退款失败" }, "v": { "iState": { "refunding": "退款中", "successful": "退款成功", "failed": "退款失败" } } }
1
+ { "name": "帐户", "attr": { "pay": "关联支付", "price": "价格", "loss": "损耗", "withdraw": "关联提现", "meta": "metadata", "externalMeta": "外部meta信息", "externalId": "外部退款流水号", "iState": "状态", "creator": "创建者", "reason": "原因", "opers": "相关账户操作", "successAt": "退款成功时间" }, "action": { "succeed": "退款成功", "fail": "退款失败" }, "v": { "iState": { "refunding": "退款中", "successful": "退款成功", "failed": "退款失败" } } }
@@ -622,5 +622,18 @@ const triggers = [
622
622
  }
623
623
  }
624
624
  },
625
+ /* 影响的地方有点多,先不做了
626
+ {
627
+ entity: 'pay',
628
+ name: '当选择pay并发现pay的状态是paying时,尝试刷新其状态',
629
+ action: 'select',
630
+ when: 'after',
631
+ fn: async ({ result }, context) => {
632
+ if (result.length === 1 && result[0].iState === 'paying') {
633
+ await refreshPayState(result[0] as EntityDict['pay']['OpSchema'], context);
634
+ }
635
+ return 0;
636
+ }
637
+ } as SelectTriggerAfter<EntityDict, 'pay', BRC>, */
625
638
  ];
626
639
  export default triggers;
@@ -115,6 +115,7 @@ async function succeedRefunding(context, refundId) {
115
115
  action,
116
116
  data: {
117
117
  refunded: refunded2,
118
+ refundable: refunded2 < paid,
118
119
  },
119
120
  filter: {
120
121
  id: pay.id,
@@ -236,7 +237,9 @@ async function failRefunding(context, refundId) {
236
237
  await context.operate('pay', {
237
238
  id: await generateNewIdAsync(),
238
239
  action,
239
- data: {},
240
+ data: {
241
+ refundable: true,
242
+ },
240
243
  filter: {
241
244
  id: pay.id,
242
245
  }
@@ -6,6 +6,6 @@ import { BRC } from '../types/RuntimeCxt';
6
6
  * @param context
7
7
  * @param refunds
8
8
  */
9
- export declare function updateWithdrawState(context: BRC, id: string): Promise<0 | 1>;
9
+ export declare function updateWithdrawState(context: BRC, id: string): Promise<1 | 0>;
10
10
  declare const triggers: Trigger<EntityDict, 'withdraw', BRC>[];
11
11
  export default triggers;
package/es/utils/pay.d.ts CHANGED
@@ -3,6 +3,14 @@ import { EntityDict } from '../oak-app-domain';
3
3
  import { EntityDict as BaseEntityDict } from 'oak-domain/lib/types/Entity';
4
4
  import BackendRuntimeContext from '../context/BackendRuntimeContext';
5
5
  import { IncomingHttpHeaders } from 'http';
6
+ import { BRC } from '../types/RuntimeCxt';
6
7
  export declare const fullPayProjection: EntityDict['pay']['Selection']['data'];
7
8
  export declare function payNotify<ED extends EntityDict & BaseEntityDict>(context: BackendRuntimeContext<ED>, body: any, payId: string, headers: IncomingHttpHeaders): Promise<void>;
8
9
  export declare function refundNotify<ED extends EntityDict & BaseEntityDict>(context: BackendRuntimeContext<ED>, body: any, refundId: string, headers: IncomingHttpHeaders): Promise<void>;
10
+ /**
11
+ * 刷新paying状态的pay的真实支付状态
12
+ * @param pay
13
+ * @param context
14
+ * @returns
15
+ */
16
+ export declare function refreshPayState(pay: EntityDict['pay']['OpSchema'], context: BRC): Promise<import("oak-domain/lib/types/Entity").OperationResult<EntityDict> | undefined>;
package/es/utils/pay.js CHANGED
@@ -41,7 +41,7 @@ export async function payNotify(context, body, payId, headers) {
41
41
  filter: {
42
42
  id: payId,
43
43
  }
44
- }, {});
44
+ }, { blockTrigger: true });
45
45
  if (!pay) {
46
46
  console.error('接受到无效的payId', payId);
47
47
  return;
@@ -135,3 +135,67 @@ export async function refundNotify(context, body, refundId, headers) {
135
135
  }, {});
136
136
  }
137
137
  }
138
+ /**
139
+ * 刷新paying状态的pay的真实支付状态
140
+ * @param pay
141
+ * @param context
142
+ * @returns
143
+ */
144
+ export async function refreshPayState(pay, context) {
145
+ let pay2 = pay;
146
+ if (!pay2.entity || !pay2.entityId || !pay2.timeoutAt || !pay2.price || !pay2.iState) {
147
+ const pays = await context.select('pay', {
148
+ data: {
149
+ id: 1,
150
+ entity: 1,
151
+ entityId: 1,
152
+ price: 1,
153
+ iState: 1,
154
+ },
155
+ filter: {
156
+ id: pay.id,
157
+ }
158
+ }, { blockTrigger: true });
159
+ pay2 = pays[0];
160
+ }
161
+ const { entity, entityId, timeoutAt, price } = pay2;
162
+ const clazz = await getPayClazz(entity, entityId, context);
163
+ const [iState, updateData] = await clazz.getState(pay);
164
+ if (iState !== pay.iState) {
165
+ let action = 'close';
166
+ switch (iState) {
167
+ case 'closed': {
168
+ // action = 'close';
169
+ break;
170
+ }
171
+ case 'paid': {
172
+ action = 'succeedPaying';
173
+ updateData.paid = price;
174
+ break;
175
+ }
176
+ default: {
177
+ assert(false);
178
+ }
179
+ }
180
+ return await context.operate('pay', {
181
+ id: await generateNewIdAsync(),
182
+ action,
183
+ data: updateData,
184
+ filter: {
185
+ id: pay.id,
186
+ }
187
+ }, {});
188
+ }
189
+ else if (iState === 'paying' && timeoutAt < Date.now()) {
190
+ // 尝试关闭订单
191
+ // 理论上用户可能在上一个getState和这个close之间支付完成,此时关闭失败,下一个Watcher到来时就能处理成功了
192
+ return await context.operate('pay', {
193
+ id: await generateNewIdAsync(),
194
+ action: 'close',
195
+ data: updateData,
196
+ filter: {
197
+ id: pay.id,
198
+ }
199
+ }, {});
200
+ }
201
+ }
@@ -55,7 +55,7 @@ export default class WechatPay extends WechatPayDebug {
55
55
  const { id, price, pay } = refund;
56
56
  const serverUrl = context.composeAccessPath();
57
57
  const endpoint = this.refundNotifyUrl;
58
- const refundNotifyUrl = `${serverUrl}/endpoint/${endpoint}/${pay.id}`;
58
+ const refundNotifyUrl = `${serverUrl}/endpoint/${endpoint}/${id}`;
59
59
  const result = await this.wechatPay.createRefund({
60
60
  out_trade_no: compressTo32(pay.id),
61
61
  out_refund_no: compressTo32(id),
@@ -73,7 +73,8 @@ export default class WechatPay extends WechatPayDebug {
73
73
  }
74
74
  async getRefundState(refund) {
75
75
  const result = await this.wechatPay.queryRefundByOutRefundNo(compressTo32(refund.id));
76
- const { status, success_time } = this.analyzeResult(result);
76
+ const externalMeta = this.analyzeResult(result);
77
+ const { status, success_time } = externalMeta;
77
78
  /**
78
79
  * 【退款状态】 退款到银行发现用户的卡作废或者冻结了,导致原路退款银行卡失败,可前往商户平台(pay.weixin.qq.com)-交易中心,手动处理此笔退款。
79
80
  可选取值:
@@ -83,7 +84,10 @@ export default class WechatPay extends WechatPayDebug {
83
84
  ABNORMAL: 退款异常
84
85
  */
85
86
  const iState = REFUND_STATE_MATRIX[status];
86
- return [iState, success_time ? { successAt: dayJs(success_time).millisecond() } : undefined];
87
+ return [iState, success_time ? {
88
+ successAt: success_time ? dayJs(success_time).valueOf() : undefined,
89
+ externalMeta,
90
+ } : undefined];
87
91
  }
88
92
  analyzeResult(result) {
89
93
  const { success, data, } = result;
@@ -189,10 +193,10 @@ export default class WechatPay extends WechatPayDebug {
189
193
  const updateData = {
190
194
  meta: {
191
195
  getState: omit(result, ['mchid', 'appid', 'out_trade_no']),
192
- }
196
+ },
193
197
  };
194
198
  if (iState === 'paid') {
195
- updateData.forbidRefundAt = dayJs(success_time).millisecond();
199
+ updateData.successAt = dayJs(success_time).valueOf();
196
200
  }
197
201
  return [iState, updateData];
198
202
  }
@@ -308,7 +312,7 @@ export default class WechatPay extends WechatPayDebug {
308
312
  meta: omit(result, ['mchid',]),
309
313
  };
310
314
  if (iState === 'paid') {
311
- extra.successAt = success_time;
315
+ extra.successAt = dayJs(success_time).valueOf();
312
316
  }
313
317
  return {
314
318
  payId,
@@ -358,10 +362,11 @@ export default class WechatPay extends WechatPayDebug {
358
362
  const iState = REFUND_STATE_MATRIX[refund_status];
359
363
  assert(iState);
360
364
  const extra = {
361
- meta: omit(result, ['mchid',]),
365
+ externalMeta: omit(result, ['mchid',]),
362
366
  };
363
367
  if (iState === 'successful') {
364
- extra.successAt = success_time;
368
+ assert(success_time);
369
+ extra.successAt = dayJs(success_time).valueOf();
365
370
  }
366
371
  return {
367
372
  refundId,
@@ -1,9 +1,6 @@
1
- import { fullPayProjection } from '../utils/pay';
2
- import { getPayClazz } from '../utils/payClazz';
3
- import assert from 'assert';
4
- import { generateNewIdAsync } from 'oak-domain/lib/utils/uuid';
1
+ import { fullPayProjection, refreshPayState } from '../utils/pay';
5
2
  import { mergeOperationResult } from 'oak-domain/lib/utils/operationResult';
6
- const QUERY_PAYING_STATE_GAP = process.env.NODE_ENV === 'production' ? 3600 * 1000 : 60 * 1000;
3
+ const QUERY_PAYING_STATE_GAP = process.env.NODE_ENV === 'production' ? 600 * 1000 : 60 * 1000;
7
4
  const watchers = [
8
5
  {
9
6
  name: '对paying状态的订单,同步其真实支付状态',
@@ -24,46 +21,8 @@ const watchers = [
24
21
  fn: async (context, data) => {
25
22
  const results = [];
26
23
  for (const pay of data) {
27
- const { applicationId, entity, entityId, timeoutAt, price } = pay;
28
- const clazz = await getPayClazz(entity, entityId, context);
29
- const [iState, updateData] = await clazz.getState(pay);
30
- if (iState !== pay.iState) {
31
- let action = 'close';
32
- switch (iState) {
33
- case 'closed': {
34
- // action = 'close';
35
- break;
36
- }
37
- case 'paid': {
38
- action = 'succeedPaying';
39
- updateData.paid = price;
40
- break;
41
- }
42
- default: {
43
- assert(false);
44
- }
45
- }
46
- const result = await context.operate('pay', {
47
- id: await generateNewIdAsync(),
48
- action,
49
- data: updateData,
50
- filter: {
51
- id: pay.id,
52
- }
53
- }, {});
54
- results.push(result);
55
- }
56
- else if (iState === 'paying' && timeoutAt < Date.now()) {
57
- // 尝试关闭订单
58
- // 理论上用户可能在上一个getState和这个close之间支付完成,此时关闭失败,下一个Watcher到来时就能处理成功了
59
- const result = await context.operate('pay', {
60
- id: await generateNewIdAsync(),
61
- action: 'close',
62
- data: updateData,
63
- filter: {
64
- id: pay.id,
65
- }
66
- }, {});
24
+ const result = await refreshPayState(pay, context);
25
+ if (result) {
67
26
  results.push(result);
68
27
  }
69
28
  }
@@ -2,7 +2,7 @@ import { getPayClazz } from '../utils/payClazz';
2
2
  import assert from 'assert';
3
3
  import { generateNewIdAsync } from 'oak-domain/lib/utils/uuid';
4
4
  import { mergeOperationResult } from 'oak-domain/lib/utils/operationResult';
5
- const QUERY_PAYING_STATE_GAP = process.env.NODE_ENV === 'production' ? 3600 * 1000 : 60 * 1000;
5
+ const QUERY_PAYING_STATE_GAP = process.env.NODE_ENV === 'production' ? 600 * 1000 : 60 * 1000;
6
6
  const watchers = [
7
7
  {
8
8
  name: '对refund状态的退款,同步其真实退款状态',
@@ -29,7 +29,8 @@ const watchers = [
29
29
  entity: 1,
30
30
  entityId: 1,
31
31
  applicationId: 1,
32
- }
32
+ },
33
+ externalId: 1,
33
34
  },
34
35
  fn: async (context, data) => {
35
36
  const results = [];
@@ -3,7 +3,7 @@ export declare function getWithdrawCreateData(params: {
3
3
  accountId: string;
4
4
  price: number;
5
5
  withdrawAccountId?: string;
6
- }, context: BRC): Promise<(Partial<Omit<import("../oak-app-domain/Withdraw/Schema").OpSchema, "creatorId" | "accountId">> & {
6
+ }, context: BRC): Promise<(Partial<Omit<import("../oak-app-domain/Withdraw/Schema").OpSchema, "accountId" | "creatorId">> & {
7
7
  id: string;
8
8
  } & {
9
9
  accountId: string;
@@ -17,7 +17,7 @@ export declare function getWithdrawCreateData(params: {
17
17
  modiEntity$entity?: import("oak-domain/lib/types").Operation<"create", Omit<import("../oak-app-domain/ModiEntity/Schema").CreateOperationData, "entity" | "entityId">[]> | import("oak-domain/lib/types").Operation<"create", Omit<import("../oak-app-domain/ModiEntity/Schema").CreateOperationData, "entity" | "entityId">>[] | undefined;
18
18
  operEntity$entity?: import("oak-domain/lib/types").Operation<"create", Omit<import("../oak-app-domain/OperEntity/Schema").CreateOperationData, "entity" | "entityId">[]> | import("oak-domain/lib/types").Operation<"create", Omit<import("../oak-app-domain/OperEntity/Schema").CreateOperationData, "entity" | "entityId">>[] | undefined;
19
19
  accountOper$entity?: import("oak-domain/lib/types").Operation<"create", Omit<import("../oak-app-domain/AccountOper/Schema").CreateOperationData, "entity" | "entityId">[]> | import("oak-domain/lib/types").Operation<"create", Omit<import("../oak-app-domain/AccountOper/Schema").CreateOperationData, "entity" | "entityId">>[] | undefined;
20
- }) | (Partial<Omit<import("../oak-app-domain/Withdraw/Schema").OpSchema, "creatorId" | "accountId">> & {
20
+ }) | (Partial<Omit<import("../oak-app-domain/Withdraw/Schema").OpSchema, "accountId" | "creatorId">> & {
21
21
  id: string;
22
22
  } & {
23
23
  accountId: string;
@@ -31,7 +31,7 @@ export declare function getWithdrawCreateData(params: {
31
31
  modiEntity$entity?: import("oak-domain/lib/types").Operation<"create", Omit<import("../oak-app-domain/ModiEntity/Schema").CreateOperationData, "entity" | "entityId">[]> | import("oak-domain/lib/types").Operation<"create", Omit<import("../oak-app-domain/ModiEntity/Schema").CreateOperationData, "entity" | "entityId">>[] | undefined;
32
32
  operEntity$entity?: import("oak-domain/lib/types").Operation<"create", Omit<import("../oak-app-domain/OperEntity/Schema").CreateOperationData, "entity" | "entityId">[]> | import("oak-domain/lib/types").Operation<"create", Omit<import("../oak-app-domain/OperEntity/Schema").CreateOperationData, "entity" | "entityId">>[] | undefined;
33
33
  accountOper$entity?: import("oak-domain/lib/types").Operation<"create", Omit<import("../oak-app-domain/AccountOper/Schema").CreateOperationData, "entity" | "entityId">[]> | import("oak-domain/lib/types").Operation<"create", Omit<import("../oak-app-domain/AccountOper/Schema").CreateOperationData, "entity" | "entityId">>[] | undefined;
34
- }) | (Partial<Omit<import("../oak-app-domain/Withdraw/Schema").OpSchema, "creatorId" | "accountId">> & {
34
+ }) | (Partial<Omit<import("../oak-app-domain/Withdraw/Schema").OpSchema, "accountId" | "creatorId">> & {
35
35
  id: string;
36
36
  } & {
37
37
  account?: undefined;
@@ -45,7 +45,7 @@ export declare function getWithdrawCreateData(params: {
45
45
  modiEntity$entity?: import("oak-domain/lib/types").Operation<"create", Omit<import("../oak-app-domain/ModiEntity/Schema").CreateOperationData, "entity" | "entityId">[]> | import("oak-domain/lib/types").Operation<"create", Omit<import("../oak-app-domain/ModiEntity/Schema").CreateOperationData, "entity" | "entityId">>[] | undefined;
46
46
  operEntity$entity?: import("oak-domain/lib/types").Operation<"create", Omit<import("../oak-app-domain/OperEntity/Schema").CreateOperationData, "entity" | "entityId">[]> | import("oak-domain/lib/types").Operation<"create", Omit<import("../oak-app-domain/OperEntity/Schema").CreateOperationData, "entity" | "entityId">>[] | undefined;
47
47
  accountOper$entity?: import("oak-domain/lib/types").Operation<"create", Omit<import("../oak-app-domain/AccountOper/Schema").CreateOperationData, "entity" | "entityId">[]> | import("oak-domain/lib/types").Operation<"create", Omit<import("../oak-app-domain/AccountOper/Schema").CreateOperationData, "entity" | "entityId">>[] | undefined;
48
- }) | (Partial<Omit<import("../oak-app-domain/Withdraw/Schema").OpSchema, "creatorId" | "accountId">> & {
48
+ }) | (Partial<Omit<import("../oak-app-domain/Withdraw/Schema").OpSchema, "accountId" | "creatorId">> & {
49
49
  id: string;
50
50
  } & {
51
51
  account?: undefined;
@@ -15,6 +15,7 @@ const checkers = [
15
15
  data.paid = 0;
16
16
  data.applicationId = context.getApplicationId();
17
17
  data.creatorId = context.getCurrentUserId();
18
+ data.refundable = false;
18
19
  if (!data.meta) {
19
20
  data.meta = {};
20
21
  }
@@ -26,7 +26,7 @@ const checkers = [
26
26
  checker(operation, context) {
27
27
  const { data, filter } = operation;
28
28
  const { externalId } = data;
29
- // 必须有ExternalId,除非是account类型的支付
29
+ // 当offline退款成功时,必须有ExternalId
30
30
  if (!externalId) {
31
31
  return (0, executor_1.pipeline)(() => context.select('refund', {
32
32
  data: {
@@ -41,7 +41,7 @@ const checkers = [
41
41
  }
42
42
  }, { dontCollect: true }), (refunds) => {
43
43
  const [refund] = refunds;
44
- if (refund.pay.entity !== 'account') {
44
+ if (refund.pay.entity === 'offlineAcount') {
45
45
  throw new types_1.OakAttrNotNullException('refund', ['externalId']);
46
46
  }
47
47
  });
@@ -86,7 +86,13 @@ const attrUpdateMatrix = {
86
86
  },
87
87
  reason: {
88
88
  actions: ['fail'],
89
- }
89
+ },
90
+ successAt: {
91
+ actions: ['succeed'],
92
+ },
93
+ externalMeta: {
94
+ actions: ['succeed', 'fail'],
95
+ },
90
96
  },
91
97
  withdraw: {
92
98
  dealLoss: {
@@ -15,7 +15,7 @@ export declare class BackendRuntimeContext<ED extends EntityDict & BaseEntityDic
15
15
  type?: number | undefined;
16
16
  systemId?: number | undefined;
17
17
  system?: import("../oak-app-domain/System/Schema").Projection | undefined;
18
- config?: number | import("oak-domain/lib/types").JsonProjection<import("../oak-app-domain/Application/Schema").WechatMpConfig | import("../oak-app-domain/Application/Schema").WebConfig | import("../oak-app-domain/Application/Schema").WechatPublicConfig | import("../oak-app-domain/Application/Schema").NativeConfig> | undefined;
18
+ config?: number | import("oak-domain/lib/types").JsonProjection<import("../oak-app-domain/Application/Schema").WebConfig | import("../oak-app-domain/Application/Schema").WechatMpConfig | import("../oak-app-domain/Application/Schema").WechatPublicConfig | import("../oak-app-domain/Application/Schema").NativeConfig> | undefined;
19
19
  style?: number | import("oak-domain/lib/types").JsonProjection<import("oak-general-business/lib/types/Style").Style> | undefined;
20
20
  domainId?: number | undefined;
21
21
  domain?: import("../oak-app-domain/Domain/Schema").Projection | undefined;
@@ -10,6 +10,7 @@ export interface Schema extends EntityShape {
10
10
  loss: Price;
11
11
  pay: Pay;
12
12
  meta?: Object;
13
+ externalMeta?: Object;
13
14
  externalId?: String<128>;
14
15
  price: Price;
15
16
  creator: User;
@@ -19,6 +19,7 @@ exports.entityDesc = {
19
19
  loss: '损耗',
20
20
  withdraw: '关联提现',
21
21
  meta: 'metadata',
22
+ externalMeta: '外部meta信息',
22
23
  externalId: '外部退款流水号',
23
24
  iState: '状态',
24
25
  creator: '创建者',
@@ -14,6 +14,7 @@ export type OpSchema = EntityShape & {
14
14
  loss: Price;
15
15
  payId: ForeignKey<"pay">;
16
16
  meta?: Object | null;
17
+ externalMeta?: Object | null;
17
18
  externalId?: String<128> | null;
18
19
  price: Price;
19
20
  creatorId: ForeignKey<"user">;
@@ -27,6 +28,7 @@ export type Schema = EntityShape & {
27
28
  loss: Price;
28
29
  payId: ForeignKey<"pay">;
29
30
  meta?: Object | null;
31
+ externalMeta?: Object | null;
30
32
  externalId?: String<128> | null;
31
33
  price: Price;
32
34
  creatorId: ForeignKey<"user">;
@@ -54,6 +56,7 @@ type AttrFilter = {
54
56
  payId: Q_StringValue;
55
57
  pay: Pay.Filter;
56
58
  meta: Object;
59
+ externalMeta: Object;
57
60
  externalId: Q_StringValue;
58
61
  price: Q_NumberValue;
59
62
  creatorId: Q_StringValue;
@@ -78,6 +81,7 @@ export type Projection = {
78
81
  payId?: number;
79
82
  pay?: Pay.Projection;
80
83
  meta?: number | Object;
84
+ externalMeta?: number | Object;
81
85
  externalId?: number;
82
86
  price?: number;
83
87
  creatorId?: number;
@@ -20,6 +20,9 @@ exports.desc = {
20
20
  meta: {
21
21
  type: "object"
22
22
  },
23
+ externalMeta: {
24
+ type: "object"
25
+ },
23
26
  externalId: {
24
27
  type: "varchar",
25
28
  params: {
@@ -1 +1 @@
1
- { "name": "帐户", "attr": { "pay": "关联支付", "price": "价格", "loss": "损耗", "withdraw": "关联提现", "meta": "metadata", "externalId": "外部退款流水号", "iState": "状态", "creator": "创建者", "reason": "原因", "opers": "相关账户操作", "successAt": "退款成功时间" }, "action": { "succeed": "退款成功", "fail": "退款失败" }, "v": { "iState": { "refunding": "退款中", "successful": "退款成功", "failed": "退款失败" } } }
1
+ { "name": "帐户", "attr": { "pay": "关联支付", "price": "价格", "loss": "损耗", "withdraw": "关联提现", "meta": "metadata", "externalMeta": "外部meta信息", "externalId": "外部退款流水号", "iState": "状态", "creator": "创建者", "reason": "原因", "opers": "相关账户操作", "successAt": "退款成功时间" }, "action": { "succeed": "退款成功", "fail": "退款失败" }, "v": { "iState": { "refunding": "退款中", "successful": "退款成功", "failed": "退款失败" } } }
@@ -625,5 +625,18 @@ const triggers = [
625
625
  }
626
626
  }
627
627
  },
628
+ /* 影响的地方有点多,先不做了
629
+ {
630
+ entity: 'pay',
631
+ name: '当选择pay并发现pay的状态是paying时,尝试刷新其状态',
632
+ action: 'select',
633
+ when: 'after',
634
+ fn: async ({ result }, context) => {
635
+ if (result.length === 1 && result[0].iState === 'paying') {
636
+ await refreshPayState(result[0] as EntityDict['pay']['OpSchema'], context);
637
+ }
638
+ return 0;
639
+ }
640
+ } as SelectTriggerAfter<EntityDict, 'pay', BRC>, */
628
641
  ];
629
642
  exports.default = triggers;
@@ -118,6 +118,7 @@ async function succeedRefunding(context, refundId) {
118
118
  action,
119
119
  data: {
120
120
  refunded: refunded2,
121
+ refundable: refunded2 < paid,
121
122
  },
122
123
  filter: {
123
124
  id: pay.id,
@@ -239,7 +240,9 @@ async function failRefunding(context, refundId) {
239
240
  await context.operate('pay', {
240
241
  id: await (0, uuid_1.generateNewIdAsync)(),
241
242
  action,
242
- data: {},
243
+ data: {
244
+ refundable: true,
245
+ },
243
246
  filter: {
244
247
  id: pay.id,
245
248
  }
@@ -14,7 +14,7 @@ export declare const mergedProjection: {
14
14
  type?: number | undefined;
15
15
  systemId?: number | undefined;
16
16
  system?: import("../oak-app-domain/System/Schema").Projection | undefined;
17
- config?: number | import("oak-domain/lib/types").JsonProjection<import("../oak-app-domain/Application/Schema").WechatMpConfig | import("../oak-app-domain/Application/Schema").WebConfig | import("../oak-app-domain/Application/Schema").WechatPublicConfig | import("../oak-app-domain/Application/Schema").NativeConfig> | undefined;
17
+ config?: number | import("oak-domain/lib/types").JsonProjection<import("../oak-app-domain/Application/Schema").WebConfig | import("../oak-app-domain/Application/Schema").WechatMpConfig | import("../oak-app-domain/Application/Schema").WechatPublicConfig | import("../oak-app-domain/Application/Schema").NativeConfig> | undefined;
18
18
  style?: number | import("oak-domain/lib/types").JsonProjection<import("oak-general-business/lib/types/Style").Style> | undefined;
19
19
  domainId?: number | undefined;
20
20
  domain?: import("../oak-app-domain/Domain/Schema").Projection | undefined;
@@ -3,6 +3,14 @@ import { EntityDict } from '../oak-app-domain';
3
3
  import { EntityDict as BaseEntityDict } from 'oak-domain/lib/types/Entity';
4
4
  import BackendRuntimeContext from '../context/BackendRuntimeContext';
5
5
  import { IncomingHttpHeaders } from 'http';
6
+ import { BRC } from '../types/RuntimeCxt';
6
7
  export declare const fullPayProjection: EntityDict['pay']['Selection']['data'];
7
8
  export declare function payNotify<ED extends EntityDict & BaseEntityDict>(context: BackendRuntimeContext<ED>, body: any, payId: string, headers: IncomingHttpHeaders): Promise<void>;
8
9
  export declare function refundNotify<ED extends EntityDict & BaseEntityDict>(context: BackendRuntimeContext<ED>, body: any, refundId: string, headers: IncomingHttpHeaders): Promise<void>;
10
+ /**
11
+ * 刷新paying状态的pay的真实支付状态
12
+ * @param pay
13
+ * @param context
14
+ * @returns
15
+ */
16
+ export declare function refreshPayState(pay: EntityDict['pay']['OpSchema'], context: BRC): Promise<import("oak-domain/lib/types/Entity").OperationResult<EntityDict> | undefined>;
package/lib/utils/pay.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.refundNotify = exports.payNotify = exports.fullPayProjection = void 0;
3
+ exports.refreshPayState = exports.refundNotify = exports.payNotify = exports.fullPayProjection = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const payClazz_1 = require("./payClazz");
6
6
  const assert_1 = tslib_1.__importDefault(require("assert"));
@@ -45,7 +45,7 @@ async function payNotify(context, body, payId, headers) {
45
45
  filter: {
46
46
  id: payId,
47
47
  }
48
- }, {});
48
+ }, { blockTrigger: true });
49
49
  if (!pay) {
50
50
  console.error('接受到无效的payId', payId);
51
51
  return;
@@ -141,3 +141,68 @@ async function refundNotify(context, body, refundId, headers) {
141
141
  }
142
142
  }
143
143
  exports.refundNotify = refundNotify;
144
+ /**
145
+ * 刷新paying状态的pay的真实支付状态
146
+ * @param pay
147
+ * @param context
148
+ * @returns
149
+ */
150
+ async function refreshPayState(pay, context) {
151
+ let pay2 = pay;
152
+ if (!pay2.entity || !pay2.entityId || !pay2.timeoutAt || !pay2.price || !pay2.iState) {
153
+ const pays = await context.select('pay', {
154
+ data: {
155
+ id: 1,
156
+ entity: 1,
157
+ entityId: 1,
158
+ price: 1,
159
+ iState: 1,
160
+ },
161
+ filter: {
162
+ id: pay.id,
163
+ }
164
+ }, { blockTrigger: true });
165
+ pay2 = pays[0];
166
+ }
167
+ const { entity, entityId, timeoutAt, price } = pay2;
168
+ const clazz = await (0, payClazz_1.getPayClazz)(entity, entityId, context);
169
+ const [iState, updateData] = await clazz.getState(pay);
170
+ if (iState !== pay.iState) {
171
+ let action = 'close';
172
+ switch (iState) {
173
+ case 'closed': {
174
+ // action = 'close';
175
+ break;
176
+ }
177
+ case 'paid': {
178
+ action = 'succeedPaying';
179
+ updateData.paid = price;
180
+ break;
181
+ }
182
+ default: {
183
+ (0, assert_1.default)(false);
184
+ }
185
+ }
186
+ return await context.operate('pay', {
187
+ id: await (0, uuid_1.generateNewIdAsync)(),
188
+ action,
189
+ data: updateData,
190
+ filter: {
191
+ id: pay.id,
192
+ }
193
+ }, {});
194
+ }
195
+ else if (iState === 'paying' && timeoutAt < Date.now()) {
196
+ // 尝试关闭订单
197
+ // 理论上用户可能在上一个getState和这个close之间支付完成,此时关闭失败,下一个Watcher到来时就能处理成功了
198
+ return await context.operate('pay', {
199
+ id: await (0, uuid_1.generateNewIdAsync)(),
200
+ action: 'close',
201
+ data: updateData,
202
+ filter: {
203
+ id: pay.id,
204
+ }
205
+ }, {});
206
+ }
207
+ }
208
+ exports.refreshPayState = refreshPayState;
@@ -58,7 +58,7 @@ class WechatPay extends WechatPay_debug_1.default {
58
58
  const { id, price, pay } = refund;
59
59
  const serverUrl = context.composeAccessPath();
60
60
  const endpoint = this.refundNotifyUrl;
61
- const refundNotifyUrl = `${serverUrl}/endpoint/${endpoint}/${pay.id}`;
61
+ const refundNotifyUrl = `${serverUrl}/endpoint/${endpoint}/${id}`;
62
62
  const result = await this.wechatPay.createRefund({
63
63
  out_trade_no: (0, uuid_1.compressTo32)(pay.id),
64
64
  out_refund_no: (0, uuid_1.compressTo32)(id),
@@ -76,7 +76,8 @@ class WechatPay extends WechatPay_debug_1.default {
76
76
  }
77
77
  async getRefundState(refund) {
78
78
  const result = await this.wechatPay.queryRefundByOutRefundNo((0, uuid_1.compressTo32)(refund.id));
79
- const { status, success_time } = this.analyzeResult(result);
79
+ const externalMeta = this.analyzeResult(result);
80
+ const { status, success_time } = externalMeta;
80
81
  /**
81
82
  * 【退款状态】 退款到银行发现用户的卡作废或者冻结了,导致原路退款银行卡失败,可前往商户平台(pay.weixin.qq.com)-交易中心,手动处理此笔退款。
82
83
  可选取值:
@@ -86,7 +87,10 @@ class WechatPay extends WechatPay_debug_1.default {
86
87
  ABNORMAL: 退款异常
87
88
  */
88
89
  const iState = REFUND_STATE_MATRIX[status];
89
- return [iState, success_time ? { successAt: (0, dayjs_1.default)(success_time).millisecond() } : undefined];
90
+ return [iState, success_time ? {
91
+ successAt: success_time ? (0, dayjs_1.default)(success_time).valueOf() : undefined,
92
+ externalMeta,
93
+ } : undefined];
90
94
  }
91
95
  analyzeResult(result) {
92
96
  const { success, data, } = result;
@@ -192,10 +196,10 @@ class WechatPay extends WechatPay_debug_1.default {
192
196
  const updateData = {
193
197
  meta: {
194
198
  getState: (0, lodash_1.omit)(result, ['mchid', 'appid', 'out_trade_no']),
195
- }
199
+ },
196
200
  };
197
201
  if (iState === 'paid') {
198
- updateData.forbidRefundAt = (0, dayjs_1.default)(success_time).millisecond();
202
+ updateData.successAt = (0, dayjs_1.default)(success_time).valueOf();
199
203
  }
200
204
  return [iState, updateData];
201
205
  }
@@ -311,7 +315,7 @@ class WechatPay extends WechatPay_debug_1.default {
311
315
  meta: (0, lodash_1.omit)(result, ['mchid',]),
312
316
  };
313
317
  if (iState === 'paid') {
314
- extra.successAt = success_time;
318
+ extra.successAt = (0, dayjs_1.default)(success_time).valueOf();
315
319
  }
316
320
  return {
317
321
  payId,
@@ -361,10 +365,11 @@ class WechatPay extends WechatPay_debug_1.default {
361
365
  const iState = REFUND_STATE_MATRIX[refund_status];
362
366
  (0, assert_1.default)(iState);
363
367
  const extra = {
364
- meta: (0, lodash_1.omit)(result, ['mchid',]),
368
+ externalMeta: (0, lodash_1.omit)(result, ['mchid',]),
365
369
  };
366
370
  if (iState === 'successful') {
367
- extra.successAt = success_time;
371
+ (0, assert_1.default)(success_time);
372
+ extra.successAt = (0, dayjs_1.default)(success_time).valueOf();
368
373
  }
369
374
  return {
370
375
  refundId,
@@ -1,4 +1,4 @@
1
1
  import WechatPay from './WechatPay';
2
2
  import WechatPayDebug from './WechatPay.debug';
3
- declare const _default: typeof WechatPayDebug | typeof WechatPay;
3
+ declare const _default: typeof WechatPay | typeof WechatPayDebug;
4
4
  export default _default;
@@ -1,12 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- const tslib_1 = require("tslib");
4
3
  const pay_1 = require("../utils/pay");
5
- const payClazz_1 = require("../utils/payClazz");
6
- const assert_1 = tslib_1.__importDefault(require("assert"));
7
- const uuid_1 = require("oak-domain/lib/utils/uuid");
8
4
  const operationResult_1 = require("oak-domain/lib/utils/operationResult");
9
- const QUERY_PAYING_STATE_GAP = process.env.NODE_ENV === 'production' ? 3600 * 1000 : 60 * 1000;
5
+ const QUERY_PAYING_STATE_GAP = process.env.NODE_ENV === 'production' ? 600 * 1000 : 60 * 1000;
10
6
  const watchers = [
11
7
  {
12
8
  name: '对paying状态的订单,同步其真实支付状态',
@@ -27,46 +23,8 @@ const watchers = [
27
23
  fn: async (context, data) => {
28
24
  const results = [];
29
25
  for (const pay of data) {
30
- const { applicationId, entity, entityId, timeoutAt, price } = pay;
31
- const clazz = await (0, payClazz_1.getPayClazz)(entity, entityId, context);
32
- const [iState, updateData] = await clazz.getState(pay);
33
- if (iState !== pay.iState) {
34
- let action = 'close';
35
- switch (iState) {
36
- case 'closed': {
37
- // action = 'close';
38
- break;
39
- }
40
- case 'paid': {
41
- action = 'succeedPaying';
42
- updateData.paid = price;
43
- break;
44
- }
45
- default: {
46
- (0, assert_1.default)(false);
47
- }
48
- }
49
- const result = await context.operate('pay', {
50
- id: await (0, uuid_1.generateNewIdAsync)(),
51
- action,
52
- data: updateData,
53
- filter: {
54
- id: pay.id,
55
- }
56
- }, {});
57
- results.push(result);
58
- }
59
- else if (iState === 'paying' && timeoutAt < Date.now()) {
60
- // 尝试关闭订单
61
- // 理论上用户可能在上一个getState和这个close之间支付完成,此时关闭失败,下一个Watcher到来时就能处理成功了
62
- const result = await context.operate('pay', {
63
- id: await (0, uuid_1.generateNewIdAsync)(),
64
- action: 'close',
65
- data: updateData,
66
- filter: {
67
- id: pay.id,
68
- }
69
- }, {});
26
+ const result = await (0, pay_1.refreshPayState)(pay, context);
27
+ if (result) {
70
28
  results.push(result);
71
29
  }
72
30
  }
@@ -5,7 +5,7 @@ const payClazz_1 = require("../utils/payClazz");
5
5
  const assert_1 = tslib_1.__importDefault(require("assert"));
6
6
  const uuid_1 = require("oak-domain/lib/utils/uuid");
7
7
  const operationResult_1 = require("oak-domain/lib/utils/operationResult");
8
- const QUERY_PAYING_STATE_GAP = process.env.NODE_ENV === 'production' ? 3600 * 1000 : 60 * 1000;
8
+ const QUERY_PAYING_STATE_GAP = process.env.NODE_ENV === 'production' ? 600 * 1000 : 60 * 1000;
9
9
  const watchers = [
10
10
  {
11
11
  name: '对refund状态的退款,同步其真实退款状态',
@@ -32,7 +32,8 @@ const watchers = [
32
32
  entity: 1,
33
33
  entityId: 1,
34
34
  applicationId: 1,
35
- }
35
+ },
36
+ externalId: 1,
36
37
  },
37
38
  fn: async (context, data) => {
38
39
  const results = [];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "oak-pay-business",
3
- "version": "2.2.7",
3
+ "version": "2.2.9",
4
4
  "description": "",
5
5
  "files": [
6
6
  "lib/**/*",
@@ -30,7 +30,7 @@
30
30
  "classnames": "^2.3.1",
31
31
  "dayjs": "^1.11.5",
32
32
  "oak-domain": "~5.0.15",
33
- "oak-frontend-base": "5.2.10",
33
+ "oak-frontend-base": "~5.2.10",
34
34
  "oak-general-business": "~5.2.5",
35
35
  "wechat-pay-nodejs": "^0.2.3"
36
36
  },
@@ -18,6 +18,7 @@ export interface Schema extends EntityShape {
18
18
  loss: Price;
19
19
  pay: Pay;
20
20
  meta?: Object,
21
+ externalMeta?: Object,
21
22
  externalId?: String<128>;
22
23
  price: Price;
23
24
  creator: User;
@@ -50,6 +51,7 @@ export const entityDesc: EntityDesc<Schema, Action, '', {
50
51
  loss: '损耗',
51
52
  withdraw: '关联提现',
52
53
  meta: 'metadata',
54
+ externalMeta: '外部meta信息',
53
55
  externalId: '外部退款流水号',
54
56
  iState: '状态',
55
57
  creator: '创建者',