From 39867b938578de8581a8d277cf2459a16e597c7c Mon Sep 17 00:00:00 2001 From: alexandrumatei Date: Mon, 28 Oct 2024 10:42:49 +0200 Subject: [PATCH 1/2] (virtual_accounts) adds update and fetch methods to virtual_account.rb --- lib/mangopay/virtual_account.rb | 43 +++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 lib/mangopay/virtual_account.rb diff --git a/lib/mangopay/virtual_account.rb b/lib/mangopay/virtual_account.rb new file mode 100644 index 0000000..294df42 --- /dev/null +++ b/lib/mangopay/virtual_account.rb @@ -0,0 +1,43 @@ +module MangoPay + + class VirtualAccount < Resource + include HTTPCalls::Create + include HTTPCalls::Update + include HTTPCalls::Fetch + + class << self + + # Updates: + # - irreversibly deactivates a virtual account with +virtual_account_id+ + # see https://docs.mangopay.com/api-reference/virtual-accounts/deactivate-virtual-account + def deactivate(wallet_id, virtual_account_id) + url = "#{MangoPay.api_path}/wallets/#{wallet_id}/virtual-accounts/#{virtual_account_id}" + MangoPay.request(:put, url) + end + + # Fetches: + # - view a virtual account with +virtual_account_id+ + # see https://docs.mangopay.com/api-reference/virtual-accounts/view-virtual-account + def fetch(wallet_id, virtual_account_id) + url = "#{MangoPay.api_path}/wallets/#{wallet_id}/virtual-accounts/#{virtual_account_id}" + MangoPay.request(:get, url, {}) + end + + # Fetches: + # - view virtual accounts for given +wallet_id+ + # see https://docs.mangopay.com/api-reference/virtual-accounts/list-virtual-accounts-wallet + def fetch_all(wallet_id, filters = {}) + url = "#{MangoPay.api_path}/wallets/#{wallet_id}/virtual-accounts" + MangoPay.request(:get, url, {}, filters) + end + + # Fetches: + # Allows to check which account countries and currencies are available + # see https://docs.mangopay.com/api-reference/virtual-accounts/view-virtual-account-availabilities + def fetch_availabilities(filters = {}) + url = "#{MangoPay.api_path}/virtual-accounts/availability" + MangoPay.request(:get, url, {}, filters) + end + end + end +end \ No newline at end of file From edf894488aaab936f25945aee2ad237c16e07425 Mon Sep 17 00:00:00 2001 From: Iulian Masar Date: Mon, 28 Oct 2024 16:30:58 +0200 Subject: [PATCH 2/2] added tests --- lib/mangopay.rb | 1 + lib/mangopay/virtual_account.rb | 13 +++---- spec/mangopay/shared_resources.rb | 17 +++++++++ spec/mangopay/virtual_account_spec.rb | 52 +++++++++++++++++++++++++++ 4 files changed, 77 insertions(+), 6 deletions(-) create mode 100644 spec/mangopay/virtual_account_spec.rb diff --git a/lib/mangopay.rb b/lib/mangopay.rb index 6a2c275..6eb8c32 100644 --- a/lib/mangopay.rb +++ b/lib/mangopay.rb @@ -47,6 +47,7 @@ module MangoPay autoload :Deposit, 'mangopay/deposit' autoload :Conversion, 'mangopay/conversion' autoload :PaymentMethodMetadata, 'mangopay/payment_method_metadata' + autoload :VirtualAccount, 'mangopay/virtual_account' # temporary autoload :Temp, 'mangopay/temp' diff --git a/lib/mangopay/virtual_account.rb b/lib/mangopay/virtual_account.rb index 294df42..4185a56 100644 --- a/lib/mangopay/virtual_account.rb +++ b/lib/mangopay/virtual_account.rb @@ -1,18 +1,19 @@ module MangoPay class VirtualAccount < Resource - include HTTPCalls::Create - include HTTPCalls::Update - include HTTPCalls::Fetch - class << self + # Creates a new virtual account + def create(wallet_id, params, idempotency_key = nil) + url = "#{MangoPay.api_path}/wallets/#{wallet_id}/virtual-accounts" + MangoPay.request(:post, url, params, {}, idempotency_key) + end # Updates: # - irreversibly deactivates a virtual account with +virtual_account_id+ # see https://docs.mangopay.com/api-reference/virtual-accounts/deactivate-virtual-account - def deactivate(wallet_id, virtual_account_id) + def deactivate(wallet_id, virtual_account_id, idempotency_key = nil) url = "#{MangoPay.api_path}/wallets/#{wallet_id}/virtual-accounts/#{virtual_account_id}" - MangoPay.request(:put, url) + MangoPay.request(:put, url, {}, {}, idempotency_key) end # Fetches: diff --git a/spec/mangopay/shared_resources.rb b/spec/mangopay/shared_resources.rb index 8b29b4a..e5b30eb 100644 --- a/spec/mangopay/shared_resources.rb +++ b/spec/mangopay/shared_resources.rb @@ -998,4 +998,21 @@ def get_payment_method_metadata Bin: pay_in['CardInfo']['BIN'] ) end +end + +############################################### +shared_context 'virtual_account' do + ############################################### + include_context 'users' + include_context 'wallets' + + def new_virtual_account(wallet_id) + create_virtual_account = { + Country: 'FR', + VirtualAccountPurpose: 'Collection', + Tag: 'create virtual account tag' + } + + MangoPay::VirtualAccount.create(wallet_id, create_virtual_account) + end end \ No newline at end of file diff --git a/spec/mangopay/virtual_account_spec.rb b/spec/mangopay/virtual_account_spec.rb new file mode 100644 index 0000000..bb0ecad --- /dev/null +++ b/spec/mangopay/virtual_account_spec.rb @@ -0,0 +1,52 @@ +describe MangoPay::VirtualAccount do + include_context 'virtual_account' + include_context 'wallets' + + describe 'CREATE' do + it 'can create a Virtual Account' do + wallet = new_wallet + virtual_account = new_virtual_account(wallet['Id']) + expect(virtual_account).not_to be_nil + end + end + + describe 'DEACTIVATE' do + it 'deactivates a Virtual Account' do + wallet = new_wallet + virtual_account = new_virtual_account(wallet['Id']) + deactivated = MangoPay::VirtualAccount.deactivate(wallet['Id'], virtual_account['Id']) + expect(deactivated).not_to be_nil + expect(deactivated['Status']).to eq 'CLOSED' + expect(deactivated['Id']).to eq(virtual_account['Id']) + end + end + + describe 'FETCH' do + it 'can get a Virtual Account' do + wallet = new_wallet + virtual_account = new_virtual_account(wallet['Id']) + fetched = MangoPay::VirtualAccount.fetch(wallet['Id'], virtual_account['Id']) + expect(fetched).not_to be_nil + expect(fetched['Id']).to eq(virtual_account['Id']) + end + end + + describe 'FETCH ALL' do + it 'can get all Virtual Accounts for a wallet' do + wallet = new_wallet + virtual_account = new_virtual_account(wallet['Id']) + fetched_list = MangoPay::VirtualAccount.fetch_all(wallet['Id']) + expect(fetched_list).not_to be_nil + expect(fetched_list[0]['Id']).to eq(virtual_account['Id']) + end + end + + describe 'FETCH AVAILABILITIES' do + it 'get availabilities' do + availabilities = MangoPay::VirtualAccount.fetch_availabilities + expect(availabilities).not_to be_nil + expect(availabilities['Collection']).not_to be_nil + expect(availabilities['UserOwned']).not_to be_nil + end + end +end