punto_pagos_rails 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (92) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/Rakefile +36 -0
  4. data/app/assets/javascripts/punto_pagos_rails/application.js +13 -0
  5. data/app/assets/stylesheets/punto_pagos_rails/application.css +49 -0
  6. data/app/controllers/punto_pagos_rails/application_controller.rb +4 -0
  7. data/app/controllers/punto_pagos_rails/transactions_controller.rb +50 -0
  8. data/app/helpers/punto_pagos_rails/application_helper.rb +4 -0
  9. data/app/models/punto_pagos_rails/transaction.rb +35 -0
  10. data/app/views/layouts/punto_pagos_rails/application.html.erb +14 -0
  11. data/config/locales/en.yml +12 -0
  12. data/config/locales/es.yml +12 -0
  13. data/config/routes.rb +6 -0
  14. data/db/migrate/20140928003539_create_punto_pagos_rails_transactions.rb +13 -0
  15. data/lib/generators/punto_pagos_rails/install/USAGE +8 -0
  16. data/lib/generators/punto_pagos_rails/install/install_generator.rb +47 -0
  17. data/lib/generators/punto_pagos_rails/install/templates/error.html.erb +6 -0
  18. data/lib/generators/punto_pagos_rails/install/templates/punto_pagos_rails.rb +3 -0
  19. data/lib/generators/punto_pagos_rails/install/templates/puntopagos.yml +13 -0
  20. data/lib/generators/punto_pagos_rails/install/templates/success.html.erb +6 -0
  21. data/lib/punto_pagos_rails.rb +18 -0
  22. data/lib/punto_pagos_rails/engine.rb +10 -0
  23. data/lib/punto_pagos_rails/resource_extension.rb +20 -0
  24. data/lib/punto_pagos_rails/transaction_service.rb +87 -0
  25. data/lib/punto_pagos_rails/version.rb +3 -0
  26. data/lib/tasks/punto_pagos_rails_tasks.rake +4 -0
  27. data/spec/dummy/README.rdoc +28 -0
  28. data/spec/dummy/Rakefile +6 -0
  29. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  30. data/spec/dummy/app/assets/stylesheets/application.css +15 -0
  31. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  32. data/spec/dummy/app/controllers/home_controller.rb +5 -0
  33. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  34. data/spec/dummy/app/models/ticket.rb +4 -0
  35. data/spec/dummy/app/views/home/index.html.erb +3 -0
  36. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  37. data/spec/dummy/app/views/punto_pagos_rails/transactions/error.html.erb +6 -0
  38. data/spec/dummy/app/views/punto_pagos_rails/transactions/success.html.erb +6 -0
  39. data/spec/dummy/bin/bundle +3 -0
  40. data/spec/dummy/bin/rails +4 -0
  41. data/spec/dummy/bin/rake +4 -0
  42. data/spec/dummy/config.ru +4 -0
  43. data/spec/dummy/config/application.rb +23 -0
  44. data/spec/dummy/config/boot.rb +5 -0
  45. data/spec/dummy/config/database.yml +25 -0
  46. data/spec/dummy/config/environment.rb +5 -0
  47. data/spec/dummy/config/environments/development.rb +37 -0
  48. data/spec/dummy/config/environments/production.rb +78 -0
  49. data/spec/dummy/config/environments/test.rb +39 -0
  50. data/spec/dummy/config/initializers/assets.rb +8 -0
  51. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  52. data/spec/dummy/config/initializers/cookies_serializer.rb +3 -0
  53. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  54. data/spec/dummy/config/initializers/inflections.rb +16 -0
  55. data/spec/dummy/config/initializers/mime_types.rb +4 -0
  56. data/spec/dummy/config/initializers/punto_pagos_rails.rb +3 -0
  57. data/spec/dummy/config/initializers/session_store.rb +3 -0
  58. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  59. data/spec/dummy/config/locales/en.yml +23 -0
  60. data/spec/dummy/config/puntopagos.yml +12 -0
  61. data/spec/dummy/config/routes.rb +6 -0
  62. data/spec/dummy/config/secrets.yml +22 -0
  63. data/spec/dummy/db/development.sqlite3 +0 -0
  64. data/spec/dummy/db/migrate/20141026172010_create_tickets.rb +9 -0
  65. data/spec/dummy/db/migrate/20141026172011_create_punto_pagos_rails_transactions.punto_pagos_rails.rb +14 -0
  66. data/spec/dummy/db/schema.rb +32 -0
  67. data/spec/dummy/db/test.sqlite3 +0 -0
  68. data/spec/dummy/log/development.log +5364 -0
  69. data/spec/dummy/log/test.log +3753 -0
  70. data/spec/dummy/public/404.html +67 -0
  71. data/spec/dummy/public/422.html +67 -0
  72. data/spec/dummy/public/500.html +66 -0
  73. data/spec/dummy/public/favicon.ico +0 -0
  74. data/spec/dummy/spec/factories/tickets.rb +5 -0
  75. data/spec/dummy/spec/lib/punto_pagos_rails/transaction_service_spec.rb +140 -0
  76. data/spec/dummy/spec/models/punto_pagos_rails/transaction_spec.rb +7 -0
  77. data/spec/dummy/spec/models/ticket_spec.rb +7 -0
  78. data/spec/dummy/tmp/cache/assets/development/sprockets/13fe41fee1fe35b49d145bcc06610705 +0 -0
  79. data/spec/dummy/tmp/cache/assets/development/sprockets/2f5173deea6c795b8fdde723bb4b63af +0 -0
  80. data/spec/dummy/tmp/cache/assets/development/sprockets/357970feca3ac29060c1e3861e2c0953 +0 -0
  81. data/spec/dummy/tmp/cache/assets/development/sprockets/913223216b0b1897b0ec8d3b22ba52c7 +0 -0
  82. data/spec/dummy/tmp/cache/assets/development/sprockets/9e9c010aa4c17a17d0ba371813cd1804 +0 -0
  83. data/spec/dummy/tmp/cache/assets/development/sprockets/b6844dbdd15de17485877b0ac65e829e +0 -0
  84. data/spec/dummy/tmp/cache/assets/development/sprockets/c9ab7add939c37b818c61fa4f1ada3ab +0 -0
  85. data/spec/dummy/tmp/cache/assets/development/sprockets/cc9bafdd79b6777f18f5680c8d5e5aee +0 -0
  86. data/spec/dummy/tmp/cache/assets/development/sprockets/cffd775d018f68ce5dba1ee0d951a994 +0 -0
  87. data/spec/dummy/tmp/cache/assets/development/sprockets/d771ace226fc8215a3572e0aa35bb0d6 +0 -0
  88. data/spec/dummy/tmp/cache/assets/development/sprockets/f0bf33738d12f42f7b9f302975b8a9d7 +0 -0
  89. data/spec/dummy/tmp/cache/assets/development/sprockets/f7cbd26ba1d28d48de824f0e94586655 +0 -0
  90. data/spec/rails_helper.rb +22 -0
  91. data/spec/spec_helper.rb +78 -0
  92. metadata +354 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4898b2ab540d763a03f9ed76e7a10c9304886b97
4
+ data.tar.gz: 7fd638ca89dd79aabe8b8ad013219acf8bb823f7
5
+ SHA512:
6
+ metadata.gz: 5d5f6c9dbf1fc877886537511850b2a1dfa37a09d1843b45b42acc844444ca330071a8a6f10853f8551faf01f3238eda17ba4915162670d00e0bd8964399efdb
7
+ data.tar.gz: 356d30fdbc7b5a58ca2fff825e6ebfc2c9a89a79be0bd87b1582b8b83a2405d28fecd0b1757dd97563aa934b2b56228c77fc622a1099ba1fce1ce8aa6d827cf4
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2014 Leandro Segovia
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,36 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'PuntoPagosRails'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+ begin
21
+ require 'rspec/core/rake_task'
22
+ RSpec::Core::RakeTask.new(:spec)
23
+ rescue LoadError
24
+ end
25
+
26
+ require 'thor'
27
+
28
+ task default: :spec
29
+
30
+ task :run_dummy_app do
31
+ Dir.chdir("spec/dummy") do
32
+ exec("bundle exec rails s -p 3000")
33
+ end
34
+ end
35
+
36
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,49 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any styles
10
+ * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
11
+ * file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
16
+
17
+ .puntopagos {
18
+ width: 100%;
19
+ margin-top: 10%;
20
+ font-family: Arial, Helvetica, sans-serif;
21
+ text-align: center;
22
+ }
23
+
24
+ .puntopagos h2 {
25
+ font-size: 30px;
26
+ }
27
+
28
+ .puntopagos p {
29
+ font-size: 16px;
30
+ }
31
+
32
+ .puntopagos .view {
33
+ width: 500px;
34
+ margin: auto;
35
+ padding: 20px 20px 30px 20px;
36
+ }
37
+
38
+ .puntopagos .view.success {
39
+ color: #565656;
40
+ background-color: #e4dddd;
41
+ border: solid 1px #ccc5c5;
42
+ }
43
+
44
+ .puntopagos .view.error {
45
+ color: #b94a48;
46
+ background-color: #f2dede;
47
+ border: solid 1px #eed3d7;
48
+ }
49
+
@@ -0,0 +1,4 @@
1
+ module PuntoPagosRails
2
+ class ApplicationController < ActionController::Base
3
+ end
4
+ end
@@ -0,0 +1,50 @@
1
+ module PuntoPagosRails
2
+ class TransactionsController < ApplicationController
3
+ def create
4
+ srv = TransactionService.new(resource_id)
5
+
6
+ if srv.create
7
+ redirect_to srv.process_url
8
+ else
9
+ @resource = resource_by_id
10
+ render_payment_error_view srv.error
11
+ end
12
+ end
13
+
14
+ def notification
15
+ response = TransactionService.notificate(params, request.headers)
16
+ render json: response
17
+ end
18
+
19
+ def success
20
+ @resource = resource_by_token
21
+ end
22
+
23
+ def error
24
+ @resource = resource_by_token
25
+ translated_error = I18n.t("punto_pagos_rails.errors.invalid_puntopagos_payment")
26
+ render_payment_error_view translated_error
27
+ end
28
+
29
+ private
30
+
31
+ def render_payment_error_view(error_message)
32
+ render 'error', locals: { error_message: error_message }
33
+ end
34
+
35
+ def resource_id
36
+ @resource_id ||= begin
37
+ params.require(:resource_id)
38
+ params[:resource_id]
39
+ end
40
+ end
41
+
42
+ def resource_by_token
43
+ Transaction.find_by(token: params[:token]).try(:resource)
44
+ end
45
+
46
+ def resource_by_id
47
+ PuntoPagosRails.resource_class.find_by(id: params[:resource_id])
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,4 @@
1
+ module PuntoPagosRails
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,35 @@
1
+ require 'aasm'
2
+
3
+ module PuntoPagosRails
4
+ class Transaction < ActiveRecord::Base
5
+ include AASM
6
+
7
+ belongs_to :resource, class_name: PuntoPagosRails.resource_class_name
8
+
9
+ delegate :amount, :to => :resource
10
+
11
+ aasm :column => :state do
12
+ state :pending, :initial => true
13
+ state :completed
14
+ state :rejected
15
+
16
+ event :complete do
17
+ transitions :from => :pending, :to => :completed
18
+ end
19
+
20
+ event :reject do
21
+ transitions :from => :pending, :to => :rejected
22
+ end
23
+ end
24
+
25
+ def reject_with(error)
26
+ self.error = error
27
+ self.reject
28
+ end
29
+
30
+ def amount_to_s
31
+ "%0.2f" % amount.to_i
32
+ end
33
+
34
+ end
35
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>PuntoPagosRails</title>
5
+ <%= stylesheet_link_tag "punto_pagos_rails/application", media: "all" %>
6
+ <%= javascript_include_tag "punto_pagos_rails/application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,12 @@
1
+ en:
2
+ punto_pagos_rails:
3
+ success:
4
+ title: Transaction successfully completed
5
+ detail: "The %{resource} #%{id} for the amount of $%{amount} was made"
6
+ error:
7
+ detail: "The payment for %{resource} #%{id} for the amount of $%{amount} was declined"
8
+ errors:
9
+ invalid_puntopagos_response: Invalid PuntoPagos response
10
+ invalid_returned_puntopagos_token: Invalid returned PuntoPagos token
11
+ repeated_token_given: PuntoPagos token already taken
12
+ invalid_puntopagos_payment: The payment was rejected by PuntoPagos
@@ -0,0 +1,12 @@
1
+ es:
2
+ punto_pagos_rails:
3
+ success:
4
+ title: Transacción completada con éxito
5
+ detail: "El pago del %{resource} <b>#%{id}</b> por el valor de <b>$%{amount}</b> fue realizado"
6
+ error:
7
+ detail: "El pago del %{resource} <b>#%{id}</b> por el valor de <b>$%{amount}</b> no se realizó"
8
+ errors:
9
+ invalid_puntopagos_response: No se pudo establecer comunicación con PuntoPagos
10
+ invalid_returned_puntopagos_token: Token inválido de PuntoPagos
11
+ repeated_token_given: El Token que identifica una transacción con PuntoPagos ya ha sido utilizado anteriormente.
12
+ invalid_puntopagos_payment: El pago fue rechazado por PuntoPagos
data/config/routes.rb ADDED
@@ -0,0 +1,6 @@
1
+ PuntoPagosRails::Engine.routes.draw do
2
+ post "transactions/notification", to: 'transactions#notification'
3
+ get "transactions/error/:token", to: 'transactions#error', as: :transaction_error
4
+ get "transactions/success/:token", to: 'transactions#success', as: :transaction_success
5
+ post "transactions/create", to: 'transactions#create', as: :transaction_create
6
+ end
@@ -0,0 +1,13 @@
1
+ class CreatePuntoPagosRailsTransactions < ActiveRecord::Migration
2
+ def change
3
+ create_table :punto_pagos_rails_transactions do |t|
4
+ t.integer :resource_id
5
+ t.string :token
6
+ t.integer :amount
7
+ t.string :error
8
+ t.string :state
9
+
10
+ t.timestamps
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Explain the generator
3
+
4
+ Example:
5
+ rails generate punto_pagos_rails:install ResourceModel
6
+
7
+ This will create:
8
+ what/will/it/create
@@ -0,0 +1,47 @@
1
+ class PuntoPagosRails::InstallGenerator < Rails::Generators::NamedBase
2
+ source_root File.expand_path('../templates', __FILE__)
3
+
4
+ def create_resource_model
5
+ Rails.application.eager_load!
6
+ models = ActiveRecord::Base.descendants.map(&:to_s)
7
+ models.include?(name.classify) ? add_amount_attribute_to_resources : create_resource
8
+ end
9
+
10
+ def extend_resource_abilities
11
+ line = "class #{name.classify} < ActiveRecord::Base"
12
+ gsub_file "app/models/#{name}.rb", /(#{Regexp.escape(line)})/mi do |match|
13
+ "#{match}\n include PuntoPagosRails::ResourceExtension\n"
14
+ end
15
+ end
16
+
17
+ def copy_migrations
18
+ rake "railties:install:migrations"
19
+ end
20
+
21
+ def create_initializer
22
+ template "puntopagos.yml", "config/puntopagos.yml"
23
+ template "punto_pagos_rails.rb", "config/initializers/punto_pagos_rails.rb"
24
+ end
25
+
26
+ def mount_routes
27
+ line = "Rails.application.routes.draw do"
28
+ gsub_file "config/routes.rb", /(#{Regexp.escape(line)})/mi do |match|
29
+ "#{match}\n mount PuntoPagosRails::Engine => '/'\n"
30
+ end
31
+ end
32
+
33
+ def copy_views
34
+ copy_file "success.html.erb", "app/views/punto_pagos_rails/transactions/success.html.erb"
35
+ copy_file "error.html.erb", "app/views/punto_pagos_rails/transactions/error.html.erb"
36
+ end
37
+
38
+ private
39
+
40
+ def add_amount_attribute_to_resources
41
+ generate "migration add_amount_to_#{name.tableize} amount:integer"
42
+ end
43
+
44
+ def create_resource
45
+ generate "model #{name.classify} amount:integer --no-fixture"
46
+ end
47
+ end
@@ -0,0 +1,6 @@
1
+ <div class="puntopagos">
2
+ <div class="view error">
3
+ <h2><%= error_message %></h2>
4
+ <p><%= t('punto_pagos_rails.error.detail', id: @resource.id, amount: @resource.amount, resource: @resource.class.name.downcase).html_safe %></p>
5
+ </div>
6
+ </div>
@@ -0,0 +1,3 @@
1
+ PuntoPagosRails.setup do |config|
2
+ config.resource_class_name = '<%= name.classify %>'
3
+ end
@@ -0,0 +1,13 @@
1
+ development:
2
+ environment: "sandbox"
3
+ puntopagos_key: "YOUR-API-KEY"
4
+ puntopagos_secret: "YOUR-APP-SECRET"
5
+ test:
6
+ environment: "sandbox"
7
+ puntopagos_key: "YOUR-API-KEY"
8
+ puntopagos_secret: "YOUR-APP-SECRET"
9
+ production:
10
+ environment: "production"
11
+ puntopagos_key: "YOUR-API-KEY"
12
+ puntopagos_secret: "YOUR-APP-SECRET"
13
+
@@ -0,0 +1,6 @@
1
+ <div class="puntopagos">
2
+ <div class="view success">
3
+ <h2><%= t('punto_pagos_rails.success.title') %></h2>
4
+ <p><%= t('punto_pagos_rails.success.detail', id: @resource.id, amount: @resource.amount, resource: @resource.class.name.downcase).html_safe %></p>
5
+ </div>
6
+ </div>
@@ -0,0 +1,18 @@
1
+ require "punto_pagos_rails/engine"
2
+ require "punto_pagos_rails/resource_extension"
3
+ require "punto_pagos_rails/transaction_service"
4
+
5
+ module PuntoPagosRails
6
+ extend self
7
+
8
+ attr_accessor :resource_class_name
9
+
10
+ def resource_class
11
+ resource_class_name.constantize
12
+ end
13
+
14
+ def setup
15
+ yield self
16
+ require "puntopagos"
17
+ end
18
+ end
@@ -0,0 +1,10 @@
1
+ module PuntoPagosRails
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace PuntoPagosRails
4
+
5
+ config.generators do |g|
6
+ g.test_framework :rspec
7
+ g.fixture_replacement :factory_girl, :dir => 'spec/factories'
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,20 @@
1
+ require 'active_support/concern'
2
+
3
+ module PuntoPagosRails
4
+ module ResourceExtension
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ has_many :transactions, class_name: 'PuntoPagosRails::Transaction', foreign_key: :resource_id
9
+
10
+ def paid?
11
+ return false unless self.transactions.any?
12
+ self.transactions.last.completed?
13
+ end
14
+ end
15
+
16
+ module ClassMethods
17
+ # TODO
18
+ end
19
+ end
20
+ end