payment-kit 1.21.9 → 1.21.10

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.
@@ -4,14 +4,18 @@ import { events } from '../../libs/event';
4
4
  import { getLock } from '../../libs/lock';
5
5
  import logger from '../../libs/logger';
6
6
  import createQueue from '../../libs/queue';
7
+ import dayjs from '../../libs/dayjs';
7
8
  import { VendorFulfillmentService } from '../../libs/vendor-util/fulfillment';
8
9
  import { CheckoutSession } from '../../store/models/checkout-session';
9
10
  import { PaymentIntent } from '../../store/models/payment-intent';
10
11
  import { Price } from '../../store/models/price';
11
12
  import { Product } from '../../store/models/product';
12
13
  import { Refund } from '../../store/models/refund';
14
+ import { Subscription } from '../../store/models/subscription';
13
15
  import { sequelize } from '../../store/sequelize';
14
16
  import { depositVaultQueue } from '../payment';
17
+ import { addSubscriptionJob } from '../subscription';
18
+ import { SubscriptionWillCanceledSchedule } from '../../crons/subscription-will-canceled';
15
19
  import { Invoice } from '../../store/models';
16
20
 
17
21
  export type VendorInfo = NonNullable<CheckoutSession['vendor_info']>[number];
@@ -634,6 +638,11 @@ export async function initiateFullRefund(invoiceId: string, reason: string): Pro
634
638
  await checkoutSession.update({ fulfillment_status: 'cancelled' });
635
639
  await requestReturnsFromCompletedVendors(checkoutSession, reason);
636
640
 
641
+ // Cancel subscription if this refund is for a subscription
642
+ if (checkoutSession.subscription_id) {
643
+ await cancelSubscriptionForRefund(checkoutSession.subscription_id, reason);
644
+ }
645
+
637
646
  // Calculate remaining amount using the same logic as subscription createProration
638
647
  const refunds = await Refund.findAll({
639
648
  where: {
@@ -707,6 +716,66 @@ export async function initiateFullRefund(invoiceId: string, reason: string): Pro
707
716
  }
708
717
  }
709
718
 
719
+ /**
720
+ * Cancel subscription when full refund is initiated due to vendor fulfillment failure
721
+ */
722
+ async function cancelSubscriptionForRefund(subscriptionId: string, reason: string): Promise<void> {
723
+ try {
724
+ const subscription = await Subscription.findByPk(subscriptionId);
725
+ if (!subscription) {
726
+ logger.warn('Subscription not found for cancellation', { subscriptionId });
727
+ return;
728
+ }
729
+
730
+ // Check if subscription is already canceled
731
+ if (subscription.status === 'canceled') {
732
+ logger.info('Subscription already canceled, skipping', { subscriptionId });
733
+ return;
734
+ }
735
+
736
+ const now = dayjs().unix() + 3;
737
+ const haveStake = !!subscription.payment_details?.arcblock?.staking?.tx_hash;
738
+
739
+ // Prepare cancellation details
740
+ const updates: Partial<Subscription> = {
741
+ status: 'canceled',
742
+ cancel_at: now,
743
+ canceled_at: now,
744
+ cancelation_details: {
745
+ comment: `Canceled due to vendor fulfillment failure: ${reason}`,
746
+ reason: 'vendor_fulfillment_failed',
747
+ feedback: 'vendor_issue',
748
+ return_stake: haveStake, // Return stake when canceled due to vendor failure
749
+ slash_stake: false,
750
+ slash_reason: '',
751
+ },
752
+ };
753
+
754
+ // Update subscription
755
+ await subscription.update(updates);
756
+
757
+ // Schedule cancellation job
758
+ await addSubscriptionJob(subscription, 'cancel', true, updates.cancel_at);
759
+
760
+ // Update scheduled tasks
761
+ await new SubscriptionWillCanceledSchedule().reScheduleSubscriptionTasks([subscription]);
762
+
763
+ logger.info('Subscription canceled due to vendor fulfillment failure', {
764
+ subscriptionId: subscription.id,
765
+ customerId: subscription.customer_id,
766
+ reason,
767
+ cancelAt: subscription.cancel_at,
768
+ returnStake: haveStake,
769
+ });
770
+ } catch (error: any) {
771
+ logger.error('Failed to cancel subscription for refund', {
772
+ subscriptionId,
773
+ reason,
774
+ error,
775
+ });
776
+ }
777
+ }
778
+
710
779
  async function requestReturnsFromCompletedVendors(checkoutSession: CheckoutSession, reason: string): Promise<void> {
711
780
  logger.info('Starting return request process', {
712
781
  checkoutSessionId: checkoutSession.id,
@@ -1329,6 +1329,49 @@ router.get('/retrieve/:id', user, async (req, res) => {
1329
1329
  });
1330
1330
  });
1331
1331
 
1332
+ // for checkout page
1333
+ router.get('/broker-status/:id', user, async (req, res) => {
1334
+ const { needShortUrl = false } = req.query;
1335
+ const doc = await CheckoutSession.findByPk(req.params.id);
1336
+
1337
+ if (!doc) {
1338
+ res.json({
1339
+ checkoutSession: {},
1340
+ paymentLink: null,
1341
+ });
1342
+ return;
1343
+ }
1344
+
1345
+ // @ts-ignore
1346
+ doc.line_items = await Price.expand(doc.line_items, { upsell: true });
1347
+
1348
+ const hasVendorConfig = doc.line_items?.some((item: any) => !!item?.price?.product?.vendor_config?.length);
1349
+
1350
+ if (!hasVendorConfig || doc.payment_status === 'unpaid' || doc.fulfillment_status === 'cancelled') {
1351
+ res.json({
1352
+ checkoutSession: {},
1353
+ paymentLink: null,
1354
+ });
1355
+ return;
1356
+ }
1357
+
1358
+ const paymentUrl = getUrl(`/checkout/pay/${doc.id}`);
1359
+ const paymentLink = needShortUrl
1360
+ ? await formatToShortUrl({
1361
+ url: paymentUrl,
1362
+ validUntil: dayjs().add(20, 'minutes').format('YYYY-MM-DDTHH:mm:ss+00:00'),
1363
+ maxVisits: 5,
1364
+ })
1365
+ : paymentUrl;
1366
+
1367
+ res.json({
1368
+ checkoutSession: {
1369
+ ...doc.toJSON(),
1370
+ },
1371
+ paymentLink,
1372
+ });
1373
+ });
1374
+
1332
1375
  async function checkVendorConfig(items: TLineItemExpanded[]) {
1333
1376
  const lineItems = await Price.expand(items, { upsell: true });
1334
1377
  return lineItems?.some((item: TLineItemExpanded) => !!item?.price?.product?.vendor_config?.length);
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.21.9
17
+ version: 1.21.10
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.21.9",
3
+ "version": "1.21.10",
4
4
  "scripts": {
5
5
  "dev": "blocklet dev --open",
6
6
  "lint": "tsc --noEmit && eslint src api/src --ext .mjs,.js,.jsx,.ts,.tsx",
@@ -56,9 +56,9 @@
56
56
  "@blocklet/error": "^0.2.5",
57
57
  "@blocklet/js-sdk": "^1.16.52",
58
58
  "@blocklet/logger": "^1.16.52",
59
- "@blocklet/payment-broker-client": "1.21.9",
60
- "@blocklet/payment-react": "1.21.9",
61
- "@blocklet/payment-vendor": "1.21.9",
59
+ "@blocklet/payment-broker-client": "1.21.10",
60
+ "@blocklet/payment-react": "1.21.10",
61
+ "@blocklet/payment-vendor": "1.21.10",
62
62
  "@blocklet/sdk": "^1.16.52",
63
63
  "@blocklet/ui-react": "^3.1.46",
64
64
  "@blocklet/uploader": "^0.2.13",
@@ -128,7 +128,7 @@
128
128
  "devDependencies": {
129
129
  "@abtnode/types": "^1.16.52",
130
130
  "@arcblock/eslint-config-ts": "^0.3.3",
131
- "@blocklet/payment-types": "1.21.9",
131
+ "@blocklet/payment-types": "1.21.10",
132
132
  "@types/cookie-parser": "^1.4.9",
133
133
  "@types/cors": "^2.8.19",
134
134
  "@types/debug": "^4.1.12",
@@ -175,5 +175,5 @@
175
175
  "parser": "typescript"
176
176
  }
177
177
  },
178
- "gitHead": "dd41fc0eab97528eeb2f696e73073518ef360043"
178
+ "gitHead": "a7288f2742e4bb2622505b79dd539616a6a59878"
179
179
  }
@@ -82,7 +82,7 @@ export default function VendorServiceList({
82
82
  {vendor.name || vendor.vendor_key}
83
83
  </Typography>
84
84
  </Stack>
85
- {isLauncher && (
85
+ {isLauncher && !isCanceled && (
86
86
  <Box>
87
87
  <Stack direction="row" spacing={0.5}>
88
88
  <Tooltip title={t('admin.subscription.serviceHome')} placement="top">