lunixi-sdk 1.2.2 → 2.0.2

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 (72) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +62 -0
  3. data/README.md +278 -10
  4. data/README.tr.md +282 -8
  5. data/docs/ruby.en.md +11 -1
  6. data/docs/ruby.tr.md +11 -1
  7. data/lib/lunixi/bank_connect.rb +340 -0
  8. data/lib/lunixi/bank_connect_filters.rb +114 -0
  9. data/lib/lunixi/bank_connect_values.rb +205 -0
  10. data/lib/lunixi/bank_transfer.rb +106 -0
  11. data/lib/lunixi/card_requests.rb +171 -0
  12. data/lib/lunixi/client.rb +57 -16
  13. data/lib/lunixi/direct_method_payment_request.rb +245 -0
  14. data/lib/lunixi/enabled_installments.rb +28 -0
  15. data/lib/lunixi/errors.rb +69 -3
  16. data/lib/lunixi/fraud.rb +81 -5
  17. data/lib/lunixi/identity.rb +94 -0
  18. data/lib/lunixi/identity_values.rb +28 -0
  19. data/lib/lunixi/insurance.rb +294 -0
  20. data/lib/lunixi/insurance_request.rb +134 -0
  21. data/lib/lunixi/insurance_values.rb +200 -0
  22. data/lib/lunixi/kyc.rb +189 -0
  23. data/lib/lunixi/kyc_values.rb +158 -0
  24. data/lib/lunixi/marketplace.rb +268 -0
  25. data/lib/lunixi/marketplace_values.rb +86 -0
  26. data/lib/lunixi/method_fields.rb +142 -0
  27. data/lib/lunixi/money.rb +7 -4
  28. data/lib/lunixi/order_details.rb +115 -0
  29. data/lib/lunixi/payment_link_filters.rb +32 -0
  30. data/lib/lunixi/payment_link_image_rules.rb +85 -0
  31. data/lib/lunixi/payment_link_rules.rb +132 -0
  32. data/lib/lunixi/payment_link_values.rb +136 -0
  33. data/lib/lunixi/payment_links.rb +152 -0
  34. data/lib/lunixi/payment_values.rb +86 -0
  35. data/lib/lunixi/payments.rb +170 -8
  36. data/lib/lunixi/physical_pos.rb +183 -0
  37. data/lib/lunixi/physical_pos_card_data.rb +117 -0
  38. data/lib/lunixi/physical_pos_card_number.rb +65 -0
  39. data/lib/lunixi/physical_pos_device_identity.rb +44 -0
  40. data/lib/lunixi/physical_pos_filters.rb +58 -0
  41. data/lib/lunixi/physical_pos_gateway.rb +153 -0
  42. data/lib/lunixi/physical_pos_identifiers.rb +62 -0
  43. data/lib/lunixi/physical_pos_management.rb +326 -0
  44. data/lib/lunixi/physical_pos_money.rb +46 -0
  45. data/lib/lunixi/physical_pos_request.rb +126 -0
  46. data/lib/lunixi/physical_pos_responses.rb +102 -0
  47. data/lib/lunixi/physical_pos_results.rb +52 -0
  48. data/lib/lunixi/physical_pos_settlement_policies.rb +71 -0
  49. data/lib/lunixi/physical_pos_value.rb +43 -0
  50. data/lib/lunixi/physical_pos_values.rb +203 -0
  51. data/lib/lunixi/physical_pos_webhook.rb +147 -0
  52. data/lib/lunixi/product_webhooks.rb +106 -0
  53. data/lib/lunixi/promissory_note.rb +155 -0
  54. data/lib/lunixi/reference.rb +41 -0
  55. data/lib/lunixi/route_request.rb +121 -0
  56. data/lib/lunixi/route_response.rb +101 -0
  57. data/lib/lunixi/route_table.rb +128 -0
  58. data/lib/lunixi/route_value.rb +60 -0
  59. data/lib/lunixi/sanction.rb +244 -0
  60. data/lib/lunixi/sanction_values.rb +132 -0
  61. data/lib/lunixi/split_payment.rb +150 -0
  62. data/lib/lunixi/subscription_resources.rb +357 -0
  63. data/lib/lunixi/subscription_values.rb +233 -0
  64. data/lib/lunixi/subscriptions.rb +202 -0
  65. data/lib/lunixi/version.rb +1 -1
  66. data/lib/lunixi/wallet.rb +153 -0
  67. data/lib/lunixi/wallet_resources.rb +407 -0
  68. data/lib/lunixi/wallet_routes.rb +126 -0
  69. data/lib/lunixi/wallet_values.rb +71 -0
  70. data/lib/lunixi/wire_money.rb +67 -0
  71. data/lib/lunixi.rb +58 -0
  72. metadata +59 -1
@@ -0,0 +1,357 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lunixi
4
+ # subscriptions.plans: the templates a subscription is created against.
5
+ class SubscriptionPlans
6
+ def initialize(routes)
7
+ @routes = routes
8
+ end
9
+
10
+ # { productId, code, name, price, currency, billing, description?, trialPeriodDays?,
11
+ # maxDunningAttempts?, dunningScheduleDays?, gracePeriodDays?, metadata? }. price is a minor-unit
12
+ # string ("0" for a free plan); billing is { frequency, interval?, byMonthDay?, byWeekDay?, timezone?,
13
+ # lastDayOfMonthFix?, count? } (interval defaults to 1).
14
+ def create(request, idempotency_key: nil)
15
+ @routes.data('plans.create', body: SubscriptionRules.plan!(request), idempotency_key: idempotency_key)
16
+ end
17
+
18
+ def get(plan_id)
19
+ @routes.data('plans.get', params: { 'planId' => plan_id })
20
+ end
21
+
22
+ # Filters: productId, includeArchived, pageSize, pageToken.
23
+ def list(**filters)
24
+ @routes.token_page('plans.list', query: filters)
25
+ end
26
+
27
+ def iterate(**filters, &block)
28
+ RouteResponse.each_item(filters, block, :pageToken) { |query| list(**query) }
29
+ end
30
+
31
+ # Price, billing, metadata ...; a price is a minor-unit string.
32
+ def update(plan_id, changes)
33
+ @routes.data('plans.update', params: { 'planId' => plan_id }, body: changes)
34
+ end
35
+
36
+ def archive(plan_id)
37
+ @routes.data('plans.archive', params: { 'planId' => plan_id })
38
+ end
39
+
40
+ def translations(plan_id)
41
+ @routes.data('plans.translations', params: { 'planId' => plan_id })
42
+ end
43
+
44
+ def upsert_translation(plan_id, locale, translation)
45
+ @routes.data('plans.upsertTranslation', params: { 'planId' => plan_id, 'locale' => locale }, body: translation)
46
+ end
47
+ end
48
+
49
+ # subscriptions.customers, upserted by your own externalId (one stable customer per user).
50
+ class SubscriptionCustomers
51
+ def initialize(routes)
52
+ @routes = routes
53
+ end
54
+
55
+ # { externalId, name, surname, email, gsmNumber, identityNumber, billingAddress, billingCity,
56
+ # billingCountry, billingZipCode, billingContactName?, shipping* ? }. Renewals take the buyer from
57
+ # this record; a missing field raises ConfigurationError before sending.
58
+ def upsert(request, idempotency_key: nil)
59
+ @routes.data('customers.upsert', body: request, idempotency_key: idempotency_key)
60
+ end
61
+
62
+ def get(customer_id)
63
+ @routes.data('customers.get', params: { 'customerId' => customer_id })
64
+ end
65
+
66
+ def get_by_external_id(external_id)
67
+ @routes.data('customers.getByExternalId', params: { 'externalId' => external_id })
68
+ end
69
+
70
+ # Filters: search, includeArchived, pageSize, pageToken.
71
+ def list(**filters)
72
+ @routes.token_page('customers.list', query: filters)
73
+ end
74
+
75
+ def iterate(**filters, &block)
76
+ RouteResponse.each_item(filters, block, :pageToken) { |query| list(**query) }
77
+ end
78
+
79
+ def credit_balance(customer_id)
80
+ @routes.data('customers.creditBalance', params: { 'customerId' => customer_id })
81
+ end
82
+
83
+ # Grants or deducts credit: { amountMinor, currency, reason, type, ... } (amountMinor a minor-unit
84
+ # string).
85
+ def adjust_credit(customer_id, adjustment)
86
+ @routes.data('customers.adjustCredit', params: { 'customerId' => customer_id }, body: adjustment)
87
+ end
88
+
89
+ def archive(customer_id)
90
+ @routes.data('customers.archive', params: { 'customerId' => customer_id })
91
+ end
92
+ end
93
+
94
+ # subscriptions.cards: saved cards for automatic charges. A card is added from the checkout SDK's
95
+ # paymentMethodToken; a card number or security code is never sent (the field is refused and the error
96
+ # names only the field).
97
+ class SubscriptionCards
98
+ def initialize(routes)
99
+ @routes = routes
100
+ end
101
+
102
+ # { customerId, paymentMethodToken, brand?, last4?, bin?, expireMonth?, expireYear?, cardholderName?,
103
+ # isDefault?, nickname?, verificationMode?, supportedCurrencies? }.
104
+ def add(request, idempotency_key: nil)
105
+ @routes.data('cards.add', body: SubscriptionRules.card!(request), idempotency_key: idempotency_key)
106
+ end
107
+
108
+ def get(card_id)
109
+ @routes.data('cards.get', params: { 'cardId' => card_id })
110
+ end
111
+
112
+ # A customer's cards.
113
+ def list(customer_id, include_inactive: false, page_token: nil, page_size: nil)
114
+ query = { 'customerId' => RouteValue.text!(customer_id, 'customerId'), 'includeInactive' => include_inactive,
115
+ 'pageToken' => page_token, 'pageSize' => page_size }
116
+ @routes.token_page('cards.list', query: RouteRequest.compact(query))
117
+ end
118
+
119
+ def iterate(customer_id, include_inactive: false, page_size: nil, &block)
120
+ filters = RouteRequest.compact(include_inactive: include_inactive, page_size: page_size)
121
+ RouteResponse.each_item(filters, block, :page_token) { |query| list(customer_id, **query) }
122
+ end
123
+
124
+ def set_default(card_id) # rubocop:disable Naming/AccessorMethodName -- catalog operation cards.setDefault
125
+ @routes.data('cards.setDefault', params: { 'cardId' => card_id })
126
+ end
127
+
128
+ def deactivate(card_id, reason: nil)
129
+ @routes.data('cards.deactivate', params: { 'cardId' => card_id },
130
+ body: RouteRequest.compact('reason' => RouteValue.optional_text(reason, 'reason')))
131
+ end
132
+
133
+ # Verifies a saved card (a small authorisation or a 3-D check); options as the gateway documents them.
134
+ def verify(card_id, options = {})
135
+ @routes.data('cards.verify', params: { 'cardId' => card_id }, body: options)
136
+ end
137
+
138
+ # Card metadata (label, expiry, billing ...); never a card number.
139
+ def update(card_id, changes)
140
+ @routes.data('cards.update', params: { 'cardId' => card_id }, body: changes)
141
+ end
142
+ end
143
+
144
+ # subscriptions.invoices: billing history, void and charge retry.
145
+ class SubscriptionInvoices
146
+ def initialize(routes)
147
+ @routes = routes
148
+ end
149
+
150
+ # Filters: subscriptionId, status, pageSize, pageToken.
151
+ def list(**filters)
152
+ @routes.token_page('invoices.list', query: filters)
153
+ end
154
+
155
+ def iterate(**filters, &block)
156
+ RouteResponse.each_item(filters, block, :pageToken) { |query| list(**query) }
157
+ end
158
+
159
+ def get(invoice_id)
160
+ @routes.data('invoices.get', params: { 'invoiceId' => invoice_id })
161
+ end
162
+
163
+ def void(invoice_id, reason: nil)
164
+ @routes.data('invoices.void', params: { 'invoiceId' => invoice_id },
165
+ body: RouteRequest.compact('reason' => RouteValue.optional_text(reason, 'reason')))
166
+ end
167
+
168
+ def retry_charge(invoice_id, idempotency_key: nil)
169
+ @routes.data('invoices.retryCharge', params: { 'invoiceId' => invoice_id }, idempotency_key: idempotency_key)
170
+ end
171
+ end
172
+
173
+ # subscriptions.portal: the customer portal. Embedded: create_sdk_session mints an origin-bound session
174
+ # for the browser portal SDK (origin must be allow-listed). Hosted: create_portal_link returns a link
175
+ # whose token opens the hosted portal (Lunixi::Subscriptions.portal_url(link)); the customer verifies
176
+ # with a one-time code.
177
+ class SubscriptionPortal
178
+ def initialize(routes)
179
+ @routes = routes
180
+ end
181
+
182
+ # Returns { portalToken, sessionProof, proofNonce, proofExpiresAt, ... } (the gateway answers at the
183
+ # envelope root); hand those four to the browser.
184
+ def create_sdk_session(customer_id, origin, sdk_mode: nil, portal_link_id: nil)
185
+ RouteValue.one_of!(sdk_mode, SubscriptionPortalSdkMode::ALL, 'sdkMode') unless sdk_mode.nil? || sdk_mode == ''
186
+ body = RouteRequest.compact('customerId' => RouteValue.text!(customer_id, 'customerId'), 'origin' => RouteValue.text!(origin, 'origin'),
187
+ 'sdkMode' => RouteValue.optional_text(sdk_mode, 'sdkMode'),
188
+ 'portalLinkId' => RouteValue.optional_text(portal_link_id, 'portalLinkId'))
189
+ session = RouteResponse.merge_root(@routes.call('portal.createSdkSession', body: body))
190
+ session['proofNonce'] = session['nonce'] if !session.key?('proofNonce') && session.key?('nonce')
191
+ session['proofExpiresAt'] = session['expiresAt'] if !session.key?('proofExpiresAt') && session.key?('expiresAt')
192
+ session
193
+ end
194
+
195
+ # options: allowedPlanIds, defaultPlanId, permissions, sessionDurationMinutes, expiresAt,
196
+ # requireEmailVerification ...
197
+ def create_portal_link(customer_id, options = {})
198
+ body = RouteRequest.wire_hash!(options, 'create_portal_link options').merge('customerId' => RouteValue.text!(customer_id, 'customerId'))
199
+ @routes.data('portal.createPortalLink', body: body)
200
+ end
201
+
202
+ # Filters: customerId, status, limit.
203
+ def list_links(**filters)
204
+ @routes.page('portal.listLinks', query: filters)
205
+ end
206
+
207
+ def get_link(link_id)
208
+ @routes.data('portal.getLink', params: { 'linkId' => link_id })
209
+ end
210
+
211
+ def update_link(link_id, changes)
212
+ @routes.data('portal.updateLink', params: { 'linkId' => link_id }, body: changes)
213
+ end
214
+
215
+ def revoke_link(link_id, reason: nil)
216
+ @routes.data('portal.revokeLink', params: { 'linkId' => link_id },
217
+ body: RouteRequest.compact('reason' => RouteValue.optional_text(reason, 'reason')))
218
+ end
219
+
220
+ def link_activity(link_id)
221
+ @routes.data('portal.linkActivity', params: { 'linkId' => link_id })
222
+ end
223
+
224
+ # Emails a portal invite: { customerId, email?, allowedPlanIds?, ... }.
225
+ def send_invite(invite)
226
+ @routes.data('portal.sendInvite', body: invite)
227
+ end
228
+
229
+ # Portal settings (branding, allowed origins).
230
+ def get_settings # rubocop:disable Naming/AccessorMethodName -- catalog operation portal.getSettings
231
+ @routes.data('portal.getSettings')
232
+ end
233
+
234
+ def update_settings(settings)
235
+ @routes.data('portal.updateSettings', body: settings)
236
+ end
237
+ end
238
+
239
+ # subscriptions.products: catalog products (groups of plans).
240
+ class SubscriptionProducts
241
+ def initialize(routes)
242
+ @routes = routes
243
+ end
244
+
245
+ def create(product)
246
+ @routes.data('products.create', body: product)
247
+ end
248
+
249
+ # Filters: limit, cursor, active.
250
+ def list(**filters)
251
+ @routes.page('products.list', query: filters)
252
+ end
253
+
254
+ def iterate(**filters, &block)
255
+ RouteResponse.each_item(filters, block) { |query| list(**query) }
256
+ end
257
+
258
+ def get(product_id)
259
+ @routes.data('products.get', params: { 'productId' => product_id })
260
+ end
261
+
262
+ def update(product_id, changes)
263
+ @routes.data('products.update', params: { 'productId' => product_id }, body: changes)
264
+ end
265
+
266
+ def archive(product_id)
267
+ @routes.data('products.archive', params: { 'productId' => product_id })
268
+ end
269
+
270
+ def translations(product_id)
271
+ @routes.data('products.translations', params: { 'productId' => product_id })
272
+ end
273
+
274
+ def upsert_translation(product_id, locale, translation)
275
+ @routes.data('products.upsertTranslation', params: { 'productId' => product_id, 'locale' => locale }, body: translation)
276
+ end
277
+ end
278
+
279
+ # subscriptions.features: entitlement features.
280
+ class SubscriptionFeatures
281
+ def initialize(routes)
282
+ @routes = routes
283
+ end
284
+
285
+ def create(feature)
286
+ @routes.data('features.create', body: feature)
287
+ end
288
+
289
+ # Any scalar filter is forwarded.
290
+ def list(**filters)
291
+ @routes.page('features.list', query: filters)
292
+ end
293
+
294
+ def get(feature_id)
295
+ @routes.data('features.get', params: { 'featureId' => feature_id })
296
+ end
297
+
298
+ def update(feature_id, changes)
299
+ @routes.data('features.update', params: { 'featureId' => feature_id }, body: changes)
300
+ end
301
+
302
+ def delete(feature_id)
303
+ @routes.data('features.delete', params: { 'featureId' => feature_id })
304
+ end
305
+
306
+ def translations(feature_id)
307
+ @routes.data('features.translations', params: { 'featureId' => feature_id })
308
+ end
309
+
310
+ def upsert_translation(feature_id, locale, translation)
311
+ @routes.data('features.upsertTranslation', params: { 'featureId' => feature_id, 'locale' => locale }, body: translation)
312
+ end
313
+ end
314
+
315
+ # subscriptions.addons.
316
+ class SubscriptionAddons
317
+ def initialize(routes)
318
+ @routes = routes
319
+ end
320
+
321
+ def create(addon)
322
+ @routes.data('addons.create', body: addon)
323
+ end
324
+
325
+ # Any scalar filter is forwarded.
326
+ def list(**filters)
327
+ @routes.page('addons.list', query: filters)
328
+ end
329
+
330
+ def update(addon_id, changes)
331
+ @routes.data('addons.update', params: { 'addonId' => addon_id }, body: changes)
332
+ end
333
+
334
+ def deactivate(addon_id)
335
+ @routes.data('addons.deactivate', params: { 'addonId' => addon_id })
336
+ end
337
+ end
338
+
339
+ # subscriptions.usage: metered usage and entitlement checks.
340
+ class SubscriptionUsage
341
+ def initialize(routes)
342
+ @routes = routes
343
+ end
344
+
345
+ # A metered-usage event: { customerId | subscriptionId, featureKey, quantity, timestamp?,
346
+ # idempotencyKey?, ... }.
347
+ def record(usage)
348
+ @routes.data('usage.record', body: usage)
349
+ end
350
+
351
+ # Whether the customer is entitled / has quota left: { customerId | subscriptionId, featureKey,
352
+ # requested?, ... }.
353
+ def check_entitlement(check)
354
+ @routes.data('usage.checkEntitlement', body: check)
355
+ end
356
+ end
357
+ end
@@ -0,0 +1,233 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lunixi
4
+ # Subscription values and routes (/api/v1/subscriptions, and the portal-link routes under
5
+ # /api/panel/v1/subscriptions/portal). The same codes as the Node and PHP SDKs.
6
+
7
+ # Subscription status.
8
+ module SubscriptionStatus
9
+ # startsAt is in the future.
10
+ PENDING = 'PENDING'
11
+ TRIALING = 'TRIALING'
12
+ ACTIVE = 'ACTIVE'
13
+ # A charge failed; dunning runs.
14
+ PAST_DUE = 'PAST_DUE'
15
+ # Dunning exhausted; grace period.
16
+ UNPAID = 'UNPAID'
17
+ # Paused by the merchant; no charges.
18
+ PAUSED = 'PAUSED'
19
+ UPGRADED = 'UPGRADED'
20
+ DOWNGRADED = 'DOWNGRADED'
21
+ CANCELED = 'CANCELED'
22
+ # The recurrence ran out.
23
+ EXPIRED = 'EXPIRED'
24
+ ALL = [PENDING, TRIALING, ACTIVE, PAST_DUE, UNPAID, PAUSED, UPGRADED, DOWNGRADED, CANCELED, EXPIRED].freeze
25
+ # Live states: the subscription may be billed.
26
+ ACTIVE_STATES = [TRIALING, ACTIVE, PAST_DUE].freeze
27
+ # Terminal states: no further transition.
28
+ TERMINAL_STATES = [UPGRADED, DOWNGRADED, CANCELED, EXPIRED].freeze
29
+ end
30
+
31
+ SUBSCRIPTION_ACTIVE_STATES = SubscriptionStatus::ACTIVE_STATES
32
+ SUBSCRIPTION_TERMINAL_STATES = SubscriptionStatus::TERMINAL_STATES
33
+
34
+ # Invoice status: DRAFT -> OPEN -> PAID | UNCOLLECTIBLE | VOID.
35
+ module SubscriptionInvoiceStatus
36
+ DRAFT = 'DRAFT'
37
+ OPEN = 'OPEN'
38
+ PAID = 'PAID'
39
+ UNCOLLECTIBLE = 'UNCOLLECTIBLE'
40
+ VOID = 'VOID'
41
+ ALL = [DRAFT, OPEN, PAID, UNCOLLECTIBLE, VOID].freeze
42
+ end
43
+
44
+ # Errors of customers.upsert: a missing or invalid customer field (details.field).
45
+ module SubscriptionCustomerErrorCode
46
+ FIELD_REQUIRED = 'subscription.customer.field_required'
47
+ FIELD_INVALID = 'subscription.customer.field_invalid'
48
+ ALL = [FIELD_REQUIRED, FIELD_INVALID].freeze
49
+ end
50
+
51
+ # How invoices are collected (collectionMethod): the saved card is charged each cycle (AUTO_CHARGE,
52
+ # the default), the invoice is finalised but not charged (SEND_INVOICE), or collection is up to you
53
+ # (MANUAL).
54
+ module SubscriptionCollectionMethod
55
+ AUTO_CHARGE = 'AUTO_CHARGE'
56
+ SEND_INVOICE = 'SEND_INVOICE'
57
+ MANUAL = 'MANUAL'
58
+ ALL = [AUTO_CHARGE, SEND_INVOICE, MANUAL].freeze
59
+ end
60
+
61
+ # billing.frequency of a plan.
62
+ module SubscriptionBillingFrequency
63
+ DAILY = 'DAILY'
64
+ WEEKLY = 'WEEKLY'
65
+ MONTHLY = 'MONTHLY'
66
+ YEARLY = 'YEARLY'
67
+ ALL = [DAILY, WEEKLY, MONTHLY, YEARLY].freeze
68
+ end
69
+
70
+ # verificationMode of a saved card.
71
+ module SubscriptionCardVerificationMode
72
+ NONE = 'NONE'
73
+ ON_ADD = 'ON_ADD'
74
+ FIRST_CHARGE = 'FIRST_CHARGE'
75
+ ALL = [NONE, ON_ADD, FIRST_CHARGE].freeze
76
+ end
77
+
78
+ # behavior of a plan change: now (prorated) or at the next period.
79
+ module SubscriptionPlanChangeBehavior
80
+ IMMEDIATE = 'IMMEDIATE'
81
+ NEXT_PERIOD = 'NEXT_PERIOD'
82
+ ALL = [IMMEDIATE, NEXT_PERIOD].freeze
83
+ end
84
+
85
+ # sdkMode of a portal SDK session.
86
+ module SubscriptionPortalSdkMode
87
+ FULL = 'FULL'
88
+ HEADLESS = 'HEADLESS'
89
+ HYBRID = 'HYBRID'
90
+ ALL = [FULL, HEADLESS, HYBRID].freeze
91
+ end
92
+
93
+ # Webhook event types (the same signed envelope as payment events; data amounts are minor-unit
94
+ # strings).
95
+ module SubscriptionEvents
96
+ CREATED = 'subscription.created'
97
+ ACTIVATED = 'subscription.activated'
98
+ TRIAL_WILL_END = 'subscription.trial_will_end'
99
+ PAUSED = 'subscription.paused'
100
+ RESUMED = 'subscription.resumed'
101
+ CANCEL_SCHEDULED = 'subscription.cancel_scheduled'
102
+ CANCELED = 'subscription.canceled'
103
+ UPGRADED = 'subscription.upgraded'
104
+ DOWNGRADED = 'subscription.downgraded'
105
+ PLAN_CHANGE_SCHEDULED = 'subscription.plan_change_scheduled'
106
+ PENDING_CHANGE_APPLIED = 'subscription.pending_change_applied'
107
+ PAYMENT_CARD_SWAPPED = 'subscription.payment_card_swapped'
108
+ PAYMENT_METHOD_LOST = 'subscription.payment_method_lost'
109
+ INVOICE_CREATED = 'invoice.created'
110
+ INVOICE_PAID = 'invoice.paid'
111
+ INVOICE_PAYMENT_FAILED = 'invoice.payment_failed'
112
+ INVOICE_VOIDED = 'invoice.voided'
113
+ CARD_ADDED = 'card.added'
114
+ CARD_SET_DEFAULT = 'card.set_default'
115
+ CARD_UPDATED = 'card.updated'
116
+ CARD_DEACTIVATED = 'card.deactivated'
117
+ CARD_EXPIRING_SOON = 'card.expiring_soon'
118
+ CARD_EXPIRED = 'card.expired'
119
+ ALL = [CREATED, ACTIVATED, TRIAL_WILL_END, PAUSED, RESUMED, CANCEL_SCHEDULED, CANCELED, UPGRADED, DOWNGRADED, PLAN_CHANGE_SCHEDULED,
120
+ PENDING_CHANGE_APPLIED, PAYMENT_CARD_SWAPPED, PAYMENT_METHOD_LOST, INVOICE_CREATED, INVOICE_PAID, INVOICE_PAYMENT_FAILED,
121
+ INVOICE_VOIDED, CARD_ADDED, CARD_SET_DEFAULT, CARD_UPDATED, CARD_DEACTIVATED, CARD_EXPIRING_SOON, CARD_EXPIRED].freeze
122
+ end
123
+
124
+ SUBSCRIPTION_EVENT_TYPES = SubscriptionEvents::ALL
125
+
126
+ # Hosted customer portal; a portal link's token opens it (Lunixi::Subscriptions.portal_url).
127
+ SUBSCRIPTION_PORTAL_HOSTED_BASE = 'https://portal.lunixi.com'
128
+
129
+ # Fields of a plan's billing rule.
130
+ SUBSCRIPTION_BILLING_RULE_FIELDS = %w[frequency interval byMonthDay byWeekDay timezone lastDayOfMonthFix count].freeze
131
+
132
+ subscriptions_base = '/api/v1/subscriptions'
133
+ portal_panel = '/api/panel/v1/subscriptions/portal'
134
+ cursor_query = %w[pageSize pageToken].freeze
135
+ route = lambda do |method, path, **extra|
136
+ RouteRequest.route(method, path, step_up: false, idempotency_key: 'none', **extra)
137
+ end
138
+
139
+ # Every subscription route the SDK calls: method, path, whether it is signed (never: the subscription
140
+ # surface takes the access token only), the Idempotency-Key rule and the body / query fields. Keys are
141
+ # the operation names ("plans.create"). The same table as the Node SDK's SUBSCRIPTION_ROUTES; the
142
+ # parity test compares it with the shared catalog.
143
+ SUBSCRIPTION_ROUTES = {
144
+ 'create' => route.call('POST', "#{subscriptions_base}/subscriptions",
145
+ idempotency_key: 'required', required: %w[customerId planId idempotencyKey],
146
+ optional: %w[paymentCardId paymentMethodId collectionMethod priceUpdatePolicy entitlementPolicy billingTimezone startsAt
147
+ chargeOnCreate billingCycleAnchorOverride skipTrial metadata]),
148
+ 'get' => route.call('GET', "#{subscriptions_base}/subscriptions/:subscriptionId"),
149
+ 'list' => route.call('GET', "#{subscriptions_base}/subscriptions", query: %w[customerId status] + cursor_query),
150
+ 'cancel' => route.call('POST', "#{subscriptions_base}/subscriptions/:subscriptionId/cancel", required: %w[cancelAtPeriodEnd], optional: %w[reason]),
151
+ 'pause' => route.call('POST', "#{subscriptions_base}/subscriptions/:subscriptionId/pause", optional: %w[resumeAt], optional_body: true),
152
+ 'resume' => route.call('POST', "#{subscriptions_base}/subscriptions/:subscriptionId/resume"),
153
+ 'changePlan' => route.call('POST', "#{subscriptions_base}/subscriptions/:subscriptionId/change-plan",
154
+ idempotency_key: 'optional', required: %w[newPlanId], optional: %w[behavior reason idempotencyKey]),
155
+ 'auditLogs' => route.call('GET', "#{subscriptions_base}/subscriptions/:subscriptionId/audit-logs", query: %w[limit pageToken]),
156
+
157
+ 'plans.create' => route.call('POST', "#{subscriptions_base}/plans",
158
+ idempotency_key: 'optional', required: %w[productId code name price currency billing],
159
+ optional: %w[description trialPeriodDays maxDunningAttempts dunningScheduleDays gracePeriodDays metadata]),
160
+ 'plans.get' => route.call('GET', "#{subscriptions_base}/plans/:planId"),
161
+ 'plans.list' => route.call('GET', "#{subscriptions_base}/plans", query: %w[productId includeArchived] + cursor_query),
162
+ 'plans.update' => route.call('PATCH', "#{subscriptions_base}/plans/:planId", free: true),
163
+ 'plans.archive' => route.call('DELETE', "#{subscriptions_base}/plans/:planId"),
164
+ 'plans.translations' => route.call('GET', "#{subscriptions_base}/plans/:planId/translations"),
165
+ 'plans.upsertTranslation' => route.call('PATCH', "#{subscriptions_base}/plans/:planId/translations/:locale", free: true),
166
+
167
+ # Renewals take the buyer from this record: identity and billing details are required (400
168
+ # subscription.customer.field_required).
169
+ 'customers.upsert' => route.call('PUT', "#{subscriptions_base}/customers",
170
+ idempotency_key: 'optional',
171
+ required: %w[externalId name surname email gsmNumber identityNumber billingAddress billingCity billingCountry
172
+ billingZipCode],
173
+ optional: %w[billingContactName shippingContactName shippingAddress shippingCity shippingCountry
174
+ shippingZipCode]),
175
+ 'customers.get' => route.call('GET', "#{subscriptions_base}/customers/:customerId"),
176
+ 'customers.getByExternalId' => route.call('GET', "#{subscriptions_base}/customers/by-external/:externalId"),
177
+ 'customers.list' => route.call('GET', "#{subscriptions_base}/customers", query: %w[search includeArchived] + cursor_query),
178
+ 'customers.creditBalance' => route.call('GET', "#{subscriptions_base}/customers/:customerId/credit-balance"),
179
+ 'customers.adjustCredit' => route.call('POST', "#{subscriptions_base}/customers/:customerId/credit-balance/adjust", free: true),
180
+ 'customers.archive' => route.call('DELETE', "#{subscriptions_base}/customers/:customerId"),
181
+
182
+ 'cards.add' => route.call('POST', "#{subscriptions_base}/cards",
183
+ idempotency_key: 'optional', required: %w[customerId paymentMethodToken],
184
+ optional: %w[brand last4 bin expireMonth expireYear cardholderName isDefault nickname verificationMode supportedCurrencies]),
185
+ 'cards.get' => route.call('GET', "#{subscriptions_base}/cards/:cardId"),
186
+ 'cards.list' => route.call('GET', "#{subscriptions_base}/cards", query: %w[customerId includeInactive] + cursor_query),
187
+ 'cards.setDefault' => route.call('POST', "#{subscriptions_base}/cards/:cardId/set-default"),
188
+ 'cards.deactivate' => route.call('DELETE', "#{subscriptions_base}/cards/:cardId", optional: %w[reason], optional_body: true),
189
+ 'cards.verify' => route.call('POST', "#{subscriptions_base}/cards/:cardId/verify", free: true, optional_body: true),
190
+ 'cards.update' => route.call('PATCH', "#{subscriptions_base}/cards/:cardId", free: true),
191
+
192
+ 'invoices.list' => route.call('GET', "#{subscriptions_base}/invoices", query: %w[subscriptionId status] + cursor_query),
193
+ 'invoices.get' => route.call('GET', "#{subscriptions_base}/invoices/:invoiceId"),
194
+ 'invoices.void' => route.call('POST', "#{subscriptions_base}/invoices/:invoiceId/void", optional: %w[reason], optional_body: true),
195
+ 'invoices.retryCharge' => route.call('POST', "#{subscriptions_base}/invoices/:invoiceId/retry-charge", idempotency_key: 'optional'),
196
+
197
+ 'portal.createSdkSession' => route.call('POST', "#{subscriptions_base}/portal/sdk-session",
198
+ required: %w[customerId origin], optional: %w[sdkMode portalLinkId]),
199
+ 'portal.createPortalLink' => route.call('POST', "#{portal_panel}/links", free: true),
200
+ 'portal.listLinks' => route.call('GET', "#{portal_panel}/links", query: %w[customerId status limit]),
201
+ 'portal.getLink' => route.call('GET', "#{portal_panel}/links/:linkId"),
202
+ 'portal.updateLink' => route.call('PATCH', "#{portal_panel}/links/:linkId", free: true),
203
+ 'portal.revokeLink' => route.call('DELETE', "#{portal_panel}/links/:linkId", optional: %w[reason], optional_body: true),
204
+ 'portal.linkActivity' => route.call('GET', "#{portal_panel}/links/:linkId/activity"),
205
+ 'portal.sendInvite' => route.call('POST', "#{portal_panel}/invite", free: true),
206
+ 'portal.getSettings' => route.call('GET', "#{portal_panel}/settings"),
207
+ 'portal.updateSettings' => route.call('PUT', "#{portal_panel}/settings", free: true),
208
+
209
+ 'products.create' => route.call('POST', "#{subscriptions_base}/products", free: true),
210
+ 'products.list' => route.call('GET', "#{subscriptions_base}/products", query: %w[limit cursor active]),
211
+ 'products.get' => route.call('GET', "#{subscriptions_base}/products/:productId"),
212
+ 'products.update' => route.call('PATCH', "#{subscriptions_base}/products/:productId", free: true),
213
+ 'products.archive' => route.call('DELETE', "#{subscriptions_base}/products/:productId"),
214
+ 'products.translations' => route.call('GET', "#{subscriptions_base}/products/:productId/translations"),
215
+ 'products.upsertTranslation' => route.call('PATCH', "#{subscriptions_base}/products/:productId/translations/:locale", free: true),
216
+
217
+ 'features.create' => route.call('POST', "#{subscriptions_base}/features", free: true),
218
+ 'features.list' => route.call('GET', "#{subscriptions_base}/features", any_query: true),
219
+ 'features.get' => route.call('GET', "#{subscriptions_base}/features/:featureId"),
220
+ 'features.update' => route.call('PATCH', "#{subscriptions_base}/features/:featureId", free: true),
221
+ 'features.delete' => route.call('DELETE', "#{subscriptions_base}/features/:featureId"),
222
+ 'features.translations' => route.call('GET', "#{subscriptions_base}/features/:featureId/translations"),
223
+ 'features.upsertTranslation' => route.call('PATCH', "#{subscriptions_base}/features/:featureId/translations/:locale", free: true),
224
+
225
+ 'addons.create' => route.call('POST', "#{subscriptions_base}/addons", free: true),
226
+ 'addons.list' => route.call('GET', "#{subscriptions_base}/addons", any_query: true),
227
+ 'addons.update' => route.call('PATCH', "#{subscriptions_base}/addons/:addonId", free: true),
228
+ 'addons.deactivate' => route.call('DELETE', "#{subscriptions_base}/addons/:addonId"),
229
+
230
+ 'usage.record' => route.call('POST', "#{subscriptions_base}/usage/record", free: true),
231
+ 'usage.checkEntitlement' => route.call('POST', "#{subscriptions_base}/usage/check-entitlement", free: true)
232
+ }.freeze
233
+ end