diff --git a/lib/investec_open_api/client.rb b/lib/investec_open_api/client.rb index df588ed..39dbf00 100644 --- a/lib/investec_open_api/client.rb +++ b/lib/investec_open_api/client.rb @@ -2,6 +2,7 @@ require "faraday_middleware" require "investec_open_api/models/account" require "investec_open_api/models/transaction" +require "investec_open_api/models/transfer" class InvestecOpenApi::Client INVESTEC_API_URL="https://openapi.investec.com/" @@ -24,6 +25,26 @@ def transactions(account_id) end end + def transfer(account_id, to_account_id, amount, my_reference, their_reference) + response = connection.post( + "za/pb/v1/accounts/#{account_id}/transfermultiple", + { + transferList: [ + { + beneficiaryAccountId: to_account_id, + amount: amount, + myReference: my_reference, + theirReference: their_reference + } + ] + } + ) + + response.body["data"]["TransferResponses"].map do |transfer_raw| + InvestecOpenApi::Models::Transfer.from_api(transfer_raw) + end + end + private def get_oauth_token diff --git a/lib/investec_open_api/models/transfer.rb b/lib/investec_open_api/models/transfer.rb new file mode 100644 index 0000000..12f7c42 --- /dev/null +++ b/lib/investec_open_api/models/transfer.rb @@ -0,0 +1,38 @@ +module InvestecOpenApi::Models + class Transfer < Base + attribute :payment_reference_number + attribute :payment_date + attribute :status + attribute :beneficiary_name + attribute :beneficiary_account_id + attribute :authorisation_required + + def self.from_api(params = {}) + if params['PaymentReferenceNumber'].present? + params['payment_reference_number'] = params['PaymentReferenceNumber'] + end + + if params['PaymentDate'].present? + params['payment_date'] = params['PaymentDate'] + end + + if params['Status'].present? + params['status'] = params['Status'] + end + + if params['BeneficiaryName'].present? + params['beneficiary_name'] = params['BeneficiaryName'] + end + + if params['BeneficiaryAccountId'].present? + params['beneficiary_account_id'] = params['BeneficiaryAccountId'] + end + + if params['AuthorisationRequired'].present? + params['authorisation_required'] = params['AuthorisationRequired'] + end + + super + end + end +end diff --git a/spec/lib/investec_open_api/client_spec.rb b/spec/lib/investec_open_api/client_spec.rb index dcfe629..a4f63fa 100644 --- a/spec/lib/investec_open_api/client_spec.rb +++ b/spec/lib/investec_open_api/client_spec.rb @@ -140,4 +140,52 @@ expect(transactions.first).to be_an_instance_of(InvestecOpenApi::Models::Transaction) end end + + describe "#transfer" do + before do + stub_request(:post, "#{api_url}za/pb/v1/accounts/12345/transfermultiple") + .with( + body: { + transferList: [ + { + beneficiaryAccountId: '6789', + amount: 10, + myReference: 'Library Transfer', + theirReference: 'Library Transfer' + } + ] + }, + headers: { + "Accept" => "application/json", + "Accept-Encoding" => "gzip;q=1.0,deflate;q=0.6,identity;q=0.3", + "Authorization" => "Bearer 123", + "User-Agent" => "Faraday v1.10.3" + } + ) + .to_return( + body: { + data: { + "TransferResponses": [ + { + "PaymentReferenceNumber": "UBP0111111111", + "PaymentDate": "10/05/2023", + "Status": "- No authorisation necessary
- Payment/Transfer effective date 10/05/2023", + "BeneficiaryName": "API transfer", + "BeneficiaryAccountId": "6789", + "AuthorisationRequired": false + } + ] + } + }.to_json + ) + + client.authenticate! + end + + it "returns transfer details based on the transfer as InvestecOpenApi::Models::Transfer instances" do + transferResponses = client.transfer('12345', '6789', 10, 'Library Transfer', 'Library Transfer') + + expect(transferResponses.first).to be_an_instance_of(InvestecOpenApi::Models::Transfer) + end + end end