payment-kit 1.13.134 → 1.13.136

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.
@@ -49,7 +49,7 @@ import { Product } from '../store/models/product';
49
49
  import { SetupIntent } from '../store/models/setup-intent';
50
50
  import { Subscription } from '../store/models/subscription';
51
51
  import { SubscriptionItem } from '../store/models/subscription-item';
52
- import { ensureInvoiceForCheckout } from './connect/shared';
52
+ import { ensureInvoiceForCheckout, ensureSetupIntent } from './connect/shared';
53
53
 
54
54
  const getInvoicePrefix = createCodeGenerator('', 8);
55
55
 
@@ -774,6 +774,10 @@ router.put('/:id/submit', user, ensureCheckoutSessionOpen, async (req, res) => {
774
774
 
775
775
  if (checkoutSession.mode === 'setup' && setupIntent && subscription) {
776
776
  if (delegation.sufficient) {
777
+ const { invoice } = await ensureSetupIntent(checkoutSession.id, customer.did);
778
+ if (invoice) {
779
+ await invoiceQueue.pushAndWait({ id: invoice.id, job: { invoiceId: invoice.id, retryOnError: false } });
780
+ }
777
781
  await setupIntent.update({ status: 'succeeded', ...paymentSettings });
778
782
  await subscription.update({
779
783
  status: subscription.trail_end ? 'trialing' : 'active',
@@ -184,7 +184,7 @@ export async function ensureSetupIntent(checkoutSessionId: string, userDid?: str
184
184
  }
185
185
  const { user } = await blocklet.getUser(userDid, { enableConnectedAccount: true });
186
186
  if (customer.did !== user.did) {
187
- throw new Error('This is not your payment intent');
187
+ throw new Error('This is not your setup intent');
188
188
  }
189
189
  }
190
190
 
@@ -205,6 +205,48 @@ export async function ensureSetupIntent(checkoutSessionId: string, userDid?: str
205
205
 
206
206
  checkoutSession.line_items = await Price.expand(checkoutSession.line_items);
207
207
 
208
+ let invoice;
209
+ if (subscription) {
210
+ // generate empty invoice here
211
+ const result = await ensureInvoiceAndItems({
212
+ customer: customer as Customer,
213
+ currency: paymentCurrency,
214
+ subscription,
215
+ lineItems: [],
216
+ trailing: !!checkoutSession.subscription_data?.trial_period_days,
217
+ metered: false,
218
+ props: {
219
+ livemode: checkoutSession.livemode,
220
+ description: checkoutSession.invoice_creation?.invoice_data?.description || 'Subscription creation',
221
+ statement_descriptor: getStatementDescriptor(checkoutSession.line_items),
222
+ period_start: subscription.current_period_start,
223
+ period_end: subscription.current_period_end,
224
+
225
+ auto_advance: true,
226
+ billing_reason: 'subscription_create',
227
+
228
+ currency_id: checkoutSession.currency_id,
229
+ payment_intent_id: '',
230
+ checkout_session_id: checkoutSession.id,
231
+
232
+ total: '0',
233
+
234
+ default_payment_method_id: subscription.default_payment_method_id as string,
235
+
236
+ custom_fields: checkoutSession.invoice_creation?.invoice_data?.custom_fields || [],
237
+ footer: checkoutSession.invoice_creation?.invoice_data?.footer || '',
238
+ metadata: checkoutSession.invoice_creation?.invoice_data?.metadata || {},
239
+ } as Invoice,
240
+ });
241
+ invoice = result.invoice;
242
+
243
+ logger.info(`Invoice created for checkoutSession ${checkoutSession.id} in setup mode: ${invoice.id}`);
244
+
245
+ // persist invoice id
246
+ await checkoutSession.update({ invoice_id: invoice.id });
247
+ await subscription.update({ latest_invoice_id: invoice.id });
248
+ }
249
+
208
250
  return {
209
251
  checkoutSession,
210
252
  setupIntent: setupIntent as SetupIntent,
@@ -212,6 +254,7 @@ export async function ensureSetupIntent(checkoutSessionId: string, userDid?: str
212
254
  subscription,
213
255
  paymentMethod,
214
256
  paymentCurrency,
257
+ invoice,
215
258
  };
216
259
  }
217
260
 
@@ -320,8 +363,8 @@ export async function ensureInvoiceAndItems({
320
363
  lineItems: TLineItemExpanded[];
321
364
  trailing: boolean; // do we have trailing
322
365
  metered: boolean; // is the quantity metered
323
- applyCredit?: boolean; // should we apply customer credit?
324
- }): Promise<{ invoice: Invoice; items: InvoiceItem[] }> {
366
+ applyCredit?: boolean; // should we apply customer credit?
367
+ }): Promise<{ invoice: Invoice; items: InvoiceItem[] }> {
325
368
  // apply possible balance to invoice
326
369
  let remaining = props.total;
327
370
  let result = { starting: {}, ending: {} };
@@ -221,7 +221,13 @@ router.put('/:id/cancel', authPortal, async (req, res) => {
221
221
  }
222
222
 
223
223
  // update cancel at
224
- const updates: Partial<Subscription> = {};
224
+ const updates: Partial<Subscription> = {
225
+ cancelation_details: {
226
+ reason: 'payment_disputed',
227
+ feedback: 'other',
228
+ comment: `Requested by ${req.user?.role}:${req.user?.did}`,
229
+ },
230
+ };
225
231
  if (req.user?.via === 'portal') {
226
232
  updates.cancel_at_period_end = true;
227
233
  updates.cancel_at = subscription.current_period_end;
@@ -293,6 +299,8 @@ router.put('/:id/cancel', authPortal, async (req, res) => {
293
299
  ending_token_balance: {},
294
300
  metadata: {
295
301
  requested_by: req.user?.did,
302
+ unused_period_start: refund === 'last' ? subscription.current_period_start : updates.cancel_at,
303
+ unused_period_end: subscription.current_period_end,
296
304
  },
297
305
  });
298
306
  logger.info('subscription cancel refund done', {
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.134
17
+ version: 1.13.136
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.134",
3
+ "version": "1.13.136",
4
4
  "scripts": {
5
5
  "dev": "cross-env COMPONENT_STORE_URL=https://test.store.blocklet.dev blocklet dev",
6
6
  "eject": "vite eject",
@@ -50,7 +50,7 @@
50
50
  "@arcblock/jwt": "^1.18.108",
51
51
  "@arcblock/ux": "^2.9.23",
52
52
  "@blocklet/logger": "1.16.23-beta-aeb9f5bd",
53
- "@blocklet/payment-react": "1.13.134",
53
+ "@blocklet/payment-react": "1.13.136",
54
54
  "@blocklet/sdk": "1.16.23-beta-aeb9f5bd",
55
55
  "@blocklet/ui-react": "^2.9.23",
56
56
  "@blocklet/uploader": "^0.0.69",
@@ -110,7 +110,7 @@
110
110
  "devDependencies": {
111
111
  "@abtnode/types": "1.16.23-beta-aeb9f5bd",
112
112
  "@arcblock/eslint-config-ts": "^0.2.4",
113
- "@blocklet/payment-types": "1.13.134",
113
+ "@blocklet/payment-types": "1.13.136",
114
114
  "@types/cookie-parser": "^1.4.6",
115
115
  "@types/cors": "^2.8.17",
116
116
  "@types/dotenv-flow": "^3.3.3",
@@ -149,5 +149,5 @@
149
149
  "parser": "typescript"
150
150
  }
151
151
  },
152
- "gitHead": "d4a11b7d69d309107ad6085827b61a7808e21f02"
152
+ "gitHead": "ab601e3842610993e41ac2951acaf040a0d345b6"
153
153
  }