diff --git a/README.md b/README.md index 653d123..db0b78b 100644 --- a/README.md +++ b/README.md @@ -248,6 +248,8 @@ customer = client.v2.customers.create(email: 'customer@example.com', name: 'Jon payment_methods = client.v2.payment_methods.list(customer: 'customer_id') client.v2.payment_methods.cancel('payment_method_id') mandate = client.v2.payment_methods.mandate_document('payment_method_id') +installments = client.v2.payment_methods.installments('payment_method_id', amount: 1000, currency: 'CLP') +# => { available_installments: [3, 6, 12] } # Mexican direct debit mandates are sent as multipart. Nested fields are sent in # bracket notation and consent documents accept a path or an IO. payment_method = client.v2.payment_methods.create( diff --git a/lib/fintoc/errors.rb b/lib/fintoc/errors.rb index e013714..05a5a28 100644 --- a/lib/fintoc/errors.rb +++ b/lib/fintoc/errors.rb @@ -214,6 +214,7 @@ class RefreshIntentInProgressError < FintocError; end class RejectedRefreshIntentError < FintocError; end class SenderBlockedAccountError < FintocError; end class AmountOutOfRangeError < FintocError; end + class ChargeProcessingFeeNotAllowedError < FintocError; end class CountryNotSupportedError < FintocError; end class DirectDebitSuspendedError < FintocError; end class FileTooLargeError < FintocError; end diff --git a/lib/fintoc/v2/managers/payment_methods_manager.rb b/lib/fintoc/v2/managers/payment_methods_manager.rb index d2ded29..b9cc01e 100644 --- a/lib/fintoc/v2/managers/payment_methods_manager.rb +++ b/lib/fintoc/v2/managers/payment_methods_manager.rb @@ -36,6 +36,11 @@ def mandate_document(payment_method_id) @client.get(version: :v2).call("payment_methods/#{payment_method_id}/mandate_document") end + def installments(payment_method_id, amount:, currency:) + @client.get(version: :v2) + .call("payment_methods/#{payment_method_id}/installments", amount:, currency:) + end + private def flatten_form(fields, prefix = nil) diff --git a/spec/lib/fintoc/base_client_spec.rb b/spec/lib/fintoc/base_client_spec.rb index 5ea07d5..174ea41 100644 --- a/spec/lib/fintoc/base_client_spec.rb +++ b/spec/lib/fintoc/base_client_spec.rb @@ -449,7 +449,8 @@ def expect_error(klass, status:, code:) 'credit_already_simulated' => 'CreditAlreadySimulatedError', 'transfer_not_cancellable' => 'TransferNotCancellableError', 'usdt_collection_already_open' => 'UsdtCollectionAlreadyOpenError', - 'payment_method_canceled' => 'PaymentMethodCanceledError' + 'payment_method_canceled' => 'PaymentMethodCanceledError', + 'charge_processing_fee_not_allowed' => 'ChargeProcessingFeeNotAllowedError' }.each do |code, klass| it "resolves the #{code} error" do expect(client.send(:error_class, code)).to eq(Fintoc::Errors.const_get(klass)) diff --git a/spec/lib/fintoc/v2/managers/payment_methods_manager_spec.rb b/spec/lib/fintoc/v2/managers/payment_methods_manager_spec.rb index dd4ccc1..aa02167 100644 --- a/spec/lib/fintoc/v2/managers/payment_methods_manager_spec.rb +++ b/spec/lib/fintoc/v2/managers/payment_methods_manager_spec.rb @@ -80,6 +80,14 @@ expect(manager.list(payment_options: { amount: 1000 })).to contain_exactly(a_kind_of(Fintoc::V2::PaymentMethod)) end + + it 'sends the off_session payment option in bracket notation' do + stub_request(:get, "#{collection_url}?payment_options%5Boff_session%5D=true") + .to_return(body: [payload].to_json, headers: headers) + + expect(manager.list(payment_options: { off_session: true })) + .to contain_exactly(a_kind_of(Fintoc::V2::PaymentMethod)) + end end describe '#get' do @@ -120,4 +128,32 @@ expect(manager.mandate_document(payload[:id])).to eq(response) end end + + describe '#installments' do + let(:response) { { available_installments: [3, 6, 12] } } + + before do + stub_request(:get, "#{resource_url}/installments") + .with(query: { amount: '1000', currency: 'CLP' }) + .to_return(body: response.to_json, headers: headers) + end + + it 'returns the available installments for the amount' do + expect(manager.installments(payload[:id], amount: 1000, currency: 'CLP')).to eq(response) + end + + it 'raises the API error when the card is not available for installments' do + stub_request(:get, "#{resource_url}/installments") + .with(query: { amount: '1000', currency: 'CLP' }) + .to_return( + status: 422, + body: { error: { type: 'invalid_request_error', code: 'invalid_payment_method_state', + message: 'Payment method is not available for installments' } }.to_json, + headers: headers + ) + + expect { manager.installments(payload[:id], amount: 1000, currency: 'CLP') } + .to raise_error(Fintoc::Errors::InvalidPaymentMethodStateError) + end + end end