mangopay 3.0.1 → 3.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -1
  3. data/README.md +43 -14
  4. data/lib/mangopay.rb +43 -34
  5. data/lib/mangopay/authorization_token.rb +76 -0
  6. data/lib/mangopay/bank_account.rb +14 -7
  7. data/lib/mangopay/client.rb +11 -17
  8. data/lib/mangopay/errors.rb +22 -2
  9. data/lib/mangopay/http_calls.rb +24 -2
  10. data/lib/mangopay/legal_user.rb +1 -1
  11. data/lib/mangopay/natural_user.rb +1 -1
  12. data/lib/mangopay/payin.rb +2 -2
  13. data/lib/mangopay/payout.rb +1 -1
  14. data/lib/mangopay/resource.rb +1 -1
  15. data/lib/mangopay/transaction.rb +17 -2
  16. data/lib/mangopay/version.rb +1 -1
  17. data/spec/lib/mangopay/authorization_token_spec.rb +72 -0
  18. data/spec/lib/mangopay/bank_account_spec.rb +25 -26
  19. data/spec/lib/mangopay/card_registration_spec.rb +40 -16
  20. data/spec/lib/mangopay/client_spec.rb +14 -9
  21. data/spec/lib/mangopay/configuration_spec.rb +20 -0
  22. data/spec/lib/mangopay/fetch_filters_spec.rb +65 -0
  23. data/spec/lib/mangopay/payin_card_direct_spec.rb +70 -0
  24. data/spec/lib/mangopay/payin_card_web_spec.rb +53 -0
  25. data/spec/lib/mangopay/payout_bankwire_spec.rb +56 -0
  26. data/spec/lib/mangopay/shared_resources.rb +123 -82
  27. data/spec/lib/mangopay/transaction_spec.rb +48 -2
  28. data/spec/lib/mangopay/transfer_spec.rb +51 -15
  29. data/spec/lib/mangopay/user_spec.rb +1 -0
  30. data/spec/lib/mangopay/wallet_spec.rb +31 -32
  31. data/spec/spec_helper.rb +11 -4
  32. data/spec/tmp/.keep +0 -0
  33. metadata +17 -8
  34. data/spec/lib/mangopay/card_spec.rb +0 -35
  35. data/spec/lib/mangopay/payin_spec.rb +0 -34
  36. data/spec/lib/mangopay/payout_spec.rb +0 -34
@@ -5,7 +5,7 @@ module MangoPay
5
5
 
6
6
  def self.url(id = nil)
7
7
  if id
8
- "/v2/#{MangoPay.configuration.client_id}/users/natural/#{CGI.escape(id)}"
8
+ "/v2/#{MangoPay.configuration.client_id}/users/natural/#{CGI.escape(id.to_s)}"
9
9
  else
10
10
  "/v2/#{MangoPay.configuration.client_id}/users/natural"
11
11
  end
@@ -8,7 +8,7 @@ module MangoPay
8
8
  class Web < Resource
9
9
  include MangoPay::HTTPCalls::Create
10
10
  private
11
- def self.url(id = nil)
11
+ def self.url(*)
12
12
  "/v2/#{MangoPay.configuration.client_id}/payins/card/#{CGI.escape(class_name.downcase)}"
13
13
  end
14
14
  end
@@ -16,7 +16,7 @@ module MangoPay
16
16
  class Direct < Resource
17
17
  include MangoPay::HTTPCalls::Create
18
18
  private
19
- def self.url(id = nil)
19
+ def self.url(*)
20
20
  "/v2/#{MangoPay.configuration.client_id}/payins/card/#{CGI.escape(class_name.downcase)}"
21
21
  end
22
22
  end
@@ -7,7 +7,7 @@ module MangoPay
7
7
 
8
8
  private
9
9
 
10
- def self.url(id = nil)
10
+ def self.url(*)
11
11
  "/v2/#{MangoPay.configuration.client_id}/payouts/bankwire"
12
12
  end
13
13
  end
@@ -13,7 +13,7 @@ module MangoPay
13
13
  raise NotImplementedError.new('Resource is an abstract class. Do not use it directly.')
14
14
  end
15
15
  if id
16
- "/v2/#{MangoPay.configuration.client_id}/#{CGI.escape(class_name.downcase)}s/#{CGI.escape(id)}"
16
+ "/v2/#{MangoPay.configuration.client_id}/#{CGI.escape(class_name.downcase)}s/#{CGI.escape(id.to_s)}"
17
17
  else
18
18
  "/v2/#{MangoPay.configuration.client_id}/#{CGI.escape(class_name.downcase)}s"
19
19
  end
@@ -2,10 +2,25 @@ module MangoPay
2
2
  class Transaction < Resource
3
3
  include MangoPay::HTTPCalls::Fetch
4
4
 
5
+ # Fetches list of transactions belonging to the given +wallet_id+.
6
+ #
7
+ # Optional +filters+ is a hash accepting following keys:
8
+ # - +page+, +per_page+: pagination params (see MangoPay::HTTPCalls::Fetch::ClassMethods#fetch)
9
+ # - +Status+: TransactionStatus {CREATED, SUCCEEDED, FAILED}
10
+ # - +Type+: TransactionType {PAYIN, PAYOUT, TRANSFER}
11
+ # - +Nature+: TransactionNature {NORMAL, REFUND, REPUDIATION}
12
+ # - +Direction+: TransactionDirection {DEBIT, CREDIT}
13
+ # - +BeforeDate+ (timestamp): filters transactions with CreationDate _before_ this date
14
+ # - +AfterDate+ (timestamp): filters transactions with CreationDate _after_ this date
15
+ #
16
+ def self.fetch(wallet_id, filters={})
17
+ MangoPay.request(:get, url(wallet_id), {}, filters)
18
+ end
19
+
5
20
  private
6
21
 
7
- def self.url(id)
8
- "/v2/#{MangoPay.configuration.client_id}/wallets/#{CGI.escape(id)}/transactions"
22
+ def self.url(wallet_id)
23
+ "/v2/#{MangoPay.configuration.client_id}/wallets/#{CGI.escape(wallet_id.to_s)}/transactions"
9
24
  end
10
25
  end
11
26
  end
@@ -1,3 +1,3 @@
1
1
  module MangoPay
2
- VERSION = '3.0.1'
2
+ VERSION = '3.0.2'
3
3
  end
@@ -0,0 +1,72 @@
1
+ require_relative '../../spec_helper'
2
+
3
+ describe MangoPay::AuthorizationToken do
4
+
5
+ _default_static_storage = MangoPay::AuthorizationToken::Manager.storage
6
+ _new_file_storage = MangoPay::AuthorizationToken::FileStorage.new
7
+
8
+ it 'uses StaticStorage strategy by default' do
9
+ storage = _default_static_storage
10
+ expect(storage).to be_kind_of MangoPay::AuthorizationToken::StaticStorage
11
+ end
12
+
13
+ it 'allows set different storage strategies' do
14
+
15
+ # set to FileStorage
16
+ file_storage = _new_file_storage
17
+ MangoPay::AuthorizationToken::Manager.storage = file_storage
18
+ storage = MangoPay::AuthorizationToken::Manager.storage
19
+ expect(storage).to be file_storage
20
+
21
+ # reset to StaticStorage
22
+ static_storage = _default_static_storage
23
+ MangoPay::AuthorizationToken::Manager.storage = static_storage
24
+ storage = MangoPay::AuthorizationToken::Manager.storage
25
+ expect(storage).to be static_storage
26
+ end
27
+
28
+ describe MangoPay::AuthorizationToken::StaticStorage do
29
+ it 'shares tokens between calls' do
30
+ MangoPay::AuthorizationToken::Manager.storage = _default_static_storage
31
+ users1 = MangoPay::User.fetch()
32
+ token1 = MangoPay::AuthorizationToken::Manager.get_token
33
+ users2 = MangoPay::User.fetch()
34
+ token2 = MangoPay::AuthorizationToken::Manager.get_token
35
+ expect(token1).to eq token2
36
+ expect(token1).to be token2 # moreover, it's the same instance
37
+ end
38
+ end
39
+
40
+ describe MangoPay::AuthorizationToken::FileStorage do
41
+ it 'shares tokens between calls' do
42
+ MangoPay::AuthorizationToken::Manager.storage = _new_file_storage
43
+
44
+ users1 = MangoPay::User.fetch()
45
+ token1 = MangoPay::AuthorizationToken::Manager.get_token
46
+ users2 = MangoPay::User.fetch()
47
+ token2 = MangoPay::AuthorizationToken::Manager.get_token
48
+ expect(token1).to eq token2
49
+ expect(token1).not_to be token2 # it's NOT the same instance
50
+
51
+ MangoPay::AuthorizationToken::Manager.storage = _default_static_storage # cleanup
52
+ end
53
+
54
+ it 'uses temp file with serialized token' do
55
+ MangoPay::AuthorizationToken::Manager.storage = _new_file_storage
56
+
57
+ file_path = _new_file_storage.file_path
58
+ dir_path = MangoPay.configuration.temp_dir
59
+ expect(file_path.start_with? dir_path).to be_true
60
+
61
+ token = MangoPay::AuthorizationToken::Manager.get_token
62
+ expect(File.exists? file_path).to be_true
63
+
64
+ f = File.open(file_path, File::RDONLY)
65
+ txt = f.read
66
+ f.close
67
+ expect(txt.include? token['access_token']).to be_true
68
+
69
+ MangoPay::AuthorizationToken::Manager.storage = _default_static_storage # cleanup
70
+ end
71
+ end
72
+ end
@@ -1,26 +1,25 @@
1
- require_relative '../../spec_helper'
2
-
3
- describe MangoPay::BankAccount do
4
- include_context 'users'
5
- include_context 'bank_details'
6
-
7
- describe 'CREATE' do
8
- it 'creates a new bank detail' do
9
- expect(new_iban_bank_detail['Id']).not_to be_nil
10
- end
11
- end
12
-
13
- describe 'FETCH' do
14
-
15
- it 'fetches all the bank details' do
16
- bank_details = MangoPay::BankAccount.fetch(new_iban_bank_detail['UserId'])
17
- expect(bank_details).to be_kind_of(Array)
18
- expect(bank_details[0]['Id']).to eq(new_iban_bank_detail['Id'])
19
- end
20
-
21
- it 'fetches one bank detail' do
22
- bank_detail = MangoPay::BankAccount.fetch(new_iban_bank_detail['UserId'], new_iban_bank_detail['Id'])
23
- expect(bank_detail['Id']).to eq(new_iban_bank_detail['Id'])
24
- end
25
- end
26
- end
1
+ require_relative '../../spec_helper'
2
+
3
+ describe MangoPay::BankAccount do
4
+ include_context 'bank_accounts'
5
+
6
+ describe 'CREATE' do
7
+ it 'creates a new bank detail' do
8
+ expect(new_bank_account['Id']).not_to be_nil
9
+ end
10
+ end
11
+
12
+ describe 'FETCH' do
13
+
14
+ it 'fetches all the bank details' do
15
+ list = MangoPay::BankAccount.fetch(new_bank_account['UserId'])
16
+ expect(list).to be_kind_of(Array)
17
+ expect(list[0]['Id']).to eq(new_bank_account['Id'])
18
+ end
19
+
20
+ it 'fetches single bank detail' do
21
+ single = MangoPay::BankAccount.fetch(new_bank_account['UserId'], new_bank_account['Id'])
22
+ expect(single['Id']).to eq(new_bank_account['Id'])
23
+ end
24
+ end
25
+ end
@@ -2,26 +2,28 @@ require_relative '../../spec_helper'
2
2
 
3
3
  describe MangoPay::CardRegistration do
4
4
  include_context 'users'
5
- include_context 'card_registration'
5
+ include_context 'payins'
6
6
 
7
7
  describe 'CREATE' do
8
8
  it 'creates a new card registration' do
9
- expect(new_card_registration['Id']).not_to be_nil
10
- expect(new_card_registration['Id'].to_i).to be > 0
11
- expect(new_card_registration['AccessKey']).not_to be_nil
12
- expect(new_card_registration['PreregistrationData']).not_to be_nil
13
- expect(new_card_registration['CardRegistrationURL']).not_to be_nil
14
- expect(new_card_registration['CardId']).to be_nil
15
- expect(new_card_registration['RegistrationData']).to be_nil
16
- expect(new_card_registration['UserId']).to eq(new_natural_user["Id"])
17
- expect(new_card_registration['Currency']).to eq('EUR')
18
- expect(new_card_registration['Status']).to eq('CREATED')
9
+ created = new_card_registration
10
+ expect(created['Id']).not_to be_nil
11
+ expect(created['Id'].to_i).to be > 0
12
+ expect(created['AccessKey']).not_to be_nil
13
+ expect(created['PreregistrationData']).not_to be_nil
14
+ expect(created['CardRegistrationURL']).not_to be_nil
15
+ expect(created['RegistrationData']).to be_nil
16
+ expect(created['CardId']).to be_nil
17
+ expect(created['UserId']).to eq(new_natural_user["Id"])
18
+ expect(created['Currency']).to eq('EUR')
19
+ expect(created['Status']).to eq('CREATED')
19
20
  end
20
21
  end
21
22
 
22
23
  describe 'UPDATE' do
23
24
  it 'updates a card registration' do
24
- updated = MangoPay::CardRegistration.update(new_card_registration['Id'] ,{
25
+ created = new_card_registration
26
+ updated = MangoPay::CardRegistration.update(created['Id'] ,{
25
27
  RegistrationData: 'test RegistrationData'
26
28
  })
27
29
  expect(updated['RegistrationData']).to eq('test RegistrationData')
@@ -30,10 +32,32 @@ describe MangoPay::CardRegistration do
30
32
 
31
33
  describe 'FETCH' do
32
34
  it 'fetches a card registration' do
33
- fetched = MangoPay::CardRegistration.fetch(new_card_registration['Id'])
34
- expect(fetched['Id']).to eq(new_card_registration['Id'])
35
- expect(fetched['UserId']).to eq(new_card_registration['UserId'])
36
- expect(fetched['Tag']).to eq(new_card_registration['Tag'])
35
+ created = new_card_registration
36
+ fetched = MangoPay::CardRegistration.fetch(created['Id'])
37
+ expect(fetched['Id']).to eq(created['Id'])
38
+ expect(fetched['UserId']).to eq(created['UserId'])
39
+ expect(fetched['Tag']).to eq(created['Tag'])
40
+ end
41
+ end
42
+
43
+ describe 'TOKENIZATION PROCESS' do
44
+ it 'fills-in registration data and links it to a newly created card' do
45
+ completed = new_card_registration_completed
46
+ reg_data = completed['RegistrationData']
47
+ card_id = completed['CardId']
48
+
49
+ # reg data filled-in
50
+ expect(reg_data).not_to be_nil
51
+ expect(reg_data).to be_kind_of String
52
+ expect(reg_data).not_to be_empty
53
+
54
+ # card id filled-in...
55
+ expect(card_id).not_to be_nil
56
+ expect(card_id.to_i).to be > 0
57
+
58
+ # ...and points to existing (newly created) card
59
+ card = MangoPay::Card.fetch(card_id)
60
+ expect(card['Id']).to eq card_id
37
61
  end
38
62
  end
39
63
 
@@ -1,5 +1,4 @@
1
1
  require_relative '../../spec_helper'
2
- require 'securerandom'
3
2
 
4
3
  describe MangoPay::Client do
5
4
  include_context 'clients'
@@ -12,17 +11,23 @@ describe MangoPay::Client do
12
11
  end
13
12
 
14
13
  it 'refuses the client id' do
15
- expect(wrong_client['errors']).not_to be_nil
14
+ expect { wrong_client['errors'] }.to raise_error { |err|
15
+ err.should be_a MangoPay::ResponseError
16
+ err.type.should eq 'param_error'
17
+ }
16
18
  end
17
19
 
18
20
  it "ClientId_already_exist" do
19
- existing_client_error = MangoPay::Client.create({
20
- 'ClientId' => new_client['ClientId'],
21
- 'Name' => 'What a nice name',
22
- 'Email' => 'clientemail@email.com'
23
- })
24
- expect(existing_client_error['Type']).to eq('ClientId_already_exist')
25
- expect(existing_client_error['Message']).to eq('A partner with this ClientId already exist')
21
+ expect {
22
+ MangoPay::Client.create({
23
+ 'ClientId' => new_client['ClientId'],
24
+ 'Name' => 'What a nice name',
25
+ 'Email' => 'clientemail@email.com'
26
+ })
27
+ }.to raise_error { |err|
28
+ err.should be_a MangoPay::ResponseError
29
+ err.type.should eq 'ClientId_already_exist'
30
+ }
26
31
  end
27
32
  end
28
33
  end
@@ -0,0 +1,20 @@
1
+ require_relative '../../spec_helper'
2
+
3
+ describe MangoPay::Configuration do
4
+
5
+ it 'fails when calling with wrong client credentials' do
6
+ expect {
7
+ c = MangoPay.configuration
8
+ c.client_id = 'test_asd'
9
+ c.client_passphrase = '00000'
10
+ MangoPay::User.fetch()
11
+ }.to raise_error(MangoPay::ResponseError)
12
+ end
13
+
14
+ it 'goes ok when calling with correct client credentials' do
15
+ reset_mangopay_configuration
16
+ users = MangoPay::User.fetch()
17
+ expect(users).to be_kind_of(Array)
18
+ end
19
+
20
+ end
@@ -0,0 +1,65 @@
1
+ require_relative '../../spec_helper'
2
+
3
+ describe 'FETCH WITH FILTERS' do
4
+ include_context 'users'
5
+ include_context 'bank_accounts'
6
+
7
+ describe 'FETCH USERS' do
8
+
9
+ it 'returns single hash when called with id' do
10
+ id = new_natural_user['Id']
11
+ res = MangoPay::User.fetch(id)
12
+ expect(res).to be_kind_of Hash
13
+ expect(res['Id']).to eq id
14
+ end
15
+
16
+ it 'returns list of hashes with default pagination when called with no param' do
17
+ res = MangoPay::User.fetch()
18
+ expect(res).to be_kind_of Array
19
+ expect(res.count).to eq 10
20
+ end
21
+
22
+ it 'returns list of hashes with default pagination when called with empty filters' do
23
+ res = MangoPay::User.fetch(filters = {})
24
+ expect(res).to be_kind_of Array
25
+ expect(res.count).to eq 10
26
+ expect(filters['total_items']).to be > 0
27
+ expect(filters['total_pages']).to be > 0
28
+ end
29
+
30
+ it 'returns list of hashes with correct pagination when called with pagination filters' do
31
+ res = MangoPay::User.fetch(filters = {page:2, per_page:3})
32
+ expect(res).to be_kind_of Array
33
+ expect(res.count).to eq 3 # 3 items per page as requested
34
+ expect(filters['total_items']).to be > 0
35
+ expect(filters['total_pages']).to be > 0
36
+ end
37
+ end
38
+
39
+ describe 'FETCH BANK ACCOUNTS OF USER' do
40
+
41
+ it 'returns single hash when called with user id and account id' do
42
+ bank_account = new_bank_account
43
+ res = MangoPay::BankAccount.fetch(bank_account['UserId'], bank_account['Id'])
44
+ expect(res).to be_kind_of Hash
45
+ expect(res['Id']).to eq bank_account['Id']
46
+ end
47
+
48
+ it 'returns list of hashes with default pagination when called with user id only' do
49
+ bank_account = new_bank_account
50
+ res = MangoPay::BankAccount.fetch(bank_account['UserId'])
51
+ expect(res).to be_kind_of Array
52
+ expect(res.count).to eq 1 # not enough items to fill-in whole page
53
+ end
54
+
55
+ it 'returns list of hashes with default pagination when called with empty filters' do
56
+ bank_account = new_bank_account
57
+ res = MangoPay::BankAccount.fetch(bank_account['UserId'], filters = {})
58
+ expect(res).to be_kind_of Array
59
+ expect(res.count).to eq 1 # not enough items to fill-in whole page
60
+ expect(filters['total_items']).to eq 1
61
+ expect(filters['total_pages']).to eq 1
62
+ end
63
+ end
64
+
65
+ end
@@ -0,0 +1,70 @@
1
+ require_relative '../../spec_helper'
2
+
3
+ describe MangoPay::PayIn::Card::Direct, type: :feature do
4
+ include_context 'wallets'
5
+ include_context 'payins'
6
+
7
+ def check_type_and_status(payin)
8
+ expect(payin['Type']).to eq('PAYIN')
9
+ expect(payin['Nature']).to eq('REGULAR')
10
+ expect(payin['PaymentType']).to eq('CARD')
11
+ expect(payin['ExecutionType']).to eq('DIRECT')
12
+
13
+ # SUCCEEDED
14
+ expect(payin['Status']).to eq('SUCCEEDED')
15
+ expect(payin['ResultCode']).to eq('000000')
16
+ expect(payin['ResultMessage']).to eq('Success')
17
+ expect(payin['ExecutionDate']).to be > 0
18
+ end
19
+
20
+ describe 'CREATE' do
21
+ it 'creates a card direct payin' do
22
+ created = new_payin_card_direct
23
+ expect(created['Id']).not_to be_nil
24
+ check_type_and_status(created)
25
+ end
26
+ end
27
+
28
+ describe 'FETCH' do
29
+ it 'fetches a payin' do
30
+ created = new_payin_card_direct
31
+ fetched = MangoPay::PayIn.fetch(created['Id'])
32
+ expect(fetched['Id']).to eq(created['Id'])
33
+ expect(fetched['CreationDate']).to eq(created['CreationDate'])
34
+ expect(fetched['CreditedFunds']).to eq(created['CreditedFunds'])
35
+ expect(fetched['CreditedWalletId']).to eq(created['CreditedWalletId'])
36
+ check_type_and_status(created)
37
+ check_type_and_status(fetched)
38
+ end
39
+ end
40
+
41
+ describe 'REFUND' do
42
+ it 'refunds a payin' do
43
+ payin = new_payin_card_direct
44
+ refund = MangoPay::PayIn.refund(payin['Id'], {AuthorId: payin['AuthorId']})
45
+ expect(refund['Id']).not_to be_nil
46
+ expect(refund['Status']).to eq('SUCCEEDED')
47
+ expect(refund['Type']).to eq('PAYOUT')
48
+ expect(refund['Nature']).to eq('REFUND')
49
+ expect(refund['InitialTransactionType']).to eq('PAYIN')
50
+ expect(refund['InitialTransactionId']).to eq(payin['Id'])
51
+ expect(refund['DebitedWalletId']).to eq(payin['CreditedWalletId'])
52
+ end
53
+ end
54
+
55
+ describe 'CASH FLOW' do
56
+ it 'changes balances correctly' do
57
+ wlt = new_wallet
58
+ wallets_check_amounts(wlt, 0)
59
+
60
+ # payin: feed wlt1 with money
61
+ payin = create_new_payin_card_direct(wlt, 1000)
62
+ wallets_reload_and_check_amounts(wlt, 1000)
63
+
64
+ # refund the payin
65
+ refund = MangoPay::PayIn.refund(payin['Id'], {AuthorId: payin['AuthorId']})
66
+ wallets_reload_and_check_amounts(wlt, 0)
67
+ end
68
+ end
69
+
70
+ end