payment-kit 1.13.71 → 1.13.72

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.
@@ -6,7 +6,7 @@ import { events } from '../libs/event';
6
6
  import logger from '../libs/logger';
7
7
  import { getGasPayerExtra, isDelegationSufficientForPayment } from '../libs/payment';
8
8
  import createQueue from '../libs/queue';
9
- import { MAX_RETRY_COUNT, getNextRetry } from '../libs/util';
9
+ import { MAX_RETRY_COUNT, MIN_RETRY_MAIL, getNextRetry } from '../libs/util';
10
10
  import { CheckoutSession } from '../store/models/checkout-session';
11
11
  import { Invoice } from '../store/models/invoice';
12
12
  import { PaymentCurrency } from '../store/models/payment-currency';
@@ -66,13 +66,7 @@ export const handlePaymentSucceed = async (paymentIntent: PaymentIntent) => {
66
66
  }
67
67
  }
68
68
 
69
- const count: number = await Invoice.count({
70
- where: {
71
- subscription_id: invoice.subscription_id,
72
- status: 'paid',
73
- },
74
- });
75
- if (count >= 2) {
69
+ if (invoice.billing_reason === 'subscription_cycle') {
76
70
  events.emit('customer.subscription.renewed', subscription, invoice);
77
71
  }
78
72
  }
@@ -131,6 +125,7 @@ export const handlePayment = async (job: PaymentJob) => {
131
125
 
132
126
  // try payment capture and reschedule on error
133
127
  logger.info(`PaymentIntent capture attempt: ${paymentIntent.id}`);
128
+ let result;
134
129
  try {
135
130
  await paymentIntent.update({ status: 'processing', last_payment_error: null });
136
131
 
@@ -138,7 +133,7 @@ export const handlePayment = async (job: PaymentJob) => {
138
133
  const payer = paymentSettings?.payment_method_options.arcblock?.payer;
139
134
 
140
135
  // check balance before capture
141
- const result = await isDelegationSufficientForPayment({
136
+ result = await isDelegationSufficientForPayment({
142
137
  paymentMethod,
143
138
  paymentCurrency,
144
139
  userDid: payer as string,
@@ -146,11 +141,6 @@ export const handlePayment = async (job: PaymentJob) => {
146
141
  });
147
142
  if (result.sufficient === false) {
148
143
  logger.error('PaymentIntent capture aborted on preCheck', { id: paymentIntent.id, result });
149
- events.emit('customer.subscription.renew_failed', {
150
- invoice,
151
- result,
152
- });
153
- // FIXME: send email to customer, pause subscription
154
144
  throw new CustomError(result.reason, 'payer balance or delegation not sufficient for this payment');
155
145
  }
156
146
 
@@ -218,11 +208,19 @@ export const handlePayment = async (job: PaymentJob) => {
218
208
  });
219
209
  }
220
210
  } else if (invoice) {
211
+ const attemptCount = invoice.attempt_count + 1;
212
+ if (attemptCount >= MIN_RETRY_MAIL && invoice.billing_reason === 'subscription_cycle') {
213
+ events.emit('customer.subscription.renew_failed', {
214
+ invoice,
215
+ result: result || { sufficient: false, reason: 'TX_SEND_FAILED' },
216
+ });
217
+ }
218
+
221
219
  if (invoice.attempt_count > MAX_RETRY_COUNT) {
222
220
  await paymentIntent.update({ status: 'requires_action', last_payment_error: error });
223
221
  await invoice.update({
224
222
  status: 'uncollectible',
225
- attempt_count: invoice.attempt_count + 1,
223
+ attempt_count: attemptCount,
226
224
  attempted: true,
227
225
  status_transitions: { ...invoice.status_transitions, marked_uncollectible_at: dayjs().unix() },
228
226
  });
@@ -230,11 +228,11 @@ export const handlePayment = async (job: PaymentJob) => {
230
228
  // FIXME: send email to customer, pause subscription
231
229
  logger.error('PaymentIntent capture failed after max retry', { id: paymentIntent.id });
232
230
  } else {
233
- const retryAt = getNextRetry(invoice.attempt_count + 1);
231
+ const retryAt = getNextRetry(attemptCount);
234
232
 
235
233
  await paymentIntent.update({ status: 'requires_capture', last_payment_error: error });
236
234
  await invoice.update({
237
- attempt_count: invoice.attempt_count + 1,
235
+ attempt_count: attemptCount,
238
236
  attempted: true,
239
237
  next_payment_attempt: retryAt,
240
238
  });
@@ -10,6 +10,7 @@ import dayjs from './dayjs';
10
10
  export const OCAP_PAYMENT_TX_TYPE = 'fg:t:transfer_v2';
11
11
 
12
12
  export const MAX_RETRY_COUNT = 18; // 2^18 seconds ~~ 3 days, total retry time: 6 days
13
+ export const MIN_RETRY_MAIL = 13; // total retry time before sending first mail: 6 hours
13
14
  export const STRIPE_API_VERSION = '2023-08-16';
14
15
  export const STRIPE_ENDPOINT: string = getUrl('/api/integrations/stripe/webhook');
15
16
  export const STRIPE_EVENTS: any[] = [
@@ -77,6 +77,7 @@ export default flat({
77
77
  noEnoughToken:
78
78
  'Your account token balance is {balance}, not enough for {price}, please replenish your tokens and renew your account',
79
79
  noSupported: 'Token renewal is not supported, please check your subscription',
80
+ txSendFailed: 'Failed to send transaction when try to collect payment.',
80
81
  },
81
82
  },
82
83
  },
@@ -72,6 +72,7 @@ export default flat({
72
72
  noToken: '您的账户没有任何代币,请充值代币后重新续费',
73
73
  noEnoughToken: '您的账户代币余额为 {balance},不足 {price},请充值代币后重新续费',
74
74
  noSupported: '不支持使用代币续费,请检查您的套餐',
75
+ txSendFailed: '扣款交易发送失败',
75
76
  },
76
77
  },
77
78
  },
package/blocklet.yml CHANGED
@@ -14,7 +14,7 @@ repository:
14
14
  type: git
15
15
  url: git+https://github.com/blocklet/payment-kit.git
16
16
  specVersion: 1.2.8
17
- version: 1.13.71
17
+ version: 1.13.72
18
18
  logo: logo.png
19
19
  files:
20
20
  - dist
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "payment-kit",
3
- "version": "1.13.71",
3
+ "version": "1.13.72",
4
4
  "scripts": {
5
5
  "dev": "COMPONENT_STORE_URL=https://test.store.blocklet.dev blocklet dev",
6
6
  "eject": "vite eject",
@@ -106,7 +106,7 @@
106
106
  "@abtnode/types": "^1.16.19",
107
107
  "@arcblock/eslint-config": "^0.2.4",
108
108
  "@arcblock/eslint-config-ts": "^0.2.4",
109
- "@did-pay/types": "1.13.71",
109
+ "@did-pay/types": "1.13.72",
110
110
  "@types/cookie-parser": "^1.4.6",
111
111
  "@types/cors": "^2.8.17",
112
112
  "@types/dotenv-flow": "^3.3.3",
@@ -143,5 +143,5 @@
143
143
  "parser": "typescript"
144
144
  }
145
145
  },
146
- "gitHead": "790a7f2f0e18c1631bf018ca5481b01ec25996e3"
146
+ "gitHead": "bbd67e67e0e6154506b12d7ce30b314e895382c0"
147
147
  }