akatus 0.1.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -16
  3. data/.rspec +2 -0
  4. data/LICENSE +1 -1
  5. data/Rakefile +6 -1
  6. data/akatus.gemspec +18 -11
  7. data/config/i18n.rb +3 -0
  8. data/lib/akatus.rb +34 -4
  9. data/lib/akatus/address.rb +11 -0
  10. data/lib/akatus/configuration.rb +43 -0
  11. data/lib/akatus/errors.rb +4 -0
  12. data/lib/akatus/formatters.rb +22 -0
  13. data/lib/akatus/installment.rb +6 -0
  14. data/lib/akatus/installment_options.rb +24 -0
  15. data/lib/akatus/item.rb +24 -0
  16. data/lib/akatus/payer.rb +35 -0
  17. data/lib/akatus/payment.rb +61 -0
  18. data/lib/akatus/payment_option.rb +6 -0
  19. data/lib/akatus/payment_types.rb +37 -0
  20. data/lib/akatus/phone.rb +6 -0
  21. data/lib/akatus/receiver.rb +6 -0
  22. data/lib/akatus/service.rb +34 -0
  23. data/lib/akatus/services/installments.rb +54 -0
  24. data/lib/akatus/services/payment_options.rb +95 -0
  25. data/lib/akatus/services/transaction.rb +50 -0
  26. data/lib/akatus/split_fee.rb +6 -0
  27. data/lib/akatus/transferrable.rb +58 -0
  28. data/lib/akatus/version.rb +1 -1
  29. data/locales/pt-BR.yml +69 -0
  30. data/spec/address_spec.rb +38 -0
  31. data/spec/factories/address_factories.rb +18 -0
  32. data/spec/factories/item_factories.rb +35 -0
  33. data/spec/factories/payer_factories.rb +8 -0
  34. data/spec/factories/phone_factories.rb +6 -0
  35. data/spec/item_spec.rb +42 -0
  36. data/spec/payer_spec.rb +31 -0
  37. data/spec/phone_spec.rb +23 -0
  38. data/spec/services/installments_spec.rb +36 -0
  39. data/spec/services/payment_options_spec.rb +22 -0
  40. data/spec/services/transaction_spec.rb +80 -0
  41. data/spec/spec_helper.rb +31 -0
  42. data/spec/support/configuration.rb +7 -0
  43. data/spec/support/shared_examples/transferrable_example.rb +22 -0
  44. metadata +142 -12
  45. data/LICENSE.txt +0 -22
  46. data/lib/akatus/configuracao.rb +0 -40
  47. data/lib/akatus/formatadores.rb +0 -7
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 514abcc9277c34932a4bf2e95a887b0797599388
4
- data.tar.gz: 73fac7d238d4fa69bf3d9f9a729c74d8925e3172
3
+ metadata.gz: 3e6394b4c41ed3be9c9d2e2c255926b7e94e7947
4
+ data.tar.gz: d083c49f1117259999bbfaec106da9441c53e7a0
5
5
  SHA512:
6
- metadata.gz: bef2bca5f2186a9ed7bd7004ee5311acf98f687858f9a977fed62e6a254c2abe5d7a46354a1b885eabb774950c057977a8540aaf95708b46aa4b5d652a7566d2
7
- data.tar.gz: 33caa2a12d3e88f49f4532becdc212ebd8850354c4b3d837e33c7603c70861bf87ffe8acfe222ee1ddda6eb3e5e32134752cc59e24e0320210d05fcbbd49d200
6
+ metadata.gz: ca9f28c3bbfb32b77598d945bdfbd64cf18a4f6a49eb25740cf9e89e40ad95eb47f80eda86fa4188f5f7a621b40bbd09e0a82b407d0336fcaba92e0e01f6698c
7
+ data.tar.gz: 08dec081d6135e838f59568517ae3147824d3750b8e33b2db4fbdcddacf0e8c88c644b1abfe1146b4c280cb2b752b63fd1adb87f2b66c165326dd3ee35fccc62
data/.gitignore CHANGED
@@ -1,17 +1,2 @@
1
- *.gem
2
- *.rbc
3
- .bundle
4
- .config
5
- .yardoc
1
+ .DS_Store
6
2
  Gemfile.lock
7
- InstalledFiles
8
- _yardoc
9
- coverage
10
- doc/
11
- lib/bundler/man
12
- pkg
13
- rdoc
14
- spec/reports
15
- test/tmp
16
- test/version_tmp
17
- tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ # Check "rspec --help" for more info on the options.
2
+ --color --format d --require spec_helper
data/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2013 Kauplus
3
+ Copyright (c) 2014 Kauplus
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy of
6
6
  this software and associated documentation files (the "Software"), to deal in
data/Rakefile CHANGED
@@ -1 +1,6 @@
1
- require "bundler/gem_tasks"
1
+ require 'bundler/gem_tasks'
2
+
3
+ require 'rspec/core/rake_task'
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/akatus.gemspec CHANGED
@@ -1,24 +1,31 @@
1
- # coding: utf-8
1
+ # encoding: utf-8
2
2
  lib = File.expand_path('../lib', __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require 'akatus/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
- spec.name = "akatus"
7
+ spec.name = 'akatus'
8
8
  spec.version = Akatus::VERSION
9
- spec.authors = ["Kauplus"]
10
- spec.email = ["tech@kauplus.com.br"]
11
- spec.description = %q{A gem foi criada para suportar todas as integrações de pagamentos da Akatus.}
12
- spec.summary = %q{Gem de integração com o meio de pagamentos Akatus}
13
- spec.homepage = ""
14
- spec.license = "MIT"
9
+ spec.author = 'Kauplus'
10
+ spec.email = 'tech@kauplus.com.br'
11
+ spec.description = 'Built to support all types of checkout integration between Akatus and your application'
12
+ spec.summary = 'Ruby gem for the Akatus payment system'
13
+ spec.homepage = 'http://github.com/kauplus/akatus'
14
+ spec.license = 'MIT'
15
15
 
16
16
  spec.files = `git ls-files`.split($/)
17
17
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
- spec.require_paths = ["lib"]
19
+ spec.require_paths = ['lib']
20
20
 
21
21
  spec.add_dependency 'activesupport'
22
- spec.add_development_dependency "bundler", "~> 1.3"
23
- spec.add_development_dependency "rake"
22
+ spec.add_dependency 'i18n'
23
+ spec.add_dependency 'json'
24
+ spec.add_dependency 'rest-client'
25
+
26
+ spec.add_development_dependency 'bundler', '~> 1.3'
27
+ spec.add_development_dependency 'debugger'
28
+ spec.add_development_dependency 'factory_girl', '~> 4.3'
29
+ spec.add_development_dependency 'rake'
30
+ spec.add_development_dependency 'rspec', '~> 2.14'
24
31
  end
data/config/i18n.rb ADDED
@@ -0,0 +1,3 @@
1
+ # Tell I18n where to find translation files.
2
+ locales = File.expand_path(File.join(File.dirname(__FILE__), '..', 'locales', 'pt-BR.yml'))
3
+ I18n.load_path << locales
data/lib/akatus.rb CHANGED
@@ -1,6 +1,36 @@
1
- require "akatus/configuracao"
2
- require "akatus/formatadores"
3
- require "akatus/version"
1
+ # External libraries
2
+ require 'active_support/inflector'
3
+ require 'active_support/number_helper'
4
+ require 'bigdecimal'
5
+ require 'i18n'
6
+ require 'json'
7
+ require 'rest-client'
8
+
9
+ # Configuration and modules
10
+ require 'akatus/configuration'
11
+ require 'akatus/errors'
12
+ require 'akatus/formatters'
13
+ require 'akatus/transferrable'
14
+ require 'akatus/version'
15
+
16
+ # Business classes
17
+ require 'akatus/address'
18
+ require 'akatus/installment'
19
+ require 'akatus/installment_options'
20
+ require 'akatus/item'
21
+ require 'akatus/payer'
22
+ require 'akatus/payment'
23
+ require 'akatus/payment_option'
24
+ require 'akatus/payment_types'
25
+ require 'akatus/phone'
26
+ require 'akatus/receiver'
27
+ require 'akatus/service'
28
+ require 'akatus/services/installments'
29
+ require 'akatus/services/payment_options'
30
+ require 'akatus/services/transaction'
31
+ require 'akatus/split_fee'
32
+
33
+ require_relative '../config/i18n'
4
34
 
5
35
  module Akatus
6
- end
36
+ end
@@ -0,0 +1,11 @@
1
+ module Akatus
2
+
3
+ class Address
4
+ include Transferrable
5
+ transferrable_attrs :type, :street, :number, :neighborhood,
6
+ :additional_details, :city, :state, :country, :postal_code
7
+
8
+ attr_accessor :reference
9
+ end
10
+
11
+ end
@@ -0,0 +1,43 @@
1
+ require 'singleton'
2
+
3
+ module Akatus
4
+
5
+ def self.config
6
+ Configuration.instance
7
+ end
8
+
9
+ class Configuration
10
+ include Singleton
11
+
12
+ attr_accessor :api_key, :email, :env, :sandbox
13
+
14
+ def initialize
15
+
16
+ @env = environment
17
+ conf = File.exists?(config_file_path) ? YAML.load_file(config_file_path)[@env] : {}
18
+
19
+ @sandbox = conf['sandbox'] == true
20
+ @api_key = conf['api_key']
21
+ @email = conf['email']
22
+
23
+ end
24
+
25
+ def config_file_path
26
+ "#{Dir.pwd}/config/akatus.yml"
27
+ end
28
+
29
+ def environment
30
+ ENV['AKATUS_ENV'] || 'development'
31
+ end
32
+
33
+ def base_url
34
+ sandbox ? 'https://sandbox.akatus.com/' : 'https://www.akatus.com/'
35
+ end
36
+
37
+ def api_url
38
+ base_url + "api/v1/"
39
+ end
40
+
41
+ end
42
+
43
+ end
@@ -0,0 +1,4 @@
1
+ module Akatus
2
+ class UnprocessableEntityError < StandardError
3
+ end
4
+ end
@@ -0,0 +1,22 @@
1
+ module Akatus
2
+
3
+ NUMERIC_FIELDS =
4
+ [
5
+ :price, :weight, :shipping_cost, :discount, :amount
6
+ ]
7
+
8
+ INTEGER_FIELDS =
9
+ [
10
+ :quantity
11
+ ]
12
+
13
+ STRING_FIELDS =
14
+ [
15
+ :reference
16
+ ]
17
+
18
+ def self.format_number(val)
19
+ ActiveSupport::NumberHelper.number_to_rounded(val, :locale => :en, :precision => 2)
20
+ end
21
+
22
+ end
@@ -0,0 +1,6 @@
1
+ module Akatus
2
+ class Installment
3
+ include Transferrable
4
+ attr_accessor :quantity, :unitary_amount, :total_amount
5
+ end
6
+ end
@@ -0,0 +1,24 @@
1
+ module Akatus
2
+ class InstallmentOptions
3
+ include Transferrable
4
+ attr_accessor :description, :installments, :taken_installments
5
+
6
+ #
7
+ # Build an empty ("dummy") object.
8
+ #
9
+ def self.blank(payment)
10
+
11
+ # The only "option" is to pay the full amount in one installment.
12
+ installment_options = [
13
+ Installment.new({
14
+ :quantity => 1,
15
+ :unitary_amount => payment.total_amount,
16
+ :total_amount => payment.total_amount
17
+ })
18
+ ]
19
+
20
+ new({ :taken_installments => 0, :installments => installment_options })
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,24 @@
1
+ module Akatus
2
+ class Item
3
+ include Transferrable
4
+ transferrable_attrs :reference, :description, :quantity, :price, :weight,
5
+ :shipping_cost, :discount, :split_fee
6
+
7
+ def initialize(opts = {})
8
+ opts = {
9
+ :shipping_cost => 0,
10
+ :discount => 0,
11
+ :weight => 0,
12
+ :quantity => 1
13
+ }.merge(opts)
14
+ super(opts)
15
+ end
16
+
17
+ def total_amount
18
+ quantity * BigDecimal.new(price.to_s) +
19
+ BigDecimal.new(shipping_cost.to_s) -
20
+ BigDecimal.new(discount.to_s)
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,35 @@
1
+ module Akatus
2
+ class Payer
3
+ include Transferrable
4
+ transferrable_attrs :name, :email
5
+
6
+ attr_accessor :addresses, :phones
7
+
8
+ def address=(val)
9
+ addresses ? addresses.unshift(val) : (@addresses = [val])
10
+ end
11
+
12
+ def address
13
+ addresses[0] if addresses
14
+ end
15
+
16
+ def phone=(val)
17
+ phones ? phones.unshift(val) : (@phones = [val])
18
+ end
19
+
20
+ def phone
21
+ phones[0] if phones
22
+ end
23
+
24
+ def to_payload(include_root = true)
25
+ payload = super(true)
26
+
27
+ # Tweak Akatus' weird array format.
28
+ payload['pagador']['enderecos'] = { 'endereco' => addresses.map { |a| a.to_payload(false) } }
29
+ payload['pagador']['telefones'] = { 'telefone' => phones.map { |p| p.to_payload(false) } }
30
+
31
+ include_root ? payload : payload.values.first
32
+ end
33
+
34
+ end
35
+ end
@@ -0,0 +1,61 @@
1
+ module Akatus
2
+ class Payment
3
+ include Transferrable
4
+
5
+ attr_reader :receiver
6
+
7
+ attr_accessor :reference, :payer, :items, :payment_method,
8
+ :weight, :shipping_cost, :discount, :currency,
9
+ :id, :status, :transaction_id, :url, :error, :total_amount
10
+
11
+ def initialize(opts = {})
12
+ options = {
13
+ :items => [],
14
+ :discount => 0,
15
+ :shipping_cost => 0,
16
+ :weight => 0
17
+ }.merge(opts)
18
+ super(options)
19
+
20
+ @receiver = Receiver.new({ :email => Akatus.config.email, :api_key => Akatus.config.api_key })
21
+ end
22
+
23
+ def to_payload
24
+ hsh = {
25
+ 'carrinho' => {
26
+ 'produtos' => { 'produto' => items.map { |i| i.to_payload(false) } },
27
+ 'transacao' => {
28
+ 'peso' => Akatus.format_number(total_weight),
29
+ 'frete' => Akatus.format_number(total_shipping_cost),
30
+ 'desconto' => Akatus.format_number(total_discount),
31
+ 'referencia' => reference,
32
+ 'moeda' => 'BRL'
33
+ }.merge(payment_method.to_payload),
34
+ 'recebedor' => receiver.to_payload(false),
35
+ 'pagador' => payer.to_payload(false)
36
+ }
37
+ }
38
+ end
39
+
40
+ def total_weight
41
+ weight + items.inject(0) { |sum, item| sum + item.weight }
42
+ end
43
+
44
+ def total_shipping_cost
45
+ shipping_cost + items.inject(0) { |sum, item| sum + item.shipping_cost }
46
+ end
47
+
48
+ def total_discount
49
+ discount + items.inject(0) { |sum, item| sum + item.discount }
50
+ end
51
+
52
+ def total_items_amount
53
+ items.inject(0) { |sum, item| sum + item.total_amount }
54
+ end
55
+
56
+ def total_amount
57
+ total_items_amount - total_discount + total_shipping_cost
58
+ end
59
+
60
+ end
61
+ end
@@ -0,0 +1,6 @@
1
+ module Akatus
2
+ class PaymentOption
3
+ include Transferrable
4
+ attr_accessor :code, :description, :installments
5
+ end
6
+ end
@@ -0,0 +1,37 @@
1
+ module Akatus
2
+
3
+ class PaymentType
4
+ include Transferrable
5
+ attr_accessor :type
6
+ end
7
+
8
+ class CreditCard < PaymentType
9
+ transferrable_attrs :brand, :number, :security_code, :installments, :expiration
10
+
11
+ attr_accessor :holder_name, :holder_cpf, :holder_phone
12
+
13
+ def to_payload
14
+ payload = super
15
+ payload['cartao_de_credito']['portador'] = {
16
+ :nome => holder_name,
17
+ :cpf => holder_cpf,
18
+ :telefone => holder_phone
19
+ }
20
+ payload['cartao_de_credito']
21
+ end
22
+ end
23
+
24
+ class ElectronicFundsTransfer < PaymentType
25
+ transferrable_attrs :brand
26
+ def to_payload
27
+ { :meio_de_pagamento => brand }
28
+ end
29
+ end
30
+
31
+ class BoletoBancario < PaymentType
32
+ attr_accessor :brand
33
+ def to_payload
34
+ { :meio_de_pagamento => 'boleto' }
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,6 @@
1
+ module Akatus
2
+ class Phone
3
+ include Transferrable
4
+ transferrable_attrs :type, :number
5
+ end
6
+ end