lunixi-sdk 1.2.2 → 1.3.1
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +45 -0
- data/README.md +262 -9
- data/README.tr.md +266 -7
- data/lib/lunixi/bank_connect.rb +340 -0
- data/lib/lunixi/bank_connect_filters.rb +114 -0
- data/lib/lunixi/bank_connect_values.rb +205 -0
- data/lib/lunixi/bank_transfer.rb +106 -0
- data/lib/lunixi/card_requests.rb +150 -0
- data/lib/lunixi/client.rb +57 -16
- data/lib/lunixi/direct_method_payment_request.rb +243 -0
- data/lib/lunixi/enabled_installments.rb +28 -0
- data/lib/lunixi/errors.rb +69 -3
- data/lib/lunixi/fraud.rb +81 -5
- data/lib/lunixi/identity.rb +94 -0
- data/lib/lunixi/identity_values.rb +28 -0
- data/lib/lunixi/insurance.rb +294 -0
- data/lib/lunixi/insurance_request.rb +134 -0
- data/lib/lunixi/insurance_values.rb +200 -0
- data/lib/lunixi/kyc.rb +189 -0
- data/lib/lunixi/kyc_values.rb +158 -0
- data/lib/lunixi/marketplace.rb +264 -0
- data/lib/lunixi/marketplace_values.rb +86 -0
- data/lib/lunixi/method_fields.rb +142 -0
- data/lib/lunixi/money.rb +7 -4
- data/lib/lunixi/payment_link_filters.rb +32 -0
- data/lib/lunixi/payment_link_image_rules.rb +85 -0
- data/lib/lunixi/payment_link_rules.rb +132 -0
- data/lib/lunixi/payment_link_values.rb +134 -0
- data/lib/lunixi/payment_links.rb +152 -0
- data/lib/lunixi/payment_values.rb +86 -0
- data/lib/lunixi/payments.rb +154 -5
- data/lib/lunixi/physical_pos.rb +183 -0
- data/lib/lunixi/physical_pos_card_data.rb +117 -0
- data/lib/lunixi/physical_pos_card_number.rb +65 -0
- data/lib/lunixi/physical_pos_device_identity.rb +44 -0
- data/lib/lunixi/physical_pos_filters.rb +58 -0
- data/lib/lunixi/physical_pos_gateway.rb +153 -0
- data/lib/lunixi/physical_pos_identifiers.rb +62 -0
- data/lib/lunixi/physical_pos_management.rb +326 -0
- data/lib/lunixi/physical_pos_money.rb +46 -0
- data/lib/lunixi/physical_pos_request.rb +126 -0
- data/lib/lunixi/physical_pos_responses.rb +102 -0
- data/lib/lunixi/physical_pos_results.rb +52 -0
- data/lib/lunixi/physical_pos_settlement_policies.rb +71 -0
- data/lib/lunixi/physical_pos_value.rb +43 -0
- data/lib/lunixi/physical_pos_values.rb +203 -0
- data/lib/lunixi/physical_pos_webhook.rb +147 -0
- data/lib/lunixi/product_webhooks.rb +106 -0
- data/lib/lunixi/promissory_note.rb +155 -0
- data/lib/lunixi/reference.rb +41 -0
- data/lib/lunixi/route_request.rb +121 -0
- data/lib/lunixi/route_response.rb +101 -0
- data/lib/lunixi/route_table.rb +128 -0
- data/lib/lunixi/route_value.rb +60 -0
- data/lib/lunixi/sanction.rb +244 -0
- data/lib/lunixi/sanction_values.rb +132 -0
- data/lib/lunixi/split_payment.rb +145 -0
- data/lib/lunixi/subscription_resources.rb +355 -0
- data/lib/lunixi/subscription_values.rb +220 -0
- data/lib/lunixi/subscriptions.rb +202 -0
- data/lib/lunixi/version.rb +1 -1
- data/lib/lunixi/wallet.rb +153 -0
- data/lib/lunixi/wallet_resources.rb +402 -0
- data/lib/lunixi/wallet_routes.rb +123 -0
- data/lib/lunixi/wallet_values.rb +71 -0
- data/lib/lunixi/wire_money.rb +67 -0
- data/lib/lunixi.rb +57 -0
- metadata +58 -1
|
@@ -0,0 +1,220 @@
|
|
|
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
|
+
# How invoices are collected (collectionMethod): the saved card is charged each cycle (AUTO_CHARGE,
|
|
45
|
+
# the default), the invoice is finalised but not charged (SEND_INVOICE), or collection is up to you
|
|
46
|
+
# (MANUAL).
|
|
47
|
+
module SubscriptionCollectionMethod
|
|
48
|
+
AUTO_CHARGE = 'AUTO_CHARGE'
|
|
49
|
+
SEND_INVOICE = 'SEND_INVOICE'
|
|
50
|
+
MANUAL = 'MANUAL'
|
|
51
|
+
ALL = [AUTO_CHARGE, SEND_INVOICE, MANUAL].freeze
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# billing.frequency of a plan.
|
|
55
|
+
module SubscriptionBillingFrequency
|
|
56
|
+
DAILY = 'DAILY'
|
|
57
|
+
WEEKLY = 'WEEKLY'
|
|
58
|
+
MONTHLY = 'MONTHLY'
|
|
59
|
+
YEARLY = 'YEARLY'
|
|
60
|
+
ALL = [DAILY, WEEKLY, MONTHLY, YEARLY].freeze
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# verificationMode of a saved card.
|
|
64
|
+
module SubscriptionCardVerificationMode
|
|
65
|
+
NONE = 'NONE'
|
|
66
|
+
ON_ADD = 'ON_ADD'
|
|
67
|
+
FIRST_CHARGE = 'FIRST_CHARGE'
|
|
68
|
+
ALL = [NONE, ON_ADD, FIRST_CHARGE].freeze
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# behavior of a plan change: now (prorated) or at the next period.
|
|
72
|
+
module SubscriptionPlanChangeBehavior
|
|
73
|
+
IMMEDIATE = 'IMMEDIATE'
|
|
74
|
+
NEXT_PERIOD = 'NEXT_PERIOD'
|
|
75
|
+
ALL = [IMMEDIATE, NEXT_PERIOD].freeze
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# sdkMode of a portal SDK session.
|
|
79
|
+
module SubscriptionPortalSdkMode
|
|
80
|
+
FULL = 'FULL'
|
|
81
|
+
HEADLESS = 'HEADLESS'
|
|
82
|
+
HYBRID = 'HYBRID'
|
|
83
|
+
ALL = [FULL, HEADLESS, HYBRID].freeze
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# Webhook event types (the same signed envelope as payment events; data amounts are minor-unit
|
|
87
|
+
# strings).
|
|
88
|
+
module SubscriptionEvents
|
|
89
|
+
CREATED = 'subscription.created'
|
|
90
|
+
ACTIVATED = 'subscription.activated'
|
|
91
|
+
TRIAL_WILL_END = 'subscription.trial_will_end'
|
|
92
|
+
PAUSED = 'subscription.paused'
|
|
93
|
+
RESUMED = 'subscription.resumed'
|
|
94
|
+
CANCEL_SCHEDULED = 'subscription.cancel_scheduled'
|
|
95
|
+
CANCELED = 'subscription.canceled'
|
|
96
|
+
UPGRADED = 'subscription.upgraded'
|
|
97
|
+
DOWNGRADED = 'subscription.downgraded'
|
|
98
|
+
PLAN_CHANGE_SCHEDULED = 'subscription.plan_change_scheduled'
|
|
99
|
+
PENDING_CHANGE_APPLIED = 'subscription.pending_change_applied'
|
|
100
|
+
PAYMENT_CARD_SWAPPED = 'subscription.payment_card_swapped'
|
|
101
|
+
PAYMENT_METHOD_LOST = 'subscription.payment_method_lost'
|
|
102
|
+
INVOICE_CREATED = 'invoice.created'
|
|
103
|
+
INVOICE_PAID = 'invoice.paid'
|
|
104
|
+
INVOICE_PAYMENT_FAILED = 'invoice.payment_failed'
|
|
105
|
+
INVOICE_VOIDED = 'invoice.voided'
|
|
106
|
+
CARD_ADDED = 'card.added'
|
|
107
|
+
CARD_SET_DEFAULT = 'card.set_default'
|
|
108
|
+
CARD_UPDATED = 'card.updated'
|
|
109
|
+
CARD_DEACTIVATED = 'card.deactivated'
|
|
110
|
+
CARD_EXPIRING_SOON = 'card.expiring_soon'
|
|
111
|
+
CARD_EXPIRED = 'card.expired'
|
|
112
|
+
ALL = [CREATED, ACTIVATED, TRIAL_WILL_END, PAUSED, RESUMED, CANCEL_SCHEDULED, CANCELED, UPGRADED, DOWNGRADED, PLAN_CHANGE_SCHEDULED,
|
|
113
|
+
PENDING_CHANGE_APPLIED, PAYMENT_CARD_SWAPPED, PAYMENT_METHOD_LOST, INVOICE_CREATED, INVOICE_PAID, INVOICE_PAYMENT_FAILED,
|
|
114
|
+
INVOICE_VOIDED, CARD_ADDED, CARD_SET_DEFAULT, CARD_UPDATED, CARD_DEACTIVATED, CARD_EXPIRING_SOON, CARD_EXPIRED].freeze
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
SUBSCRIPTION_EVENT_TYPES = SubscriptionEvents::ALL
|
|
118
|
+
|
|
119
|
+
# Hosted customer portal; a portal link's token opens it (Lunixi::Subscriptions.portal_url).
|
|
120
|
+
SUBSCRIPTION_PORTAL_HOSTED_BASE = 'https://portal.lunixi.com'
|
|
121
|
+
|
|
122
|
+
# Fields of a plan's billing rule.
|
|
123
|
+
SUBSCRIPTION_BILLING_RULE_FIELDS = %w[frequency interval byMonthDay byWeekDay timezone lastDayOfMonthFix count].freeze
|
|
124
|
+
|
|
125
|
+
subscriptions_base = '/api/v1/subscriptions'
|
|
126
|
+
portal_panel = '/api/panel/v1/subscriptions/portal'
|
|
127
|
+
cursor_query = %w[pageSize pageToken].freeze
|
|
128
|
+
route = lambda do |method, path, **extra|
|
|
129
|
+
RouteRequest.route(method, path, step_up: false, idempotency_key: 'none', **extra)
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
# Every subscription route the SDK calls: method, path, whether it is signed (never: the subscription
|
|
133
|
+
# surface takes the access token only), the Idempotency-Key rule and the body / query fields. Keys are
|
|
134
|
+
# the operation names ("plans.create"). The same table as the Node SDK's SUBSCRIPTION_ROUTES; the
|
|
135
|
+
# parity test compares it with the shared catalog.
|
|
136
|
+
SUBSCRIPTION_ROUTES = {
|
|
137
|
+
'create' => route.call('POST', "#{subscriptions_base}/subscriptions",
|
|
138
|
+
idempotency_key: 'required', required: %w[customerId planId idempotencyKey],
|
|
139
|
+
optional: %w[paymentCardId paymentMethodId collectionMethod priceUpdatePolicy entitlementPolicy billingTimezone startsAt
|
|
140
|
+
chargeOnCreate billingCycleAnchorOverride skipTrial metadata]),
|
|
141
|
+
'get' => route.call('GET', "#{subscriptions_base}/subscriptions/:subscriptionId"),
|
|
142
|
+
'list' => route.call('GET', "#{subscriptions_base}/subscriptions", query: %w[customerId status] + cursor_query),
|
|
143
|
+
'cancel' => route.call('POST', "#{subscriptions_base}/subscriptions/:subscriptionId/cancel", required: %w[cancelAtPeriodEnd], optional: %w[reason]),
|
|
144
|
+
'pause' => route.call('POST', "#{subscriptions_base}/subscriptions/:subscriptionId/pause", optional: %w[resumeAt], optional_body: true),
|
|
145
|
+
'resume' => route.call('POST', "#{subscriptions_base}/subscriptions/:subscriptionId/resume"),
|
|
146
|
+
'changePlan' => route.call('POST', "#{subscriptions_base}/subscriptions/:subscriptionId/change-plan",
|
|
147
|
+
idempotency_key: 'optional', required: %w[newPlanId], optional: %w[behavior reason idempotencyKey]),
|
|
148
|
+
'auditLogs' => route.call('GET', "#{subscriptions_base}/subscriptions/:subscriptionId/audit-logs", query: %w[limit pageToken]),
|
|
149
|
+
|
|
150
|
+
'plans.create' => route.call('POST', "#{subscriptions_base}/plans",
|
|
151
|
+
idempotency_key: 'optional', required: %w[productId code name price currency billing],
|
|
152
|
+
optional: %w[description trialPeriodDays maxDunningAttempts dunningScheduleDays gracePeriodDays metadata]),
|
|
153
|
+
'plans.get' => route.call('GET', "#{subscriptions_base}/plans/:planId"),
|
|
154
|
+
'plans.list' => route.call('GET', "#{subscriptions_base}/plans", query: %w[productId includeArchived] + cursor_query),
|
|
155
|
+
'plans.update' => route.call('PATCH', "#{subscriptions_base}/plans/:planId", free: true),
|
|
156
|
+
'plans.archive' => route.call('DELETE', "#{subscriptions_base}/plans/:planId"),
|
|
157
|
+
'plans.translations' => route.call('GET', "#{subscriptions_base}/plans/:planId/translations"),
|
|
158
|
+
'plans.upsertTranslation' => route.call('PATCH', "#{subscriptions_base}/plans/:planId/translations/:locale", free: true),
|
|
159
|
+
|
|
160
|
+
'customers.upsert' => route.call('PUT', "#{subscriptions_base}/customers",
|
|
161
|
+
idempotency_key: 'optional', required: %w[externalId], optional: %w[email name phone locale metadata]),
|
|
162
|
+
'customers.get' => route.call('GET', "#{subscriptions_base}/customers/:customerId"),
|
|
163
|
+
'customers.getByExternalId' => route.call('GET', "#{subscriptions_base}/customers/by-external/:externalId"),
|
|
164
|
+
'customers.list' => route.call('GET', "#{subscriptions_base}/customers", query: %w[search includeArchived] + cursor_query),
|
|
165
|
+
'customers.creditBalance' => route.call('GET', "#{subscriptions_base}/customers/:customerId/credit-balance"),
|
|
166
|
+
'customers.adjustCredit' => route.call('POST', "#{subscriptions_base}/customers/:customerId/credit-balance/adjust", free: true),
|
|
167
|
+
'customers.archive' => route.call('DELETE', "#{subscriptions_base}/customers/:customerId"),
|
|
168
|
+
|
|
169
|
+
'cards.add' => route.call('POST', "#{subscriptions_base}/cards",
|
|
170
|
+
idempotency_key: 'optional', required: %w[customerId paymentMethodToken],
|
|
171
|
+
optional: %w[brand last4 bin expireMonth expireYear cardholderName isDefault nickname verificationMode supportedCurrencies]),
|
|
172
|
+
'cards.get' => route.call('GET', "#{subscriptions_base}/cards/:cardId"),
|
|
173
|
+
'cards.list' => route.call('GET', "#{subscriptions_base}/cards", query: %w[customerId includeInactive] + cursor_query),
|
|
174
|
+
'cards.setDefault' => route.call('POST', "#{subscriptions_base}/cards/:cardId/set-default"),
|
|
175
|
+
'cards.deactivate' => route.call('DELETE', "#{subscriptions_base}/cards/:cardId", optional: %w[reason], optional_body: true),
|
|
176
|
+
'cards.verify' => route.call('POST', "#{subscriptions_base}/cards/:cardId/verify", free: true, optional_body: true),
|
|
177
|
+
'cards.update' => route.call('PATCH', "#{subscriptions_base}/cards/:cardId", free: true),
|
|
178
|
+
|
|
179
|
+
'invoices.list' => route.call('GET', "#{subscriptions_base}/invoices", query: %w[subscriptionId status] + cursor_query),
|
|
180
|
+
'invoices.get' => route.call('GET', "#{subscriptions_base}/invoices/:invoiceId"),
|
|
181
|
+
'invoices.void' => route.call('POST', "#{subscriptions_base}/invoices/:invoiceId/void", optional: %w[reason], optional_body: true),
|
|
182
|
+
'invoices.retryCharge' => route.call('POST', "#{subscriptions_base}/invoices/:invoiceId/retry-charge", idempotency_key: 'optional'),
|
|
183
|
+
|
|
184
|
+
'portal.createSdkSession' => route.call('POST', "#{subscriptions_base}/portal/sdk-session",
|
|
185
|
+
required: %w[customerId origin], optional: %w[sdkMode portalLinkId]),
|
|
186
|
+
'portal.createPortalLink' => route.call('POST', "#{portal_panel}/links", free: true),
|
|
187
|
+
'portal.listLinks' => route.call('GET', "#{portal_panel}/links", query: %w[customerId status limit]),
|
|
188
|
+
'portal.getLink' => route.call('GET', "#{portal_panel}/links/:linkId"),
|
|
189
|
+
'portal.updateLink' => route.call('PATCH', "#{portal_panel}/links/:linkId", free: true),
|
|
190
|
+
'portal.revokeLink' => route.call('DELETE', "#{portal_panel}/links/:linkId", optional: %w[reason], optional_body: true),
|
|
191
|
+
'portal.linkActivity' => route.call('GET', "#{portal_panel}/links/:linkId/activity"),
|
|
192
|
+
'portal.sendInvite' => route.call('POST', "#{portal_panel}/invite", free: true),
|
|
193
|
+
'portal.getSettings' => route.call('GET', "#{portal_panel}/settings"),
|
|
194
|
+
'portal.updateSettings' => route.call('PUT', "#{portal_panel}/settings", free: true),
|
|
195
|
+
|
|
196
|
+
'products.create' => route.call('POST', "#{subscriptions_base}/products", free: true),
|
|
197
|
+
'products.list' => route.call('GET', "#{subscriptions_base}/products", query: %w[limit cursor active]),
|
|
198
|
+
'products.get' => route.call('GET', "#{subscriptions_base}/products/:productId"),
|
|
199
|
+
'products.update' => route.call('PATCH', "#{subscriptions_base}/products/:productId", free: true),
|
|
200
|
+
'products.archive' => route.call('DELETE', "#{subscriptions_base}/products/:productId"),
|
|
201
|
+
'products.translations' => route.call('GET', "#{subscriptions_base}/products/:productId/translations"),
|
|
202
|
+
'products.upsertTranslation' => route.call('PATCH', "#{subscriptions_base}/products/:productId/translations/:locale", free: true),
|
|
203
|
+
|
|
204
|
+
'features.create' => route.call('POST', "#{subscriptions_base}/features", free: true),
|
|
205
|
+
'features.list' => route.call('GET', "#{subscriptions_base}/features", any_query: true),
|
|
206
|
+
'features.get' => route.call('GET', "#{subscriptions_base}/features/:featureId"),
|
|
207
|
+
'features.update' => route.call('PATCH', "#{subscriptions_base}/features/:featureId", free: true),
|
|
208
|
+
'features.delete' => route.call('DELETE', "#{subscriptions_base}/features/:featureId"),
|
|
209
|
+
'features.translations' => route.call('GET', "#{subscriptions_base}/features/:featureId/translations"),
|
|
210
|
+
'features.upsertTranslation' => route.call('PATCH', "#{subscriptions_base}/features/:featureId/translations/:locale", free: true),
|
|
211
|
+
|
|
212
|
+
'addons.create' => route.call('POST', "#{subscriptions_base}/addons", free: true),
|
|
213
|
+
'addons.list' => route.call('GET', "#{subscriptions_base}/addons", any_query: true),
|
|
214
|
+
'addons.update' => route.call('PATCH', "#{subscriptions_base}/addons/:addonId", free: true),
|
|
215
|
+
'addons.deactivate' => route.call('DELETE', "#{subscriptions_base}/addons/:addonId"),
|
|
216
|
+
|
|
217
|
+
'usage.record' => route.call('POST', "#{subscriptions_base}/usage/record", free: true),
|
|
218
|
+
'usage.checkEntitlement' => route.call('POST', "#{subscriptions_base}/usage/check-entitlement", free: true)
|
|
219
|
+
}.freeze
|
|
220
|
+
end
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Lunixi
|
|
4
|
+
# Subscriptions (/api/v1/subscriptions, client.subscriptions).
|
|
5
|
+
#
|
|
6
|
+
# Calls carry the access token minted from your API key (no per-request signature). Creating a
|
|
7
|
+
# subscription needs an Idempotency-Key (reuse it when retrying the same subscription; it is sent in the
|
|
8
|
+
# body too); plan, customer, card, plan-change and charge-retry writes accept one.
|
|
9
|
+
#
|
|
10
|
+
# Bodies and filters are checked against SUBSCRIPTION_ROUTES before anything is sent: an unknown field
|
|
11
|
+
# or filter, a missing required field, a value outside its set or a non-canonical amount raises
|
|
12
|
+
# ConfigurationError. Keys are the wire names (String or Symbol; snake_case works too). Amounts are
|
|
13
|
+
# canonical minor-unit strings ("9900" = 99.00 TRY; "0" for a free plan), checked in bodies, filters and
|
|
14
|
+
# answers.
|
|
15
|
+
#
|
|
16
|
+
# Entity calls return the envelope's data; lists return a Lunixi::ListPage. For the subscription, plan,
|
|
17
|
+
# customer, card, invoice and audit-log lists send next_cursor back as page_token: (page_size is the
|
|
18
|
+
# envelope's pageSize). The related resources hang off plans, customers, cards, invoices, portal,
|
|
19
|
+
# products, features, addons and usage.
|
|
20
|
+
class Subscriptions
|
|
21
|
+
attr_reader :plans, :customers, :cards, :invoices, :portal, :products, :features, :addons, :usage
|
|
22
|
+
|
|
23
|
+
# True while the subscription may be billed (TRIALING, ACTIVE, PAST_DUE). Takes a subscription Hash or
|
|
24
|
+
# its status.
|
|
25
|
+
def self.active?(subscription)
|
|
26
|
+
SubscriptionStatus::ACTIVE_STATES.include?(status_of(subscription))
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# True for UPGRADED, DOWNGRADED, CANCELED and EXPIRED.
|
|
30
|
+
def self.terminal?(subscription)
|
|
31
|
+
SubscriptionStatus::TERMINAL_STATES.include?(status_of(subscription))
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Hosted portal URL of a portal link (the portal.create_portal_link answer, or its token).
|
|
35
|
+
def self.portal_url(link, base = SUBSCRIPTION_PORTAL_HOSTED_BASE)
|
|
36
|
+
token = link.is_a?(Hash) ? link['token'] : link
|
|
37
|
+
token = '' unless token.is_a?(String)
|
|
38
|
+
"#{base.to_s.sub(%r{/+\z}, '')}/?token=#{URI.encode_www_form_component(token).gsub('+', '%20')}"
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def self.status_of(subscription)
|
|
42
|
+
return subscription if subscription.is_a?(String)
|
|
43
|
+
|
|
44
|
+
subscription.is_a?(Hash) && subscription['status'].is_a?(String) ? subscription['status'] : ''
|
|
45
|
+
end
|
|
46
|
+
private_class_method :status_of
|
|
47
|
+
|
|
48
|
+
def initialize(client)
|
|
49
|
+
@routes = RouteTable.new(client, SUBSCRIPTION_ROUTES)
|
|
50
|
+
@plans = SubscriptionPlans.new(@routes)
|
|
51
|
+
@customers = SubscriptionCustomers.new(@routes)
|
|
52
|
+
@cards = SubscriptionCards.new(@routes)
|
|
53
|
+
@invoices = SubscriptionInvoices.new(@routes)
|
|
54
|
+
@portal = SubscriptionPortal.new(@routes)
|
|
55
|
+
@products = SubscriptionProducts.new(@routes)
|
|
56
|
+
@features = SubscriptionFeatures.new(@routes)
|
|
57
|
+
@addons = SubscriptionAddons.new(@routes)
|
|
58
|
+
@usage = SubscriptionUsage.new(@routes)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# { customerId, planId, paymentCardId?, paymentMethodId?, collectionMethod?, priceUpdatePolicy?,
|
|
62
|
+
# entitlementPolicy?, billingTimezone?, startsAt?, chargeOnCreate?, billingCycleAnchorOverride?,
|
|
63
|
+
# skipTrial?, metadata? }. The Idempotency-Key is required (idempotency_key: or the request's
|
|
64
|
+
# idempotencyKey; both given, they must agree) and is sent in the body as well.
|
|
65
|
+
def create(request, idempotency_key: nil)
|
|
66
|
+
body = RouteRequest.wire_hash!(request, 'create request')
|
|
67
|
+
key = RouteTable.idempotency_key_of('create', body, idempotency_key)
|
|
68
|
+
body['idempotencyKey'] = key.strip if key.is_a?(String) && !key.strip.empty?
|
|
69
|
+
RouteValue.one_of!(body['collectionMethod'], SubscriptionCollectionMethod::ALL, 'collectionMethod') if body.key?('collectionMethod')
|
|
70
|
+
SubscriptionRules.boolean!(body, 'chargeOnCreate')
|
|
71
|
+
SubscriptionRules.boolean!(body, 'skipTrial')
|
|
72
|
+
raise ConfigurationError, "'metadata' must be a Hash." if body.key?('metadata') && !body['metadata'].is_a?(Hash)
|
|
73
|
+
|
|
74
|
+
@routes.data('create', body: body, idempotency_key: key)
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def get(subscription_id)
|
|
78
|
+
@routes.data('get', params: { 'subscriptionId' => subscription_id })
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# Filters: customerId, status, pageSize, pageToken.
|
|
82
|
+
def list(**filters)
|
|
83
|
+
@routes.token_page('list', query: filters)
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def iterate(**filters, &block)
|
|
87
|
+
RouteResponse.each_item(filters, block, :pageToken) { |query| list(**query) }
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# Cancels now, or at the period end with at_period_end: true.
|
|
91
|
+
def cancel(subscription_id, at_period_end: false, reason: nil)
|
|
92
|
+
raise ConfigurationError, "'at_period_end' must be true or false." unless [true, false].include?(at_period_end)
|
|
93
|
+
|
|
94
|
+
body = RouteRequest.compact('cancelAtPeriodEnd' => at_period_end, 'reason' => RouteValue.optional_text(reason, 'reason'))
|
|
95
|
+
@routes.data('cancel', params: { 'subscriptionId' => subscription_id }, body: body)
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
# Pauses billing; resume_at (ISO 8601) resumes it automatically.
|
|
99
|
+
def pause(subscription_id, resume_at: nil)
|
|
100
|
+
body = RouteRequest.compact('resumeAt' => RouteValue.optional_text(resume_at, 'resumeAt'))
|
|
101
|
+
@routes.data('pause', params: { 'subscriptionId' => subscription_id }, body: body)
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def resume(subscription_id)
|
|
105
|
+
@routes.data('resume', params: { 'subscriptionId' => subscription_id })
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
# { newPlanId, behavior?: IMMEDIATE | NEXT_PERIOD, reason? }. An Idempotency-Key (idempotency_key: or
|
|
109
|
+
# the request's idempotencyKey) is sent in the header and the body.
|
|
110
|
+
def change_plan(subscription_id, request, idempotency_key: nil)
|
|
111
|
+
body = RouteRequest.wire_hash!(request, 'changePlan request')
|
|
112
|
+
key = RouteTable.idempotency_key_of('changePlan', body, idempotency_key)
|
|
113
|
+
body['idempotencyKey'] = key.strip if key.is_a?(String) && !key.strip.empty?
|
|
114
|
+
body['behavior'] = RouteValue.one_of!(body['behavior'].to_s.upcase, SubscriptionPlanChangeBehavior::ALL, 'behavior') if body.key?('behavior')
|
|
115
|
+
@routes.data('changePlan', params: { 'subscriptionId' => subscription_id }, body: body, idempotency_key: key)
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
# The audit trail of a subscription; filters limit, pageToken.
|
|
119
|
+
def audit_logs(subscription_id, **filters)
|
|
120
|
+
@routes.token_page('auditLogs', params: { 'subscriptionId' => subscription_id }, query: filters)
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def inspect
|
|
124
|
+
'#<Lunixi::Subscriptions>'
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
# Field rules of the subscription bodies (plans, cards, the subscription itself). Not a public API.
|
|
129
|
+
module SubscriptionRules
|
|
130
|
+
module_function
|
|
131
|
+
|
|
132
|
+
def boolean!(body, field, label = field)
|
|
133
|
+
return unless body.key?(field)
|
|
134
|
+
raise ConfigurationError, "'#{label}' must be true or false." unless [true, false].include?(body[field])
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
def count!(body, field, min, label = field)
|
|
138
|
+
return unless body.key?(field)
|
|
139
|
+
raise ConfigurationError, "'#{label}' must be an integer of at least #{min}." unless body[field].is_a?(Integer) && body[field] >= min
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def integers!(values, field, min, max = nil)
|
|
143
|
+
valid = values.is_a?(Array) && values.all? { |value| value.is_a?(Integer) && value >= min && (max.nil? || value <= max) }
|
|
144
|
+
raise ConfigurationError, "'#{field}' must be an array of integers from #{min}#{" to #{max}" if max}." unless valid
|
|
145
|
+
|
|
146
|
+
values.dup
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
# A gateway currency (ISO 4217, upper case).
|
|
150
|
+
def currency!(value, field = 'currency')
|
|
151
|
+
currency = value.is_a?(String) ? value.strip.upcase : ''
|
|
152
|
+
return currency if Money::EXPONENTS.key?(currency)
|
|
153
|
+
|
|
154
|
+
raise ConfigurationError, "'#{field}' must be a supported currency (#{Money::EXPONENTS.keys.join(', ')})."
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
# The plan body: trimmed ids, the price present (its form is checked with the other money members),
|
|
158
|
+
# a supported currency, the billing rule and the dunning counts.
|
|
159
|
+
def plan!(request)
|
|
160
|
+
body = RouteRequest.wire_hash!(request, 'plans.create request')
|
|
161
|
+
%w[productId code name].each { |field| body[field] = RouteValue.text!(body[field], field) if body.key?(field) }
|
|
162
|
+
raise ConfigurationError, 'plans.create requires price.' unless body.key?('price')
|
|
163
|
+
|
|
164
|
+
body['currency'] = currency!(body['currency']) if body.key?('currency')
|
|
165
|
+
body['billing'] = billing_rule!(body['billing']) if body.key?('billing')
|
|
166
|
+
%w[trialPeriodDays maxDunningAttempts gracePeriodDays].each { |field| count!(body, field, 0) }
|
|
167
|
+
body['dunningScheduleDays'] = integers!(body['dunningScheduleDays'], 'dunningScheduleDays', 0) if body.key?('dunningScheduleDays')
|
|
168
|
+
body
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
# { frequency, interval? (default 1), byMonthDay?, byWeekDay?, timezone?, lastDayOfMonthFix?, count? }.
|
|
172
|
+
def billing_rule!(value)
|
|
173
|
+
rule = RouteRequest.wire_keys!(value, SUBSCRIPTION_BILLING_RULE_FIELDS, 'billing')
|
|
174
|
+
rule['frequency'] = RouteValue.one_of!(rule['frequency'].to_s.upcase, SubscriptionBillingFrequency::ALL, 'billing.frequency')
|
|
175
|
+
rule['interval'] = 1 unless rule.key?('interval')
|
|
176
|
+
count!(rule, 'interval', 1, 'billing.interval')
|
|
177
|
+
count!(rule, 'count', 0, 'billing.count')
|
|
178
|
+
rule['byMonthDay'] = integers!(rule['byMonthDay'], 'billing.byMonthDay', 1, 31) if rule.key?('byMonthDay')
|
|
179
|
+
if rule.key?('byWeekDay') && !(rule['byWeekDay'].is_a?(Array) && rule['byWeekDay'].all?(String))
|
|
180
|
+
raise ConfigurationError, "'billing.byWeekDay' must be an array of week days (MO, TU, WE, TH, FR, SA, SU)."
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
boolean!(rule, 'lastDayOfMonthFix', 'billing.lastDayOfMonthFix')
|
|
184
|
+
rule
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
# The saved-card body: a paymentMethodToken, never a card number (an unknown field is refused by name).
|
|
188
|
+
def card!(request)
|
|
189
|
+
body = RouteRequest.wire_hash!(request, 'cards.add request')
|
|
190
|
+
RouteValue.one_of!(body['verificationMode'], SubscriptionCardVerificationMode::ALL, 'verificationMode') if body.key?('verificationMode')
|
|
191
|
+
count!(body, 'expireMonth', 1)
|
|
192
|
+
count!(body, 'expireYear', 1)
|
|
193
|
+
boolean!(body, 'isDefault')
|
|
194
|
+
if body.key?('supportedCurrencies')
|
|
195
|
+
raise ConfigurationError, "'supportedCurrencies' must be an array." unless body['supportedCurrencies'].is_a?(Array)
|
|
196
|
+
|
|
197
|
+
body['supportedCurrencies'] = body['supportedCurrencies'].map { |currency| currency!(currency, 'supportedCurrencies') }
|
|
198
|
+
end
|
|
199
|
+
body
|
|
200
|
+
end
|
|
201
|
+
end
|
|
202
|
+
end
|
data/lib/lunixi/version.rb
CHANGED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Lunixi
|
|
4
|
+
# Wallet (/api/v1/wallet/*): the merchant, server-to-server surface of the closed-loop wallet.
|
|
5
|
+
#
|
|
6
|
+
# Every call is signed, reads included; the merchant, its programs and its end users come from the
|
|
7
|
+
# signing key. The key needs the WALLET_SERVICE product (403 PRODUCT_NOT_ENABLED otherwise) and the
|
|
8
|
+
# wallet permission of the route (403 WALLET_INSUFFICIENT_SCOPE otherwise): wallet:*:read for reads,
|
|
9
|
+
# wallet:program|enduser|limit|fee:manage for set-up, wallet:operation:intervene to move money inside
|
|
10
|
+
# the wallet, wallet:bank-payout:operate for IBAN transfers and bank withdrawals, wallet:qr:manage,
|
|
11
|
+
# wallet:bulk:create, wallet:bulk:approve and wallet:enduser:kyc-override for the KYC level.
|
|
12
|
+
#
|
|
13
|
+
# Bodies and filters are checked against WALLET_ROUTES before anything is sent: an unknown field, a
|
|
14
|
+
# missing required field, a non-canonical amount (a number or a decimal) or a missing Idempotency-Key
|
|
15
|
+
# raises ConfigurationError. Keys are the wire names (String or Symbol; snake_case works too). Amounts
|
|
16
|
+
# are minor units as digit strings ("12550" = 125.50 TRY). A call that moves money for an end user names
|
|
17
|
+
# that end user (endUserId, buyerEndUserId); the source account must belong to them (404
|
|
18
|
+
# WALLET_NOT_FOUND otherwise).
|
|
19
|
+
#
|
|
20
|
+
# Answers are the envelope's data; lists return a Lunixi::ListPage.
|
|
21
|
+
class Wallet
|
|
22
|
+
attr_reader :programs, :end_users, :wallets, :deposits, :corporate, :fees, :transfers, :withdrawals, :operations, :topups,
|
|
23
|
+
:payments, :qr, :bulk_payouts
|
|
24
|
+
|
|
25
|
+
def initialize(client)
|
|
26
|
+
@invoker = WalletInvoker.new(client)
|
|
27
|
+
@programs = WalletPrograms.new(@invoker)
|
|
28
|
+
@end_users = WalletEndUsers.new(@invoker)
|
|
29
|
+
@wallets = WalletAccounts.new(@invoker)
|
|
30
|
+
@deposits = WalletDeposits.new(@invoker)
|
|
31
|
+
@corporate = WalletCorporate.new(@invoker)
|
|
32
|
+
@fees = WalletFees.new(@invoker)
|
|
33
|
+
@transfers = WalletTransfers.new(@invoker)
|
|
34
|
+
@withdrawals = WalletWithdrawals.new(@invoker)
|
|
35
|
+
@operations = WalletOperations.new(@invoker)
|
|
36
|
+
@topups = WalletTopups.new(@invoker)
|
|
37
|
+
@payments = WalletPayments.new(@invoker)
|
|
38
|
+
@qr = WalletQr.new(@invoker)
|
|
39
|
+
@bulk_payouts = WalletBulkPayouts.new(@invoker)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# Checks that the key reaches the wallet (signature, product, a wallet:*:manage permission).
|
|
43
|
+
def ping
|
|
44
|
+
@invoker.data('ping')
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def inspect
|
|
48
|
+
'#<Lunixi::Wallet>'
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# Sends one route of WALLET_ROUTES, signed, after checking the path segments, the body (field list,
|
|
53
|
+
# amounts, nested rows), the filters and the Idempotency-Key rule. Not a public API.
|
|
54
|
+
class WalletInvoker
|
|
55
|
+
ROUTE = %r{\A/api/v1/wallet(?:[/?]|\z)}
|
|
56
|
+
QR_IMAGE_ACCEPT = 'image/svg+xml, image/png, application/json'
|
|
57
|
+
CONTROL_CHARACTERS = /[\x00-\x1f\x7f]/
|
|
58
|
+
|
|
59
|
+
# The Idempotency-Key of a wallet route: 1–128 characters without control characters, trimmed (the
|
|
60
|
+
# gateway trims the header too).
|
|
61
|
+
def self.idempotency_key!(key, operation = nil)
|
|
62
|
+
value = key.is_a?(String) ? key.strip : ''
|
|
63
|
+
if value.empty?
|
|
64
|
+
raise ConfigurationError, "A stable Idempotency-Key is required#{" for #{operation}" if operation}. Reuse the same key when " \
|
|
65
|
+
'retrying the same operation.'
|
|
66
|
+
end
|
|
67
|
+
if value.length > WALLET_IDEMPOTENCY_KEY_MAX || value.match?(CONTROL_CHARACTERS)
|
|
68
|
+
raise ConfigurationError, "The Idempotency-Key must be 1-#{WALLET_IDEMPOTENCY_KEY_MAX} characters without control characters."
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
value
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# Minor units only: a positive canonical digit string of at most 18 digits. A number or a decimal is
|
|
75
|
+
# refused, never rounded.
|
|
76
|
+
def self.amount!(value, field)
|
|
77
|
+
return value if value.is_a?(String) && value.match?(WALLET_AMOUNT_PATTERN) && value.match?(/\A[1-9]/)
|
|
78
|
+
|
|
79
|
+
raise ConfigurationError, "'#{field}' must be minor units as a digit string (e.g. \"12550\" for 125.50), not a number."
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def initialize(client)
|
|
83
|
+
@client = client
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# The raw answer of one route. params: path segments by name; body: the request Hash; query: filters.
|
|
87
|
+
def call(name, params: {}, body: nil, query: nil, idempotency_key: nil)
|
|
88
|
+
route = WALLET_ROUTES.fetch(name)
|
|
89
|
+
path = RouteRequest.path!(route['path'], params)
|
|
90
|
+
filters = RouteRequest.query!(query, route['query'] || [], name)
|
|
91
|
+
payload = body_for(name, route, body)
|
|
92
|
+
key = key_for(name, route, idempotency_key)
|
|
93
|
+
binary = route['binary'] == true
|
|
94
|
+
@client.request(route['method'], path, body: payload, query: filters, idempotency_key: key, binary: binary,
|
|
95
|
+
accept: binary ? QR_IMAGE_ACCEPT : Client::ACCEPT_JSON)
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def data(name, **)
|
|
99
|
+
RouteResponse.unwrap(call(name, **))
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def page(name, **)
|
|
103
|
+
RouteResponse.page(call(name, **))
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
# The complete routes require operationId in the body as well; the path wins on the server.
|
|
107
|
+
def with_operation_id(name, operation_id, request)
|
|
108
|
+
route = WALLET_ROUTES.fetch(name)
|
|
109
|
+
given = RouteRequest.wire_keys!(request || {}, route['required'] + route['optional'], name)
|
|
110
|
+
if given.key?('operationId') && given['operationId'] != operation_id
|
|
111
|
+
raise ConfigurationError, 'operationId in the body must match the operation being completed.'
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
given.merge('operationId' => operation_id)
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
private
|
|
118
|
+
|
|
119
|
+
def body_for(name, route, body)
|
|
120
|
+
unless route.key?('required') || route.key?('optional')
|
|
121
|
+
raise ConfigurationError, "#{name} takes no body." unless body.nil? || body == {}
|
|
122
|
+
|
|
123
|
+
return nil
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
plain = RouteRequest.fields!(body || {}, route['required'] || [], route['optional'] || [], name)
|
|
127
|
+
plain.keys.each do |field|
|
|
128
|
+
plain[field] = self.class.amount!(plain[field], "#{name}.#{field}") if WALLET_AMOUNT_FIELDS.include?(field)
|
|
129
|
+
nested = WALLET_NESTED_FIELDS["#{name}.#{field}"]
|
|
130
|
+
plain[field] = rows!(plain[field], nested, "#{name}.#{field}") if nested
|
|
131
|
+
end
|
|
132
|
+
plain
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def rows!(rows, spec, label)
|
|
136
|
+
raise ConfigurationError, "'#{label}' must be a non-empty array." unless rows.is_a?(Array) && !rows.empty?
|
|
137
|
+
|
|
138
|
+
rows.each_with_index.map do |row, index|
|
|
139
|
+
path = "#{label}[#{index}]"
|
|
140
|
+
plain = RouteRequest.fields!(row, spec['required'], spec['optional'], path)
|
|
141
|
+
plain['amount'] = self.class.amount!(plain['amount'], "#{path}.amount") if plain.key?('amount')
|
|
142
|
+
plain
|
|
143
|
+
end
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
def key_for(name, route, key)
|
|
147
|
+
return self.class.idempotency_key!(key, name) if route['idempotencyKey']
|
|
148
|
+
raise ConfigurationError, "#{name} does not take an Idempotency-Key." unless key.nil?
|
|
149
|
+
|
|
150
|
+
nil
|
|
151
|
+
end
|
|
152
|
+
end
|
|
153
|
+
end
|