oak-pay-business 2.2.6 → 2.2.8

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.
@@ -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
  });
@@ -17,14 +17,20 @@ declare const List: <T extends keyof EntityDict>(props: ReactComponentProps<Enti
17
17
  attributes: OakAbsAttrDef[];
18
18
  data: RowWithActions<EntityDict, T>[];
19
19
  loading: boolean;
20
- tablePagination?: any;
21
- rowSelection?: any;
20
+ tablePagination?: React.ReactNode;
21
+ rowSelection?: import("antd/es/table/interface").TableRowSelection<RowWithActions<EntityDict, T>> | undefined;
22
22
  hideHeader?: boolean | undefined;
23
23
  disableSerialNumber?: boolean | undefined;
24
- size?: "large" | "middle" | "small" | undefined;
25
- scroll?: any;
26
- locale?: any;
24
+ size?: "small" | "middle" | "large" | undefined;
25
+ scroll?: ({
26
+ x?: string | number | true | undefined;
27
+ y?: string | number | undefined;
28
+ } & {
29
+ scrollToFirstRowOnChange?: boolean | undefined;
30
+ }) | undefined;
31
+ empty?: React.ReactNode;
27
32
  opWidth?: number | undefined;
33
+ ellipsis?: boolean | undefined;
28
34
  }>) => React.ReactElement;
29
35
  declare const ListPro: <T extends keyof EntityDict>(props: {
30
36
  title?: any;
@@ -40,16 +46,21 @@ declare const ListPro: <T extends keyof EntityDict>(props: {
40
46
  data: RowWithActions<EntityDict, T>[];
41
47
  loading?: boolean | undefined;
42
48
  tablePagination?: any;
43
- rowSelection?: any;
49
+ rowSelection?: import("antd/es/table/interface").TableRowSelection<RowWithActions<EntityDict, T>> | undefined;
44
50
  disableSerialNumber?: boolean | undefined;
45
- size?: "large" | "middle" | "small" | undefined;
46
- scroll?: any;
47
- locale?: any;
51
+ size?: "small" | "middle" | "large" | undefined;
52
+ scroll?: ({
53
+ x?: string | number | true | undefined;
54
+ y?: string | number | undefined;
55
+ } & {
56
+ scrollToFirstRowOnChange?: boolean | undefined;
57
+ }) | undefined;
58
+ empty?: any;
48
59
  opWidth?: number | undefined;
49
60
  oakPath?: string | undefined;
50
61
  }) => React.ReactElement;
51
62
  declare const Detail: <T extends keyof EntityDict>(props: ReactComponentProps<EntityDict, T, false, {
52
- column?: number | Record<Breakpoint, number> | undefined;
63
+ column?: number | Record<import("antd").Breakpoint, number> | undefined;
53
64
  entity: T;
54
65
  attributes: OakAbsAttrDef[];
55
66
  data: Partial<EntityDict[T]["Schema"]>;
@@ -127,7 +127,13 @@ export default OakComponent({
127
127
  onGoToUnfinishedPay && onGoToUnfinishedPay(payId);
128
128
  },
129
129
  setDepositOpen(depositOpen) {
130
- this.setState({ depositOpen });
130
+ this.setState({ depositOpen }, () => {
131
+ setTimeout(() => {
132
+ this.setState({
133
+ focus: depositOpen
134
+ });
135
+ }, 333);
136
+ });
131
137
  },
132
138
  setUfOpen(ufOpen) {
133
139
  this.setState({ ufOpen });
@@ -218,6 +224,7 @@ export default OakComponent({
218
224
  depositing: false,
219
225
  setDepPriceMp(price) { this.setDepPrice(price); },
220
226
  setDepositChannelMp(depositChannel) { this.setDepositChannel(depositChannel); },
227
+ focus: false,
221
228
  },
222
229
  lifetimes: {
223
230
  ready() {
@@ -91,7 +91,16 @@
91
91
  .ad-container {
92
92
  padding: 24rpx;
93
93
  background-color: white;
94
- .safe-area-inset-bottom();
94
+ // .safe-area-inset-bottom();
95
+ height: 40vh;
96
+
97
+ .btnBox {
98
+ position: fixed;
99
+ bottom: 0;
100
+ left: 0;
101
+ width: 100%;
102
+ .safe-area-inset-bottom();
103
+ }
95
104
  }
96
105
 
97
106
  .uf {
@@ -47,14 +47,19 @@
47
47
  </view>
48
48
  </view>
49
49
  <block wx:if="{{depositOpen}}">
50
- <l-arc-popup
50
+ <l-popup
51
+ show="{{depositOpen}}"
52
+ content-align="bottom"
53
+ bind:lintap="onDepositModalClose"
54
+ >
55
+ <!-- <l-arc-popup
51
56
  show="{{depositOpen}}"
52
57
  direction="bottom"
53
58
  bind:lintap="onDepositModalClose"
54
59
  min-height="200"
55
- max-height="800"
60
+ max-height="1000"
56
61
  z-index="1000"
57
- >
62
+ > -->
58
63
  <view class="ad-container">
59
64
  <deposit-new
60
65
  depositMinCent="{{depositMinCent}}"
@@ -65,8 +70,9 @@
65
70
  onSetPrice="{{setDepPriceMp}}"
66
71
  onSetChannel="{{setDepositChannelMp}}"
67
72
  onSetPrice="{{setDepPriceMp}}"
73
+ focus="{{focus}}"
68
74
  />
69
- <view style="margin-top: 12rpx">
75
+ <view style="margin-top:20rpx">
70
76
  <l-button
71
77
  type="default"
72
78
  size="long"
@@ -78,7 +84,7 @@
78
84
  </l-button>
79
85
  </view>
80
86
  </view>
81
- </l-arc-popup>
87
+ </l-popup>
82
88
  </block>
83
89
  <block wx:if="{{ufOpen}}">
84
90
  <l-dialog
@@ -8,5 +8,6 @@ declare const _default: (props: import("oak-frontend-base").ReactComponentProps<
8
8
  onSetPrice: (price: null | number) => void;
9
9
  onSetChannel: (channel: PayChannel) => void;
10
10
  loss: [number, string, any];
11
+ focus: boolean;
11
12
  }>) => React.ReactElement;
12
13
  export default _default;
@@ -9,6 +9,7 @@ export default OakComponent({
9
9
  onSetPrice: (price) => undefined,
10
10
  onSetChannel: (channel) => undefined,
11
11
  loss: [0, '', {}],
12
+ focus: false,
12
13
  },
13
14
  data: {
14
15
  onChooseChannelMp(channel) { this.onChooseChannel(channel); },
@@ -1,7 +1,7 @@
1
1
  <block wx:if="{{payChannels && payChannels.length > 0}}">
2
2
  <l-form>
3
3
  <l-form-item label="{{t('deposit:attr.price')}}" label-placement="column">
4
- <l-input
4
+ <!-- <l-input
5
5
  label="{{t('common::pay.symbol')}}"
6
6
  label-width="{{70}}"
7
7
  show-row="{{false}}"
@@ -9,7 +9,20 @@
9
9
  type="number"
10
10
  placeholder="一次最大充值{{depositMax}}元"
11
11
  bind:lininput="onDepPriceChangeMp"
12
- />
12
+ focus="{{focus}}"
13
+ /> -->
14
+ <view style="display:flex;margin-top:8rpx;">
15
+ <span style="margin-right:8rpx;">{{t('common::pay.symbol')}}</span>
16
+ <input
17
+ cursor-spacing="260"
18
+ value="{{priceStr || ''}}"
19
+ type="digit"
20
+ placeholder="一次最大充值{{depositMax}}元"
21
+ bindinput="onDepPriceChangeMp"
22
+ focus="{{focus}}"
23
+ style="flex:1"
24
+ />
25
+ </view>
13
26
  </l-form-item>
14
27
  <block wx:if="{{price > 0}}">
15
28
  <l-form-item label="{{t('pay:attr.entity')}}" label-placement="column">
@@ -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'),
@@ -6,7 +6,7 @@ export default function render(props) {
6
6
  const { wpAccount, wechatPay, oakFullpath, systemId } = props.data;
7
7
  const { t, update } = props.methods;
8
8
  if (wpAccount) {
9
- return (<Form labelCol={{ span: 8 }} wrapperCol={{ span: 12 }} layout="horizontal" style={{ minWidth: 860 }}>
9
+ return (<Form labelCol={{ span: 6 }} wrapperCol={{ span: 16 }} layout="horizontal" style={{ minWidth: 860 }}>
10
10
  {(!wechatPay || wechatPay.$$createAt$$ === 1) && <WechatPayUpsert oakPath={`${oakFullpath}.wechatPay`} systemId={systemId} key="wpCreate"/>}
11
11
  {(wpAccount.wechatPayId && wpAccount?.$$createAt$$ !== 1) && <WechatPayUpsert oakPath={`${oakFullpath}.wechatPay`} systemId={systemId} key="wpUpdate" oakId={wpAccount.wechatPayId}/>}
12
12
  <Form.Item label="说明">
@@ -84,6 +84,9 @@ const attrUpdateMatrix = {
84
84
  },
85
85
  reason: {
86
86
  actions: ['fail'],
87
+ },
88
+ successAt: {
89
+ actions: ['succeed'],
87
90
  }
88
91
  },
89
92
  withdraw: {
@@ -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,
@@ -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<1 | 0>;
9
+ export declare function updateWithdrawState(context: BRC, id: string): Promise<0 | 1>;
10
10
  declare const triggers: Trigger<EntityDict, 'withdraw', BRC>[];
11
11
  export default triggers;
package/es/utils/pay.d.ts CHANGED
@@ -3,5 +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>;
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;
@@ -83,3 +83,119 @@ export async function payNotify(context, body, payId, headers) {
83
83
  }
84
84
  return;
85
85
  }
86
+ export async function refundNotify(context, body, refundId, headers) {
87
+ const [refund] = await context.select('refund', {
88
+ data: {
89
+ id: 1,
90
+ price: 1,
91
+ iState: 1,
92
+ pay: {
93
+ entity: 1,
94
+ entityId: 1,
95
+ }
96
+ },
97
+ filter: {
98
+ id: refundId,
99
+ }
100
+ }, {});
101
+ if (!refund) {
102
+ console.error('接受到无效的refundId', refundId);
103
+ return;
104
+ }
105
+ const { entity, entityId } = refund.pay;
106
+ const payClazz = await getPayClazz(entity, entityId, context);
107
+ const { refundId: refundId2, extra, iState, } = await payClazz.decodeRefundNotification({ headers }, body);
108
+ assert(refundId2 === refundId);
109
+ if (iState !== refund.iState) {
110
+ let action = 'succeed';
111
+ const updateData = {};
112
+ switch (iState) {
113
+ case 'successful': {
114
+ // action = 'close';
115
+ break;
116
+ }
117
+ case 'failed': {
118
+ action = 'fail';
119
+ break;
120
+ }
121
+ default: {
122
+ assert(false);
123
+ }
124
+ }
125
+ if (extra) {
126
+ Object.assign(updateData, extra);
127
+ }
128
+ await context.operate('refund', {
129
+ id: await generateNewIdAsync(),
130
+ action,
131
+ data: updateData,
132
+ filter: {
133
+ id: refundId,
134
+ }
135
+ }, {});
136
+ }
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
+ }
@@ -83,7 +83,7 @@ export default class WechatPay extends WechatPayDebug {
83
83
  ABNORMAL: 退款异常
84
84
  */
85
85
  const iState = REFUND_STATE_MATRIX[status];
86
- return [iState, success_time ? { successAt: dayJs(success_time).millisecond() } : undefined];
86
+ return [iState, success_time ? { successAt: dayJs(success_time).valueOf() } : undefined];
87
87
  }
88
88
  analyzeResult(result) {
89
89
  const { success, data, } = result;
@@ -189,10 +189,10 @@ export default class WechatPay extends WechatPayDebug {
189
189
  const updateData = {
190
190
  meta: {
191
191
  getState: omit(result, ['mchid', 'appid', 'out_trade_no']),
192
- }
192
+ },
193
193
  };
194
194
  if (iState === 'paid') {
195
- updateData.forbidRefundAt = dayJs(success_time).millisecond();
195
+ updateData.successAt = dayJs(success_time).valueOf();
196
196
  }
197
197
  return [iState, updateData];
198
198
  }
@@ -308,7 +308,7 @@ export default class WechatPay extends WechatPayDebug {
308
308
  meta: omit(result, ['mchid',]),
309
309
  };
310
310
  if (iState === 'paid') {
311
- extra.successAt = success_time;
311
+ extra.successAt = dayJs(success_time).valueOf();
312
312
  }
313
313
  return {
314
314
  payId,
@@ -361,7 +361,7 @@ export default class WechatPay extends WechatPayDebug {
361
361
  meta: omit(result, ['mchid',]),
362
362
  };
363
363
  if (iState === 'successful') {
364
- extra.successAt = success_time;
364
+ extra.successAt = dayJs(success_time).valueOf();
365
365
  }
366
366
  return {
367
367
  refundId,
@@ -1,7 +1,4 @@
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
3
  const QUERY_PAYING_STATE_GAP = process.env.NODE_ENV === 'production' ? 3600 * 1000 : 60 * 1000;
7
4
  const watchers = [
@@ -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
  }
@@ -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 = [];
@@ -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,6 +86,9 @@ const attrUpdateMatrix = {
86
86
  },
87
87
  reason: {
88
88
  actions: ['fail'],
89
+ },
90
+ successAt: {
91
+ actions: ['succeed'],
89
92
  }
90
93
  },
91
94
  withdraw: {
@@ -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,
@@ -3,5 +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>;
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.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;
@@ -88,3 +88,121 @@ async function payNotify(context, body, payId, headers) {
88
88
  return;
89
89
  }
90
90
  exports.payNotify = payNotify;
91
+ async function refundNotify(context, body, refundId, headers) {
92
+ const [refund] = await context.select('refund', {
93
+ data: {
94
+ id: 1,
95
+ price: 1,
96
+ iState: 1,
97
+ pay: {
98
+ entity: 1,
99
+ entityId: 1,
100
+ }
101
+ },
102
+ filter: {
103
+ id: refundId,
104
+ }
105
+ }, {});
106
+ if (!refund) {
107
+ console.error('接受到无效的refundId', refundId);
108
+ return;
109
+ }
110
+ const { entity, entityId } = refund.pay;
111
+ const payClazz = await (0, payClazz_1.getPayClazz)(entity, entityId, context);
112
+ const { refundId: refundId2, extra, iState, } = await payClazz.decodeRefundNotification({ headers }, body);
113
+ (0, assert_1.default)(refundId2 === refundId);
114
+ if (iState !== refund.iState) {
115
+ let action = 'succeed';
116
+ const updateData = {};
117
+ switch (iState) {
118
+ case 'successful': {
119
+ // action = 'close';
120
+ break;
121
+ }
122
+ case 'failed': {
123
+ action = 'fail';
124
+ break;
125
+ }
126
+ default: {
127
+ (0, assert_1.default)(false);
128
+ }
129
+ }
130
+ if (extra) {
131
+ Object.assign(updateData, extra);
132
+ }
133
+ await context.operate('refund', {
134
+ id: await (0, uuid_1.generateNewIdAsync)(),
135
+ action,
136
+ data: updateData,
137
+ filter: {
138
+ id: refundId,
139
+ }
140
+ }, {});
141
+ }
142
+ }
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;
@@ -86,7 +86,7 @@ class WechatPay extends WechatPay_debug_1.default {
86
86
  ABNORMAL: 退款异常
87
87
  */
88
88
  const iState = REFUND_STATE_MATRIX[status];
89
- return [iState, success_time ? { successAt: (0, dayjs_1.default)(success_time).millisecond() } : undefined];
89
+ return [iState, success_time ? { successAt: (0, dayjs_1.default)(success_time).valueOf() } : undefined];
90
90
  }
91
91
  analyzeResult(result) {
92
92
  const { success, data, } = result;
@@ -192,10 +192,10 @@ class WechatPay extends WechatPay_debug_1.default {
192
192
  const updateData = {
193
193
  meta: {
194
194
  getState: (0, lodash_1.omit)(result, ['mchid', 'appid', 'out_trade_no']),
195
- }
195
+ },
196
196
  };
197
197
  if (iState === 'paid') {
198
- updateData.forbidRefundAt = (0, dayjs_1.default)(success_time).millisecond();
198
+ updateData.successAt = (0, dayjs_1.default)(success_time).valueOf();
199
199
  }
200
200
  return [iState, updateData];
201
201
  }
@@ -311,7 +311,7 @@ class WechatPay extends WechatPay_debug_1.default {
311
311
  meta: (0, lodash_1.omit)(result, ['mchid',]),
312
312
  };
313
313
  if (iState === 'paid') {
314
- extra.successAt = success_time;
314
+ extra.successAt = (0, dayjs_1.default)(success_time).valueOf();
315
315
  }
316
316
  return {
317
317
  payId,
@@ -364,7 +364,7 @@ class WechatPay extends WechatPay_debug_1.default {
364
364
  meta: (0, lodash_1.omit)(result, ['mchid',]),
365
365
  };
366
366
  if (iState === 'successful') {
367
- extra.successAt = success_time;
367
+ extra.successAt = (0, dayjs_1.default)(success_time).valueOf();
368
368
  }
369
369
  return {
370
370
  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,10 +1,6 @@
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
5
  const QUERY_PAYING_STATE_GAP = process.env.NODE_ENV === 'production' ? 3600 * 1000 : 60 * 1000;
10
6
  const watchers = [
@@ -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
  }
@@ -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.6",
3
+ "version": "2.2.8",
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.9",
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
  },