openpay_copemx 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +19 -0
- data/.travis.yml +6 -0
- data/Gemfile +10 -0
- data/LICENSE.txt +13 -0
- data/README.md +1984 -0
- data/Rakefile +16 -0
- data/lib/openpay/bankaccounts.rb +58 -0
- data/lib/openpay/cards.rb +73 -0
- data/lib/openpay/charges.rb +101 -0
- data/lib/openpay/colombia/cards_co.rb +73 -0
- data/lib/openpay/colombia/charges_co.rb +76 -0
- data/lib/openpay/colombia/customers_co.rb +166 -0
- data/lib/openpay/colombia/plans_co.rb +17 -0
- data/lib/openpay/colombia/pse_co.rb +17 -0
- data/lib/openpay/colombia/subscriptions_co.rb +50 -0
- data/lib/openpay/colombia/tokens_co.rb +5 -0
- data/lib/openpay/colombia/webhooks_co.rb +5 -0
- data/lib/openpay/customers.rb +200 -0
- data/lib/openpay/errors/openpay_connection_exception.rb +3 -0
- data/lib/openpay/errors/openpay_exception.rb +29 -0
- data/lib/openpay/errors/openpay_exception_factory.rb +56 -0
- data/lib/openpay/errors/openpay_transaction_exception.rb +5 -0
- data/lib/openpay/fees.rb +5 -0
- data/lib/openpay/open_pay_resource.rb +295 -0
- data/lib/openpay/open_pay_resource_factory.rb +17 -0
- data/lib/openpay/openpay_api.rb +72 -0
- data/lib/openpay/payouts.rb +55 -0
- data/lib/openpay/peru/cards_pe.rb +73 -0
- data/lib/openpay/peru/charges_pe.rb +76 -0
- data/lib/openpay/peru/checkouts_pe.rb +51 -0
- data/lib/openpay/peru/customers_pe.rb +79 -0
- data/lib/openpay/peru/tokens_pe.rb +5 -0
- data/lib/openpay/peru/webhooks_pe.rb +5 -0
- data/lib/openpay/plans.rb +17 -0
- data/lib/openpay/points.rb +10 -0
- data/lib/openpay/subscriptions.rb +50 -0
- data/lib/openpay/tokens.rb +7 -0
- data/lib/openpay/transfers.rb +39 -0
- data/lib/openpay/utils/country.rb +3 -0
- data/lib/openpay/utils/search_params.rb +24 -0
- data/lib/openpay/webhooks.rb +9 -0
- data/lib/openpay.rb +55 -0
- data/lib/version.rb +3 -0
- data/openpay.gemspec +30 -0
- data/test/Factories.rb +524 -0
- data/test/spec/bankaccounts_spec.rb +52 -0
- data/test/spec/cards_spec.rb +437 -0
- data/test/spec/charges_spec.rb +382 -0
- data/test/spec/colombia/cards_col_spec.rb +364 -0
- data/test/spec/colombia/charges_col_spec.rb +258 -0
- data/test/spec/colombia/customers_co_spec.rb +151 -0
- data/test/spec/colombia/plans_col_spec.rb +133 -0
- data/test/spec/colombia/pse_col_spec.rb +49 -0
- data/test/spec/colombia/subscriptions_col_spec.rb +230 -0
- data/test/spec/colombia/tokens_col_spec.rb +38 -0
- data/test/spec/customers_spec.rb +172 -0
- data/test/spec/exceptions_spec.rb +105 -0
- data/test/spec/fees_spec.rb +166 -0
- data/test/spec/openpayresource_spec.rb +54 -0
- data/test/spec/payouts_spec.rb +231 -0
- data/test/spec/peru/cards_pe_spec.rb +363 -0
- data/test/spec/peru/charges_pe_spec.rb +218 -0
- data/test/spec/peru/checkouts_pe_spec.rb +71 -0
- data/test/spec/peru/customers_pe_spec.rb +151 -0
- data/test/spec/peru/tokens_pe_spec.rb +38 -0
- data/test/spec/peru/webhook_pe_spec.rb +50 -0
- data/test/spec/plans_spec.rb +158 -0
- data/test/spec/points_spec.rb +26 -0
- data/test/spec/requesttimeout_spec.rb +22 -0
- data/test/spec/subscriptions_spec.rb +294 -0
- data/test/spec/transfers_spec.rb +219 -0
- data/test/spec/utils/search_params_spec.rb +36 -0
- data/test/spec_helper.rb +24 -0
- metadata +219 -0
@@ -0,0 +1,200 @@
|
|
1
|
+
require 'open_pay_resource'
|
2
|
+
|
3
|
+
class Customers < OpenPayResource
|
4
|
+
|
5
|
+
#Bankaccount
|
6
|
+
def create_bank_account(customer, bank_account)
|
7
|
+
create(bank_account, "#{customer}/bankaccounts")
|
8
|
+
end
|
9
|
+
|
10
|
+
def get_bank_account(customer, bank_id)
|
11
|
+
get("#{customer}/bankaccounts/#{bank_id}")
|
12
|
+
end
|
13
|
+
|
14
|
+
def all_bank_accounts(customer)
|
15
|
+
get("#{customer}/bankaccounts/")
|
16
|
+
end
|
17
|
+
|
18
|
+
def list_bankaccounts(customer, search_params)
|
19
|
+
get("#{customer}/bankaccounts#{search_params.to_s}")
|
20
|
+
end
|
21
|
+
|
22
|
+
def each_bank_account(customer)
|
23
|
+
get("#{customer}/bankaccounts/").each do |account|
|
24
|
+
yield account
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def delete_bank_account(customer, bank_id)
|
29
|
+
delete("#{customer}/bankaccounts/#{bank_id}")
|
30
|
+
end
|
31
|
+
|
32
|
+
def delete_all_bank_accounts(customer)
|
33
|
+
if env == :production
|
34
|
+
raise OpenpayException.new('This method is not supported on PRODUCTION', false)
|
35
|
+
end
|
36
|
+
each_bank_account(customer) do |account|
|
37
|
+
delete("#{customer}/bankaccounts/#{account['id']}")
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
#Charges
|
42
|
+
# customers.create_charge(customer_id,charge)
|
43
|
+
def create_charge(customer_id, charge)
|
44
|
+
create(charge, "#{customer_id}/charges")
|
45
|
+
end
|
46
|
+
|
47
|
+
#gets a charge_id for a given customer_id
|
48
|
+
def get_charge(customer_id, charge_id)
|
49
|
+
get("#{customer_id}/charges/#{charge_id}")
|
50
|
+
end
|
51
|
+
|
52
|
+
def list_charges(customer, search_params)
|
53
|
+
get("#{customer}/charges#{search_params.to_s}")
|
54
|
+
end
|
55
|
+
|
56
|
+
#return all charges for the given customer_id
|
57
|
+
def all_charges(customer_id)
|
58
|
+
get("#{customer_id}/charges/")
|
59
|
+
end
|
60
|
+
|
61
|
+
def cancel_charge(customer_id, charge_id)
|
62
|
+
post(charge_id, "#{customer_id}/charges/#{charge_id}/cancel")
|
63
|
+
end
|
64
|
+
|
65
|
+
def refund_charge(customer_id, charge_id, description)
|
66
|
+
post(description, "#{customer_id}/charges/#{charge_id}/refund")
|
67
|
+
end
|
68
|
+
|
69
|
+
def capture_charge(customer_id, charge_id)
|
70
|
+
post('', "#{customer_id}/charges/#{charge_id}/capture")
|
71
|
+
end
|
72
|
+
|
73
|
+
def confirm_customer_capture(customer_id, charge_id, amount)
|
74
|
+
post(amount, "#{customer_id}/charges/#{charge_id}/capture")
|
75
|
+
end
|
76
|
+
|
77
|
+
#Payouts
|
78
|
+
def create_payout(customer_id, payout)
|
79
|
+
post(payout, "#{customer_id}/payouts")
|
80
|
+
end
|
81
|
+
|
82
|
+
def all_payouts(customer_id)
|
83
|
+
get("#{customer_id}/payouts")
|
84
|
+
end
|
85
|
+
|
86
|
+
def get_payout(customer_id, payout_id)
|
87
|
+
get("#{customer_id}/payouts/#{payout_id}")
|
88
|
+
end
|
89
|
+
|
90
|
+
def each_payout(customer_id)
|
91
|
+
all_payouts(customer_id).each do |pay|
|
92
|
+
yield pay
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
def list_payouts(customer, search_params)
|
97
|
+
get("#{customer}/payouts#{search_params.to_s}")
|
98
|
+
end
|
99
|
+
|
100
|
+
#Transfers
|
101
|
+
def create_transfer(customer_id, transfer)
|
102
|
+
post(transfer, "#{customer_id}/transfers")
|
103
|
+
end
|
104
|
+
|
105
|
+
def all_transfers(customer_id)
|
106
|
+
get("#{customer_id}/transfers/")
|
107
|
+
end
|
108
|
+
|
109
|
+
def list_transfers(customer, search_params)
|
110
|
+
get("#{customer}/transfers#{search_params.to_s}")
|
111
|
+
end
|
112
|
+
|
113
|
+
def get_transfer(customer_id, transfer_id)
|
114
|
+
get("#{customer_id}/transfers/#{transfer_id}")
|
115
|
+
end
|
116
|
+
|
117
|
+
def each_transfer(customer_id)
|
118
|
+
all_transfers(customer_id).each do |trans|
|
119
|
+
yield trans
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
#Subscriptions
|
124
|
+
def create_subscription(subscription, customer_id)
|
125
|
+
#revisar la url
|
126
|
+
post(subscription, "#{customer_id}/subscriptions")
|
127
|
+
end
|
128
|
+
|
129
|
+
def delete_subscription(customer_id, subscription_id)
|
130
|
+
delete("#{customer_id}/subscriptions/#{subscription_id}")
|
131
|
+
end
|
132
|
+
|
133
|
+
def get_subscription(customer_id, subscription_id)
|
134
|
+
get("#{customer_id}/subscriptions/#{subscription_id}")
|
135
|
+
end
|
136
|
+
|
137
|
+
def all_subscriptions(customer_id)
|
138
|
+
get("#{customer_id}/subscriptions/")
|
139
|
+
end
|
140
|
+
|
141
|
+
def list_subscriptions(customer, search_params)
|
142
|
+
get("#{customer}/subscriptions#{search_params.to_s}")
|
143
|
+
end
|
144
|
+
|
145
|
+
def each_subscription(customer_id)
|
146
|
+
all_subscriptions(customer_id).each do |cust|
|
147
|
+
yield cust
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
def update_subscription(subscription, customer, params)
|
152
|
+
put(params, "#{customer}/subscriptions/#{subscription}")
|
153
|
+
end
|
154
|
+
|
155
|
+
def delete_all_subscriptions(customer_id)
|
156
|
+
if env == :production
|
157
|
+
raise OpenpayException.new('This method is not supported on PRODUCTION', false)
|
158
|
+
end
|
159
|
+
all_subscriptions(customer_id).each do |sub|
|
160
|
+
delete_subscription(customer_id, sub['id'])
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
#Card
|
165
|
+
def create_card(customer, card)
|
166
|
+
create(card, "#{customer}/cards")
|
167
|
+
end
|
168
|
+
|
169
|
+
def get_card(customer, card_id)
|
170
|
+
get("#{customer}/cards/#{card_id}")
|
171
|
+
end
|
172
|
+
|
173
|
+
def delete_card(customer, card_id)
|
174
|
+
delete("#{customer}/cards/#{card_id}")
|
175
|
+
end
|
176
|
+
|
177
|
+
def delete_all_cards(customer_id)
|
178
|
+
if env == :production
|
179
|
+
raise OpenpayException.new('This method is not supported on PRODUCTION', false)
|
180
|
+
end
|
181
|
+
each_card(customer_id) do |card|
|
182
|
+
delete_card(customer_id, card['id'])
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
def each_card(customer)
|
187
|
+
get("#{customer}/cards").each do |card|
|
188
|
+
yield card
|
189
|
+
end
|
190
|
+
end
|
191
|
+
|
192
|
+
def list_cards(customer, search_params)
|
193
|
+
get("#{customer}/cards#{search_params.to_s}")
|
194
|
+
end
|
195
|
+
|
196
|
+
def all_cards(customer)
|
197
|
+
get("#{customer}/cards")
|
198
|
+
end
|
199
|
+
|
200
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
class OpenpayException < StandardError
|
2
|
+
|
3
|
+
attr_reader :description
|
4
|
+
attr_reader :http_code
|
5
|
+
attr_reader :http_body
|
6
|
+
attr_reader :json_body
|
7
|
+
attr_reader :error_code
|
8
|
+
attr_reader :category
|
9
|
+
|
10
|
+
def initialize(message=nil,json_message=true)
|
11
|
+
#openpay errors have a json error string
|
12
|
+
if json_message
|
13
|
+
json= JSON.parse message
|
14
|
+
@description = json['description']
|
15
|
+
@http_code = json['http_code']
|
16
|
+
@error_code = json['error_code']
|
17
|
+
@json_body = message
|
18
|
+
@category = json['category']
|
19
|
+
#other errors may or not have a json error string, so this case is for them
|
20
|
+
else
|
21
|
+
@description = message
|
22
|
+
@http_code = ''
|
23
|
+
@http_body = ''
|
24
|
+
@json_body = ''
|
25
|
+
@error_code = ''
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
class OpenpayExceptionFactory
|
2
|
+
|
3
|
+
def OpenpayExceptionFactory::create(exception)
|
4
|
+
|
5
|
+
LOG.warn("An exception has been raised (original exception class: #{exception.class})")
|
6
|
+
|
7
|
+
case exception
|
8
|
+
|
9
|
+
#resource not found
|
10
|
+
#malformed jason, invalid data, invalid request
|
11
|
+
when RestClient::BadRequest, RestClient::ResourceNotFound,
|
12
|
+
RestClient::Conflict, RestClient::PaymentRequired,
|
13
|
+
RestClient::UnprocessableEntity
|
14
|
+
|
15
|
+
oe=OpenpayTransactionException.new exception.http_body
|
16
|
+
LOG.warn "-OpenpayTransactionException: #{exception.http_body}"
|
17
|
+
@errors=true
|
18
|
+
raise oe
|
19
|
+
|
20
|
+
#connection, timeouts, network related errors
|
21
|
+
when Errno::EADDRINUSE, Errno::ETIMEDOUT, OpenSSL::SSL::SSLError
|
22
|
+
#since this exceptions are not based on the rest api exceptions
|
23
|
+
#we do not have the json message so we just build the exception
|
24
|
+
#with the original exception message set in e.description and e.message
|
25
|
+
oe=OpenpayConnectionException.new(exception.message,false)
|
26
|
+
LOG.warn exception.message
|
27
|
+
@errors=true
|
28
|
+
raise oe
|
29
|
+
#badgateway-connection error, invalid credentials
|
30
|
+
when RestClient::BadGateway, RestClient::Unauthorized, RestClient::RequestTimeout
|
31
|
+
LOG.warn exception
|
32
|
+
if exception.http_body
|
33
|
+
oe=OpenpayConnectionException.new exception.http_body
|
34
|
+
else
|
35
|
+
oe=OpenpayConnectionException.new(exception.message, false)
|
36
|
+
end
|
37
|
+
@errors=true
|
38
|
+
raise oe
|
39
|
+
|
40
|
+
when RestClient::Exception , RestClient::InternalServerError
|
41
|
+
LOG.warn exception
|
42
|
+
if exception.http_body
|
43
|
+
oe=OpenpayException.new exception.http_body
|
44
|
+
else
|
45
|
+
oe=OpenpayException.new(exception.message, false)
|
46
|
+
end
|
47
|
+
@errors=true
|
48
|
+
raise oe
|
49
|
+
else
|
50
|
+
#We won't hide unknown exceptions , those should be raised
|
51
|
+
LOG.warn exception.message
|
52
|
+
raise exception
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
data/lib/openpay/fees.rb
ADDED
@@ -0,0 +1,295 @@
|
|
1
|
+
#This class is the abstract base class for other Openpay resources
|
2
|
+
#This class defines the basic rest verbs making use of the rest-api gem.
|
3
|
+
#Method aliases are created to provide friendly names.
|
4
|
+
class OpenPayResource
|
5
|
+
|
6
|
+
attr_accessor :api_hook
|
7
|
+
|
8
|
+
def initialize(merchant_id, private_key, production = false, timeout = 90, country)
|
9
|
+
@merchant_id = merchant_id
|
10
|
+
@private_key = private_key
|
11
|
+
#assigns base url depending the requested env
|
12
|
+
@country = country
|
13
|
+
@base_url = OpenpayApi::base_url(production, country)
|
14
|
+
@errors = false
|
15
|
+
@production = production
|
16
|
+
@timeout = timeout
|
17
|
+
#created resources should have a hook with the base class to keep control of created resources
|
18
|
+
@api_hook = nil
|
19
|
+
end
|
20
|
+
|
21
|
+
#returns the env set
|
22
|
+
def env
|
23
|
+
if @production
|
24
|
+
:production
|
25
|
+
else
|
26
|
+
:test
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
#errors on last call
|
31
|
+
def errors?
|
32
|
+
@errors
|
33
|
+
end
|
34
|
+
|
35
|
+
def list(search_params)
|
36
|
+
get(search_params.to_s)
|
37
|
+
end
|
38
|
+
|
39
|
+
def each
|
40
|
+
all.each do |line|
|
41
|
+
yield line
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def delete_all
|
46
|
+
|
47
|
+
if env == :production
|
48
|
+
raise OpenpayException.new('delete_all method cannot be used on production', false)
|
49
|
+
end
|
50
|
+
|
51
|
+
each do |res|
|
52
|
+
self.delete(res['id'])
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
def get(args = '')
|
58
|
+
@errors = false
|
59
|
+
terminated = true
|
60
|
+
if is_filter_string?(args)
|
61
|
+
terminated = false
|
62
|
+
end
|
63
|
+
strUrl = url(args, terminated)
|
64
|
+
strUrlAux = delete_ending_slash(strUrl)
|
65
|
+
LOG.debug("#{resource_name}:")
|
66
|
+
LOG.debug(" GET Resource URL:#{url(args, terminated)}")
|
67
|
+
res = RestClient::Request.new(
|
68
|
+
:method => :get,
|
69
|
+
:url => strUrlAux,
|
70
|
+
:user => @private_key,
|
71
|
+
:timeout => @timeout,
|
72
|
+
:ssl_version => :TLSv1_2,
|
73
|
+
:headers => { :accept => :json,
|
74
|
+
:content_type => :json,
|
75
|
+
:user_agent => 'Openpay/v1 Ruby-API',
|
76
|
+
}
|
77
|
+
)
|
78
|
+
json_out = nil
|
79
|
+
begin
|
80
|
+
json_out = res.execute
|
81
|
+
#exceptions
|
82
|
+
rescue Exception => e
|
83
|
+
@errors = true
|
84
|
+
#will raise the appropriate exception and return
|
85
|
+
OpenpayExceptionFactory::create(e)
|
86
|
+
end
|
87
|
+
JSON[json_out]
|
88
|
+
end
|
89
|
+
|
90
|
+
def get_with_custom_url(url = '')
|
91
|
+
@errors = false
|
92
|
+
res = RestClient::Request.new(
|
93
|
+
:method => :get,
|
94
|
+
:url => url,
|
95
|
+
:user => @private_key,
|
96
|
+
:timeout => @timeout,
|
97
|
+
:ssl_version => :TLSv1_2,
|
98
|
+
:headers => { :accept => :json,
|
99
|
+
:content_type => :json,
|
100
|
+
:user_agent => 'Openpay/v1 Ruby-API',
|
101
|
+
}
|
102
|
+
)
|
103
|
+
json_out = nil
|
104
|
+
begin
|
105
|
+
json_out = res.execute
|
106
|
+
#exceptions
|
107
|
+
rescue Exception => e
|
108
|
+
@errors = true
|
109
|
+
#will raise the appropriate exception and return
|
110
|
+
OpenpayExceptionFactory::create(e)
|
111
|
+
end
|
112
|
+
JSON[json_out]
|
113
|
+
end
|
114
|
+
|
115
|
+
def delete(args)
|
116
|
+
|
117
|
+
@errors = false
|
118
|
+
|
119
|
+
strUrl = url(args)
|
120
|
+
strUrlAux = delete_ending_slash(strUrl)
|
121
|
+
|
122
|
+
LOG.debug("#{self.class.name.downcase}:")
|
123
|
+
LOG.debug(" DELETE URL:#{strUrlAux}")
|
124
|
+
|
125
|
+
res = ''
|
126
|
+
req = RestClient::Request.new(
|
127
|
+
:method => :delete,
|
128
|
+
:url => strUrlAux,
|
129
|
+
:user => @private_key,
|
130
|
+
:timeout => @timeout,
|
131
|
+
:ssl_version => :TLSv1_2,
|
132
|
+
:headers => { :accept => :json,
|
133
|
+
:content_type => :json,
|
134
|
+
:user_agent => 'Openpay/v1 Ruby-API',
|
135
|
+
}
|
136
|
+
)
|
137
|
+
|
138
|
+
begin
|
139
|
+
res = req.execute
|
140
|
+
#exceptions
|
141
|
+
rescue Exception => e
|
142
|
+
@errors = true
|
143
|
+
#will raise the appropriate exception and return
|
144
|
+
OpenpayExceptionFactory::create(e)
|
145
|
+
end
|
146
|
+
#returns a hash
|
147
|
+
JSON[res] if not res.empty?
|
148
|
+
end
|
149
|
+
|
150
|
+
def post(message, args = '')
|
151
|
+
|
152
|
+
@errors = false
|
153
|
+
|
154
|
+
if message.is_a?(Hash)
|
155
|
+
return_hash = true
|
156
|
+
json = hash2json message
|
157
|
+
else
|
158
|
+
json = message
|
159
|
+
return_hash = false
|
160
|
+
end
|
161
|
+
|
162
|
+
strUrl = url(args)
|
163
|
+
strUrlAux = delete_ending_slash(strUrl)
|
164
|
+
|
165
|
+
# LOG.debug("#{self.class.name.downcase}:")
|
166
|
+
LOG.debug " POST URL:#{strUrlAux}"
|
167
|
+
LOG.debug " json: #{json}"
|
168
|
+
|
169
|
+
begin
|
170
|
+
#request
|
171
|
+
res = RestClient::Request.new(
|
172
|
+
:method => :post,
|
173
|
+
:url => strUrlAux,
|
174
|
+
:user => @private_key,
|
175
|
+
:timeout => @timeout,
|
176
|
+
:ssl_version => :TLSv1_2,
|
177
|
+
:payload => json,
|
178
|
+
:headers => { :accept => :json,
|
179
|
+
:content_type => :json,
|
180
|
+
:user_agent => 'Openpay/v1 Ruby-API',
|
181
|
+
:json => json }
|
182
|
+
).execute
|
183
|
+
|
184
|
+
#exceptions
|
185
|
+
rescue Exception => e
|
186
|
+
@errors = true
|
187
|
+
#will raise the appropriate exception and return
|
188
|
+
OpenpayExceptionFactory::create(e)
|
189
|
+
end
|
190
|
+
|
191
|
+
#return
|
192
|
+
if return_hash
|
193
|
+
JSON.parse res
|
194
|
+
else
|
195
|
+
res
|
196
|
+
end
|
197
|
+
|
198
|
+
end
|
199
|
+
|
200
|
+
def put(message, args = '')
|
201
|
+
|
202
|
+
return_hash = false
|
203
|
+
|
204
|
+
if message.is_a?(Hash)
|
205
|
+
return_hash = true
|
206
|
+
json = hash2json message
|
207
|
+
else
|
208
|
+
json = message
|
209
|
+
return_hash = false
|
210
|
+
end
|
211
|
+
|
212
|
+
strUrl = url(args)
|
213
|
+
strUrlAux = delete_ending_slash(strUrl)
|
214
|
+
|
215
|
+
LOG.info "PUT URL:#{strUrlAux}"
|
216
|
+
#LOG.info " json: #{json}"
|
217
|
+
|
218
|
+
begin
|
219
|
+
res = RestClient::Request.new(
|
220
|
+
:method => :put,
|
221
|
+
:url => strUrlAux,
|
222
|
+
:user => @private_key,
|
223
|
+
:timeout => @timeout,
|
224
|
+
:ssl_version => :TLSv1_2,
|
225
|
+
:payload => json,
|
226
|
+
:headers => { :accept => :json,
|
227
|
+
:content_type => :json,
|
228
|
+
:user_agent => 'Openpay/v1 Ruby-API',
|
229
|
+
:json => json }
|
230
|
+
).execute
|
231
|
+
rescue RestClient::BadRequest => e
|
232
|
+
warn e.http_body
|
233
|
+
@errors = true
|
234
|
+
return JSON.parse e.http_body
|
235
|
+
end
|
236
|
+
|
237
|
+
if return_hash
|
238
|
+
JSON.parse res
|
239
|
+
else
|
240
|
+
res
|
241
|
+
end
|
242
|
+
|
243
|
+
end
|
244
|
+
|
245
|
+
#aliases for rest verbs
|
246
|
+
alias_method :all, :get
|
247
|
+
alias_method :update, :put
|
248
|
+
alias_method :create, :post
|
249
|
+
|
250
|
+
def hash2json(jash)
|
251
|
+
JSON.generate(jash)
|
252
|
+
end
|
253
|
+
|
254
|
+
def json2hash(json)
|
255
|
+
JSON[json]
|
256
|
+
end
|
257
|
+
|
258
|
+
private
|
259
|
+
|
260
|
+
def url(args = '', terminated = true)
|
261
|
+
if args.nil? || args.empty?
|
262
|
+
terminated = false
|
263
|
+
end
|
264
|
+
termination = terminated ? '/' : ''
|
265
|
+
@base_url + "#{@merchant_id}/" + resource_name + termination + args
|
266
|
+
end
|
267
|
+
|
268
|
+
def resource_name
|
269
|
+
if self.class.name.downcase.gsub(@country, "") == "pse"
|
270
|
+
'charges'
|
271
|
+
else
|
272
|
+
self.class.name.downcase.gsub(@country, "")
|
273
|
+
end
|
274
|
+
end
|
275
|
+
|
276
|
+
def delete_ending_slash(url)
|
277
|
+
slash = '/'
|
278
|
+
strUrl = url
|
279
|
+
strUrlAux = url
|
280
|
+
ending = strUrl.to_s[-1, 1]
|
281
|
+
if ending == slash
|
282
|
+
strUrlAux = strUrl.to_s[0, strUrl.length - 1]
|
283
|
+
end
|
284
|
+
strUrlAux
|
285
|
+
end
|
286
|
+
|
287
|
+
def is_filter_string?(args)
|
288
|
+
is_filter = false
|
289
|
+
if args =~ /^\?/
|
290
|
+
is_filter = true
|
291
|
+
end
|
292
|
+
is_filter
|
293
|
+
end
|
294
|
+
|
295
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
class OpenPayResourceFactory
|
2
|
+
def OpenPayResourceFactory::create(resource, merchant_id, private_key, production, timeout, country)
|
3
|
+
begin
|
4
|
+
resource = resource.capitalize
|
5
|
+
if country == "co"
|
6
|
+
resource = "#{resource}Co".to_sym
|
7
|
+
end
|
8
|
+
if country == "pe"
|
9
|
+
resource = "#{resource}Pe".to_sym
|
10
|
+
end
|
11
|
+
Object.const_get(resource).new(merchant_id, private_key, production, timeout, country)
|
12
|
+
rescue NameError
|
13
|
+
raise OpenpayException.new("Invalid resource name:#{resource}", false)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
@@ -0,0 +1,72 @@
|
|
1
|
+
require 'logger'
|
2
|
+
require 'base64'
|
3
|
+
require 'rest-client'
|
4
|
+
require 'uri'
|
5
|
+
|
6
|
+
require 'openpay/open_pay_resource_factory'
|
7
|
+
require 'errors/openpay_exception'
|
8
|
+
|
9
|
+
LOG = Logger.new(STDOUT)
|
10
|
+
#change to Logger::DEBUG if need trace information
|
11
|
+
#due the nature of the information, we recommend to never use a log file when in debug
|
12
|
+
LOG.level = Logger::FATAL
|
13
|
+
|
14
|
+
class OpenpayApi
|
15
|
+
#API Endpoints
|
16
|
+
API_DEV = 'https://sandbox-api.openpay.mx/v1/'
|
17
|
+
API_PROD = 'https://api.openpay.mx/v1/'
|
18
|
+
API_DEV_CO = 'https://sandbox-api.openpay.co/v1/'
|
19
|
+
API_PROD_CO = 'https://api.openpay.co/v1/'
|
20
|
+
API_DEV_PE = 'https://sandbox-api.openpay.pe/v1/'
|
21
|
+
API_PROD_PE = 'https://api.openpay.pe/v1/'
|
22
|
+
|
23
|
+
# by default testing environment is used
|
24
|
+
# country can take value 'mx' (Mexico) or 'co' Colombia
|
25
|
+
def initialize(merchant_id, private_key, country = "mx", production = false, timeout = 90)
|
26
|
+
@merchant_id = merchant_id
|
27
|
+
@private_key = private_key
|
28
|
+
@production = production
|
29
|
+
@timeout = timeout
|
30
|
+
@country = country
|
31
|
+
end
|
32
|
+
|
33
|
+
def create(resource)
|
34
|
+
klass = OpenPayResourceFactory::create(resource, @merchant_id, @private_key, @production, @timeout, @country)
|
35
|
+
klass.api_hook = self
|
36
|
+
klass
|
37
|
+
end
|
38
|
+
|
39
|
+
def OpenpayApi::base_url(production, country)
|
40
|
+
if country == "mx"
|
41
|
+
if production
|
42
|
+
API_PROD
|
43
|
+
else
|
44
|
+
API_DEV
|
45
|
+
end
|
46
|
+
elsif country == "co"
|
47
|
+
if production
|
48
|
+
API_PROD_CO
|
49
|
+
else
|
50
|
+
API_DEV_CO
|
51
|
+
end
|
52
|
+
elsif country == "pe"
|
53
|
+
if production
|
54
|
+
API_PROD_PE
|
55
|
+
else
|
56
|
+
API_DEV_PE
|
57
|
+
end
|
58
|
+
else
|
59
|
+
LOG.error "Country not found, only mx (México) or co (Colombia)"
|
60
|
+
raise OpenpayException.new "Country not found, only 'mx' (México) or 'co' (Colombia)", false
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def env
|
65
|
+
if @production
|
66
|
+
:production
|
67
|
+
else
|
68
|
+
:test
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|