cielo-api30 1.0.0 → 1.1.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.
@@ -0,0 +1,31 @@
1
+ module Cielo
2
+ module API30
3
+ # API Environment URLs
4
+ #
5
+ # @attr [String] api API URL
6
+ # @attr [String] apiQuery API Query URL
7
+ class Environment
8
+ attr_accessor :api,
9
+ :api_query
10
+
11
+ def initialize(api, api_query)
12
+ @api = api
13
+ @api_query = api_query
14
+ end
15
+
16
+ # The production environment
17
+ #
18
+ # @return [Environment] a configured Environment for production
19
+ def self.production
20
+ new("https://api.cieloecommerce.cielo.com.br/", "https://apiquery.cieloecommerce.cielo.com.br/")
21
+ end
22
+
23
+ # The sandbox environment
24
+ #
25
+ # @return [Environment] a configured Environment for testing
26
+ def self.sandbox
27
+ new("https://apisandbox.cieloecommerce.cielo.com.br/", "https://apiquerysandbox.cieloecommerce.cielo.com.br/")
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,19 @@
1
+ module Cielo
2
+ module API30
3
+ # Merchant identification on Cielo
4
+ #
5
+ # @attr [String] merchant_id the merchant identification number
6
+ # @attr [String] merchant_key the merchant identification key
7
+ class Merchant
8
+ attr_accessor :merchant_id,
9
+ :merchant_key
10
+
11
+ # @param merchant_id [String] the merchant identification number
12
+ # @param merchant_key [String] the merchant identification key
13
+ def initialize(merchant_id, merchant_key)
14
+ @merchant_id = merchant_id
15
+ @merchant_key = merchant_key
16
+ end
17
+ end
18
+ end
19
+ end
@@ -1,125 +1,136 @@
1
+ require "cielo/api30/payment/status"
2
+ require "cielo/api30/return_info"
3
+
1
4
  module Cielo
5
+ module API30
2
6
  class Payment
3
- PAYMENTTYPE_CREDITCARD = "CreditCard"
4
- PAYMENTTYPE_DEBITCARD = "DebitCard"
5
- PAYMENTTYPE_ELECTRONIC_TRANSFER = "ElectronicTransfer"
6
- PAYMENTTYPE_BOLETO = "Boleto"
7
+ PAYMENTTYPE_CREDITCARD = "CreditCard"
8
+ PAYMENTTYPE_DEBITCARD = "DebitCard"
9
+ PAYMENTTYPE_ELECTRONIC_TRANSFER = "ElectronicTransfer"
10
+ PAYMENTTYPE_BOLETO = "Boleto"
11
+
12
+ PROVIDER_BRADESCO = "Bradesco"
13
+ PROVIDER_BANCO_DO_BRASIL = "BancoDoBrasil"
14
+ PROVIDER_SIMULADO = "Simulado"
15
+
16
+ attr_accessor :service_tax_amount,
17
+ :installments,
18
+ :interest,
19
+ :capture,
20
+ :authenticate,
21
+ :recurrent,
22
+ :recurrent_payment,
23
+ :credit_card,
24
+ :proof_of_sale,
25
+ :authorization_code,
26
+ :soft_descriptor,
27
+ :return_url,
28
+ :provider,
29
+ :payment_id,
30
+ :tid,
31
+ :type,
32
+ :amount,
33
+ :received_date,
34
+ :captured_amount,
35
+ :captured_date,
36
+ :currency,
37
+ :country,
38
+ :return_code,
39
+ :return_message,
40
+ :status,
41
+ :links,
42
+ :extra_data_collection,
43
+ :expiration_date,
44
+ :url,
45
+ :number,
46
+ :bar_code_number,
47
+ :digitable_line,
48
+ :address,
49
+ :return_info
7
50
 
8
- PROVIDER_BRADESCO = "Bradesco"
9
- PROVIDER_BANCO_DO_BRASIL = "BancoDoBrasil"
10
- PROVIDER_SIMULADO = "Simulado"
51
+ def initialize(amount, installments: 1)
52
+ @amount = amount
53
+ @installments = installments
54
+ end
11
55
 
12
- attr_accessor :service_tax_amount,
13
- :installments,
14
- :interest,
15
- :capture,
16
- :authenticate,
17
- :recurrent,
18
- :recurrent_payment,
19
- :credit_card,
20
- :proof_of_sale,
21
- :authorization_code,
22
- :soft_descriptor,
23
- :return_url,
24
- :provider,
25
- :payment_id,
26
- :tid,
27
- :type,
28
- :amount,
29
- :received_date,
30
- :captured_amount,
31
- :captured_date,
32
- :currency,
33
- :country,
34
- :return_code,
35
- :return_message,
36
- :status,
37
- :links,
38
- :extra_data_collection,
39
- :expiration_date,
40
- :url,
41
- :number,
42
- :bar_code_number,
43
- :digitable_line,
44
- :address
56
+ def to_json(*options)
57
+ hash = as_json(*options)
58
+ hash.reject! {|k,v| v.nil?}
59
+ hash.to_json(*options)
60
+ end
45
61
 
46
- def initialize(amount, installments: 1)
47
- @amount = amount
48
- @installments = installments
49
- end
62
+ def self.from_json(data)
63
+ return if data.nil?
50
64
 
51
- def to_json(*options)
52
- hash = as_json(*options)
53
- hash.reject! {|k,v| v.nil?}
54
- hash.to_json(*options)
55
- end
65
+ payment = new(data["Amount"])
56
66
 
57
- def self.from_json(data)
58
- if (data != nil)
59
- payment = Payment.new(data["Amount"] || nil)
67
+ payment.service_tax_amount = data["ServiceTaxAmount"]
68
+ payment.installments = data["Installments"]
69
+ payment.interest = data["Interest"]
70
+ payment.capture = data["Capture"]
71
+ payment.authenticate = data["Authenticate"]
72
+ payment.recurrent = data["Recurrent"]
73
+ payment.recurrent_payment = RecurrentPayment.from_json(data["RecurrentPayment"])
74
+ payment.credit_card = CreditCard.from_json(data["CreditCard"])
75
+ payment.proof_of_sale = data["ProofOfSale"]
76
+ payment.authorization_code = data["AuthorizationCode"]
77
+ payment.soft_descriptor = data["SoftDescriptor"]
78
+ payment.return_url = data["ReturnUrl"]
79
+ payment.provider = data["Provider"]
80
+ payment.payment_id = data["PaymentId"]
81
+ payment.tid = data["Tid"]
82
+ payment.type = data["Type"]
83
+ payment.received_date = data["ReceivedDate"]
84
+ payment.captured_amount = data["CapturedAmount"]
85
+ payment.captured_date = data["CapturedDate"]
86
+ payment.currency = data["Currency"]
87
+ payment.country = data["Country"]
88
+ payment.return_code = data["ReturnCode"]
89
+ payment.return_message = data["ReturnMessage"]
90
+ payment.status = data["Status"]
60
91
 
61
- payment.service_tax_amount = data["ServiceTaxAmount"] || nil
62
- payment.installments = data["Installments"] || nil
63
- payment.interest = data["Interest"] || nil
64
- payment.capture = data["Capture"] || nil
65
- payment.authenticate = data["Authenticate"] || nil
66
- payment.recurrent = data["Recurrent"] || nil
67
- payment.recurrent_payment = Cielo::RecurrentPayment.from_json(data["RecurrentPayment"] || nil)
68
- payment.credit_card = Cielo::CreditCard.from_json(data["CreditCard"] || nil)
69
- payment.proof_of_sale = data["ProofOfSale"] || nil
70
- payment.authorization_code = data["AuthorizationCode"] || nil
71
- payment.soft_descriptor = data["SoftDescriptor"] || nil
72
- payment.return_url = data["ReturnUrl"] || nil
73
- payment.provider = data["Provider"] || nil
74
- payment.payment_id = data["PaymentId"] || nil
75
- payment.tid = data["Tid"] || nil
76
- payment.type = data["Type"] || nil
77
- payment.received_date = data["ReceivedDate"] || nil
78
- payment.captured_amount = data["CapturedAmount"] || nil
79
- payment.captured_date = data["CapturedDate"] || nil
80
- payment.currency = data["Currency"] || nil
81
- payment.country = data["Country"] || nil
82
- payment.return_code = data["ReturnCode"] || nil
83
- payment.return_message = data["ReturnMessage"] || nil
84
- payment.status = data["Status"] || nil
92
+ payment.links = data["Links"]
93
+ payment.extra_data_collection = data["ExtraDataCollection"]
85
94
 
86
- payment.links = data["Links"] || nil
87
- payment.extra_data_collection = data["ExtraDataCollection"] || nil
95
+ payment.expiration_date = data["ExpirationDate"]
96
+ payment.url = data["Url"]
97
+ payment.number = data["Number"]
98
+ payment.bar_code_number = data["BarCodeNumber"]
99
+ payment.digitable_line = data["DigitableLine"]
100
+ payment.address = data["Address"]
101
+ payment.return_info = ReturnInfo.new(payment.return_code)
88
102
 
89
- payment.expiration_date = data["ExpirationDate"] || nil
90
- payment.url = data["Url"] || nil
91
- payment.number = data["Number"] || nil
92
- payment.bar_code_number = data["BarCodeNumber"] || nil
93
- payment.digitable_line = data["DigitableLine"] || nil
94
- payment.address = data["Address"] || nil
103
+ payment
104
+ end
95
105
 
96
- return payment
97
- end
98
- end
106
+ def success?
107
+ Status.success?(status)
108
+ end
99
109
 
100
- private
101
- def as_json(options={})
102
- {
103
- ServiceTaxAmount: @service_tax_amount,
104
- Installments: @installments,
105
- Interest: @interest,
106
- Capture: @capture,
107
- Authenticate: @authenticate,
108
- Recurrent: @recurrent,
109
- RecurrentPayment: @recurrent_payment,
110
- CreditCard: @credit_card,
111
- SoftDescriptor: @soft_descriptor,
112
- ReturnUrl: @return_url,
113
- Provider: @provider,
114
- Type: @type,
115
- Amount: @amount,
116
- Currency: @currency,
117
- Country: @country,
118
- Number: @number,
119
- BarCodeNumber: @bar_code_number,
120
- DigitableLine: @digitable_line,
121
- Address: @address
122
- }
123
- end
110
+ def as_json(options={})
111
+ {
112
+ ServiceTaxAmount: @service_tax_amount,
113
+ Installments: @installments,
114
+ Interest: @interest,
115
+ Capture: @capture,
116
+ Authenticate: @authenticate,
117
+ Recurrent: @recurrent,
118
+ RecurrentPayment: @recurrent_payment,
119
+ CreditCard: @credit_card,
120
+ SoftDescriptor: @soft_descriptor,
121
+ ReturnUrl: @return_url,
122
+ Provider: @provider,
123
+ Type: @type,
124
+ Amount: @amount,
125
+ Currency: @currency,
126
+ Country: @country,
127
+ Number: @number,
128
+ BarCodeNumber: @bar_code_number,
129
+ DigitableLine: @digitable_line,
130
+ Address: @address,
131
+ ReturnInfo: @return_info
132
+ }
133
+ end
124
134
  end
135
+ end
125
136
  end
@@ -0,0 +1,28 @@
1
+ module Cielo
2
+ module API30
3
+ class Payment
4
+ module Status
5
+ NOT_FINISHED = 0
6
+ AUTHORIZED = 1
7
+ PAYMENT_CONFIRMED = 2
8
+ DENIED = 3
9
+ VOIDED = 10
10
+ REFUNDED = 11
11
+ PENDING = 12
12
+ ABORTED = 13
13
+ SCHEDULED = 20
14
+
15
+ def self.success?(code)
16
+ [
17
+ AUTHORIZED,
18
+ PAYMENT_CONFIRMED,
19
+ VOIDED,
20
+ REFUNDED,
21
+ PENDING,
22
+ SCHEDULED
23
+ ].include?(code)
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -1,46 +1,45 @@
1
1
  module Cielo
2
+ module API30
2
3
  class RecurrentPayment
3
- INTERVAL_MONTHLY = "Monthly"
4
- INTERVAL_BIMONTHLY = "Bimonthly"
5
- INTERVAL_QUARTERLY = "Quarterly"
6
- INTERVAL_SEMIANNUAL = "SemiAnnual"
7
- INTERVAL_ANNUAL = "Annual"
4
+ INTERVAL_MONTHLY = "Monthly"
5
+ INTERVAL_BIMONTHLY = "Bimonthly"
6
+ INTERVAL_QUARTERLY = "Quarterly"
7
+ INTERVAL_SEMIANNUAL = "SemiAnnual"
8
+ INTERVAL_ANNUAL = "Annual"
8
9
 
9
- attr_accessor :authorize_now,
10
- :start_date,
11
- :end_date,
12
- :interval
10
+ attr_accessor :authorize_now,
11
+ :start_date,
12
+ :end_date,
13
+ :interval
13
14
 
14
- def initialize(authorize_now=true)
15
- @authorize_now = authorize_now
16
- end
15
+ def initialize(authorize_now=true)
16
+ @authorize_now = authorize_now
17
+ end
17
18
 
18
- def to_json(*options)
19
- hash = as_json(*options)
20
- hash.reject! {|k,v| v.nil?}
21
- hash.to_json(*options)
22
- end
19
+ def to_json(*options)
20
+ hash = as_json(*options)
21
+ hash.reject! {|k,v| v.nil?}
22
+ hash.to_json(*options)
23
+ end
23
24
 
24
- def self.from_json(data)
25
- if (data != nil)
26
- recurrent_payment = RecurrentPayment.new(data["AuthorizeNow"] || false)
25
+ def self.from_json(data)
26
+ return if data.nil?
27
27
 
28
- recurrent_payment.start_date =data["StartDate"] || nil
29
- recurrent_payment.end_date =data["EndDate"] || nil
30
- recurrent_payment.interval =data["Interval"] || nil
28
+ recurrent_payment = new(data["AuthorizeNow"])
29
+ recurrent_payment.start_date =data["StartDate"]
30
+ recurrent_payment.end_date =data["EndDate"]
31
+ recurrent_payment.interval =data["Interval"]
32
+ recurrent_payment
33
+ end
31
34
 
32
- return recurrent_payment
33
- end
34
- end
35
-
36
- private
37
- def as_json(options={})
38
- {
39
- AuthorizeNow: @authorize_now,
40
- StartDate: @start_date,
41
- EndDate: @end_date,
42
- Interval: @interval
43
- }
44
- end
35
+ def as_json(options={})
36
+ {
37
+ AuthorizeNow: @authorize_now,
38
+ StartDate: @start_date,
39
+ EndDate: @end_date,
40
+ Interval: @interval
41
+ }
42
+ end
45
43
  end
44
+ end
46
45
  end
@@ -1,7 +1,15 @@
1
- module Cielo::Request
2
- class CieloError < StandardError
3
- def initialize(msg=nil)
4
- super
1
+ module Cielo
2
+ module API30
3
+ module Request
4
+ class CieloError < StandardError
5
+ attr_reader :code, :body
6
+
7
+ def initialize(code, body)
8
+ @code = code
9
+ @body = body
10
+ super("Error [#{code}] #{body}")
5
11
  end
12
+ end
6
13
  end
14
+ end
7
15
  end
@@ -4,40 +4,44 @@ require "net/http"
4
4
 
5
5
  require "cielo/api30/request/cielo_error"
6
6
 
7
- module Cielo::Request
8
- class CieloRequest
7
+ module Cielo
8
+ module API30
9
+ module Request
10
+ class CieloRequest
9
11
  attr_accessor :merchant
10
12
  private :merchant
11
13
 
12
14
  def initialize(merchant)
13
- @merchant = merchant
15
+ @merchant = merchant
14
16
  end
15
17
 
16
18
  protected
17
19
  def send_request(method, uri, data=nil)
18
- body = nil
19
- headers = {"User-Agent" => "CieloEcommerce/3.0 Ruby SDK",
20
- "RequestId" => UUIDTools::UUID.random_create.to_s,
21
- "MerchantId" => merchant.merchant_id,
22
- "MerchantKey" => merchant.merchant_key}
20
+ body = nil
21
+ headers = {"User-Agent" => "CieloEcommerce/3.0 Ruby SDK",
22
+ "RequestId" => UUIDTools::UUID.random_create.to_s,
23
+ "MerchantId" => merchant.merchant_id,
24
+ "MerchantKey" => merchant.merchant_key}
23
25
 
24
- if (data == nil)
25
- headers["Content-Length"] = "0"
26
- else
27
- headers["Content-Type"] = "application/json"
28
- body = data.to_json
29
- end
26
+ if data.nil?
27
+ headers["Content-Length"] = "0"
28
+ else
29
+ headers["Content-Type"] = "application/json"
30
+ body = data.to_json
31
+ end
30
32
 
31
- client = Net::HTTP.new(uri.host, uri.port)
32
- client.use_ssl = true
33
+ client = Net::HTTP.new(uri.host, uri.port)
34
+ client.use_ssl = true
33
35
 
34
- response = client.send_request(method, uri.path, body, headers)
36
+ response = client.send_request(method, uri.path, body, headers)
35
37
 
36
- data = JSON.parse(response.body)
38
+ data = JSON.parse(response.body)
37
39
 
38
- raise CieloError, "Error [" + data[0]["Code"].to_s + "] " + data[0]["Message"] if response.code.to_i >= 400
40
+ raise CieloError.new(data.first["Code"], data.first["Message"]) if response.code.to_i >= 400
39
41
 
40
- return data
42
+ data
41
43
  end
44
+ end
42
45
  end
46
+ end
43
47
  end