braspag-rest 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 188d46b28c046616b6cc4d02057b85839c9955ee
4
+ data.tar.gz: c1a09fb9365da297b85a6bb5c00c6a089b71b126
5
+ SHA512:
6
+ metadata.gz: 4d743c8c6c8f3c050e5e1f354c8c04891cf33075e553dcf2f503cc6efbdffa609955ef9e218e81fc3f5373d14b932435ac606153677cc43eb61e0592d34d1e5e
7
+ data.tar.gz: 76f4513012ad3af7838f8f5661e21bc952ce22995e7c712af455d12f98bfa70b54b0b8c9f07ce5874c19a06d1358cdaaec353aaa1c2aa59f63915a2a25696ca8
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /config/braspag-rest.yml
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.10.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in braspag-rest.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Antonio Filho
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,78 @@
1
+ # BraspagRest
2
+
3
+ [![Travis-CI](https://travis-ci.org/Dinda-com-br/braspag-rest.svg?branch=master)](https://travis-ci.org/Dinda-com-br/braspag-rest)
4
+
5
+ Gem to use Braspag gateway in his REST version.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'braspag-rest'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install braspag-rest
22
+
23
+ ## Usage
24
+
25
+ ### Authorize an order
26
+
27
+ ```rb
28
+ sale = BraspagRest::Sale.new(
29
+ order_id: '123456',
30
+ request_id: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
31
+ customer: {
32
+ name: 'Comprador Teste'
33
+ },
34
+ payment: {
35
+ type: 'CreditCard',
36
+ amount: 15700,
37
+ provider: 'Simulado',
38
+ installments: 1,
39
+ credit_card: {
40
+ number: '0000000000000001',
41
+ holder: 'Teste Holder',
42
+ expiration_date: '12/2021',
43
+ security_code: '123',
44
+ brand: 'Visa'
45
+ }
46
+ }
47
+ )
48
+
49
+ sale.save
50
+ ```
51
+
52
+ And to create a protected credit card, you should set the credit card saved as true:
53
+
54
+ ```rb
55
+ credit_card = BraspagRest::CreditCard.new
56
+ credit_card.number = '0000000000000001'
57
+ credit_card.holder = 'Teste Holder'
58
+ credit_card.expiration_date = '12/2021'
59
+ credit_card.security_code = '123'
60
+ credit_card.brand = 'Visa'
61
+ credit_card.saved = true
62
+ ```
63
+
64
+ ## Development
65
+
66
+ 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.
67
+
68
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
69
+
70
+ ## Contributing
71
+
72
+ Bug reports and pull requests are welcome on GitHub at https://github.com/Dinda-com-br/braspag-rest.
73
+
74
+
75
+ ## License
76
+
77
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
78
+
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "braspag-rest"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,28 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'braspag-rest/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "braspag-rest"
7
+ spec.version = BraspagRest::VERSION
8
+ spec.authors = ["Dinda Dev Team"]
9
+ spec.email = ["dev@dinda.com.br"]
10
+
11
+ spec.summary = %q{Gem to use Braspag gateway in his REST version}
12
+ spec.description = %q{Gem to use Braspag gateway in his REST version}
13
+ spec.homepage = "https://github.com/Dinda-com-br/braspag-rest"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency "rest-client", "~> 1.8"
21
+ spec.add_dependency "hashie", "~> 3.4"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.10"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec", "~> 3"
26
+ spec.add_development_dependency "pry"
27
+ spec.add_development_dependency "pry-nav"
28
+ end
@@ -0,0 +1,19 @@
1
+ module BraspagRest
2
+ def self.config
3
+ yield BraspagRest::Configuration.instance if block_given?
4
+
5
+ BraspagRest::Configuration.instance
6
+ end
7
+ end
8
+
9
+ require 'hashie'
10
+ require 'rest-client'
11
+ require 'braspag-rest/hashie'
12
+ require 'braspag-rest/version'
13
+ require 'braspag-rest/configuration'
14
+ require 'braspag-rest/response'
15
+ require 'braspag-rest/request'
16
+ require 'braspag-rest/customer'
17
+ require 'braspag-rest/credit_card'
18
+ require 'braspag-rest/payment'
19
+ require 'braspag-rest/sale'
@@ -0,0 +1,44 @@
1
+ require 'singleton'
2
+ require 'yaml'
3
+
4
+ module BraspagRest
5
+ class Configuration
6
+ include Singleton
7
+
8
+ attr_accessor :environment, :logger, :config_file_path
9
+
10
+ def config_file_path
11
+ @config_file_path || 'config/braspag-rest.yml'
12
+ end
13
+
14
+ def environment
15
+ @environment || ENV['RACK_ENV'] || ENV['RAILS_ENV'] || raise('You must set the environment!')
16
+ end
17
+
18
+ def log_enabled?
19
+ config['log_enable'] && logger
20
+ end
21
+
22
+ def url
23
+ config['url']
24
+ end
25
+
26
+ def query_url
27
+ config['query_url']
28
+ end
29
+
30
+ def merchant_id
31
+ config['merchant_id']
32
+ end
33
+
34
+ def merchant_key
35
+ config['merchant_key']
36
+ end
37
+
38
+ private
39
+
40
+ def config
41
+ @config ||= YAML.load(File.read(config_file_path))[environment]
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,11 @@
1
+ module BraspagRest
2
+ class CreditCard < Hashie::IUTrash
3
+ property :number, from: 'CardNumber'
4
+ property :holder, from: 'Holder'
5
+ property :expiration_date, from: 'ExpirationDate'
6
+ property :security_code, from: 'SecurityCode'
7
+ property :brand, from: 'Brand'
8
+ property :saved, from: 'SaveCard'
9
+ property :token, from: 'CardToken'
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ module BraspagRest
2
+ class Customer < Hashie::IUTrash
3
+ property :name, from: 'Name'
4
+ end
5
+ end
@@ -0,0 +1,2 @@
1
+ require 'braspag-rest/hashie/iudash'
2
+ require 'braspag-rest/hashie/iutrash'
@@ -0,0 +1,7 @@
1
+ require 'hashie/extensions/ignore_undeclared'
2
+
3
+ module Hashie
4
+ class IUDash < Hashie::Dash
5
+ include Hashie::Extensions::IgnoreUndeclared
6
+ end
7
+ end
@@ -0,0 +1,18 @@
1
+ require 'hashie/extensions/dash/property_translation'
2
+
3
+ module Hashie
4
+ class IUTrash < Hashie::IUDash
5
+ include Hashie::Extensions::Dash::PropertyTranslation
6
+
7
+ def inverse_attributes
8
+ {}.tap do |attributes|
9
+ self.class.translations.each do |from, property|
10
+ value = self.send(property)
11
+ value = value.respond_to?(:inverse_attributes) ? value.inverse_attributes : value
12
+
13
+ attributes[from] = value unless value.nil?
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,23 @@
1
+ module BraspagRest
2
+ class Payment < Hashie::IUTrash
3
+ include Hashie::Extensions::Coercion
4
+
5
+ property :id, from: 'PaymentId'
6
+ property :type, from: 'Type'
7
+ property :amount, from: 'Amount'
8
+ property :status, from: 'Status'
9
+ property :provider, from: 'Provider'
10
+ property :installments, from: 'Installments'
11
+ property :credit_card, from: 'CreditCard', with: ->(values) { BraspagRest::CreditCard.new(values) }
12
+ property :transaction_id, from: 'AcquirerTransactionId'
13
+ property :authorization_code, from: 'AuthorizationCode'
14
+ property :reason_code, from: 'ReasonCode'
15
+ property :reason_message, from: 'ReasonMessage'
16
+
17
+ coerce_key :credit_card, BraspagRest::CreditCard
18
+
19
+ def authorized?
20
+ status.to_i == 1
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,37 @@
1
+ module BraspagRest
2
+ class Request
3
+ class << self
4
+ SALE_ENDPOINT = '/v2/sales/'
5
+
6
+ def authorize(request_id, params)
7
+ gateway_response = RestClient.post(sale_url, params.to_json, default_headers.merge('RequestId' => request_id))
8
+ BraspagRest::Response.new(gateway_response)
9
+ rescue RestClient::ExceptionWithResponse => e
10
+ config.logger.warn("[BraspagRest] #{e}") if config.log_enabled?
11
+ BraspagRest::Response.new(e.response)
12
+ rescue RestClient::Exception => e
13
+ config.logger.error("[BraspagRest] #{e}") if config.log_enabled?
14
+ raise
15
+ end
16
+
17
+ private
18
+
19
+ def sale_url
20
+ config.url + SALE_ENDPOINT
21
+ end
22
+
23
+ def default_headers
24
+ {
25
+ 'Accept' => 'application/json',
26
+ 'Content-Type' => 'application/json',
27
+ 'MerchantId' => config.merchant_id,
28
+ 'MerchantKey' => config.merchant_key
29
+ }
30
+ end
31
+
32
+ def config
33
+ @config ||= BraspagRest::Configuration.instance
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,17 @@
1
+ module BraspagRest
2
+ class Response
3
+ attr_reader :gateway_response
4
+
5
+ def initialize(gateway_response)
6
+ @gateway_response = gateway_response
7
+ end
8
+
9
+ def success?
10
+ (200..207).include?(@gateway_response.code)
11
+ end
12
+
13
+ def parsed_body
14
+ JSON.parse(@gateway_response.body) rescue {}
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,33 @@
1
+ module BraspagRest
2
+ class Sale < Hashie::IUTrash
3
+ include Hashie::Extensions::Coercion
4
+
5
+ attr_reader :errors
6
+
7
+ property :request_id, from: 'RequestId'
8
+ property :order_id, from: 'MerchantOrderId'
9
+ property :customer, from: 'Customer', with: ->(values) { BraspagRest::Customer.new(values) }
10
+ property :payment, from: 'Payment', with: ->(values) { BraspagRest::Payment.new(values) }
11
+
12
+ coerce_key :customer, BraspagRest::Customer
13
+ coerce_key :payment, BraspagRest::Payment
14
+
15
+ def save
16
+ response = BraspagRest::Request.authorize(request_id, inverse_attributes)
17
+
18
+ if response.success?
19
+ initialize_attributes(response.parsed_body)
20
+ else
21
+ initialize_errors(response.parsed_body) and return false
22
+ end
23
+
24
+ true
25
+ end
26
+
27
+ private
28
+
29
+ def initialize_errors(errors)
30
+ @errors = errors.map{ |error| { code: error['Code'], message: error['Message'] } }
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,3 @@
1
+ module BraspagRest
2
+ VERSION = "0.1.1"
3
+ end
metadata ADDED
@@ -0,0 +1,166 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: braspag-rest
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Dinda Dev Team
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-07-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rest-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.8'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: hashie
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.4'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.4'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.10'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.10'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry-nav
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Gem to use Braspag gateway in his REST version
112
+ email:
113
+ - dev@dinda.com.br
114
+ executables:
115
+ - console
116
+ - setup
117
+ extensions: []
118
+ extra_rdoc_files: []
119
+ files:
120
+ - ".gitignore"
121
+ - ".rspec"
122
+ - ".travis.yml"
123
+ - Gemfile
124
+ - LICENSE.txt
125
+ - README.md
126
+ - Rakefile
127
+ - bin/console
128
+ - bin/setup
129
+ - braspag-rest.gemspec
130
+ - lib/braspag-rest.rb
131
+ - lib/braspag-rest/configuration.rb
132
+ - lib/braspag-rest/credit_card.rb
133
+ - lib/braspag-rest/customer.rb
134
+ - lib/braspag-rest/hashie.rb
135
+ - lib/braspag-rest/hashie/iudash.rb
136
+ - lib/braspag-rest/hashie/iutrash.rb
137
+ - lib/braspag-rest/payment.rb
138
+ - lib/braspag-rest/request.rb
139
+ - lib/braspag-rest/response.rb
140
+ - lib/braspag-rest/sale.rb
141
+ - lib/braspag-rest/version.rb
142
+ homepage: https://github.com/Dinda-com-br/braspag-rest
143
+ licenses:
144
+ - MIT
145
+ metadata: {}
146
+ post_install_message:
147
+ rdoc_options: []
148
+ require_paths:
149
+ - lib
150
+ required_ruby_version: !ruby/object:Gem::Requirement
151
+ requirements:
152
+ - - ">="
153
+ - !ruby/object:Gem::Version
154
+ version: '0'
155
+ required_rubygems_version: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ requirements: []
161
+ rubyforge_project:
162
+ rubygems_version: 2.4.8
163
+ signing_key:
164
+ specification_version: 4
165
+ summary: Gem to use Braspag gateway in his REST version
166
+ test_files: []