better-mundipagg 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/Berksfile +3 -0
  4. data/Berksfile.lock +59 -0
  5. data/Gemfile.lock +35 -3
  6. data/Vagrantfile +40 -0
  7. data/lib/mundipagg/boleto.rb +30 -0
  8. data/lib/mundipagg/client.rb +30 -0
  9. data/lib/mundipagg/credit_card.rb +52 -0
  10. data/lib/mundipagg/error.rb +10 -0
  11. data/lib/mundipagg/gateway.rb +35 -0
  12. data/lib/mundipagg/key.rb +21 -0
  13. data/lib/mundipagg/message.rb +18 -0
  14. data/lib/mundipagg/refund.rb +28 -0
  15. data/lib/mundipagg/response.rb +27 -0
  16. data/lib/mundipagg/version.rb +1 -1
  17. data/lib/mundipagg.rb +10 -19
  18. data/mundipagg.gemspec +3 -0
  19. data/spec/fixtures/vcr_cassettes/Mundipagg_Client/_create_order/sends_a_create_order_command.yml +150 -0
  20. data/spec/fixtures/vcr_cassettes/Mundipagg_Client/_manage_order/sends_a_manager_order_command.yml +149 -0
  21. data/spec/fixtures/vcr_cassettes/Mundipagg_Gateway_Client/_create_order/sends_a_create_order_command.yml +150 -0
  22. data/spec/fixtures/vcr_cassettes/Mundipagg_Gateway_Client/_manage_order/sends_a_manager_order_command.yml +149 -0
  23. data/spec/lib/mundipagg/boleto_spec.rb +67 -0
  24. data/spec/lib/mundipagg/client_spec.rb +79 -0
  25. data/spec/lib/mundipagg/credit_card_spec.rb +117 -0
  26. data/spec/lib/mundipagg/gateway_spec.rb +82 -0
  27. data/spec/lib/mundipagg/{gateway/message_spec.rb → message_spec.rb} +1 -1
  28. data/spec/lib/mundipagg/refund_spec.rb +65 -0
  29. data/spec/lib/mundipagg/response_spec.rb +43 -0
  30. data/spec/spec_helper.rb +14 -3
  31. metadata +67 -12
  32. data/lib/mundipagg/buyer.rb +0 -4
  33. data/lib/mundipagg/gateway/client.rb +0 -28
  34. data/lib/mundipagg/gateway/key.rb +0 -23
  35. data/lib/mundipagg/gateway/message.rb +0 -20
  36. data/lib/mundipagg/order.rb +0 -26
  37. data/lib/mundipagg/transaction/boleto.rb +0 -23
  38. data/spec/lib/mundipagg/gateway/client_spec.rb +0 -23
  39. data/spec/lib/mundipagg/order_spec.rb +0 -54
  40. data/spec/lib/mundipagg/transaction/boleto_spec.rb +0 -18
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bf6c2fa6efbdc60ab99a7fd6cd7e1366ada9a353
4
- data.tar.gz: cd36a9dade4a75ec020155d19d2d78c3fdd3bcad
3
+ metadata.gz: d075c32d2d62f4449c782dd94a29a1b4d3cb5dec
4
+ data.tar.gz: 87eb137a2dc51629fd61e227450a3df58231ede0
5
5
  SHA512:
6
- metadata.gz: 4ea9480b10ef21375e1f1869785751e8d7efda76d44ecca1f98e0a0125b9efa903b65f3d2d0ecbb3d7aab0974da39bdad0102bfeae0e4f67cba3b1f4e43a91d2
7
- data.tar.gz: 90e43a293df2f3964b51056e57fa2a47e8225d594577b57122ac1736b51d50c1adb5c00d7bc7da628d14b3d9602bf8d7e8d331e27016dc9025706a9f25ec54d5
6
+ metadata.gz: df041e31aa47b8647cbb2fe8ad2ac8ef8e769aa9715ea888cec2cfdcda38d2634eaf876f9a4dbc42f55d65eeeac48a96a1a9c9a5e8c6ec5efa8094f4b8c379c6
7
+ data.tar.gz: a4f14ced45d365e8743056d60f537bd810d919677912d8a24c1a3d7c4f4e80625e2f96efcaef1801656090ede8fdd5e9a7aa18ac607aea64eb6f817a0c80303c
data/.gitignore CHANGED
@@ -1 +1,2 @@
1
1
  coverage
2
+ .vagrant
data/Berksfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://api.berkshelf.com"
2
+
3
+ cookbook "docker", git: "git://github.com/bflad/chef-docker.git"
data/Berksfile.lock ADDED
@@ -0,0 +1,59 @@
1
+ DEPENDENCIES
2
+ docker
3
+ git: git://github.com/bflad/chef-docker.git
4
+ revision: ccaaaee6d8311fb04e5b9a1d3ad96943f5436bd4
5
+
6
+ GRAPH
7
+ apt (2.6.0)
8
+ aufs (0.1.1)
9
+ apt (>= 0.0.0)
10
+ modules (>= 0.0.0)
11
+ build-essential (2.1.3)
12
+ chef_handler (1.1.6)
13
+ device-mapper (0.1.0)
14
+ apt (>= 0.0.0)
15
+ modules (>= 0.0.0)
16
+ dmg (2.2.2)
17
+ docker (0.36.0)
18
+ apt (>= 0.0.0)
19
+ aufs (>= 0.1.1)
20
+ device-mapper (>= 0.1.0)
21
+ git (>= 0.0.0)
22
+ golang (>= 0.0.0)
23
+ homebrew (>= 0.0.0)
24
+ iptables (>= 0.0.0)
25
+ lxc (>= 1.1.6)
26
+ modules (>= 0.0.0)
27
+ runit (>= 0.0.0)
28
+ sysctl (>= 0.0.0)
29
+ yum-epel (>= 0.0.0)
30
+ dpkg_autostart (0.1.12)
31
+ git (4.0.2)
32
+ build-essential (>= 0.0.0)
33
+ dmg (>= 0.0.0)
34
+ runit (>= 1.0.0)
35
+ windows (>= 0.0.0)
36
+ yum (~> 3.0)
37
+ yum-epel (>= 0.0.0)
38
+ golang (1.5.0)
39
+ homebrew (1.10.0)
40
+ iptables (0.14.0)
41
+ iptables-ng (2.2.4)
42
+ lxc (2.0.0)
43
+ dpkg_autostart (>= 0.1.10)
44
+ iptables-ng (>= 0.0.0)
45
+ polipo (>= 0.0.0)
46
+ modules (0.2.0)
47
+ ohai (2.0.1)
48
+ polipo (0.1.0)
49
+ runit (1.5.12)
50
+ build-essential (>= 0.0.0)
51
+ yum (~> 3.0)
52
+ yum-epel (>= 0.0.0)
53
+ sysctl (0.6.2)
54
+ ohai (>= 0.0.0)
55
+ windows (1.36.1)
56
+ chef_handler (>= 0.0.0)
57
+ yum (3.5.1)
58
+ yum-epel (0.5.3)
59
+ yum (~> 3.0)
data/Gemfile.lock CHANGED
@@ -1,13 +1,25 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- mundipagg (0.0.1)
4
+ better-mundipagg (0.0.1)
5
+ activemerchant
5
6
  activemodel
6
7
  savon
7
8
 
8
9
  GEM
9
10
  remote: https://rubygems.org/
10
11
  specs:
12
+ active_utils (2.2.3)
13
+ activesupport (>= 2.3.11)
14
+ i18n
15
+ activemerchant (1.45.0)
16
+ active_utils (~> 2.2.0)
17
+ activesupport (>= 3.2.14, < 5.0.0)
18
+ builder (>= 2.1.2, < 4.0.0)
19
+ i18n (>= 0.6.9)
20
+ json (~> 1.7)
21
+ nokogiri (~> 1.4)
22
+ offsite_payments (~> 2.0.0)
11
23
  activemodel (4.1.8)
12
24
  activesupport (= 4.1.8)
13
25
  builder (~> 3.1)
@@ -17,10 +29,13 @@ GEM
17
29
  minitest (~> 5.1)
18
30
  thread_safe (~> 0.1)
19
31
  tzinfo (~> 1.1)
32
+ addressable (2.3.6)
20
33
  akami (1.2.2)
21
34
  gyoku (>= 0.4.0)
22
35
  nokogiri
23
36
  builder (3.2.2)
37
+ crack (0.4.2)
38
+ safe_yaml (~> 1.0.0)
24
39
  diff-lcs (1.2.5)
25
40
  docile (1.1.5)
26
41
  gyoku (1.2.2)
@@ -33,11 +48,21 @@ GEM
33
48
  systemu (~> 2.6.2)
34
49
  mime-types (1.25.1)
35
50
  mini_portile (0.6.1)
36
- minitest (5.4.3)
51
+ minitest (5.5.0)
52
+ money (6.4.0)
53
+ i18n (>= 0.6.4, <= 0.7.0.beta1)
37
54
  multi_json (1.10.1)
38
55
  nokogiri (1.6.5)
39
56
  mini_portile (~> 0.6.0)
40
57
  nori (2.4.0)
58
+ offsite_payments (2.0.1)
59
+ active_utils (~> 2.2.0)
60
+ activesupport (>= 3.2.14, < 5.0.0)
61
+ builder (>= 2.1.2, < 4.0.0)
62
+ i18n (~> 0.5)
63
+ json (~> 1.7)
64
+ money (< 7.0.0)
65
+ nokogiri (~> 1.4)
41
66
  rack (1.5.2)
42
67
  rspec (3.1.0)
43
68
  rspec-core (~> 3.1.0)
@@ -51,6 +76,7 @@ GEM
51
76
  rspec-mocks (3.1.3)
52
77
  rspec-support (~> 3.1.0)
53
78
  rspec-support (3.1.2)
79
+ safe_yaml (1.0.4)
54
80
  savon (2.8.0)
55
81
  akami (~> 1.2)
56
82
  builder (>= 2.1.2)
@@ -71,15 +97,21 @@ GEM
71
97
  thread_safe (~> 0.1)
72
98
  uuid (2.3.7)
73
99
  macaddr (~> 1.0)
100
+ vcr (2.9.3)
74
101
  wasabi (3.3.0)
75
102
  httpi (~> 2.0)
76
103
  mime-types (< 2.0.0)
77
104
  nokogiri (>= 1.4.0)
105
+ webmock (1.20.4)
106
+ addressable (>= 2.3.6)
107
+ crack (>= 0.3.2)
78
108
 
79
109
  PLATFORMS
80
110
  ruby
81
111
 
82
112
  DEPENDENCIES
83
- mundipagg!
113
+ better-mundipagg!
84
114
  rspec (~> 3.1.0)
85
115
  simplecov (~> 0.9.0)
116
+ vcr (~> 2.9.3)
117
+ webmock
data/Vagrantfile ADDED
@@ -0,0 +1,40 @@
1
+ # -*- mode: ruby -*-
2
+ # vi: set ft=ruby :
3
+
4
+ VAGRANTFILE_API_VERSION = "2"
5
+
6
+ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
7
+ config.vm.box = "ubuntu/trusty64"
8
+ config.vm.network :private_network, ip: "10.10.10.10"
9
+ config.ssh.forward_agent = true
10
+ config.vm.synced_folder ".", "/vagrant", nfs: true
11
+ config.vm.boot_timeout = 120
12
+
13
+ config.omnibus.chef_version = :latest
14
+ config.berkshelf.enabled = true
15
+
16
+ config.vm.provider "virtualbox" do |v|
17
+ host = RbConfig::CONFIG['host_os']
18
+ if host =~ /darwin/
19
+ cpus = `sysctl -n hw.ncpu`.to_i
20
+ mem = `sysctl -n hw.memsize`.to_i / 1024 / 1024 / 2
21
+ else
22
+ cpus = `nproc`.to_i
23
+ mem = `grep 'MemTotal' /proc/meminfo | sed -e 's/MemTotal://' -e 's/ kB//'`.to_i / 1024 / 2
24
+ end
25
+
26
+ v.customize ["modifyvm", :id, "--memory", mem]
27
+ v.customize ["modifyvm", :id, "--cpus", cpus]
28
+ end
29
+
30
+ config.vm.provision :chef_solo do |chef|
31
+ chef.add_recipe "docker"
32
+
33
+ chef.json = {
34
+ "docker" => {
35
+ "group_members" => ["vagrant"],
36
+ "image_cmd_timeout" => 3600
37
+ }
38
+ }
39
+ end
40
+ end
@@ -0,0 +1,30 @@
1
+ module Mundipagg
2
+ class Boleto < ActiveMerchant::Billing::Model
3
+ attr_accessor :bank_number, :days_to_expire, :bank_code
4
+
5
+ def payload(amount)
6
+ content = {
7
+ amount_in_cents: amount,
8
+ bank_number: bank_number,
9
+ days_to_add_in_boleto_expiration_date: days_to_expire || 5,
10
+ nosso_numero: bank_code,
11
+ }
12
+
13
+ {
14
+ amount_in_cents: amount,
15
+ currency_iso_enum: "BRL",
16
+ boleto_transaction_collection: { boleto_transaction: content }
17
+ }
18
+ end
19
+
20
+ class Response < Mundipagg::Response
21
+ def payload
22
+ body[:create_order_response][:create_order_result]
23
+ end
24
+
25
+ def error_item
26
+ payload[:error_report][:error_item_collection][:error_item]
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,30 @@
1
+ require "savon"
2
+ require "mundipagg/message"
3
+
4
+ module Mundipagg
5
+ class Client
6
+ def adapter
7
+ @adapter ||= Savon.client do
8
+ wsdl "https://transaction.mundipaggone.com/MundiPaggService.svc?wsdl"
9
+ namespaces "xmlns:mun" => "http://schemas.datacontract.org/2004/07/MundiPagg.One.Service.DataContracts"
10
+ log_level ::ActiveMerchant::Billing::Base.test? ? :debug : :error
11
+ log true
12
+ end
13
+ end
14
+
15
+ def call(operation, command, message)
16
+ root = Mundipagg::Key.new(command, "tns").to_s
17
+ message = Mundipagg::Message.new(message).translate
18
+
19
+ adapter.call(operation, message: { root => message })
20
+ end
21
+
22
+ def manage_order(message)
23
+ call :manage_order, :manage_order_request, message
24
+ end
25
+
26
+ def create_order(message)
27
+ call :create_order, :create_order_request, message
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,52 @@
1
+ module Mundipagg
2
+ class CreditCard < ActiveMerchant::Billing::CreditCard
3
+ attr_accessor :installment_count
4
+
5
+ def brand_name
6
+ return "" if !brand.present?
7
+
8
+ if brand == "master"
9
+ "Mastercard"
10
+ else
11
+ brand.capitalize
12
+ end
13
+ end
14
+
15
+ def payload(amount)
16
+ content = {
17
+ amount_in_cents: amount,
18
+ credit_card_brand_enum: brand_name,
19
+ credit_card_number: number,
20
+ credit_card_operation_enum: "AuthAndCapture",
21
+ exp_month: month,
22
+ exp_year: year,
23
+ holder_name: name,
24
+ installment_count: installment_count || 1,
25
+ security_code: verification_value,
26
+ }
27
+
28
+ if ::ActiveMerchant::Billing::Base.test?
29
+ content.merge!(payment_method_code: 1)
30
+ end
31
+
32
+ {
33
+ amount_in_cents: amount,
34
+ currency_iso_enum: "BRL",
35
+ credit_card_transaction_collection: { credit_card_transaction: content }
36
+ }
37
+ end
38
+
39
+ class Response < Mundipagg::Response
40
+ def payload
41
+ body[:create_order_response][:create_order_result]
42
+ end
43
+
44
+ def error_item
45
+ {
46
+ description: payload[:acquirer_message].split("|").last,
47
+ error_code: payload[:acquirer_return_code]
48
+ }
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,10 @@
1
+ module Mundipagg
2
+ class Error < RuntimeError
3
+ attr_reader :code
4
+
5
+ def initialize(message, code)
6
+ @code = code
7
+ super(message)
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,35 @@
1
+ module Mundipagg
2
+ class Gateway < ActiveMerchant::Billing::Gateway
3
+ def initialize(options={})
4
+ requires! options, :merchant_key
5
+ super
6
+ end
7
+
8
+ def purchase(amount, object)
9
+ call amount, object, :create_order
10
+ end
11
+
12
+ def refund(amount, object)
13
+ call amount, object, :manage_order
14
+ end
15
+
16
+ private
17
+ def call(amount, object, method)
18
+ payload = apply_merchant_key object.payload(amount)
19
+
20
+ response = client.send(method, payload)
21
+ object.class::Response.new(response.body)
22
+ end
23
+
24
+ def apply_merchant_key(body)
25
+ payload = body.to_a
26
+ payload.insert(-2, [ :merchant_key, options[:merchant_key] ])
27
+
28
+ payload.each_with_object({}) { |(k, v), h| h[k] = v }
29
+ end
30
+
31
+ def client
32
+ @client ||= ::Mundipagg::Client.new
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,21 @@
1
+ require "active_support/core_ext/string/conversions"
2
+
3
+ module Mundipagg
4
+ class Key
5
+ def initialize(value, prefix = "mun")
6
+ @prefix, @value = prefix, value.to_s
7
+ end
8
+
9
+ def to_s
10
+ "#{@prefix}:#{camelize}"
11
+ end
12
+
13
+ def camelize
14
+ @value.camelize(format)
15
+ end
16
+
17
+ def format
18
+ @prefix == "tns" ? :lower : :upper
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,18 @@
1
+ require "mundipagg/key"
2
+
3
+ module Mundipagg
4
+ class Message
5
+ def initialize(hash)
6
+ @hash = hash
7
+ end
8
+
9
+ def translate
10
+ @hash.inject(Hash.new) do |hash, (key, value)|
11
+ key = Mundipagg::Key.new(key).to_s
12
+
13
+ hash[key] = value.is_a?(Hash) ? self.class.new(value).translate : value
14
+ hash
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,28 @@
1
+ module Mundipagg
2
+ class Refund < ActiveMerchant::Billing::Model
3
+ attr_accessor :transaction_key, :order_key
4
+
5
+ def payload(amount)
6
+ {
7
+ manage_credit_card_transaction_collection: {
8
+ manage_credit_card_transaction_request: {
9
+ amount_in_cents: amount,
10
+ transaction_key: transaction_key
11
+ }
12
+ },
13
+ manage_order_operation_enum: "Void",
14
+ order_key: order_key
15
+ }
16
+ end
17
+
18
+ class Response < Mundipagg::Response
19
+ def payload
20
+ body[:manage_order_response][:manage_order_result]
21
+ end
22
+
23
+ def error_item
24
+ payload[:error_report][:error_item_collection][:error_item]
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,27 @@
1
+ module Mundipagg
2
+ class Response
3
+ attr_reader :body
4
+
5
+ alias :payload :body
6
+
7
+ def initialize(body = {})
8
+ @body = body
9
+ end
10
+
11
+ def success?
12
+ payload[:success]
13
+ end
14
+
15
+ alias :valid? :success?
16
+
17
+ def error
18
+ ::Mundipagg::Error.new error_item[:description], error_item[:error_code]
19
+ rescue NoMethodError
20
+ nil
21
+ end
22
+
23
+ def error_item
24
+ raise "Not yet implemented"
25
+ end
26
+ end
27
+ end
@@ -1,3 +1,3 @@
1
1
  module Mundipagg
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/mundipagg.rb CHANGED
@@ -1,23 +1,14 @@
1
1
  require "active_support"
2
- require "active_model"
3
- require "mundipagg/gateway/client"
4
- require "mundipagg/buyer"
5
- require "mundipagg/order"
6
- require "mundipagg/transaction/boleto"
2
+ require "active_merchant"
3
+ require "active_merchant/billing/rails"
7
4
 
8
- module Mundipagg
9
- mattr_accessor :merchant_key, :bank_number, :bank_code, :days_to_expire, :env
10
- @@env = :test
11
- @@days_to_expire = 5
12
- @@bank_number = 341
13
-
14
- class << self
15
- def setup
16
- yield self
17
- end
5
+ require "mundipagg/error"
6
+ require "mundipagg/response"
7
+ require "mundipagg/client"
8
+ require "mundipagg/gateway"
9
+ require "mundipagg/credit_card"
10
+ require "mundipagg/boleto"
11
+ require "mundipagg/refund"
18
12
 
19
- def test?
20
- env == :test
21
- end
22
- end
13
+ module Mundipagg
23
14
  end
data/mundipagg.gemspec CHANGED
@@ -15,7 +15,10 @@ Gem::Specification.new do |s|
15
15
 
16
16
  s.add_development_dependency "rspec", "~> 3.1.0"
17
17
  s.add_development_dependency "simplecov", "~> 0.9.0"
18
+ s.add_development_dependency "vcr", "~> 2.9.3"
19
+ s.add_development_dependency "webmock"
18
20
  s.add_dependency "activemodel"
21
+ s.add_dependency "activemerchant"
19
22
  s.add_dependency "savon"
20
23
 
21
24
  s.files = `git ls-files`.split("\n")