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,185 @@
1
+ /* eslint-disable @typescript-eslint/indent */
2
+ /* eslint-disable @typescript-eslint/lines-between-class-members */
3
+ import { CreationOptional, DataTypes, InferAttributes, InferCreationAttributes, Model } from 'sequelize';
4
+
5
+ import { createIdGenerator } from '../../libs/util';
6
+ import type { DiscountAmount } from './types';
7
+
8
+ const nextId = createIdGenerator('ii', 24);
9
+
10
+ export type InvoiceLineItem = {
11
+ id: string;
12
+ };
13
+
14
+ // @link https://stripe.com/docs/api/invoiceitems
15
+ // eslint-disable-next-line prettier/prettier
16
+ export class InvoiceItem extends Model<InferAttributes<InvoiceItem>, InferCreationAttributes<InvoiceItem>> {
17
+ declare id: CreationOptional<string>;
18
+ declare livemode: boolean;
19
+
20
+ declare amount: string;
21
+ declare quantity: number;
22
+ declare description: string;
23
+ declare period?: {
24
+ start: number;
25
+ end: number;
26
+ };
27
+
28
+ declare currency_id: string;
29
+ declare customer_id: string;
30
+ declare price_id: string;
31
+ declare invoice_id: string;
32
+ declare subscription_id?: string;
33
+ declare subscription_item_id?: string;
34
+ declare test_clock_id?: string;
35
+
36
+ declare discountable: boolean;
37
+ declare discount_amounts: DiscountAmount[];
38
+ declare discounts: string[];
39
+
40
+ declare proration: boolean;
41
+ declare proration_details: any;
42
+
43
+ declare metadata: Record<string, any>;
44
+
45
+ // TODO: following fields not supported
46
+ // tax_amounts: any;
47
+ // tax_rates: any;
48
+ // unit_amount_excluding_tax: string;
49
+
50
+ declare created_at: CreationOptional<Date>;
51
+ declare updated_at: CreationOptional<Date>;
52
+
53
+ public static readonly GENESIS_ATTRIBUTES = {
54
+ id: {
55
+ type: DataTypes.STRING(30),
56
+ primaryKey: true,
57
+ allowNull: false,
58
+ defaultValue: nextId,
59
+ },
60
+ livemode: {
61
+ type: DataTypes.BOOLEAN,
62
+ allowNull: false,
63
+ },
64
+ amount: {
65
+ type: DataTypes.STRING(32),
66
+ allowNull: false,
67
+ },
68
+ quantity: {
69
+ type: DataTypes.INTEGER,
70
+ allowNull: false,
71
+ },
72
+ description: {
73
+ type: DataTypes.STRING(256),
74
+ allowNull: false,
75
+ },
76
+ period: {
77
+ type: DataTypes.JSON,
78
+ allowNull: true,
79
+ },
80
+ customer_id: {
81
+ type: DataTypes.STRING(18),
82
+ allowNull: false,
83
+ },
84
+ currency_id: {
85
+ type: DataTypes.STRING(15),
86
+ allowNull: false,
87
+ },
88
+ price_id: {
89
+ type: DataTypes.STRING(30),
90
+ allowNull: false,
91
+ },
92
+ invoice_id: {
93
+ type: DataTypes.STRING(30),
94
+ allowNull: false,
95
+ },
96
+ subscription_id: {
97
+ type: DataTypes.STRING(30),
98
+ allowNull: true,
99
+ },
100
+ subscription_item_id: {
101
+ type: DataTypes.STRING(30),
102
+ allowNull: true,
103
+ },
104
+ discountable: {
105
+ type: DataTypes.BOOLEAN,
106
+ allowNull: false,
107
+ },
108
+ // Dynamically generated?
109
+ discount_amounts: {
110
+ type: DataTypes.JSON,
111
+ defaultValue: [],
112
+ },
113
+ discounts: {
114
+ type: DataTypes.JSON,
115
+ defaultValue: [],
116
+ },
117
+ proration: {
118
+ type: DataTypes.BOOLEAN,
119
+ defaultValue: false,
120
+ },
121
+ proration_details: {
122
+ type: DataTypes.JSON,
123
+ allowNull: true,
124
+ },
125
+ metadata: {
126
+ type: DataTypes.JSON,
127
+ defaultValue: {},
128
+ },
129
+ created_at: {
130
+ type: DataTypes.DATE,
131
+ defaultValue: DataTypes.NOW,
132
+ allowNull: false,
133
+ },
134
+ updated_at: {
135
+ type: DataTypes.DATE,
136
+ defaultValue: DataTypes.NOW,
137
+ allowNull: false,
138
+ },
139
+ };
140
+
141
+ public static initialize(sequelize: any) {
142
+ this.init(InvoiceItem.GENESIS_ATTRIBUTES, {
143
+ sequelize,
144
+ modelName: 'InvoiceItem',
145
+ tableName: 'invoice_items',
146
+ createdAt: 'created_at',
147
+ updatedAt: 'updated_at',
148
+ });
149
+ }
150
+
151
+ public static associate(models: any) {
152
+ this.hasOne(models.PaymentCurrency, {
153
+ sourceKey: 'currency_id',
154
+ foreignKey: 'id',
155
+ as: 'currency',
156
+ });
157
+ this.hasOne(models.Customer, {
158
+ sourceKey: 'customer_id',
159
+ foreignKey: 'id',
160
+ as: 'customer',
161
+ });
162
+ this.hasOne(models.Price, {
163
+ sourceKey: 'price_id',
164
+ foreignKey: 'id',
165
+ as: 'price',
166
+ });
167
+ this.hasOne(models.Invoice, {
168
+ sourceKey: 'invoice_id',
169
+ foreignKey: 'id',
170
+ as: 'invoice',
171
+ });
172
+ this.hasOne(models.Subscription, {
173
+ sourceKey: 'subscription_id',
174
+ foreignKey: 'id',
175
+ as: 'subscription',
176
+ });
177
+ this.hasOne(models.SubscriptionItem, {
178
+ sourceKey: 'subscription_item_id',
179
+ foreignKey: 'id',
180
+ as: 'subscriptionItem',
181
+ });
182
+ }
183
+ }
184
+
185
+ export type TInvoiceItem = InferAttributes<InvoiceItem>;
@@ -0,0 +1,492 @@
1
+ /* eslint-disable @typescript-eslint/indent */
2
+ /* eslint-disable @typescript-eslint/lines-between-class-members */
3
+ import { CreationOptional, DataTypes, InferAttributes, InferCreationAttributes, Model } from 'sequelize';
4
+ import type { LiteralUnion } from 'type-fest';
5
+
6
+ import { createEvent, createStatusEvent } from '../../libs/audit';
7
+ import { createIdGenerator } from '../../libs/util';
8
+ import type {
9
+ CustomerAddress,
10
+ CustomerShipping,
11
+ DiscountAmount,
12
+ PaymentError,
13
+ PaymentSettings,
14
+ SimpleCustomField,
15
+ } from './types';
16
+
17
+ const nextId = createIdGenerator('in', 24);
18
+
19
+ // eslint-disable-next-line prettier/prettier
20
+ export class Invoice extends Model<InferAttributes<Invoice>, InferCreationAttributes<Invoice>> {
21
+ declare id: CreationOptional<string>;
22
+
23
+ // A unique, identifying string that appears on emails sent to the customer for this invoice
24
+ declare number: string;
25
+
26
+ declare livemode: boolean;
27
+ declare description?: string;
28
+ declare statement_descriptor: string;
29
+ declare period_end: number;
30
+ declare period_start: number;
31
+
32
+ declare auto_advance: boolean;
33
+ // Whether payment was successfully collected for this invoice
34
+ declare paid: boolean;
35
+ // Returns true if the invoice was manually marked paid
36
+ declare paid_out_of_band: boolean;
37
+
38
+ declare status: LiteralUnion<'draft' | 'open' | 'void' | 'paid' | 'uncollectible', string>;
39
+ declare collection_method: LiteralUnion<'charge_automatically' | 'send_invoice', string>;
40
+ declare billing_reason: LiteralUnion<
41
+ | 'subscription_create'
42
+ | 'subscription_cycle'
43
+ | 'subscription_update'
44
+ | 'subscription_threshold'
45
+ | 'subscription'
46
+ | 'manual'
47
+ | 'upcoming',
48
+ string
49
+ >;
50
+
51
+ declare currency_id: string;
52
+ declare customer_id: string;
53
+ declare payment_intent_id?: string;
54
+ declare subscription_id?: string;
55
+ declare checkout_session_id?: string;
56
+ declare quote_id?: string;
57
+ declare charge_id?: string;
58
+
59
+ declare subscription_details?: any;
60
+ declare subscription_proration_date?: number;
61
+
62
+ declare subtotal: string;
63
+ declare subtotal_excluding_tax: string;
64
+ declare tax: string;
65
+ declare total: string;
66
+ declare amount_due: string;
67
+ declare amount_paid: string;
68
+ declare amount_remaining: string;
69
+ declare amount_shipping: string;
70
+
71
+ declare ending_balance: string;
72
+ declare starting_balance: string;
73
+
74
+ declare attempt_count: number;
75
+ declare attempted: boolean;
76
+ declare next_payment_attempt?: number;
77
+
78
+ declare custom_fields?: SimpleCustomField[];
79
+ declare customer_address?: CustomerAddress;
80
+ declare customer_email?: string;
81
+ declare customer_name?: string;
82
+ declare customer_phone?: string;
83
+ declare customer_shipping?: CustomerShipping;
84
+
85
+ declare discounts?: string[];
86
+ declare total_discount_amounts?: DiscountAmount[];
87
+
88
+ declare due_date?: number;
89
+ declare effective_at?: number;
90
+
91
+ declare footer?: string;
92
+
93
+ declare from_invoice?: any;
94
+
95
+ declare account_country?: string;
96
+ declare account_name?: string;
97
+ declare hosted_invoice_url?: string;
98
+
99
+ // The link to download the PDF for the invoice
100
+ declare invoice_pdf?: string;
101
+ declare last_finalization_error?: PaymentError;
102
+ declare latest_revision?: string;
103
+
104
+ declare payment_settings?: PaymentSettings;
105
+ declare default_payment_method_id: string;
106
+
107
+ declare post_payment_credit_notes_amount?: string;
108
+ declare pre_payment_credit_notes_amount?: string;
109
+
110
+ declare rendering_options?: any;
111
+
112
+ declare status_transitions?: {
113
+ finalized_at?: number;
114
+ marked_uncollectible_at?: number;
115
+ paid_at?: number;
116
+ voided_at?: number;
117
+ };
118
+
119
+ declare test_clock_id?: string;
120
+
121
+ declare threshold_reason?: any;
122
+
123
+ declare webhooks_delivered_at?: number;
124
+
125
+ // This is the transaction number that appears on email receipts sent for this invoice.
126
+ declare receipt_number?: string;
127
+
128
+ declare metadata?: Record<string, any>;
129
+
130
+ // TODO: following fields not supported
131
+ // account_tax_ids
132
+ // application
133
+ // application_fee_amount
134
+ // automatic_tax
135
+ // customer_tax_exempt
136
+ // customer_tax_ids
137
+ // default_tax_rates
138
+ // on_behalf_of
139
+ // shipping_cost
140
+ // shipping_details
141
+ // total_tax_amounts
142
+ // transfer_data
143
+
144
+ declare created_at: CreationOptional<Date>;
145
+ declare updated_at: CreationOptional<Date>;
146
+
147
+ public static readonly GENESIS_ATTRIBUTES = {
148
+ id: {
149
+ type: DataTypes.STRING(30),
150
+ primaryKey: true,
151
+ allowNull: false,
152
+ defaultValue: nextId,
153
+ },
154
+ livemode: {
155
+ type: DataTypes.BOOLEAN,
156
+ allowNull: false,
157
+ },
158
+ auto_advance: {
159
+ type: DataTypes.BOOLEAN,
160
+ allowNull: false,
161
+ },
162
+ account_country: {
163
+ type: DataTypes.STRING(64),
164
+ allowNull: true,
165
+ },
166
+ account_name: {
167
+ type: DataTypes.STRING(64),
168
+ allowNull: true,
169
+ },
170
+ charge_id: {
171
+ type: DataTypes.STRING(30),
172
+ allowNull: true,
173
+ },
174
+ collection_method: {
175
+ type: DataTypes.ENUM('charge_automatically', 'send_invoice'),
176
+ },
177
+ currency_id: {
178
+ type: DataTypes.STRING(15),
179
+ allowNull: false,
180
+ },
181
+ customer_id: {
182
+ type: DataTypes.STRING(18),
183
+ allowNull: false,
184
+ },
185
+ description: {
186
+ type: DataTypes.STRING(512),
187
+ allowNull: true,
188
+ },
189
+ hosted_invoice_url: {
190
+ type: DataTypes.STRING(512),
191
+ allowNull: true,
192
+ },
193
+ payment_intent_id: {
194
+ type: DataTypes.STRING(30),
195
+ allowNull: true,
196
+ },
197
+ period_end: {
198
+ type: DataTypes.INTEGER,
199
+ defaultValue: 0,
200
+ },
201
+ period_start: {
202
+ type: DataTypes.INTEGER,
203
+ defaultValue: 0,
204
+ },
205
+ status: {
206
+ type: DataTypes.ENUM('draft', 'open', 'void', 'paid', 'uncollectible'),
207
+ },
208
+ checkout_session_id: {
209
+ type: DataTypes.STRING(64),
210
+ allowNull: true,
211
+ },
212
+ subscription_id: {
213
+ type: DataTypes.STRING(30),
214
+ allowNull: true,
215
+ },
216
+ subscription_details: {
217
+ type: DataTypes.JSON,
218
+ defaultValue: {},
219
+ },
220
+ subscription_proration_date: {
221
+ type: DataTypes.INTEGER,
222
+ allowNull: true,
223
+ },
224
+ total: {
225
+ type: DataTypes.STRING(32),
226
+ allowNull: false,
227
+ },
228
+ subtotal: {
229
+ type: DataTypes.STRING(32),
230
+ allowNull: false,
231
+ },
232
+ subtotal_excluding_tax: {
233
+ type: DataTypes.STRING(32),
234
+ allowNull: false,
235
+ },
236
+ tax: {
237
+ type: DataTypes.STRING(32),
238
+ defaultValue: '0',
239
+ },
240
+ amount_due: {
241
+ type: DataTypes.STRING(32),
242
+ allowNull: false,
243
+ },
244
+ amount_paid: {
245
+ type: DataTypes.STRING(32),
246
+ defaultValue: '0',
247
+ },
248
+ amount_remaining: {
249
+ type: DataTypes.STRING(32),
250
+ allowNull: false,
251
+ },
252
+ amount_shipping: {
253
+ type: DataTypes.STRING(32),
254
+ defaultValue: '0',
255
+ },
256
+ attempt_count: {
257
+ type: DataTypes.INTEGER,
258
+ defaultValue: 0,
259
+ },
260
+ attempted: {
261
+ type: DataTypes.BOOLEAN,
262
+ defaultValue: false,
263
+ },
264
+ next_payment_attempt: {
265
+ type: DataTypes.INTEGER,
266
+ allowNull: true,
267
+ },
268
+ billing_reason: {
269
+ type: DataTypes.ENUM(
270
+ 'subscription_create',
271
+ 'subscription_cycle',
272
+ 'subscription_update',
273
+ 'subscription_threshold',
274
+ 'subscription',
275
+ 'manual',
276
+ 'upcoming'
277
+ ),
278
+ },
279
+ custom_fields: {
280
+ type: DataTypes.JSON,
281
+ defaultValue: [],
282
+ },
283
+ customer_address: {
284
+ type: DataTypes.JSON,
285
+ allowNull: true,
286
+ },
287
+ customer_name: {
288
+ type: DataTypes.STRING(64),
289
+ allowNull: true,
290
+ },
291
+ customer_email: {
292
+ type: DataTypes.STRING(128),
293
+ allowNull: true,
294
+ },
295
+ customer_phone: {
296
+ type: DataTypes.STRING(32),
297
+ allowNull: true,
298
+ },
299
+ customer_shipping: {
300
+ type: DataTypes.JSON,
301
+ allowNull: true,
302
+ },
303
+ default_payment_method_id: {
304
+ type: DataTypes.STRING(30),
305
+ allowNull: true,
306
+ },
307
+ discounts: {
308
+ type: DataTypes.JSON,
309
+ defaultValue: [],
310
+ },
311
+ due_date: {
312
+ type: DataTypes.INTEGER,
313
+ allowNull: true,
314
+ },
315
+ effective_at: {
316
+ type: DataTypes.INTEGER,
317
+ allowNull: true,
318
+ },
319
+ ending_balance: {
320
+ type: DataTypes.STRING(32),
321
+ defaultValue: '0',
322
+ },
323
+ footer: {
324
+ type: DataTypes.STRING(512),
325
+ allowNull: true,
326
+ },
327
+ from_invoice_id: {
328
+ type: DataTypes.STRING(30),
329
+ allowNull: true,
330
+ },
331
+ invoice_pdf: {
332
+ type: DataTypes.STRING(512),
333
+ allowNull: true,
334
+ },
335
+ last_finalization_error: {
336
+ type: DataTypes.JSON,
337
+ allowNull: true,
338
+ },
339
+ latest_revision: {
340
+ type: DataTypes.STRING(30),
341
+ allowNull: true,
342
+ },
343
+ number: {
344
+ type: DataTypes.STRING(64),
345
+ allowNull: true,
346
+ },
347
+ paid: {
348
+ type: DataTypes.BOOLEAN,
349
+ defaultValue: false,
350
+ },
351
+ paid_out_of_band: {
352
+ type: DataTypes.BOOLEAN,
353
+ defaultValue: false,
354
+ },
355
+ payment_settings: {
356
+ type: DataTypes.JSON,
357
+ allowNull: true,
358
+ },
359
+ post_payment_credit_notes_amount: {
360
+ type: DataTypes.STRING(32),
361
+ defaultValue: '0',
362
+ },
363
+ pre_payment_credit_notes_amount: {
364
+ type: DataTypes.STRING(32),
365
+ defaultValue: '0',
366
+ },
367
+ quote_id: {
368
+ type: DataTypes.STRING(30),
369
+ allowNull: true,
370
+ },
371
+ receipt_number: {
372
+ type: DataTypes.STRING(64),
373
+ allowNull: true,
374
+ },
375
+ days_until_due: {
376
+ type: DataTypes.NUMBER,
377
+ allowNull: true,
378
+ },
379
+ rendering_options: {
380
+ type: DataTypes.JSON,
381
+ allowNull: true,
382
+ },
383
+ starting_balance: {
384
+ type: DataTypes.STRING(32),
385
+ defaultValue: '0',
386
+ },
387
+ statement_descriptor: {
388
+ type: DataTypes.STRING(64),
389
+ allowNull: false,
390
+ },
391
+ status_transitions: {
392
+ type: DataTypes.JSON,
393
+ defaultValue: {},
394
+ },
395
+ test_clock_id: {
396
+ type: DataTypes.STRING(30),
397
+ allowNull: true,
398
+ },
399
+ threshold_reason: {
400
+ type: DataTypes.JSON,
401
+ allowNull: true,
402
+ },
403
+ total_discount_amounts: {
404
+ type: DataTypes.JSON,
405
+ defaultValue: [],
406
+ },
407
+ webhooks_delivered_at: {
408
+ type: DataTypes.INTEGER,
409
+ allowNull: true,
410
+ },
411
+ metadata: {
412
+ type: DataTypes.JSON,
413
+ allowNull: true,
414
+ },
415
+ created_at: {
416
+ type: DataTypes.DATE,
417
+ defaultValue: DataTypes.NOW,
418
+ allowNull: false,
419
+ },
420
+ updated_at: {
421
+ type: DataTypes.DATE,
422
+ defaultValue: DataTypes.NOW,
423
+ allowNull: false,
424
+ },
425
+ };
426
+
427
+ public static initialize(sequelize: any) {
428
+ this.init(Invoice.GENESIS_ATTRIBUTES, {
429
+ sequelize,
430
+ modelName: 'Invoice',
431
+ tableName: 'invoices',
432
+ createdAt: 'created_at',
433
+ updatedAt: 'updated_at',
434
+ hooks: {
435
+ afterCreate: (model: Invoice, options) =>
436
+ createEvent('Invoice', 'invoice.created', model, options).catch(console.error),
437
+ afterUpdate: (model: Invoice, options) => {
438
+ createEvent('Invoice', 'invoice.updated', model, options).catch(console.error);
439
+ createStatusEvent(
440
+ 'Invoice',
441
+ 'invoice',
442
+ { open: 'finalized', void: 'voided', paid: 'paid', uncollectible: 'marked_uncollectible' },
443
+ model,
444
+ options
445
+ ).catch(console.error);
446
+ },
447
+ afterDestroy: (model: Invoice, options) =>
448
+ createEvent('Invoice', 'invoice.deleted', model, options).catch(console.error),
449
+ },
450
+ });
451
+ }
452
+
453
+ public static associate(models: any) {
454
+ this.hasOne(models.PaymentCurrency, {
455
+ sourceKey: 'currency_id',
456
+ foreignKey: 'id',
457
+ as: 'paymentCurrency',
458
+ });
459
+ this.hasOne(models.PaymentMethod, {
460
+ sourceKey: 'default_payment_method_id',
461
+ foreignKey: 'id',
462
+ as: 'paymentMethod',
463
+ });
464
+ this.hasOne(models.Customer, {
465
+ sourceKey: 'customer_id',
466
+ foreignKey: 'id',
467
+ as: 'customer',
468
+ });
469
+ this.hasOne(models.PaymentIntent, {
470
+ sourceKey: 'payment_intent_id',
471
+ foreignKey: 'id',
472
+ as: 'paymentIntent',
473
+ });
474
+ this.hasOne(models.Subscription, {
475
+ sourceKey: 'subscription_id',
476
+ foreignKey: 'id',
477
+ as: 'subscription',
478
+ });
479
+ this.hasOne(models.SubscriptionItem, {
480
+ sourceKey: 'id',
481
+ foreignKey: 'subscription_id',
482
+ as: 'subscriptionItem',
483
+ });
484
+ this.hasMany(models.InvoiceItem, {
485
+ sourceKey: 'id',
486
+ foreignKey: 'invoice_id',
487
+ as: 'lines',
488
+ });
489
+ }
490
+ }
491
+
492
+ export type TInvoice = InferAttributes<Invoice>;