braspag-rest 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 188d46b28c046616b6cc4d02057b85839c9955ee
4
- data.tar.gz: c1a09fb9365da297b85a6bb5c00c6a089b71b126
3
+ metadata.gz: 5f7832645172c17c368aeb177d4357004eabfc98
4
+ data.tar.gz: 36dbf5a8871c02b35266c3e5614044d7658afb6b
5
5
  SHA512:
6
- metadata.gz: 4d743c8c6c8f3c050e5e1f354c8c04891cf33075e553dcf2f503cc6efbdffa609955ef9e218e81fc3f5373d14b932435ac606153677cc43eb61e0592d34d1e5e
7
- data.tar.gz: 76f4513012ad3af7838f8f5661e21bc952ce22995e7c712af455d12f98bfa70b54b0b8c9f07ce5874c19a06d1358cdaaec353aaa1c2aa59f63915a2a25696ca8
6
+ metadata.gz: 4aa89fed13f00f3d6838fed3a8743f7f98fab764ea21d219a3f890d1ba023a82e45a66239a6bbe41107b16f963993c1cbbd79fce26a7b4e7209903c44a70788a
7
+ data.tar.gz: 86a7e23f7ab0ac05b44cc4f76d536055edad4d769c2003f2daa964ba58c6a34d6087c742600104e51366082dba8e8fad36dfaf24808178f844fb30983406fbcc
data/README.md CHANGED
@@ -61,6 +61,28 @@ credit_card.brand = 'Visa'
61
61
  credit_card.saved = true
62
62
  ```
63
63
 
64
+ ### Find a sale
65
+
66
+ ```rb
67
+ sale = BraspagRest::Sale.find('REQUEST_ID', 'PAYMENT_ID')
68
+ sale.customer.name
69
+ => Maria
70
+ ```
71
+
72
+ ### Cancel a sale
73
+
74
+ ```rb
75
+ sale = BraspagRest::Sale.find('REQUEST_ID', 'PAYMENT_ID')
76
+ sale.cancel
77
+ ```
78
+
79
+ ### Capture a sale
80
+
81
+ ```rb
82
+ sale = BraspagRest::Sale.find('REQUEST_ID', 'PAYMENT_ID')
83
+ sale.capture
84
+ ```
85
+
64
86
  ## Development
65
87
 
66
88
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -2,6 +2,10 @@ module BraspagRest
2
2
  class Payment < Hashie::IUTrash
3
3
  include Hashie::Extensions::Coercion
4
4
 
5
+ STATUS_AUTHORIZED = 1
6
+ STATUS_CONFIRMED = 2
7
+ STATUS_VOIDED = 10
8
+
5
9
  property :id, from: 'PaymentId'
6
10
  property :type, from: 'Type'
7
11
  property :amount, from: 'Amount'
@@ -17,7 +21,15 @@ module BraspagRest
17
21
  coerce_key :credit_card, BraspagRest::CreditCard
18
22
 
19
23
  def authorized?
20
- status.to_i == 1
24
+ status.to_i.eql?(STATUS_AUTHORIZED)
25
+ end
26
+
27
+ def captured?
28
+ status.to_i.eql?(STATUS_CONFIRMED)
29
+ end
30
+
31
+ def cancelled?
32
+ status.to_i.eql?(STATUS_VOIDED)
21
33
  end
22
34
  end
23
35
  end
@@ -2,10 +2,41 @@ module BraspagRest
2
2
  class Request
3
3
  class << self
4
4
  SALE_ENDPOINT = '/v2/sales/'
5
+ VOID_ENDPOINT = '/void'
6
+ CAPTURE_ENDPOINT = '/capture'
5
7
 
6
8
  def authorize(request_id, params)
7
- gateway_response = RestClient.post(sale_url, params.to_json, default_headers.merge('RequestId' => request_id))
9
+ execute_braspag_request do
10
+ RestClient.post(sale_url, params.to_json, default_headers.merge('RequestId' => request_id))
11
+ end
12
+ end
13
+
14
+ def void(request_id, payment_id, amount)
15
+ execute_braspag_request do
16
+ RestClient.put(void_url(payment_id), { Amount: amount }.to_json, default_headers.merge('RequestId' => request_id))
17
+ end
18
+ end
19
+
20
+ def get_sale(request_id, payment_id)
21
+ execute_braspag_request do
22
+ RestClient.get(search_sale_url(payment_id), default_headers.merge('RequestId' => request_id))
23
+ end
24
+ end
25
+
26
+ def capture(request_id, payment_id, amount)
27
+ execute_braspag_request do
28
+ RestClient.put(capture_url(payment_id), { Amount: amount }.to_json, default_headers.merge('RequestId' => request_id))
29
+ end
30
+ end
31
+
32
+ private
33
+
34
+ def execute_braspag_request(&block)
35
+ gateway_response = block.call
8
36
  BraspagRest::Response.new(gateway_response)
37
+ rescue RestClient::ResourceNotFound => e
38
+ config.logger.error("[BraspagRest] #{e}") if config.log_enabled?
39
+ raise
9
40
  rescue RestClient::ExceptionWithResponse => e
10
41
  config.logger.warn("[BraspagRest] #{e}") if config.log_enabled?
11
42
  BraspagRest::Response.new(e.response)
@@ -14,12 +45,22 @@ module BraspagRest
14
45
  raise
15
46
  end
16
47
 
17
- private
18
-
19
48
  def sale_url
20
49
  config.url + SALE_ENDPOINT
21
50
  end
22
51
 
52
+ def void_url(payment_id)
53
+ sale_url + payment_id.to_s + VOID_ENDPOINT
54
+ end
55
+
56
+ def search_sale_url(payment_id)
57
+ config.query_url + SALE_ENDPOINT + payment_id.to_s
58
+ end
59
+
60
+ def capture_url(payment_id)
61
+ sale_url + payment_id.to_s + CAPTURE_ENDPOINT
62
+ end
63
+
23
64
  def default_headers
24
65
  {
25
66
  'Accept' => 'application/json',
@@ -12,6 +12,12 @@ module BraspagRest
12
12
  coerce_key :customer, BraspagRest::Customer
13
13
  coerce_key :payment, BraspagRest::Payment
14
14
 
15
+ def self.find(request_id, payment_id)
16
+ response = BraspagRest::Request.get_sale(request_id, payment_id)
17
+
18
+ new(response.parsed_body)
19
+ end
20
+
15
21
  def save
16
22
  response = BraspagRest::Request.authorize(request_id, inverse_attributes)
17
23
 
@@ -24,10 +30,34 @@ module BraspagRest
24
30
  true
25
31
  end
26
32
 
33
+ def cancel(amount = nil)
34
+ response = BraspagRest::Request.void(request_id, payment.id, (amount || payment.amount))
35
+
36
+ if response.success?
37
+ initialize_attributes('Payment' => response.parsed_body)
38
+ else
39
+ initialize_errors(response.parsed_body) and return false
40
+ end
41
+
42
+ payment.cancelled?
43
+ end
44
+
45
+ def capture(amount = nil)
46
+ response = BraspagRest::Request.capture(request_id, payment.id, (amount || payment.amount))
47
+
48
+ if response.success?
49
+ initialize_attributes('Payment' => response.parsed_body)
50
+ else
51
+ initialize_errors(response.parsed_body) and return false
52
+ end
53
+
54
+ payment.captured?
55
+ end
56
+
27
57
  private
28
58
 
29
59
  def initialize_errors(errors)
30
- @errors = errors.map{ |error| { code: error['Code'], message: error['Message'] } }
60
+ @errors = errors.map { |error| { code: error['Code'], message: error['Message'] } }
31
61
  end
32
62
  end
33
63
  end
@@ -1,3 +1,3 @@
1
1
  module BraspagRest
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: braspag-rest
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dinda Dev Team
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-05 00:00:00.000000000 Z
11
+ date: 2015-07-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client