var.rb
2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
require "var/version"
require 'conekta'
module Var
@@valid_services = [:conekta]
def self.valid_services
@@valid_services
end
def self.create_charge(service, object, options = {})
if(!@@valid_services.include?(service))
return { error_message: 'Service is not supported' }
end
if(!object.respond_to?(:charge_with))
return { error_message: "#{object.class.to_s} doesn't support charges"}
end
object.charge_with(:conekta, options)
end
end
module ActsAsChargeable
extend ActiveSupport::Concern
module ClassMethods
def acts_as_chargeable(keys = {})
include ChargeableInstanceMethods
cattr_accessor :sync_attributes
self.sync_attributes = keys
end
end
module ChargeableInstanceMethods
def charge_with(service, options)
begin
self.send("charge_with_#{service.to_s}", options)
rescue => exception
return { error_message: exception.message }
end
end
def charge_with_conekta(options)
if(!options.include? :card_token)
error_message = "Conekta needs a card token sent as a third paramater"
return { error_message: error_message}
end
@charge ||= Conekta::Charge.create({
description: self.sync_attribute('description'),
amount: self.sync_attribute('amount'),
currency: "MXN",
reference_id: self.sync_attribute('reference_id'),
card: options[:card_token],
details: {
name: self.sync_attribute('details_name'),
email: self.sync_attribute('details_email'),
line_items: [{
name: self.sync_attribute('details_name'),
description: self.sync_attribute('description'),
unit_price: self.sync_attribute('amount'),
quantity: 1,
}]
}
})
end
def find_charge(service)
begin
if(!Var.valid_services.include?(service))
return { error_message: 'Service is not supported' }
end
self.send("find_#{service.to_s}_charge")
rescue => exception
return { error_message: exception.message }
end
end
def find_conekta_charge
ref_id = self.sync_attribute('reference_id')
Conekta::Charge.where({'status.ne'=>'paid', 'reference_id'=> ref_id})
end
def sync_attribute(key)
return self.send(key) unless self.sync_attributes.include? key.to_sym
self.send(sync_attributes[key.to_sym])
end
end
end
ActiveRecord::Base.send(:include, ActsAsChargeable) if defined? ActiveRecord::Base