Showing
7 changed files
with
75 additions
and
65 deletions
| 1 | #!/usr/bin/env ruby | 1 | #!/usr/bin/env ruby |
| 2 | 2 | ||
| 3 | -require "bundler/setup" | 3 | +require 'bundler/setup' |
| 4 | -require "var" | 4 | +require 'var' |
| 5 | 5 | ||
| 6 | # You can add fixtures and/or initialization code here to make experimenting | 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. | 7 | # with your gem easier. You can also use a different console, if you like. |
| ... | @@ -10,5 +10,5 @@ require "var" | ... | @@ -10,5 +10,5 @@ require "var" |
| 10 | # require "pry" | 10 | # require "pry" |
| 11 | # Pry.start | 11 | # Pry.start |
| 12 | 12 | ||
| 13 | -require "irb" | 13 | +require 'irb' |
| 14 | IRB.start | 14 | IRB.start | ... | ... |
| 1 | class AddVarStatusTo<%=@model_name.pluralize.camelize%> < ActiveRecord::Migration | 1 | class AddVarStatusTo<%=@model_name.pluralize.camelize%> < ActiveRecord::Migration |
| 2 | def change | 2 | def change |
| 3 | add_column :<%=@model_name.pluralize.underscore%>, :var_status, :string, default: 'pending' | 3 | add_column :<%=@model_name.pluralize.underscore%>, :var_status, :string, default: 'pending' |
| 4 | - add_column :<%=@model_name.pluralize.underscore%>, :barcode_url, :string | 4 | + add_column :<%=@model_name.pluralize.underscore%>, :var_barcode_url, :string |
| 5 | + add_column :<%=@model_name.pluralize.underscore%>, :var_barcode, :string | ||
| 6 | + add_column :<%=@model_name.pluralize.underscore%>, :var_payment_expires_at, :datetime | ||
| 7 | + add_column :<%=@model_name.pluralize.underscore%>, :var_id, :string | ||
| 8 | + add_column :<%=@model_name.pluralize.underscore%>, :var_service, :string | ||
| 5 | end | 9 | end |
| 6 | end | 10 | end | ... | ... |
| 1 | require 'rails/generators' | 1 | require 'rails/generators' |
| 2 | - | 2 | +# Generator for models |
| 3 | class VarModelGenerator < Rails::Generators::NamedBase | 3 | class VarModelGenerator < Rails::Generators::NamedBase |
| 4 | source_root File.expand_path('../templates', __FILE__) | 4 | source_root File.expand_path('../templates', __FILE__) |
| 5 | - desc "This generator generates layout file with navigation." | 5 | + desc 'This generator generates layout file with navigation.' |
| 6 | 6 | ||
| 7 | def generate_model | 7 | def generate_model |
| 8 | d = Time.now.strftime('%Y%m%d%H%M%S') | 8 | d = Time.now.strftime('%Y%m%d%H%M%S') |
| 9 | @model_name = name | 9 | @model_name = name |
| 10 | - template "var_model.rb", | 10 | + template 'var_model.rb', |
| 11 | "db/migrate/#{d}_add_var_status_to_#{name.pluralize.underscore}.rb" | 11 | "db/migrate/#{d}_add_var_status_to_#{name.pluralize.underscore}.rb" |
| 12 | end | 12 | end |
| 13 | end | 13 | end | ... | ... |
| 1 | -require "var/version" | 1 | +require 'var/version' |
| 2 | require 'conekta' | 2 | require 'conekta' |
| 3 | require 'paypal-sdk-rest' | 3 | require 'paypal-sdk-rest' |
| 4 | 4 | ||
| 5 | +# Main Module | ||
| 5 | module Var | 6 | module Var |
| 6 | # TODO: add paypal | 7 | # TODO: add paypal |
| 7 | # @@valid_services = [:conekta, :paypal] | 8 | # @@valid_services = [:conekta, :paypal] |
| ... | @@ -13,15 +14,15 @@ module Var | ... | @@ -13,15 +14,15 @@ module Var |
| 13 | 14 | ||
| 14 | def self.create_charge(service, object, options = {}) | 15 | def self.create_charge(service, object, options = {}) |
| 15 | return { error_message: 'Service is not supported' } unless VALID_SERVICES.include? service | 16 | return { error_message: 'Service is not supported' } unless VALID_SERVICES.include? service |
| 16 | - return { error_message: "#{object.class.to_s} doesn't support charges"} unless object.respond_to?(:charge_with) | 17 | + return { error_message: "#{object.class} doesn't support charges" } unless object.respond_to?(:charge_with) |
| 17 | charge = object.charge_with(service, options) | 18 | charge = object.charge_with(service, options) |
| 18 | - return charge | 19 | + charge |
| 19 | end | 20 | end |
| 20 | end | 21 | end |
| 21 | - | 22 | +# Module for models |
| 22 | module ActsAsChargeable | 23 | module ActsAsChargeable |
| 23 | extend ActiveSupport::Concern | 24 | extend ActiveSupport::Concern |
| 24 | - | 25 | + # Class Methods |
| 25 | module ClassMethods | 26 | module ClassMethods |
| 26 | def acts_as_chargeable(keys = {}) | 27 | def acts_as_chargeable(keys = {}) |
| 27 | include ChargeableInstanceMethods | 28 | include ChargeableInstanceMethods |
| ... | @@ -29,55 +30,62 @@ module ActsAsChargeable | ... | @@ -29,55 +30,62 @@ module ActsAsChargeable |
| 29 | self.sync_attributes = keys | 30 | self.sync_attributes = keys |
| 30 | end | 31 | end |
| 31 | end | 32 | end |
| 32 | - | 33 | + # Instance Methods |
| 33 | module ChargeableInstanceMethods | 34 | module ChargeableInstanceMethods |
| 34 | def charge_with(service, options) | 35 | def charge_with(service, options) |
| 35 | - if !instance_support?(service) | 36 | + unless instance_support?(service) |
| 36 | - error_message = "#{self.class.to_s} doesn't support" \ | 37 | + error_message = "#{self.class} doesn't support" \ |
| 37 | - " charges with #{service.to_s}" | 38 | + " charges with #{service}" |
| 38 | - return { error_message: error_message} | 39 | + return { error_message: error_message } |
| 39 | end | 40 | end |
| 40 | - self.send("charge_with_#{service.to_s}", options) | 41 | + send("charge_with_#{service}", options) |
| 41 | rescue Exception => exception | 42 | rescue Exception => exception |
| 42 | { error_message: exception.message } | 43 | { error_message: exception.message } |
| 43 | end | 44 | end |
| 44 | 45 | ||
| 45 | def charge_with_conekta(options) | 46 | def charge_with_conekta(options) |
| 46 | charge = conekta_charge(options) | 47 | charge = conekta_charge(options) |
| 47 | - object.update_columns(var_status: charge.status) | 48 | + update_columns(var_status: charge.status, var_id: charge.id, |
| 49 | + var_service: 'conekta') | ||
| 50 | + update_conekta_barcode(charge) if options[:conekta_type] == 'oxxo' | ||
| 48 | charge | 51 | charge |
| 49 | - rescue Conekta::ParameterValidationError, Conekta::ProcessingError, Conekta::Error => e | 52 | + rescue Conekta::ParameterValidationError, Conekta::ProcessingError, |
| 50 | - self.update_columns(var_status: 'failed') | 53 | + Conekta::Error => e |
| 54 | + update_columns(var_status: 'failed') | ||
| 51 | { error_message: e.message } | 55 | { error_message: e.message } |
| 52 | end | 56 | end |
| 53 | 57 | ||
| 54 | - def conekta_charge(conekta_type) | 58 | + def conekta_charge(options) |
| 55 | @charge ||= Conekta::Charge.create({ | 59 | @charge ||= Conekta::Charge.create({ |
| 56 | - description: self.sync(:conekta, 'description'), | 60 | + description: sync(:conekta, 'description'), |
| 57 | - amount: self.sync(:conekta, 'amount'), currency: "MXN", | 61 | + amount: sync(:conekta, 'amount'), currency: 'MXN', |
| 58 | - reference_id: self.sync(:conekta, 'reference_id'), | 62 | + reference_id: sync(:conekta, 'reference_id'), |
| 59 | details: { | 63 | details: { |
| 60 | - name: self.sync(:conekta, 'name'), | 64 | + name: sync(:conekta, 'name'), email: sync(:conekta, 'email'), |
| 61 | - email: self.sync(:conekta, 'email'), | ||
| 62 | line_items: [{ | 65 | line_items: [{ |
| 63 | - name: self.sync(:conekta, 'name'), | 66 | + description: sync(:conekta, 'description'), quantity: 1, |
| 64 | - description: self.sync(:conekta, 'description'), | 67 | + unit_price: sync(:conekta, 'amount'), name: sync(:conekta, 'name') |
| 65 | - unit_price: self.sync(:conekta, 'amount'), quantity: 1, | 68 | + }] } |
| 66 | - }] | ||
| 67 | - } | ||
| 68 | }.merge(conekta_type_of_charge(options))) | 69 | }.merge(conekta_type_of_charge(options))) |
| 69 | end | 70 | end |
| 70 | 71 | ||
| 71 | - def conekta_type_of_charge(conekta_type) | 72 | + def conekta_type_of_charge(options) |
| 72 | if options[:conekta_type] == 'card' | 73 | if options[:conekta_type] == 'card' |
| 73 | { card: options[:card_token] } | 74 | { card: options[:card_token] } |
| 74 | elsif options[:conekta_type] == 'oxxo' | 75 | elsif options[:conekta_type] == 'oxxo' |
| 75 | - { cash: { type: "oxxo", | 76 | + { cash: { type: 'oxxo', |
| 76 | - expires_at: (Time.zone.today + 3.days).strftime('%Y-%m-%d')} | 77 | + expires_at: (Time.zone.today + 3.days).strftime('%Y-%m-%d') } |
| 77 | } | 78 | } |
| 78 | end | 79 | end |
| 79 | end | 80 | end |
| 80 | 81 | ||
| 82 | + def update_conekta_barcode(charge) | ||
| 83 | + method = charge.payment_method | ||
| 84 | + update_columns(var_barcode: method.barcode, | ||
| 85 | + var_barcode_url: method.barcode_url, | ||
| 86 | + var_payment_expires_at: Time.at(method.expires_at)) | ||
| 87 | + end | ||
| 88 | + | ||
| 81 | # def charge_with_paypal(options) | 89 | # def charge_with_paypal(options) |
| 82 | # if(!options.include? :card) | 90 | # if(!options.include? :card) |
| 83 | # error_message = "Paypal needs a card sent as a third paramater" | 91 | # error_message = "Paypal needs a card sent as a third paramater" |
| ... | @@ -110,13 +118,9 @@ module ActsAsChargeable | ... | @@ -110,13 +118,9 @@ module ActsAsChargeable |
| 110 | # description: self.sync(:paypal, 'description') }]}) | 118 | # description: self.sync(:paypal, 'description') }]}) |
| 111 | # end | 119 | # end |
| 112 | 120 | ||
| 113 | - def find_charge(service) | 121 | + def find_charge |
| 114 | - if !instance_support?(service) | 122 | + return { error_message: 'Not charged yet' } unless var_service |
| 115 | - error_message = "#{self.class.to_s} doesn't support" \ | 123 | + send("find_#{var_service}_charge") |
| 116 | - " charges with #{service.to_s}" | ||
| 117 | - return { error_message: error_message} | ||
| 118 | - end | ||
| 119 | - self.send("find_#{service.to_s}_charge") | ||
| 120 | rescue Exception => exception | 124 | rescue Exception => exception |
| 121 | { error_message: exception.message } | 125 | { error_message: exception.message } |
| 122 | end | 126 | end |
| ... | @@ -127,18 +131,17 @@ module ActsAsChargeable | ... | @@ -127,18 +131,17 @@ module ActsAsChargeable |
| 127 | end | 131 | end |
| 128 | 132 | ||
| 129 | def find_conekta_charge | 133 | def find_conekta_charge |
| 130 | - ref_id = self.sync(:conekta, 'reference_id') | 134 | + Conekta::Charge.find(var_id) |
| 131 | - Conekta::Charge.where({'status.ne'=>'paid', 'reference_id'=> ref_id}) | ||
| 132 | end | 135 | end |
| 133 | 136 | ||
| 134 | def instance_support?(service) | 137 | def instance_support?(service) |
| 135 | - return self.sync_attributes.include?(service) | 138 | + sync_attributes.include?(service) |
| 136 | end | 139 | end |
| 137 | 140 | ||
| 138 | def sync(service, key) | 141 | def sync(service, key) |
| 139 | - service_attributes = self.send("#{service.to_s}_attributes") | 142 | + service_attributes = send("#{service}_attributes") |
| 140 | - return self.send(key) if !service_attributes.include? key.to_sym | 143 | + return send(key) unless service_attributes.include? key.to_sym |
| 141 | - self.send(service_attributes[key.to_sym]) | 144 | + send(service_attributes[key.to_sym]) |
| 142 | end | 145 | end |
| 143 | 146 | ||
| 144 | # def sync_attribute(key) | 147 | # def sync_attribute(key) |
| ... | @@ -147,13 +150,15 @@ module ActsAsChargeable | ... | @@ -147,13 +150,15 @@ module ActsAsChargeable |
| 147 | # end | 150 | # end |
| 148 | 151 | ||
| 149 | def conekta_attributes | 152 | def conekta_attributes |
| 150 | - self.sync_attributes[:conekta] || {} | 153 | + sync_attributes[:conekta] || {} |
| 151 | end | 154 | end |
| 152 | 155 | ||
| 153 | def paypal_attributes | 156 | def paypal_attributes |
| 154 | - self.sync_attributes[:paypal] || {} | 157 | + sync_attributes[:paypal] || {} |
| 155 | end | 158 | end |
| 156 | end | 159 | end |
| 157 | end | 160 | end |
| 158 | 161 | ||
| 159 | -ActiveRecord::Base.send(:include, ActsAsChargeable) if defined? ActiveRecord::Base | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
| 162 | +if defined? ActiveRecord::Base | ||
| 163 | + ActiveRecord::Base.send(:include, ActsAsChargeable) | ||
| 164 | +end | ... | ... |
| ... | @@ -4,30 +4,30 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) | ... | @@ -4,30 +4,30 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) |
| 4 | require 'var/version' | 4 | require 'var/version' |
| 5 | 5 | ||
| 6 | Gem::Specification.new do |spec| | 6 | Gem::Specification.new do |spec| |
| 7 | - spec.name = "var" | 7 | + spec.name = 'var' |
| 8 | spec.version = Var::VERSION | 8 | spec.version = Var::VERSION |
| 9 | - spec.authors = ["abrahamrq"] | 9 | + spec.authors = ['abrahamrq'] |
| 10 | - spec.email = ["abraham.rq03@gmail.com"] | 10 | + spec.email = ['abraham.rq03@gmail.com'] |
| 11 | 11 | ||
| 12 | - spec.summary = %q{summary of var} | 12 | + spec.summary = 'summary of var' |
| 13 | - spec.description = %q{description of var} | 13 | + spec.description = 'description of var' |
| 14 | - spec.homepage = "https://github.com/abrahamrq" | 14 | + spec.homepage = 'https://github.com/abrahamrq' |
| 15 | 15 | ||
| 16 | # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or | 16 | # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or |
| 17 | # delete this section to allow pushing this gem to any host. | 17 | # delete this section to allow pushing this gem to any host. |
| 18 | if spec.respond_to?(:metadata) | 18 | if spec.respond_to?(:metadata) |
| 19 | spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'" | 19 | spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'" |
| 20 | else | 20 | else |
| 21 | - raise "RubyGems 2.0 or newer is required to protect against public gem pushes." | 21 | + fail 'RubyGems 2.0+ is required to protect against public gem pushes.' |
| 22 | end | 22 | end |
| 23 | 23 | ||
| 24 | spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) } | 24 | spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) } |
| 25 | - spec.bindir = "exe" | 25 | + spec.bindir = 'exe' |
| 26 | spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } | 26 | spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } |
| 27 | - spec.require_paths = ["lib"] | 27 | + spec.require_paths = ['lib'] |
| 28 | 28 | ||
| 29 | - spec.add_dependency 'conekta', "~> 0.5" | 29 | + spec.add_dependency 'conekta', '~> 0.5' |
| 30 | spec.add_dependency 'paypal-sdk-rest' | 30 | spec.add_dependency 'paypal-sdk-rest' |
| 31 | - spec.add_development_dependency "bundler", "~> 1.10.a" | 31 | + spec.add_development_dependency 'bundler', '~> 1.10.a' |
| 32 | - spec.add_development_dependency "rake", "~> 10.0" | 32 | + spec.add_development_dependency 'rake', '~> 10.0' |
| 33 | end | 33 | end | ... | ... |
-
Please register or login to post a comment