payment-kit 1.13.15

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.
Files changed (222) hide show
  1. package/.eslintrc.js +15 -0
  2. package/README.md +3 -0
  3. package/api/dev.ts +6 -0
  4. package/api/hooks/pre-start.js +12 -0
  5. package/api/src/hooks/pre-start.ts +21 -0
  6. package/api/src/index.ts +92 -0
  7. package/api/src/jobs/event.ts +72 -0
  8. package/api/src/jobs/invoice.ts +148 -0
  9. package/api/src/jobs/payment.ts +208 -0
  10. package/api/src/jobs/subscription.ts +301 -0
  11. package/api/src/jobs/webhook.ts +113 -0
  12. package/api/src/libs/audit.ts +73 -0
  13. package/api/src/libs/auth.ts +40 -0
  14. package/api/src/libs/chain/arcblock.ts +13 -0
  15. package/api/src/libs/dayjs.ts +17 -0
  16. package/api/src/libs/env.ts +5 -0
  17. package/api/src/libs/hooks.ts +42 -0
  18. package/api/src/libs/logger.ts +27 -0
  19. package/api/src/libs/middleware.ts +12 -0
  20. package/api/src/libs/payment.ts +53 -0
  21. package/api/src/libs/queue/index.ts +263 -0
  22. package/api/src/libs/queue/store.ts +47 -0
  23. package/api/src/libs/security.ts +95 -0
  24. package/api/src/libs/session.ts +164 -0
  25. package/api/src/libs/util.ts +93 -0
  26. package/api/src/locales/en.ts +3 -0
  27. package/api/src/locales/index.ts +37 -0
  28. package/api/src/locales/zh.ts +3 -0
  29. package/api/src/routes/checkout-sessions.ts +536 -0
  30. package/api/src/routes/connect/collect.ts +109 -0
  31. package/api/src/routes/connect/pay.ts +116 -0
  32. package/api/src/routes/connect/setup.ts +121 -0
  33. package/api/src/routes/connect/shared.ts +410 -0
  34. package/api/src/routes/connect/subscribe.ts +128 -0
  35. package/api/src/routes/customers.ts +70 -0
  36. package/api/src/routes/events.ts +76 -0
  37. package/api/src/routes/index.ts +59 -0
  38. package/api/src/routes/invoices.ts +126 -0
  39. package/api/src/routes/payment-currencies.ts +38 -0
  40. package/api/src/routes/payment-intents.ts +122 -0
  41. package/api/src/routes/payment-links.ts +221 -0
  42. package/api/src/routes/payment-methods.ts +39 -0
  43. package/api/src/routes/prices.ts +134 -0
  44. package/api/src/routes/products.ts +191 -0
  45. package/api/src/routes/settings.ts +33 -0
  46. package/api/src/routes/subscription-items.ts +148 -0
  47. package/api/src/routes/subscriptions.ts +254 -0
  48. package/api/src/routes/usage-records.ts +120 -0
  49. package/api/src/routes/webhook-attempts.ts +57 -0
  50. package/api/src/routes/webhook-endpoints.ts +105 -0
  51. package/api/src/store/migrate.ts +16 -0
  52. package/api/src/store/migrations/20230905-genesis.ts +52 -0
  53. package/api/src/store/migrations/20230911-seeding.ts +145 -0
  54. package/api/src/store/models/checkout-session.ts +395 -0
  55. package/api/src/store/models/coupon.ts +137 -0
  56. package/api/src/store/models/customer.ts +199 -0
  57. package/api/src/store/models/discount.ts +116 -0
  58. package/api/src/store/models/event.ts +111 -0
  59. package/api/src/store/models/index.ts +165 -0
  60. package/api/src/store/models/invoice-item.ts +185 -0
  61. package/api/src/store/models/invoice.ts +492 -0
  62. package/api/src/store/models/job.ts +75 -0
  63. package/api/src/store/models/payment-currency.ts +139 -0
  64. package/api/src/store/models/payment-intent.ts +282 -0
  65. package/api/src/store/models/payment-link.ts +219 -0
  66. package/api/src/store/models/payment-method.ts +169 -0
  67. package/api/src/store/models/price.ts +266 -0
  68. package/api/src/store/models/product.ts +162 -0
  69. package/api/src/store/models/promotion-code.ts +112 -0
  70. package/api/src/store/models/setup-intent.ts +206 -0
  71. package/api/src/store/models/subscription-item.ts +103 -0
  72. package/api/src/store/models/subscription-schedule.ts +157 -0
  73. package/api/src/store/models/subscription.ts +307 -0
  74. package/api/src/store/models/types.ts +406 -0
  75. package/api/src/store/models/usage-record.ts +132 -0
  76. package/api/src/store/models/webhook-attempt.ts +96 -0
  77. package/api/src/store/models/webhook-endpoint.ts +96 -0
  78. package/api/src/store/sequelize.ts +15 -0
  79. package/api/third.d.ts +28 -0
  80. package/blocklet.md +3 -0
  81. package/blocklet.yml +89 -0
  82. package/index.html +14 -0
  83. package/logo.png +0 -0
  84. package/package.json +133 -0
  85. package/public/.gitkeep +0 -0
  86. package/screenshots/.gitkeep +0 -0
  87. package/screenshots/1-subscription.png +0 -0
  88. package/screenshots/2-customer-1.png +0 -0
  89. package/screenshots/3-customer-2.png +0 -0
  90. package/screenshots/4-admin-3.png +0 -0
  91. package/screenshots/5-admin-4.png +0 -0
  92. package/scripts/build-clean.js +6 -0
  93. package/scripts/bump-version.mjs +35 -0
  94. package/src/app.tsx +68 -0
  95. package/src/components/actions.tsx +85 -0
  96. package/src/components/blockchain/tx.tsx +29 -0
  97. package/src/components/checkout/amount.tsx +24 -0
  98. package/src/components/checkout/error.tsx +30 -0
  99. package/src/components/checkout/footer.tsx +12 -0
  100. package/src/components/checkout/form/address.tsx +38 -0
  101. package/src/components/checkout/form/index.tsx +295 -0
  102. package/src/components/checkout/header.tsx +23 -0
  103. package/src/components/checkout/pay.tsx +222 -0
  104. package/src/components/checkout/product-card.tsx +56 -0
  105. package/src/components/checkout/product-item.tsx +37 -0
  106. package/src/components/checkout/skeleton/overview.tsx +21 -0
  107. package/src/components/checkout/skeleton/payment.tsx +35 -0
  108. package/src/components/checkout/success.tsx +183 -0
  109. package/src/components/checkout/summary.tsx +34 -0
  110. package/src/components/collapse.tsx +50 -0
  111. package/src/components/confirm.tsx +55 -0
  112. package/src/components/copyable.tsx +38 -0
  113. package/src/components/currency.tsx +15 -0
  114. package/src/components/customer/actions.tsx +73 -0
  115. package/src/components/data.tsx +20 -0
  116. package/src/components/drawer-form.tsx +77 -0
  117. package/src/components/error-fallback.tsx +7 -0
  118. package/src/components/error.tsx +39 -0
  119. package/src/components/event/list.tsx +217 -0
  120. package/src/components/info-card.tsx +40 -0
  121. package/src/components/info-metric.tsx +35 -0
  122. package/src/components/info-row.tsx +28 -0
  123. package/src/components/input.tsx +40 -0
  124. package/src/components/invoice/action.tsx +94 -0
  125. package/src/components/invoice/list.tsx +225 -0
  126. package/src/components/invoice/table.tsx +110 -0
  127. package/src/components/layout.tsx +70 -0
  128. package/src/components/livemode.tsx +23 -0
  129. package/src/components/metadata/editor.tsx +57 -0
  130. package/src/components/metadata/form.tsx +45 -0
  131. package/src/components/payment-intent/actions.tsx +81 -0
  132. package/src/components/payment-intent/list.tsx +204 -0
  133. package/src/components/payment-link/actions.tsx +114 -0
  134. package/src/components/payment-link/after-pay.tsx +87 -0
  135. package/src/components/payment-link/before-pay.tsx +175 -0
  136. package/src/components/payment-link/item.tsx +135 -0
  137. package/src/components/payment-link/product-select.tsx +66 -0
  138. package/src/components/payment-link/rename.tsx +64 -0
  139. package/src/components/portal/invoice/list.tsx +110 -0
  140. package/src/components/portal/subscription/cancel.tsx +83 -0
  141. package/src/components/portal/subscription/list.tsx +232 -0
  142. package/src/components/price/actions.tsx +21 -0
  143. package/src/components/price/form.tsx +292 -0
  144. package/src/components/product/actions.tsx +125 -0
  145. package/src/components/product/add-price.tsx +59 -0
  146. package/src/components/product/create.tsx +97 -0
  147. package/src/components/product/edit-price.tsx +75 -0
  148. package/src/components/product/edit.tsx +67 -0
  149. package/src/components/product/features.tsx +32 -0
  150. package/src/components/product/form.tsx +76 -0
  151. package/src/components/relative-time.tsx +41 -0
  152. package/src/components/section/header.tsx +29 -0
  153. package/src/components/status.tsx +12 -0
  154. package/src/components/subscription/actions/cancel.tsx +66 -0
  155. package/src/components/subscription/actions/index.tsx +172 -0
  156. package/src/components/subscription/actions/pause.tsx +83 -0
  157. package/src/components/subscription/items/actions.tsx +31 -0
  158. package/src/components/subscription/items/index.tsx +107 -0
  159. package/src/components/subscription/list.tsx +200 -0
  160. package/src/components/switch.tsx +48 -0
  161. package/src/components/table.tsx +66 -0
  162. package/src/components/uploader.tsx +81 -0
  163. package/src/components/webhook/attempts.tsx +149 -0
  164. package/src/contexts/products.tsx +42 -0
  165. package/src/contexts/session.ts +10 -0
  166. package/src/contexts/settings.tsx +54 -0
  167. package/src/env.d.ts +17 -0
  168. package/src/global.css +97 -0
  169. package/src/hooks/mobile.ts +15 -0
  170. package/src/index.tsx +6 -0
  171. package/src/libs/api.ts +19 -0
  172. package/src/libs/dayjs.ts +17 -0
  173. package/src/libs/util.ts +474 -0
  174. package/src/locales/en.tsx +395 -0
  175. package/src/locales/index.tsx +8 -0
  176. package/src/locales/zh.tsx +389 -0
  177. package/src/pages/admin/billing/index.tsx +56 -0
  178. package/src/pages/admin/billing/invoices/detail.tsx +215 -0
  179. package/src/pages/admin/billing/invoices/index.tsx +5 -0
  180. package/src/pages/admin/billing/subscriptions/detail.tsx +237 -0
  181. package/src/pages/admin/billing/subscriptions/index.tsx +5 -0
  182. package/src/pages/admin/customers/customers/detail.tsx +209 -0
  183. package/src/pages/admin/customers/customers/index.tsx +109 -0
  184. package/src/pages/admin/customers/index.tsx +47 -0
  185. package/src/pages/admin/developers/events/detail.tsx +77 -0
  186. package/src/pages/admin/developers/events/index.tsx +5 -0
  187. package/src/pages/admin/developers/index.tsx +60 -0
  188. package/src/pages/admin/developers/logs.tsx +3 -0
  189. package/src/pages/admin/developers/overview.tsx +3 -0
  190. package/src/pages/admin/developers/webhooks/detail.tsx +109 -0
  191. package/src/pages/admin/developers/webhooks/index.tsx +102 -0
  192. package/src/pages/admin/index.tsx +120 -0
  193. package/src/pages/admin/overview.tsx +3 -0
  194. package/src/pages/admin/payments/index.tsx +65 -0
  195. package/src/pages/admin/payments/intents/detail.tsx +205 -0
  196. package/src/pages/admin/payments/intents/index.tsx +5 -0
  197. package/src/pages/admin/payments/links/create.tsx +141 -0
  198. package/src/pages/admin/payments/links/detail.tsx +318 -0
  199. package/src/pages/admin/payments/links/index.tsx +167 -0
  200. package/src/pages/admin/products/coupons/index.tsx +3 -0
  201. package/src/pages/admin/products/index.tsx +81 -0
  202. package/src/pages/admin/products/prices/actions.tsx +151 -0
  203. package/src/pages/admin/products/prices/detail.tsx +203 -0
  204. package/src/pages/admin/products/prices/list.tsx +95 -0
  205. package/src/pages/admin/products/pricing-tables.tsx +3 -0
  206. package/src/pages/admin/products/products/create.tsx +105 -0
  207. package/src/pages/admin/products/products/detail.tsx +246 -0
  208. package/src/pages/admin/products/products/index.tsx +154 -0
  209. package/src/pages/admin/settings/branding.tsx +3 -0
  210. package/src/pages/admin/settings/business.tsx +3 -0
  211. package/src/pages/admin/settings/index.tsx +47 -0
  212. package/src/pages/admin/settings/payment-methods.tsx +80 -0
  213. package/src/pages/checkout/index.tsx +38 -0
  214. package/src/pages/checkout/pay.tsx +89 -0
  215. package/src/pages/customer/index.tsx +93 -0
  216. package/src/pages/customer/invoice.tsx +147 -0
  217. package/src/pages/home.tsx +9 -0
  218. package/tsconfig.api.json +9 -0
  219. package/tsconfig.eslint.json +7 -0
  220. package/tsconfig.json +99 -0
  221. package/tsconfig.types.json +11 -0
  222. package/vite.config.ts +19 -0
@@ -0,0 +1,109 @@
1
+ import type { Transaction, TransferV3Tx } from '@ocap/client';
2
+ import { fromAddress } from '@ocap/wallet';
3
+
4
+ import { invoiceQueue } from '../../jobs/invoice';
5
+ import { paymentQueue } from '../../jobs/payment';
6
+ import type { CallbackArgs } from '../../libs/auth';
7
+ import { wallet } from '../../libs/auth';
8
+ import { getClient } from '../../libs/chain/arcblock';
9
+ import dayjs from '../../libs/dayjs';
10
+ import { ensureInvoiceForCollect } from './shared';
11
+
12
+ // Used to collect an open invoice failed to collect automatically
13
+ // TODO: support multiple chain and multiple currency
14
+ export default {
15
+ action: 'collect',
16
+ onConnect: async ({ userDid, userPk, extraParams }: CallbackArgs) => {
17
+ const { invoiceId } = extraParams;
18
+ const { invoice, paymentIntent, paymentCurrency, paymentMethod } = await ensureInvoiceForCollect(invoiceId);
19
+
20
+ const tokens = [{ address: paymentCurrency.contract as string, value: invoice.amount_due }];
21
+ // @ts-ignore
22
+ const itx: TransferV3Tx = {
23
+ outputs: [{ owner: wallet.address, tokens, assets: [] }],
24
+ data: {
25
+ type: 'json',
26
+ // @ts-ignore
27
+ value: {
28
+ appId: wallet.address,
29
+ paymentIntentId: paymentIntent.id,
30
+ invoiceId,
31
+ },
32
+ },
33
+ };
34
+
35
+ if (paymentMethod.type === 'arcblock') {
36
+ return {
37
+ prepareTx: {
38
+ type: 'TransferV3Tx',
39
+ description: `Pay for invoice ${invoice.number}`,
40
+ partialTx: { from: userDid, pk: userPk, itx },
41
+ requirement: { tokens },
42
+ chainInfo: {
43
+ host: paymentMethod.settings?.arcblock?.api_host as string,
44
+ id: paymentMethod.settings?.arcblock?.chain_id as string,
45
+ },
46
+ },
47
+ };
48
+ }
49
+
50
+ throw new Error(`Payment method ${paymentMethod.type} not supported`);
51
+ },
52
+ onAuth: async ({ userDid, claims, request, extraParams }: CallbackArgs) => {
53
+ const { invoiceId } = extraParams;
54
+ const { invoice, paymentIntent, paymentMethod } = await ensureInvoiceForCollect(invoiceId);
55
+
56
+ if (paymentMethod.type === 'arcblock') {
57
+ await paymentIntent.update({ status: 'processing' });
58
+ const client = getClient(paymentMethod.settings.arcblock?.api_host as string);
59
+ const claim = claims.find((x) => x.type === 'prepareTx');
60
+
61
+ const tx: Partial<Transaction> = client.decodeTx(claim.finalTx);
62
+ if (claim.delegator && claim.from) {
63
+ tx.delegator = claim.delegator;
64
+ tx.from = claim.from;
65
+ }
66
+
67
+ const txHash = await client.sendTransferV3Tx(
68
+ // @ts-ignore
69
+ { tx, wallet: fromAddress(userDid) },
70
+ { headers: client.pickGasPayerHeaders(request) }
71
+ );
72
+
73
+ await paymentIntent.update({
74
+ status: 'succeeded',
75
+ amount_received: invoice.amount_due,
76
+ capture_method: 'manual',
77
+ payment_details: {
78
+ tx_hash: txHash,
79
+ payer: userDid,
80
+ },
81
+ });
82
+
83
+ await invoice.update({
84
+ paid: true,
85
+ status: 'paid',
86
+ amount_due: '0',
87
+ amount_paid: invoice.amount_due,
88
+ amount_remaining: '0',
89
+ attempt_count: invoice.attempt_count + 1,
90
+ status_transitions: { ...invoice.status_transitions, paid_at: dayjs().unix() },
91
+ collection_method: 'send_invoice',
92
+ });
93
+
94
+ // cleanup the queue
95
+ let exist = await paymentQueue.get(paymentIntent.id);
96
+ if (exist) {
97
+ await paymentQueue.cancel(paymentIntent.id);
98
+ }
99
+ exist = await invoiceQueue.get(invoice.id);
100
+ if (exist) {
101
+ await invoiceQueue.cancel(invoice.id);
102
+ }
103
+
104
+ return { hash: txHash };
105
+ }
106
+
107
+ throw new Error(`Payment method ${paymentMethod.type} not supported`);
108
+ },
109
+ };
@@ -0,0 +1,116 @@
1
+ import type { Transaction, TransferV3Tx } from '@ocap/client';
2
+ import { fromAddress } from '@ocap/wallet';
3
+
4
+ import type { CallbackArgs } from '../../libs/auth';
5
+ import { wallet } from '../../libs/auth';
6
+ import { getClient } from '../../libs/chain/arcblock';
7
+ import dayjs from '../../libs/dayjs';
8
+ import { ensureInvoiceForCheckout, ensurePaymentIntent } from './shared';
9
+
10
+ export default {
11
+ action: 'pay',
12
+ onConnect: async ({ userDid, userPk, extraParams }: CallbackArgs) => {
13
+ const { checkoutSessionId } = extraParams;
14
+ const { paymentIntent, paymentCurrency, paymentMethod } = await ensurePaymentIntent(checkoutSessionId, userDid);
15
+ if (!paymentIntent) {
16
+ throw new Error('Payment intent not found');
17
+ }
18
+
19
+ const tokens = [{ address: paymentCurrency.contract as string, value: paymentIntent.amount }];
20
+ // @ts-ignore
21
+ const itx: TransferV3Tx = {
22
+ outputs: [{ owner: wallet.address, tokens, assets: [] }],
23
+ data: {
24
+ type: 'json',
25
+ // @ts-ignore
26
+ value: {
27
+ appId: wallet.address,
28
+ paymentIntentId: paymentIntent.id,
29
+ checkoutSessionId,
30
+ },
31
+ },
32
+ };
33
+
34
+ // TODO: support multiple chain and multiple currency
35
+ if (paymentMethod.type === 'arcblock') {
36
+ return {
37
+ prepareTx: {
38
+ type: 'TransferV3Tx',
39
+ description: `Approve the transfer to complete checkout ${paymentIntent.id}`,
40
+ partialTx: { from: userDid, pk: userPk, itx },
41
+ requirement: { tokens },
42
+ chainInfo: {
43
+ host: paymentMethod.settings?.arcblock?.api_host as string,
44
+ id: paymentMethod.settings?.arcblock?.chain_id as string,
45
+ },
46
+ },
47
+ };
48
+ }
49
+
50
+ throw new Error(`Payment method ${paymentMethod.type} not supported`);
51
+ },
52
+ onAuth: async ({ userDid, claims, request, extraParams }: CallbackArgs) => {
53
+ const { checkoutSessionId } = extraParams;
54
+ const { checkoutSession, customer, paymentIntent, paymentMethod } = await ensurePaymentIntent(
55
+ checkoutSessionId,
56
+ userDid
57
+ );
58
+ if (!paymentIntent) {
59
+ throw new Error('Payment intent not found');
60
+ }
61
+
62
+ const { invoice } = await ensureInvoiceForCheckout({ checkoutSession, customer, paymentIntent });
63
+
64
+ if (paymentMethod.type === 'arcblock') {
65
+ await paymentIntent.update({ status: 'processing' });
66
+ const client = getClient(paymentMethod.settings.arcblock?.api_host as string);
67
+ const claim = claims.find((x) => x.type === 'prepareTx');
68
+
69
+ const tx: Partial<Transaction> = client.decodeTx(claim.finalTx);
70
+ if (claim.delegator && claim.from) {
71
+ tx.delegator = claim.delegator;
72
+ tx.from = claim.from;
73
+ }
74
+
75
+ const txHash = await client.sendTransferV3Tx(
76
+ // @ts-ignore
77
+ { tx, wallet: fromAddress(userDid) },
78
+ { headers: client.pickGasPayerHeaders(request) }
79
+ );
80
+
81
+ await checkoutSession.update({
82
+ status: 'complete',
83
+ payment_status: 'paid',
84
+ payment_details: {
85
+ tx_hash: txHash,
86
+ payer: userDid,
87
+ },
88
+ });
89
+ await paymentIntent.update({
90
+ status: 'succeeded',
91
+ amount_received: paymentIntent.amount,
92
+ payment_details: {
93
+ tx_hash: txHash,
94
+ payer: userDid,
95
+ },
96
+ });
97
+
98
+ if (invoice) {
99
+ await invoice.update({
100
+ paid: true,
101
+ status: 'paid',
102
+ amount_due: '0',
103
+ amount_paid: paymentIntent.amount,
104
+ amount_remaining: '0',
105
+ attempt_count: invoice.attempt_count + 1,
106
+ attempted: true,
107
+ status_transitions: { ...invoice.status_transitions, paid_at: dayjs().unix() },
108
+ });
109
+ }
110
+
111
+ return { hash: txHash };
112
+ }
113
+
114
+ throw new Error(`Payment method ${paymentMethod.type} not supported`);
115
+ },
116
+ };
@@ -0,0 +1,121 @@
1
+ import { toTypeInfo } from '@arcblock/did';
2
+ import { toDelegateAddress } from '@arcblock/did-util';
3
+ import type { Transaction } from '@ocap/client';
4
+ import { fromPublicKey } from '@ocap/wallet';
5
+
6
+ import { subscriptionQueue } from '../../jobs/subscription';
7
+ import type { CallbackArgs } from '../../libs/auth';
8
+ import { wallet } from '../../libs/auth';
9
+ import { getClient } from '../../libs/chain/arcblock';
10
+ import { ensureSetupIntent } from './shared';
11
+
12
+ export default {
13
+ action: 'setup',
14
+ onConnect: async ({ userDid, userPk, extraParams }: CallbackArgs) => {
15
+ const { checkoutSessionId } = extraParams;
16
+ const { paymentMethod, subscription } = await ensureSetupIntent(checkoutSessionId, userDid);
17
+ if (!subscription) {
18
+ throw new Error('Subscription for checkoutSession not found');
19
+ }
20
+
21
+ // TODO: support multiple chain and multiple currency
22
+ if (paymentMethod.type === 'arcblock') {
23
+ return {
24
+ signature: {
25
+ type: 'DelegateTx',
26
+ description: `Sign the delegation to setup subscription ${subscription.id}`,
27
+ wallet: fromPublicKey(userPk, toTypeInfo(userDid)),
28
+ data: {
29
+ itx: {
30
+ address: toDelegateAddress(userDid, wallet.address),
31
+ to: wallet.address,
32
+ ops: [{ typeUrl: 'fg:t:transfer_v2', rules: [] }],
33
+ data: {
34
+ type: 'json',
35
+ // @ts-ignore
36
+ value: {
37
+ appId: wallet.address,
38
+ subscriptionId: subscription.id,
39
+ checkoutSessionId,
40
+ },
41
+ },
42
+ },
43
+ },
44
+ chainInfo: {
45
+ host: paymentMethod.settings?.arcblock?.api_host as string,
46
+ id: paymentMethod.settings?.arcblock?.chain_id as string,
47
+ },
48
+ },
49
+ };
50
+ }
51
+
52
+ throw new Error(`Payment method ${paymentMethod.type} not supported`);
53
+ },
54
+ onAuth: async ({ userDid, userPk, claims, request, extraParams }: CallbackArgs) => {
55
+ const { checkoutSessionId } = extraParams;
56
+ const { setupIntent, checkoutSession, paymentMethod, subscription } = await ensureSetupIntent(
57
+ checkoutSessionId,
58
+ userDid
59
+ );
60
+
61
+ if (!subscription) {
62
+ throw new Error('Subscription for checkoutSession not found');
63
+ }
64
+
65
+ if (paymentMethod.type === 'arcblock') {
66
+ await subscription.update({
67
+ payment_settings: {
68
+ payment_method_types: ['arcblock'],
69
+ payment_method_options: {
70
+ arcblock: { payer: userDid },
71
+ },
72
+ },
73
+ });
74
+
75
+ const client = getClient(paymentMethod.settings.arcblock?.api_host as string);
76
+ const claim = claims.find((x) => x.type === 'signature');
77
+
78
+ // execute the delegate tx
79
+ const tx: Partial<Transaction> = client.decodeTx(claim.origin);
80
+ const txHash = await client.sendDelegateTx(
81
+ // @ts-ignore
82
+ { tx, wallet: fromPublicKey(userPk, toTypeInfo(userDid)), signature: claim.sig },
83
+ { headers: client.pickGasPayerHeaders(request) }
84
+ );
85
+
86
+ await setupIntent.update({
87
+ status: 'succeeded',
88
+ payment_method_types: ['arcblock'],
89
+ payment_method_options: {
90
+ arcblock: { payer: userDid },
91
+ },
92
+ setup_details: {
93
+ tx_hash: txHash,
94
+ payer: userDid,
95
+ },
96
+ });
97
+
98
+ await subscription.update({
99
+ status: subscription.trail_end ? 'trialing' : 'active',
100
+ payment_details: {
101
+ tx_hash: txHash,
102
+ payer: userDid,
103
+ },
104
+ });
105
+
106
+ await checkoutSession.update({ status: 'complete', payment_status: 'paid' });
107
+
108
+ // FIXME: for trialing subscriptions should we do this after 1st normal cycle
109
+ subscriptionQueue.push({
110
+ id: subscription.id,
111
+ job: { subscriptionId: subscription.id, action: 'cycle' },
112
+ // our next invoice should be generated at the end of current period, either trailing or normal
113
+ runAt: subscription.trail_end || subscription.current_period_end,
114
+ });
115
+
116
+ return { hash: txHash };
117
+ }
118
+
119
+ throw new Error(`Payment method ${paymentMethod.type} not supported`);
120
+ },
121
+ };