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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +62 -0
- data/README.md +278 -10
- data/README.tr.md +282 -8
- data/docs/ruby.en.md +11 -1
- data/docs/ruby.tr.md +11 -1
- 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 +171 -0
- data/lib/lunixi/client.rb +57 -16
- data/lib/lunixi/direct_method_payment_request.rb +245 -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 +268 -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/order_details.rb +115 -0
- 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 +136 -0
- data/lib/lunixi/payment_links.rb +152 -0
- data/lib/lunixi/payment_values.rb +86 -0
- data/lib/lunixi/payments.rb +170 -8
- 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 +150 -0
- data/lib/lunixi/subscription_resources.rb +357 -0
- data/lib/lunixi/subscription_values.rb +233 -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 +407 -0
- data/lib/lunixi/wallet_routes.rb +126 -0
- data/lib/lunixi/wallet_values.rb +71 -0
- data/lib/lunixi/wire_money.rb +67 -0
- data/lib/lunixi.rb +58 -0
- metadata +59 -1
|
@@ -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
|