oak-pay-business 4.1.0 → 4.1.1

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.
@@ -1,6 +1,6 @@
1
1
  import { OakInputIllegalException } from 'oak-domain/es/types';
2
2
  import { pipeline } from 'oak-domain/es/utils/executor';
3
- import { decimalEq, decimalGt, decimalLt, requireDecimalString, } from 'oak-domain/es/utils/decimal';
3
+ import { decimalEq, decimalGt, decimalLt, decimalLte, requireDecimalString, } from 'oak-domain/es/utils/decimal';
4
4
  import { calcDepositLoss } from '../utils/loss';
5
5
  function throwInvalidDeposit(attrs, message) {
6
6
  throw new OakInputIllegalException('deposit', attrs, message);
@@ -19,6 +19,11 @@ function checkDeposit(data, payConfig) {
19
19
  if (decimalLt(price, '0')) {
20
20
  throwInvalidDeposit(['price'], '充值金额不能小于零');
21
21
  }
22
+ const depositMinCent = payConfig?.depositMinCent;
23
+ if (depositMinCent !== undefined
24
+ && decimalLte(price, String(depositMinCent))) {
25
+ throwInvalidDeposit(['price'], `充值金额必须大于最低充值金额${depositMinCent}分`);
26
+ }
22
27
  const expectedLoss = calcDepositLoss(price, payConfig?.depositLoss).value;
23
28
  const submittedLoss = data.loss;
24
29
  if (submittedLoss !== undefined
@@ -29,6 +29,11 @@ export function normalizeAndCheckPayConfig(payConfig) {
29
29
  }
30
30
  payConfig.defaultCurrency = currency || undefined;
31
31
  }
32
+ if (payConfig.depositMinCent !== undefined
33
+ && (!Number.isSafeInteger(payConfig.depositMinCent)
34
+ || payConfig.depositMinCent < 0)) {
35
+ throwInvalidPayConfig('最低充值金额必须是非负整数');
36
+ }
32
37
  const { depositLoss, withdrawLoss } = payConfig;
33
38
  if (depositLoss) {
34
39
  checkLimit(depositLoss.ratio, '充值手续费比例', { max: 20 });
@@ -73,6 +73,10 @@
73
73
  gap: 20rpx;
74
74
  }
75
75
 
76
+ .metric-grid-single {
77
+ grid-template-columns: 1fr;
78
+ }
79
+
76
80
  .metric-item {
77
81
  min-width: 0;
78
82
  display: flex;
@@ -12,12 +12,12 @@
12
12
  <view class="balance-label">{{t('avail')}}{{t('yuan')}}</view>
13
13
  <view class="balance-value" style="color:{{priceColor}}">{{t('common::pay.symbol')}} {{availStr}}</view>
14
14
  </view>
15
- <view class="metric-grid">
15
+ <view class="metric-grid {{!showLoanAmount ? 'metric-grid-single' : ''}}">
16
16
  <view class="metric-item">
17
17
  <view class="metric-label">{{t('total')}}{{t('yuan')}}</view>
18
18
  <view class="metric-value" style="color:{{priceColor}}">{{t('common::pay.symbol')}} {{totalStr}}</view>
19
19
  </view>
20
- <view class="metric-item">
20
+ <view class="metric-item" wx:if="{{showLoanAmount}}">
21
21
  <view class="metric-label">{{t('loan')}}{{t('yuan')}}</view>
22
22
  <view class="metric-value" style="color:{{priceColor}}">{{t('common::pay.symbol')}} {{loanStr}}</view>
23
23
  </view>
@@ -26,14 +26,14 @@
26
26
  <block wx:if="{{payRefreshing}}">
27
27
  <l-loading show="{{payRefreshing}}" type="circle" opacity="0.5">
28
28
  <view class="action-bar">
29
- <view class="item">
29
+ <view class="item" wx:if="{{showWithdrawAction}}">
30
30
  <l-button
31
31
  size="long"
32
32
  bind:lintap="onDepositClick"
33
33
  bgColor="{{textColor}}"
34
34
  l-class="my-button"
35
35
  >
36
- {{t('account:action.deposit')}}
36
+ {{depositActionText || t('account:action.deposit')}}
37
37
  </l-button>
38
38
  </view>
39
39
  <view class="item">
@@ -51,14 +51,14 @@
51
51
  </block>
52
52
  <block wx:else>
53
53
  <view class="action-bar">
54
- <view class="item">
54
+ <view class="item" wx:if="{{showWithdrawAction}}">
55
55
  <l-button
56
56
  size="long"
57
57
  bind:lintap="onDepositClick"
58
58
  bgColor="{{textColor}}"
59
59
  l-class="my-button"
60
60
  >
61
- {{t('account:action.deposit')}}
61
+ {{depositActionText || t('account:action.deposit')}}
62
62
  </l-button>
63
63
  </view>
64
64
  <view class="item">
@@ -74,6 +74,10 @@
74
74
  gap: 10px;
75
75
  }
76
76
 
77
+ .metricGridSingle {
78
+ grid-template-columns: 1fr;
79
+ }
80
+
77
81
  .metricItem {
78
82
  min-width: 0;
79
83
  display: flex;
@@ -100,16 +104,19 @@
100
104
 
101
105
  .actionBar {
102
106
  width: 100%;
103
- display: grid;
104
- grid-template-columns: repeat(2, minmax(0, 1fr));
107
+ display: flex;
108
+ flex-wrap: wrap;
105
109
  gap: 12px;
106
110
  }
107
111
 
108
- .item {
112
+ .item,
113
+ .actionBar > * {
114
+ flex: 1 1 120px;
109
115
  min-width: 0;
110
116
  }
111
117
 
112
118
  .actionBar :global(.adm-button) {
119
+ width: 100%;
113
120
  height: 44px;
114
121
  font-weight: 600;
115
122
  }
@@ -99,6 +99,7 @@ export default function Render(props: import("oak-frontend-base").WebComponentPr
99
99
  translationError?: import("oak-domain/es/types/DataType").Text | null;
100
100
  payConfig?: {
101
101
  defaultCurrency?: string;
102
+ depositMinCent?: number;
102
103
  withdrawLoss: {
103
104
  conservative: boolean;
104
105
  ratio?: number;
@@ -99,6 +99,7 @@ export default function Render(props: import("oak-frontend-base").WebComponentPr
99
99
  translationError?: import("oak-domain/es/types/DataType").Text | null;
100
100
  payConfig?: {
101
101
  defaultCurrency?: string;
102
+ depositMinCent?: number;
102
103
  withdrawLoss: {
103
104
  conservative: boolean;
104
105
  ratio?: number;
@@ -5,6 +5,9 @@
5
5
  "label": {
6
6
  "defaultCurrency": "默认货币",
7
7
  "currencyCode": "货币代码",
8
+ "depositLimit": "充值限制",
9
+ "depositMinCent": "最低充值金额",
10
+ "cent": "分",
8
11
  "depositLoss": "充值手续费",
9
12
  "withdrawLoss": "提现手续费",
10
13
  "conservative": "保守策略",
@@ -18,6 +21,7 @@
18
21
  },
19
22
  "help": {
20
23
  "defaultCurrency": "创建支付记录时若未指定货币,将使用此货币代码",
24
+ "depositMinCent": "充值金额必须严格大于此值;留空表示不限制",
21
25
  "depositLoss": "若不配置手续费,用户充值时渠道的手续费损失将被计算到system关联的账户之中",
22
26
  "withdrawLoss": "若配置为保守策略,系统将自动计算渠道的损耗,在system账户不贴现的前提下让用户可提现的额度最大化"
23
27
  }
@@ -25,6 +25,16 @@ function PayConfig(props) {
25
25
  const { payConfig, update, t } = props;
26
26
  const withdrawLoss = payConfig?.withdrawLoss;
27
27
  const depositLoss = payConfig?.depositLoss;
28
+ const updatePayConfig = (data) => {
29
+ update && update({
30
+ ...payConfig,
31
+ ...data,
32
+ depositLoss: depositLoss || {},
33
+ withdrawLoss: withdrawLoss || {
34
+ conservative: false,
35
+ },
36
+ });
37
+ };
28
38
  const updateDepositLoss = (data) => {
29
39
  update && update({
30
40
  ...payConfig,
@@ -52,13 +62,17 @@ function PayConfig(props) {
52
62
  <Card className={Styles.currencyCard} title={t('payConfig.label.defaultCurrency')}>
53
63
  <Form labelCol={{ span: 8 }} wrapperCol={{ span: 18 }}>
54
64
  <Form.Item label={t('payConfig.label.currencyCode')} extra={t('payConfig.help.defaultCurrency')}>
55
- <Input value={payConfig?.defaultCurrency ?? 'CNY'} maxLength={8} disabled={!update} onChange={({ target }) => update && update({
56
- ...payConfig,
65
+ <Input value={payConfig?.defaultCurrency ?? 'CNY'} maxLength={8} disabled={!update} onChange={({ target }) => updatePayConfig({
57
66
  defaultCurrency: target.value.toUpperCase(),
58
- depositLoss: depositLoss || {},
59
- withdrawLoss: withdrawLoss || {
60
- conservative: false,
61
- },
67
+ })}/>
68
+ </Form.Item>
69
+ </Form>
70
+ </Card>
71
+ <Card className={Styles.currencyCard} title={t('payConfig.label.depositLimit')}>
72
+ <Form labelCol={{ span: 8 }} wrapperCol={{ span: 18 }}>
73
+ <Form.Item label={t('payConfig.label.depositMinCent')} extra={t('payConfig.help.depositMinCent')}>
74
+ <InputNumber value={payConfig?.depositMinCent} min={0} step={1} precision={0} addonAfter={t('payConfig.label.cent')} disabled={!update} onChange={(value) => updatePayConfig({
75
+ depositMinCent: value === null ? undefined : value,
62
76
  })}/>
63
77
  </Form.Item>
64
78
  </Form>
@@ -87,6 +87,7 @@ export default function render(props: import("oak-frontend-base").WebComponentPr
87
87
  translationError?: import("oak-domain/es/types/DataType").Text | null;
88
88
  payConfig?: {
89
89
  defaultCurrency?: string;
90
+ depositMinCent?: number;
90
91
  withdrawLoss: {
91
92
  conservative: boolean;
92
93
  ratio?: number;
@@ -6,6 +6,7 @@ import { Schema as Account } from './Account';
6
6
  import { Schema as WithdrawAccount } from './WithdrawAccount';
7
7
  type PayConfig = {
8
8
  defaultCurrency?: string;
9
+ depositMinCent?: number;
9
10
  withdrawLoss: {
10
11
  conservative: boolean;
11
12
  ratio?: number;
@@ -8,6 +8,7 @@ import { Style } from "oak-general-business/es/types/Style";
8
8
  import { TranslationConfig } from "oak-general-business/es/types/Translation";
9
9
  type PayConfig = {
10
10
  defaultCurrency?: string;
11
+ depositMinCent?: number;
11
12
  withdrawLoss: {
12
13
  conservative: boolean;
13
14
  ratio?: number;
@@ -20,6 +20,7 @@ const triggers = [
20
20
  const [account] = await context.select('account', {
21
21
  data: {
22
22
  id: 1,
23
+ entity: 1,
23
24
  total: 1,
24
25
  avail: 1,
25
26
  refundable: 1,
@@ -51,7 +52,13 @@ const triggers = [
51
52
  const nextTotal = decimalAdd(account.total, totalPlus);
52
53
  const nextAvail = decimalAdd(account.avail, availPlus);
53
54
  const nextRefundable = decimalAdd(account.refundable, refundablePlus ?? '0');
54
- if (decimalLt(nextTotal, '0') || decimalLt(nextAvail, '0') || decimalLt(nextRefundable, '0')) {
55
+ const systemTax = account.entity === 'system' && type === 'tax';
56
+ const overdrawsTotal = decimalLt(totalPlus, '0') && decimalLt(nextTotal, '0');
57
+ const overdrawsAvail = decimalLt(availPlus, '0') && decimalLt(nextAvail, '0');
58
+ const overdrawsRefundable = typeof refundablePlus === 'string'
59
+ && decimalLt(refundablePlus, '0')
60
+ && decimalLt(nextRefundable, '0');
61
+ if (!systemTax && (overdrawsTotal || overdrawsAvail || overdrawsRefundable)) {
55
62
  throw new OakUserException('账户余额不足');
56
63
  }
57
64
  await context.operate('account', {
@@ -78,9 +78,8 @@ const triggers = [
78
78
  }
79
79
  }, {});
80
80
  cnt++;
81
- if (!decimalIsZero(tax)) {
82
- // tax产生的损失由sys account来承担
83
- const [account] = await context.select('account', {
81
+ if (decimalGt(loss, '0') || !decimalIsZero(tax)) {
82
+ const [systemAccount] = await context.select('account', {
84
83
  data: {
85
84
  id: 1,
86
85
  },
@@ -89,19 +88,40 @@ const triggers = [
89
88
  entityId: application.systemId,
90
89
  }
91
90
  }, { dontCollect: true });
92
- await context.operate('accountOper', {
93
- id: await generateNewIdAsync(),
94
- action: 'create',
95
- data: {
91
+ assert(systemAccount?.id);
92
+ // 充值手续费由用户承担时,先入账再扣渠道费,避免中间余额误判为负数。
93
+ if (decimalGt(loss, '0')) {
94
+ await context.operate('accountOper', {
96
95
  id: await generateNewIdAsync(),
97
- accountId: account.id,
98
- type: 'tax',
99
- totalPlus: decimalNeg(tax),
100
- availPlus: decimalNeg(tax),
101
- entity: 'pay',
102
- entityId: pay.id,
103
- },
104
- }, {});
96
+ action: 'create',
97
+ data: {
98
+ id: await generateNewIdAsync(),
99
+ totalPlus: loss,
100
+ availPlus: loss,
101
+ accountId: systemAccount.id,
102
+ type: 'earn',
103
+ entity: 'deposit',
104
+ entityId: deposit.id,
105
+ },
106
+ }, {});
107
+ cnt++;
108
+ }
109
+ if (!decimalIsZero(tax)) {
110
+ await context.operate('accountOper', {
111
+ id: await generateNewIdAsync(),
112
+ action: 'create',
113
+ data: {
114
+ id: await generateNewIdAsync(),
115
+ accountId: systemAccount.id,
116
+ type: 'tax',
117
+ totalPlus: decimalNeg(tax),
118
+ availPlus: decimalNeg(tax),
119
+ entity: 'pay',
120
+ entityId: pay.id,
121
+ },
122
+ }, {});
123
+ cnt++;
124
+ }
105
125
  }
106
126
  accountOpers.push({
107
127
  id: await generateNewIdAsync(),
@@ -115,30 +135,6 @@ const triggers = [
115
135
  },
116
136
  action: 'create',
117
137
  });
118
- if (decimalGt(loss, '0')) {
119
- // 如果有loss就充入system的account账户
120
- const [account] = await context.select('account', {
121
- data: {
122
- id: 1,
123
- },
124
- filter: {
125
- entity: 'system',
126
- entityId: application?.systemId,
127
- },
128
- }, { dontCollect: true });
129
- accountOpers.push({
130
- id: await generateNewIdAsync(),
131
- data: {
132
- id: await generateNewIdAsync(),
133
- totalPlus: loss,
134
- availPlus: loss,
135
- accountId: account.id,
136
- type: 'earn',
137
- },
138
- action: 'create',
139
- });
140
- cnt++;
141
- }
142
138
  }
143
139
  }
144
140
  if (accountOpers && accountOpers.length > 0) {
@@ -510,9 +510,11 @@ const triggers = [
510
510
  }
511
511
  return cnt;
512
512
  }
513
+ let tax = toDecimalString('0');
513
514
  if (entity !== 'account') {
514
515
  const clazz = await getPayClazz(entity, entityId, context);
515
- const [tax, sysAccountEntity, sysAccountEntityId] = clazz.calcPayTax(paid);
516
+ const [payTax, sysAccountEntity, sysAccountEntityId] = clazz.calcPayTax(paid);
517
+ tax = payTax;
516
518
  await context.operate('sysAccountOper', {
517
519
  id: await generateNewIdAsync(),
518
520
  action: 'create',
@@ -525,23 +527,43 @@ const triggers = [
525
527
  type: 'pay',
526
528
  }
527
529
  }, {});
528
- if (!decimalIsZero(tax)) {
529
- // tax产生的损失由sys account来承担
530
- const [account] = await context.select('account', {
530
+ }
531
+ const depositLoss = depositId ? deposit.loss : toDecimalString('0');
532
+ if (decimalGt(depositLoss, '0') || !decimalIsZero(tax)) {
533
+ const [systemAccount] = await context.select('account', {
534
+ data: {
535
+ id: 1,
536
+ },
537
+ filter: {
538
+ entity: 'system',
539
+ entityId: application.systemId,
540
+ },
541
+ }, { dontCollect: true });
542
+ assert(systemAccount?.id);
543
+ // 充值手续费由用户承担时,先入账再扣渠道费,避免中间余额误判为负数。
544
+ if (depositId && decimalGt(depositLoss, '0')) {
545
+ await context.operate('accountOper', {
546
+ id: await generateNewIdAsync(),
547
+ action: 'create',
531
548
  data: {
532
- id: 1,
549
+ id: await generateNewIdAsync(),
550
+ totalPlus: depositLoss,
551
+ availPlus: depositLoss,
552
+ accountId: systemAccount.id,
553
+ type: 'earn',
554
+ entity: 'deposit',
555
+ entityId: depositId,
533
556
  },
534
- filter: {
535
- entity: 'system',
536
- entityId: application.systemId,
537
- }
538
- }, { dontCollect: true });
557
+ }, {});
558
+ cnt++;
559
+ }
560
+ if (!decimalIsZero(tax)) {
539
561
  await context.operate('accountOper', {
540
562
  id: await generateNewIdAsync(),
541
563
  action: 'create',
542
564
  data: {
543
565
  id: await generateNewIdAsync(),
544
- accountId: account.id,
566
+ accountId: systemAccount.id,
545
567
  type: 'tax',
546
568
  totalPlus: decimalNeg(tax),
547
569
  availPlus: decimalNeg(tax),
@@ -570,30 +592,6 @@ const triggers = [
570
592
  action: 'create',
571
593
  }
572
594
  ];
573
- if (decimalGt(loss, '0')) {
574
- // 如果有loss就充入system的account账户
575
- const [account] = await context.select('account', {
576
- data: {
577
- id: 1,
578
- },
579
- filter: {
580
- entity: 'system',
581
- entityId: application?.systemId,
582
- },
583
- }, { dontCollect: true });
584
- accountOpers.push({
585
- id: await generateNewIdAsync(),
586
- data: {
587
- id: await generateNewIdAsync(),
588
- totalPlus: loss,
589
- availPlus: loss,
590
- accountId: account.id,
591
- type: 'earn',
592
- },
593
- action: 'create',
594
- });
595
- cnt++;
596
- }
597
595
  await context.operate('deposit', {
598
596
  id: await generateNewIdAsync(),
599
597
  action: 'succeed',
package/es/utils/pay.js CHANGED
@@ -42,7 +42,7 @@ export async function payNotify(context, body, payId, headers, req) {
42
42
  filter: {
43
43
  id: payId,
44
44
  }
45
- }, { blockTrigger: true });
45
+ }, { blockTrigger: true, forUpdate: true });
46
46
  if (!pay) {
47
47
  console.error('接受到无效的payId', payId);
48
48
  return;
@@ -21,6 +21,11 @@ function checkDeposit(data, payConfig) {
21
21
  if ((0, decimal_1.decimalLt)(price, '0')) {
22
22
  throwInvalidDeposit(['price'], '充值金额不能小于零');
23
23
  }
24
+ const depositMinCent = payConfig?.depositMinCent;
25
+ if (depositMinCent !== undefined
26
+ && (0, decimal_1.decimalLte)(price, String(depositMinCent))) {
27
+ throwInvalidDeposit(['price'], `充值金额必须大于最低充值金额${depositMinCent}分`);
28
+ }
24
29
  const expectedLoss = (0, loss_1.calcDepositLoss)(price, payConfig?.depositLoss).value;
25
30
  const submittedLoss = data.loss;
26
31
  if (submittedLoss !== undefined
@@ -32,6 +32,11 @@ function normalizeAndCheckPayConfig(payConfig) {
32
32
  }
33
33
  payConfig.defaultCurrency = currency || undefined;
34
34
  }
35
+ if (payConfig.depositMinCent !== undefined
36
+ && (!Number.isSafeInteger(payConfig.depositMinCent)
37
+ || payConfig.depositMinCent < 0)) {
38
+ throwInvalidPayConfig('最低充值金额必须是非负整数');
39
+ }
35
40
  const { depositLoss, withdrawLoss } = payConfig;
36
41
  if (depositLoss) {
37
42
  checkLimit(depositLoss.ratio, '充值手续费比例', { max: 20 });
@@ -6,6 +6,7 @@ import { Schema as Account } from './Account';
6
6
  import { Schema as WithdrawAccount } from './WithdrawAccount';
7
7
  type PayConfig = {
8
8
  defaultCurrency?: string;
9
+ depositMinCent?: number;
9
10
  withdrawLoss: {
10
11
  conservative: boolean;
11
12
  ratio?: number;
@@ -8,6 +8,7 @@ import { Style } from "oak-general-business/lib/types/Style";
8
8
  import { TranslationConfig } from "oak-general-business/lib/types/Translation";
9
9
  type PayConfig = {
10
10
  defaultCurrency?: string;
11
+ depositMinCent?: number;
11
12
  withdrawLoss: {
12
13
  conservative: boolean;
13
14
  ratio?: number;
@@ -23,6 +23,7 @@ const triggers = [
23
23
  const [account] = await context.select('account', {
24
24
  data: {
25
25
  id: 1,
26
+ entity: 1,
26
27
  total: 1,
27
28
  avail: 1,
28
29
  refundable: 1,
@@ -54,7 +55,13 @@ const triggers = [
54
55
  const nextTotal = (0, decimal_1.decimalAdd)(account.total, totalPlus);
55
56
  const nextAvail = (0, decimal_1.decimalAdd)(account.avail, availPlus);
56
57
  const nextRefundable = (0, decimal_1.decimalAdd)(account.refundable, refundablePlus ?? '0');
57
- if ((0, decimal_1.decimalLt)(nextTotal, '0') || (0, decimal_1.decimalLt)(nextAvail, '0') || (0, decimal_1.decimalLt)(nextRefundable, '0')) {
58
+ const systemTax = account.entity === 'system' && type === 'tax';
59
+ const overdrawsTotal = (0, decimal_1.decimalLt)(totalPlus, '0') && (0, decimal_1.decimalLt)(nextTotal, '0');
60
+ const overdrawsAvail = (0, decimal_1.decimalLt)(availPlus, '0') && (0, decimal_1.decimalLt)(nextAvail, '0');
61
+ const overdrawsRefundable = typeof refundablePlus === 'string'
62
+ && (0, decimal_1.decimalLt)(refundablePlus, '0')
63
+ && (0, decimal_1.decimalLt)(nextRefundable, '0');
64
+ if (!systemTax && (overdrawsTotal || overdrawsAvail || overdrawsRefundable)) {
58
65
  throw new types_1.OakUserException('账户余额不足');
59
66
  }
60
67
  await context.operate('account', {
@@ -81,9 +81,8 @@ const triggers = [
81
81
  }
82
82
  }, {});
83
83
  cnt++;
84
- if (!(0, decimal_1.decimalIsZero)(tax)) {
85
- // tax产生的损失由sys account来承担
86
- const [account] = await context.select('account', {
84
+ if ((0, decimal_1.decimalGt)(loss, '0') || !(0, decimal_1.decimalIsZero)(tax)) {
85
+ const [systemAccount] = await context.select('account', {
87
86
  data: {
88
87
  id: 1,
89
88
  },
@@ -92,19 +91,40 @@ const triggers = [
92
91
  entityId: application.systemId,
93
92
  }
94
93
  }, { dontCollect: true });
95
- await context.operate('accountOper', {
96
- id: await (0, uuid_1.generateNewIdAsync)(),
97
- action: 'create',
98
- data: {
94
+ (0, assert_1.default)(systemAccount?.id);
95
+ // 充值手续费由用户承担时,先入账再扣渠道费,避免中间余额误判为负数。
96
+ if ((0, decimal_1.decimalGt)(loss, '0')) {
97
+ await context.operate('accountOper', {
99
98
  id: await (0, uuid_1.generateNewIdAsync)(),
100
- accountId: account.id,
101
- type: 'tax',
102
- totalPlus: (0, decimal_1.decimalNeg)(tax),
103
- availPlus: (0, decimal_1.decimalNeg)(tax),
104
- entity: 'pay',
105
- entityId: pay.id,
106
- },
107
- }, {});
99
+ action: 'create',
100
+ data: {
101
+ id: await (0, uuid_1.generateNewIdAsync)(),
102
+ totalPlus: loss,
103
+ availPlus: loss,
104
+ accountId: systemAccount.id,
105
+ type: 'earn',
106
+ entity: 'deposit',
107
+ entityId: deposit.id,
108
+ },
109
+ }, {});
110
+ cnt++;
111
+ }
112
+ if (!(0, decimal_1.decimalIsZero)(tax)) {
113
+ await context.operate('accountOper', {
114
+ id: await (0, uuid_1.generateNewIdAsync)(),
115
+ action: 'create',
116
+ data: {
117
+ id: await (0, uuid_1.generateNewIdAsync)(),
118
+ accountId: systemAccount.id,
119
+ type: 'tax',
120
+ totalPlus: (0, decimal_1.decimalNeg)(tax),
121
+ availPlus: (0, decimal_1.decimalNeg)(tax),
122
+ entity: 'pay',
123
+ entityId: pay.id,
124
+ },
125
+ }, {});
126
+ cnt++;
127
+ }
108
128
  }
109
129
  accountOpers.push({
110
130
  id: await (0, uuid_1.generateNewIdAsync)(),
@@ -118,30 +138,6 @@ const triggers = [
118
138
  },
119
139
  action: 'create',
120
140
  });
121
- if ((0, decimal_1.decimalGt)(loss, '0')) {
122
- // 如果有loss就充入system的account账户
123
- const [account] = await context.select('account', {
124
- data: {
125
- id: 1,
126
- },
127
- filter: {
128
- entity: 'system',
129
- entityId: application?.systemId,
130
- },
131
- }, { dontCollect: true });
132
- accountOpers.push({
133
- id: await (0, uuid_1.generateNewIdAsync)(),
134
- data: {
135
- id: await (0, uuid_1.generateNewIdAsync)(),
136
- totalPlus: loss,
137
- availPlus: loss,
138
- accountId: account.id,
139
- type: 'earn',
140
- },
141
- action: 'create',
142
- });
143
- cnt++;
144
- }
145
141
  }
146
142
  }
147
143
  if (accountOpers && accountOpers.length > 0) {
@@ -513,9 +513,11 @@ const triggers = [
513
513
  }
514
514
  return cnt;
515
515
  }
516
+ let tax = (0, decimal_1.toDecimalString)('0');
516
517
  if (entity !== 'account') {
517
518
  const clazz = await (0, payClazz_1.getPayClazz)(entity, entityId, context);
518
- const [tax, sysAccountEntity, sysAccountEntityId] = clazz.calcPayTax(paid);
519
+ const [payTax, sysAccountEntity, sysAccountEntityId] = clazz.calcPayTax(paid);
520
+ tax = payTax;
519
521
  await context.operate('sysAccountOper', {
520
522
  id: await (0, uuid_1.generateNewIdAsync)(),
521
523
  action: 'create',
@@ -528,23 +530,43 @@ const triggers = [
528
530
  type: 'pay',
529
531
  }
530
532
  }, {});
531
- if (!(0, decimal_1.decimalIsZero)(tax)) {
532
- // tax产生的损失由sys account来承担
533
- const [account] = await context.select('account', {
533
+ }
534
+ const depositLoss = depositId ? deposit.loss : (0, decimal_1.toDecimalString)('0');
535
+ if ((0, decimal_1.decimalGt)(depositLoss, '0') || !(0, decimal_1.decimalIsZero)(tax)) {
536
+ const [systemAccount] = await context.select('account', {
537
+ data: {
538
+ id: 1,
539
+ },
540
+ filter: {
541
+ entity: 'system',
542
+ entityId: application.systemId,
543
+ },
544
+ }, { dontCollect: true });
545
+ (0, assert_1.default)(systemAccount?.id);
546
+ // 充值手续费由用户承担时,先入账再扣渠道费,避免中间余额误判为负数。
547
+ if (depositId && (0, decimal_1.decimalGt)(depositLoss, '0')) {
548
+ await context.operate('accountOper', {
549
+ id: await (0, uuid_1.generateNewIdAsync)(),
550
+ action: 'create',
534
551
  data: {
535
- id: 1,
552
+ id: await (0, uuid_1.generateNewIdAsync)(),
553
+ totalPlus: depositLoss,
554
+ availPlus: depositLoss,
555
+ accountId: systemAccount.id,
556
+ type: 'earn',
557
+ entity: 'deposit',
558
+ entityId: depositId,
536
559
  },
537
- filter: {
538
- entity: 'system',
539
- entityId: application.systemId,
540
- }
541
- }, { dontCollect: true });
560
+ }, {});
561
+ cnt++;
562
+ }
563
+ if (!(0, decimal_1.decimalIsZero)(tax)) {
542
564
  await context.operate('accountOper', {
543
565
  id: await (0, uuid_1.generateNewIdAsync)(),
544
566
  action: 'create',
545
567
  data: {
546
568
  id: await (0, uuid_1.generateNewIdAsync)(),
547
- accountId: account.id,
569
+ accountId: systemAccount.id,
548
570
  type: 'tax',
549
571
  totalPlus: (0, decimal_1.decimalNeg)(tax),
550
572
  availPlus: (0, decimal_1.decimalNeg)(tax),
@@ -573,30 +595,6 @@ const triggers = [
573
595
  action: 'create',
574
596
  }
575
597
  ];
576
- if ((0, decimal_1.decimalGt)(loss, '0')) {
577
- // 如果有loss就充入system的account账户
578
- const [account] = await context.select('account', {
579
- data: {
580
- id: 1,
581
- },
582
- filter: {
583
- entity: 'system',
584
- entityId: application?.systemId,
585
- },
586
- }, { dontCollect: true });
587
- accountOpers.push({
588
- id: await (0, uuid_1.generateNewIdAsync)(),
589
- data: {
590
- id: await (0, uuid_1.generateNewIdAsync)(),
591
- totalPlus: loss,
592
- availPlus: loss,
593
- accountId: account.id,
594
- type: 'earn',
595
- },
596
- action: 'create',
597
- });
598
- cnt++;
599
- }
600
598
  await context.operate('deposit', {
601
599
  id: await (0, uuid_1.generateNewIdAsync)(),
602
600
  action: 'succeed',
package/lib/utils/pay.js CHANGED
@@ -49,7 +49,7 @@ async function payNotify(context, body, payId, headers, req) {
49
49
  filter: {
50
50
  id: payId,
51
51
  }
52
- }, { blockTrigger: true });
52
+ }, { blockTrigger: true, forUpdate: true });
53
53
  if (!pay) {
54
54
  console.error('接受到无效的payId', payId);
55
55
  return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "oak-pay-business",
3
- "version": "4.1.0",
3
+ "version": "4.1.1",
4
4
  "description": "",
5
5
  "oak": {
6
6
  "package": true,
@@ -28,8 +28,6 @@
28
28
  ],
29
29
  "scripts": {
30
30
  "make:domain": "cross-env COMPLING_AS_LIB=yes oak-cli make:domain --atDomain",
31
- "make:dep": "oak-cli make:dependency",
32
- "postinstall": "npm run make:dep",
33
31
  "clean": "rimraf lib/* && rimraf es/*",
34
32
  "copy-js": "copyfiles -u 1 src/**/*.js es/ & copyfiles -u 1 src/**/*.js lib/",
35
33
  "copy-svg": "copyfiles -u 1 src/**/*.svg es/ & copyfiles -u 1 src/**/*.png es/",
@@ -43,6 +41,8 @@
43
41
  "build:analyze": "tsc -p tsconfig.es.json --diagnostics",
44
42
  "test": "node --test test/pay-default-currency.test.js",
45
43
  "test:account-transfer": "vitest run test/account-transfer.test.ts",
44
+ "test:deposit-minimum": "vitest run test/deposit-minimum.test.ts",
45
+ "test:deposit-settlement": "vitest run test/deposit-settlement.test.ts",
46
46
  "prepare": "rimraf node_modules/react & rimraf node_modules/react-native & rimraf node_modules/react-dom & rimraf node_modules/react-router & rimraf node_modules/react-router-dom"
47
47
  },
48
48
  "keywords": [],
@@ -53,7 +53,6 @@
53
53
  "bignumber.js": "^11.1.1",
54
54
  "classnames": "^2.3.1",
55
55
  "dayjs": "^1.11.5",
56
- "oak-general-business": "^6.1.0",
57
56
  "react-markdown": "^9.0.3",
58
57
  "stripe": "^19.3.1",
59
58
  "wechat-pay-nodejs": "^0.2.3"
@@ -69,6 +68,7 @@
69
68
  "react-router": ">=6.3.0",
70
69
  "react-router-dom": ">=6.3.0",
71
70
  "oak-domain": "^6.1.0",
71
+ "oak-general-business": "^6.1.1",
72
72
  "oak-external-sdk": "^3.0.2",
73
73
  "oak-frontend-base": "^6.1.0"
74
74
  },
@@ -13,6 +13,7 @@ import { Schema as WithdrawAccount } from './WithdrawAccount';
13
13
 
14
14
  type PayConfig = {
15
15
  defaultCurrency?: string;
16
+ depositMinCent?: number;
16
17
  withdrawLoss: {
17
18
  conservative: boolean;
18
19
  ratio?: number;