var.rb
4.75 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
require "var/version"
require 'conekta'
require 'paypal-sdk-rest'
module Var
# TODO: add paypal
# @@valid_services = [:conekta, :paypal]
@@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(service, 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)
if !instance_support?(service)
error_message = "#{self.class.to_s} doesn't support" \
" charges with #{service.to_s}"
return { error_message: error_message}
end
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
begin
@charge ||= Conekta::Charge.create({
description: self.sync(:conekta, 'description'),
amount: self.sync(:conekta, 'amount'),
currency: "MXN",
reference_id: self.sync(:conekta, 'reference_id'),
card: options[:card_token],
details: {
name: self.sync(:conekta, 'name'),
email: self.sync(:conekta, 'email'),
line_items: [{
name: self.sync(:conekta, 'name'),
description: self.sync(:conekta, 'description'),
unit_price: self.sync(:conekta, 'amount'),
quantity: 1,
}]
}
})
rescue => exception
return { error_message: exception.message }
end
end
def charge_with_paypal(options)
if(!options.include? :card)
error_message = "Paypal needs a card sent as a third paramater"
return { error_message: error_message}
end
@payment = PayPal::SDK::REST::Payment.new({
intent: "sale",
payer: {
payer_info: {
email: self.sync(:paypal, 'email')},
payment_method: "credit_card",
funding_instruments: [{
credit_card: {
type: options[:card][:type],
number: options[:card][:number],
expire_month: options[:card][:expire_month],
expire_year: options[:card][:expire_year],
cvv2: options[:card][:cvv2]}}]},
transactions: [{
item_list: {
items: [{
name: self.sync(:paypal, 'name'),
sku: self.sync(:paypal, 'sku'),
price: self.sync(:paypal, 'price'),
currency: "MXN",
quantity: 1 }]},
amount: {
total: self.sync(:paypal, 'price'),
currency: "MXN" },
description: self.sync(:paypal, 'description') }]})
end
def find_charge(service)
begin
if !instance_support?(service)
error_message = "#{self.class.to_s} doesn't support" \
" charges with #{service.to_s}"
return { error_message: error_message}
end
self.send("find_#{service.to_s}_charge")
rescue => exception
return { error_message: exception.message }
end
end
def charged?(service)
charge = find_charge(service)
charge.any? && !charge.include?(:error_message)
end
def find_conekta_charge
ref_id = self.sync(:conekta, 'reference_id')
Conekta::Charge.where({'status.ne'=>'paid', 'reference_id'=> ref_id})
end
def instance_support?(service)
return self.sync_attributes.include?(service)
end
def sync(service, key)
service_attributes = self.send("#{service.to_s}_attributes")
if !service_attributes.include? key.to_sym
return self.send(key)
end
self.send(service_attributes[key.to_sym])
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
def conekta_attributes
self.sync_attributes[:conekta] || {}
end
def paypal_attributes
self.sync_attributes[:paypal] || {}
end
end
end
ActiveRecord::Base.send(:include, ActsAsChargeable) if defined? ActiveRecord::Base