mangopay 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +7 -0
  2. data/CONTRIBUTING.md +51 -0
  3. data/Gemfile +3 -0
  4. data/README.md +138 -0
  5. data/Rakefile +5 -0
  6. data/lib/mangopay.rb +45 -0
  7. data/lib/mangopay/beneficiary.rb +72 -0
  8. data/lib/mangopay/card.rb +42 -0
  9. data/lib/mangopay/contribution.rb +61 -0
  10. data/lib/mangopay/expense.rb +17 -0
  11. data/lib/mangopay/immediate_contribution.rb +58 -0
  12. data/lib/mangopay/operation.rb +16 -0
  13. data/lib/mangopay/recurrent_contribution.rb +62 -0
  14. data/lib/mangopay/ressource.rb +96 -0
  15. data/lib/mangopay/strong_authentication.rb +28 -0
  16. data/lib/mangopay/transfer.rb +58 -0
  17. data/lib/mangopay/user.rb +148 -0
  18. data/lib/mangopay/wallet.rb +93 -0
  19. data/lib/mangopay/withdrawal.rb +40 -0
  20. data/lib/mangopay/withdrawal_contribution.rb +32 -0
  21. data/spec/lib/mangopay/beneficiary_spec.rb +124 -0
  22. data/spec/lib/mangopay/card_spec.rb +52 -0
  23. data/spec/lib/mangopay/contribution_spec.rb +65 -0
  24. data/spec/lib/mangopay/expense_spec.rb +10 -0
  25. data/spec/lib/mangopay/immediate_contribution_spec.rb +73 -0
  26. data/spec/lib/mangopay/operation_spec.rb +8 -0
  27. data/spec/lib/mangopay/recurrent_contribution_spec.rb +55 -0
  28. data/spec/lib/mangopay/ressource_spec.rb +5 -0
  29. data/spec/lib/mangopay/strong_authentication_spec.rb +82 -0
  30. data/spec/lib/mangopay/transfer_spec.rb +88 -0
  31. data/spec/lib/mangopay/user_spec.rb +124 -0
  32. data/spec/lib/mangopay/wallet_spec.rb +81 -0
  33. data/spec/lib/mangopay/withdrawal_contribution_spec.rb +44 -0
  34. data/spec/lib/mangopay/withdrawal_spec.rb +98 -0
  35. data/spec/spec_helper.rb +42 -0
  36. data/spec/support-files/example.pem +49 -0
  37. data/spec/support-files/test_upload.gif +0 -0
  38. data/spec/support-files/test_upload.jpg +0 -0
  39. data/spec/support-files/test_upload.pdf +0 -0
  40. data/spec/support-files/test_upload.png +0 -0
  41. metadata +213 -0
@@ -0,0 +1,58 @@
1
+ module MangoPay
2
+ # An immediate contribution is a request to process directly a payment with a payment card already registred by the user.
3
+ class ImmediateContribution < MangoPay::Ressource
4
+
5
+ # Create an immediate contribution
6
+ #
7
+ # * *Args* :
8
+ # - +data+ -> A JSON with the following attributes (Square brackets for optionals):
9
+ # * [Tag]
10
+ # * UserID
11
+ # * WalletID
12
+ # * Amount
13
+ # * PaymentCardID
14
+ # * [ClientFeeAmount]
15
+ # * *Returns* :
16
+ # - An immediate contribution object
17
+ #
18
+ def self.create(data)
19
+ post_request('immediate-contributions', data)
20
+ end
21
+
22
+ # Get an immediate contribution
23
+ #
24
+ # * *Args* :
25
+ # - +immediate_contribution_id+ -> The id of the immediate contribution you want to retrieve
26
+ # * *Returns* :
27
+ # - An immediate contribution object
28
+ #
29
+ def self.details(immediate_contribution_id)
30
+ get_request(File.join('immediate-contributions', immediate_contribution_id.to_s))
31
+ end
32
+
33
+ # Refund a given immediate contribution
34
+ #
35
+ # * *Args* :
36
+ # - +data+ -> A JSON with the following attributes (Square brackets for optionals):
37
+ # * [Tag]
38
+ # * ImmediateContributionID
39
+ # * UserID
40
+ # * *Returns* :
41
+ # - A refund object
42
+ #
43
+ def self.refund(data)
44
+ post_request(File.join('refunds'), data)
45
+ end
46
+
47
+ # Get a refund object
48
+ #
49
+ # * *Args* :
50
+ # - +immediate_contribution_refund_id+ -> The id of the refund you want to retrieve
51
+ # * *Returns* :
52
+ # - A refund object
53
+ #
54
+ def self.get_refund(immediate_contribution_refund_id)
55
+ get_request(File.join('refunds', immediate_contribution_refund_id.to_s))
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,16 @@
1
+ module MangoPay
2
+ # Operation
3
+ class Operation < MangoPay::Ressource
4
+
5
+ # Get an operation
6
+ #
7
+ # * *Args* :
8
+ # - +operation_id+ -> The id of the operation you want to retrieve
9
+ # * *Returns* :
10
+ # - An operation object
11
+ #
12
+ def self.get(operation_id)
13
+ get_request(File.join('operations', operation_id.to_s))
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,62 @@
1
+ module MangoPay
2
+
3
+ # Recurrent Contribution are available in test environment only, soon in production.
4
+ class RecurrentContribution < MangoPay::Ressource
5
+
6
+ # Create a recurrent contribution
7
+ #
8
+ # * *Args* :
9
+ # - +data+ -> A JSON with the following attributes (Square brackets for optionals):
10
+ # * [Tag]
11
+ # * UserID
12
+ # * WalletID
13
+ # * Amount
14
+ # * [ClientFeeAmount]
15
+ # * ReturnURL
16
+ # * [TemplateURL]
17
+ # * StartDate
18
+ # * FrequencyCode
19
+ # * NumberOfExecutions
20
+ # * *Returns* :
21
+ # - A recurrent contribution object
22
+ #
23
+ def self.create(data)
24
+ post_request('recurrent-contributions', data)
25
+ end
26
+
27
+ # Get a recurrent contribution
28
+ #
29
+ # * *Args* :
30
+ # - +recurrent_contributions_id+ -> The id of the recurrent contribution you want to retrieve
31
+ # * *Returns* :
32
+ # - A recurrent contribution object
33
+ #
34
+ def self.get(recurrent_contributions_id)
35
+ get_request(File.join('recurrent-contributions', recurrent_contributions_id.to_s))
36
+ end
37
+
38
+ # Update a recurrent contribution
39
+ #
40
+ # * *Args* :
41
+ # - +data+ -> A JSON with the following attributes (Square brackets for optionals):
42
+ # * IsEnabled
43
+ # * [Tag]
44
+ # * *Returns* :
45
+ # - A recurrent contribution object
46
+ #
47
+ def self.update(recurrent_contributions_id, data)
48
+ put_request(File.join('recurrent-contributions', recurrent_contributions_id.to_s), data)
49
+ end
50
+
51
+ # Fetch the list of a recurrent contribution executions
52
+ #
53
+ # * *Args* :
54
+ # - +recurrent_contributions_id+ -> The id of the recurrent contribution you want to retrieve operations from
55
+ # * *Returns* :
56
+ # - An array of payment execution
57
+ #
58
+ def self.get_executions(recurrent_contributions_id)
59
+ get_request(File.join('recurrent-contributions', recurrent_contributions_id.to_s, 'executions'))
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,96 @@
1
+ module MangoPay
2
+ class Ressource
3
+
4
+ protected
5
+
6
+ def self.post_request(route, data)
7
+ request('POST', route, data)
8
+ end
9
+
10
+ def self.get_request(route, options=nil)
11
+ request('GET', route, nil, options)
12
+ end
13
+
14
+ def self.put_request(route, data)
15
+ request('PUT', route, data)
16
+ end
17
+
18
+ def self.delete_request(route)
19
+ request('DELETE', route)
20
+ end
21
+
22
+ def self.form_request(upload_url, file_name, file_path)
23
+ url = URI(upload_url)
24
+ File.open(file_path) do |file|
25
+ req = Net::HTTP::Post::Multipart.new(url.request_uri, :file => UploadIO.new(file, file_type(file_path), file_name))
26
+ res = Net::HTTP.start(url.host, url.port, :use_ssl => url.scheme == 'https') do |http|
27
+ http.request(req)
28
+ end
29
+ res.code == "200" ? true : false
30
+ end
31
+ end
32
+
33
+ private
34
+
35
+ def self.request(method, route, data=nil, options=nil)
36
+ path = path_for(route, options)
37
+ uri = uri_for(path)
38
+ method = method.upcase
39
+ data = data.to_json unless data.nil?
40
+ headers = header_for(method, path, data)
41
+ res = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
42
+ case method
43
+ when 'POST' then request = Net::HTTP::Post.new(uri.request_uri, headers)
44
+ when 'GET' then request = Net::HTTP::Get.new(uri.request_uri, headers)
45
+ when 'PUT' then request = Net::HTTP::Put.new(uri.request_uri, headers)
46
+ when 'DELETE' then request = Net::HTTP::Delete.new(uri.request_uri, headers)
47
+ else
48
+ return {}
49
+ end
50
+ request.body = data unless data.nil?
51
+ http.request request
52
+ end
53
+ begin
54
+ JSON.parse(res.body)
55
+ rescue JSON::ParserError
56
+ res.body.is_a?(String) ? res.body : {' ErrorCode' => -1 }
57
+ end
58
+ end
59
+
60
+ def self.key
61
+ OpenSSL::PKey::RSA.new(File.read(MangoPay.configuration.key_path), MangoPay.configuration.key_password)
62
+ end
63
+
64
+ def self.path_for(route, options)
65
+ File.join('', 'v1', 'partner', MangoPay.configuration.partner_id, route.to_s) + "?ts=#{Time.now.to_i.to_s}" + (options.nil? ? '' : ('&' + options))
66
+ end
67
+
68
+ def self.uri_for(path)
69
+ URI(File.join(MangoPay.configuration.base_url, path))
70
+ end
71
+
72
+ def self.sign(data)
73
+ Base64.encode64(key.sign('sha1', data)).to_s.chomp.gsub(/\n/, '')
74
+ end
75
+
76
+ def self.signature_for(method, path, data)
77
+ sign("#{method}|#{path}|" + (data.nil? ? '' : "#{data}|"))
78
+ end
79
+
80
+ def self.header_for(method, path, data)
81
+ { 'X-Leetchi-Signature' => signature_for(method, path, data), 'Content-Type' => 'application/json' }
82
+ end
83
+
84
+ def self.file_type(file_path)
85
+ file_types = {
86
+ 'jpg' => 'image/jpeg',
87
+ 'jpeg' => 'image/jpeg',
88
+ 'gif' => 'image/gif',
89
+ 'png' => 'image/png',
90
+ 'pdf' => 'image/pdf'
91
+ }
92
+ file_types[file_path.gsub(/^[^\.]+\./, "")]
93
+ end
94
+ end
95
+ end
96
+
@@ -0,0 +1,28 @@
1
+ module MangoPay
2
+ # Get all the strongAuthentication created who need to be validated.
3
+ # It's important to understand that MangoPay can create himself the request of strongAuthentication, if a user reaches some limits.
4
+ class StrongAuthentication < MangoPay::Ressource
5
+
6
+ # Get all the strongAuthentication created who need to be validated
7
+ #
8
+ # * *Returns* :
9
+ # - A array of strongAuthentication objects
10
+ #
11
+ def self.get
12
+ get_request(File.join('strongAuthentication'))
13
+ end
14
+
15
+ # Upload your Strong Authentication required files using the MangoPay API
16
+ #
17
+ # * *Args* :
18
+ # - +upload_url+ -> The url to where the StrongAuthentication documents need to be uploaded
19
+ # - +file_path+ -> Local path to the file
20
+ # * *Returns* :
21
+ # - true if the request is done
22
+ # - false if the request failed
23
+ #
24
+ def self.upload(upload_url, file_path)
25
+ form_request(upload_url, "StrongValidationDto.Picture", file_path)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,58 @@
1
+ module MangoPay
2
+ # The Transfer class if you want to make any transfer between two wallets
3
+ class Transfer < MangoPay::Ressource
4
+
5
+ # Create a transfer from one personal wallet to another wallet
6
+ #
7
+ # * *Args* :
8
+ # - +data+ -> A JSON with the following attributes (Square brackets for optionals):
9
+ # * [Tag]
10
+ # * [PayerID]
11
+ # * [BeneficiaryID]
12
+ # * Amount
13
+ # * [ClientFeeAmount] (In preproduction environnment only, soon in production)
14
+ # * [PayerWalletID]
15
+ # * [BeneficiaryWalletID]
16
+ # * *Returns* :
17
+ # - A transfer object
18
+ #
19
+ def self.create(data)
20
+ post_request('transfers', data)
21
+ end
22
+
23
+ # Get a transfer
24
+ #
25
+ # * *Args* :
26
+ # - +tranfer_id+ -> The id of the transfer you want to retrieve
27
+ # * *Returns* :
28
+ # - A transfer object
29
+ #
30
+ def self.details(transfer_id)
31
+ get_request(File.join('transfers', transfer_id.to_s))
32
+ end
33
+
34
+ # Refund a contribution from a personal wallet to a shared walletTransferID
35
+ #
36
+ # * *Args* :
37
+ # - +data+ -> A JSON with the following attributes (Square brackets for optionals):
38
+ # * TransfertID
39
+ # * UserID
40
+ # * *Returns* :
41
+ # - A transfer refund object
42
+ #
43
+ def self.refund(data)
44
+ post_request(File.join('transfer-refunds'), data)
45
+ end
46
+
47
+ # Get a refund transfer object
48
+ #
49
+ # * *Args* :
50
+ # - +transfer_refund_id+ -> The id of the transfer refund you want to retrieve
51
+ # * *Returns* :
52
+ # - A transfer refund object
53
+ #
54
+ def self.get_refund(transfer_refund_id)
55
+ get_request(File.join('transfer-refunds', transfer_refund_id.to_s))
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,148 @@
1
+ module MangoPay
2
+ # Use the User class for any operations related to the user
3
+ class User < MangoPay::Ressource
4
+
5
+ # Create a new user
6
+ #
7
+ # * *Args* :
8
+ # - +data+ -> A JSON with the following attributes (Square brackets for optionals):
9
+ # * [Tag]
10
+ # * [Email]
11
+ # * [FirstName]
12
+ # * [LastName]
13
+ # * [CanRegisterMeanOfPayment] true by default
14
+ # * IP
15
+ # * [Birthday]
16
+ # * [Password]
17
+ # * *Returns* :
18
+ # - A user object
19
+ #
20
+ def self.create(data)
21
+ post_request('users', data)
22
+ end
23
+
24
+ # Get a user
25
+ #
26
+ # * *Args* :
27
+ # - +user_id+ -> The id of the user you want to retrieve
28
+ # * *Returns* :
29
+ # - A user object
30
+ #
31
+ def self.details(user_id)
32
+ get_request(File.join('users', user_id.to_s))
33
+ end
34
+
35
+ # Update a given user
36
+ #
37
+ # * *Args* :
38
+ # - +user_id+ -> The id of the user you want to update
39
+ # - +data+ -> A JSON with the following attributes (Square brackets for optionals):
40
+ # * [Tag]
41
+ # * [Email]
42
+ # * [FirstName]
43
+ # * [LastName]
44
+ # * [CanRegisterMeanOfPayment]
45
+ # * [Password]
46
+ # * *Returns* :
47
+ # - A user object
48
+ #
49
+ def self.update(user_id, data)
50
+ put_request(File.join('users', user_id.to_s), data)
51
+ end
52
+
53
+ # Get a list of the given user wallets
54
+ #
55
+ # * *Args* :
56
+ # - +user_id+ -> The id of the user you want to retrieve wallets from
57
+ # * *Returns* :
58
+ # - An array of wallets objects
59
+ #
60
+ def self.get_wallets(user_id)
61
+ get_request(File.join('users', user_id.to_s, 'wallets'))
62
+ end
63
+
64
+ # Get a list of the given user payment cards
65
+ #
66
+ # * *Args* :
67
+ # - +user_id+ -> The id of the user you want to retrieve cards from
68
+ # * *Returns* :
69
+ # - An array of registered payment cards objects
70
+ #
71
+ def self.cards(user_id)
72
+ get_request(File.join('users', user_id.to_s, 'cards'))
73
+ end
74
+
75
+ # Get operations associated with a user
76
+ #
77
+ # * *Args* :
78
+ # - +user_id+ -> The id of the user you want to retrieve operations from
79
+ # * *Returns* :
80
+ # - An array of operations objects
81
+ #
82
+ def self.operations(user_id)
83
+ get_request(File.join('users', user_id.to_s, 'operations'))
84
+ end
85
+
86
+ # Get operations from the given user personal account
87
+ #
88
+ # * *Args* :
89
+ # - +user_id+ -> The id of the user you want to retrieve operations from
90
+ # * *Returns* :
91
+ # - An array of operations objects
92
+ #
93
+ def self.personal_operations(user_id)
94
+ get_request(File.join('users', user_id.to_s, 'operations', 'personal'))
95
+ end
96
+
97
+ # Create a request of strong user authentication.
98
+ # If a strongAuthentication object already exist for the user, this request returns the existing object.
99
+ #
100
+ # * *Args* :
101
+ # - +user_id+ -> The id of the user you want to strongly authenticate
102
+ # - +data+ -> A JSON with the following attributes (Square brackets for optionals):
103
+ # * [Tag]
104
+ # * *Returns* :
105
+ # - A Strong Authentication object
106
+ #
107
+ def self.create_strong_authentication(user_id, data)
108
+ post_request(File.join('users', user_id.to_s, 'strongAuthentication'), data)
109
+ end
110
+
111
+ # Get a Strong Authentication object
112
+ #
113
+ # * *Args* :
114
+ # - +user_id+ -> The id of the user you want to retrieve the strong authentication object from
115
+ # * *Returns* :
116
+ # - A Strong Authentication object
117
+ #
118
+ def self.get_strong_authentication(user_id)
119
+ get_request(File.join('users', user_id.to_s, 'strongAuthentication'))
120
+ end
121
+
122
+ # Update a Strong Authentication object
123
+ #
124
+ # * *Args* :
125
+ # - +user_id+ -> The id of the user you want to update the strong authentication object from
126
+ # - +data+ -> A JSON with the following attributes (Square brackets for optionals):
127
+ # * [Tag]
128
+ # * [IsDocumentsTransmitted]
129
+ # * *Returns* :
130
+ # - A Strong Authentication object
131
+ #
132
+ def self.update_strong_authentication(user_id, data)
133
+ put_request(File.join('users', user_id.to_s, 'strongAuthentication'), data)
134
+ end
135
+
136
+ # Get the expense sites for a given user
137
+ #
138
+ # * *Args* :
139
+ # - +user_id+ -> The id of the user you want to retrieve the expense sites from
140
+ # - +wallet_id+ -> The id of the wallet you want to retrieve the expense sites from
141
+ # * *Returns* :
142
+ # - An array of expense site objects
143
+ #
144
+ def self.expense_sites(user_id, wallet_id)
145
+ get_request(File.join('expense-sites'), "userID=#{user_id.to_s}&walletID=#{wallet_id.to_s}")
146
+ end
147
+ end
148
+ end