Showing
1 changed file
with
77 additions
and
15 deletions
| 1 | require "var/version" | 1 | require "var/version" |
| 2 | require 'conekta' | 2 | require 'conekta' |
| 3 | +require 'paypal-sdk-rest' | ||
| 3 | 4 | ||
| 4 | module Var | 5 | module Var |
| 6 | + # TODO: add paypal | ||
| 7 | + # @@valid_services = [:conekta, :paypal] | ||
| 5 | @@valid_services = [:conekta] | 8 | @@valid_services = [:conekta] |
| 6 | 9 | ||
| 7 | def self.valid_services | 10 | def self.valid_services |
| ... | @@ -15,7 +18,7 @@ module Var | ... | @@ -15,7 +18,7 @@ module Var |
| 15 | if(!object.respond_to?(:charge_with)) | 18 | if(!object.respond_to?(:charge_with)) |
| 16 | return { error_message: "#{object.class.to_s} doesn't support charges"} | 19 | return { error_message: "#{object.class.to_s} doesn't support charges"} |
| 17 | end | 20 | end |
| 18 | - object.charge_with(:conekta, options) | 21 | + object.charge_with(service, options) |
| 19 | end | 22 | end |
| 20 | end | 23 | end |
| 21 | 24 | ||
| ... | @@ -32,6 +35,11 @@ module ActsAsChargeable | ... | @@ -32,6 +35,11 @@ module ActsAsChargeable |
| 32 | 35 | ||
| 33 | module ChargeableInstanceMethods | 36 | module ChargeableInstanceMethods |
| 34 | def charge_with(service, options) | 37 | def charge_with(service, options) |
| 38 | + if !instance_support?(service) | ||
| 39 | + error_message = "#{self.class.to_s} doesn't support" \ | ||
| 40 | + " charges with #{service.to_s}" | ||
| 41 | + return { error_message: error_message} | ||
| 42 | + end | ||
| 35 | begin | 43 | begin |
| 36 | self.send("charge_with_#{service.to_s}", options) | 44 | self.send("charge_with_#{service.to_s}", options) |
| 37 | rescue => exception | 45 | rescue => exception |
| ... | @@ -45,28 +53,62 @@ module ActsAsChargeable | ... | @@ -45,28 +53,62 @@ module ActsAsChargeable |
| 45 | return { error_message: error_message} | 53 | return { error_message: error_message} |
| 46 | end | 54 | end |
| 47 | @charge ||= Conekta::Charge.create({ | 55 | @charge ||= Conekta::Charge.create({ |
| 48 | - description: self.sync_attribute('description'), | 56 | + description: self.sync(:conekta, 'description'), |
| 49 | - amount: self.sync_attribute('amount'), | 57 | + amount: self.sync(:conekta, 'amount'), |
| 50 | currency: "MXN", | 58 | currency: "MXN", |
| 51 | - reference_id: self.sync_attribute('reference_id'), | 59 | + reference_id: self.sync(:conekta, 'reference_id'), |
| 52 | card: options[:card_token], | 60 | card: options[:card_token], |
| 53 | details: { | 61 | details: { |
| 54 | - name: self.sync_attribute('details_name'), | 62 | + name: self.sync(:conekta, 'name'), |
| 55 | - email: self.sync_attribute('details_email'), | 63 | + email: self.sync(:conekta, 'email'), |
| 56 | line_items: [{ | 64 | line_items: [{ |
| 57 | - name: self.sync_attribute('details_name'), | 65 | + name: self.sync(:conekta, 'name'), |
| 58 | - description: self.sync_attribute('description'), | 66 | + description: self.sync(:conekta, 'description'), |
| 59 | - unit_price: self.sync_attribute('amount'), | 67 | + unit_price: self.sync(:conekta, 'amount'), |
| 60 | quantity: 1, | 68 | quantity: 1, |
| 61 | }] | 69 | }] |
| 62 | } | 70 | } |
| 63 | }) | 71 | }) |
| 64 | end | 72 | end |
| 65 | 73 | ||
| 74 | + def charge_with_paypal(options) | ||
| 75 | + if(!options.include? :card) | ||
| 76 | + error_message = "Paypal needs a card sent as a third paramater" | ||
| 77 | + return { error_message: error_message} | ||
| 78 | + end | ||
| 79 | + @payment = PayPal::SDK::REST::Payment.new({ | ||
| 80 | + intent: "sale", | ||
| 81 | + payer: { | ||
| 82 | + payer_info: { | ||
| 83 | + email: self.sync(:paypal, 'email')}, | ||
| 84 | + payment_method: "credit_card", | ||
| 85 | + funding_instruments: [{ | ||
| 86 | + credit_card: { | ||
| 87 | + type: options[:card][:type], | ||
| 88 | + number: options[:card][:number], | ||
| 89 | + expire_month: options[:card][:expire_month], | ||
| 90 | + expire_year: options[:card][:expire_year], | ||
| 91 | + cvv2: options[:card][:cvv2]}}]}, | ||
| 92 | + transactions: [{ | ||
| 93 | + item_list: { | ||
| 94 | + items: [{ | ||
| 95 | + name: self.sync(:paypal, 'name'), | ||
| 96 | + sku: self.sync(:paypal, 'sku'), | ||
| 97 | + price: self.sync(:paypal, 'price'), | ||
| 98 | + currency: "MXN", | ||
| 99 | + quantity: 1 }]}, | ||
| 100 | + amount: { | ||
| 101 | + total: self.sync(:paypal, 'price'), | ||
| 102 | + currency: "MXN" }, | ||
| 103 | + description: self.sync(:paypal, 'description') }]}) | ||
| 104 | + end | ||
| 105 | + | ||
| 66 | def find_charge(service) | 106 | def find_charge(service) |
| 67 | begin | 107 | begin |
| 68 | - if(!Var.valid_services.include?(service)) | 108 | + if !instance_support?(service) |
| 69 | - return { error_message: 'Service is not supported' } | 109 | + error_message = "#{self.class.to_s} doesn't support" \ |
| 110 | + " charges with #{service.to_s}" | ||
| 111 | + return { error_message: error_message} | ||
| 70 | end | 112 | end |
| 71 | self.send("find_#{service.to_s}_charge") | 113 | self.send("find_#{service.to_s}_charge") |
| 72 | rescue => exception | 114 | rescue => exception |
| ... | @@ -75,13 +117,33 @@ module ActsAsChargeable | ... | @@ -75,13 +117,33 @@ module ActsAsChargeable |
| 75 | end | 117 | end |
| 76 | 118 | ||
| 77 | def find_conekta_charge | 119 | def find_conekta_charge |
| 78 | - ref_id = self.sync_attribute('reference_id') | 120 | + ref_id = self.sync(:conekta, 'reference_id') |
| 79 | Conekta::Charge.where({'status.ne'=>'paid', 'reference_id'=> ref_id}) | 121 | Conekta::Charge.where({'status.ne'=>'paid', 'reference_id'=> ref_id}) |
| 80 | end | 122 | end |
| 81 | 123 | ||
| 82 | - def sync_attribute(key) | 124 | + def instance_support?(service) |
| 83 | - return self.send(key) unless self.sync_attributes.include? key.to_sym | 125 | + return self.sync_attributes.include?(service) |
| 84 | - self.send(sync_attributes[key.to_sym]) | 126 | + end |
| 127 | + | ||
| 128 | + def sync(service, key) | ||
| 129 | + service_attributes = self.send("#{service.to_s}_attributes") | ||
| 130 | + if !service_attributes.include? key.to_sym | ||
| 131 | + return self.send(key) | ||
| 132 | + end | ||
| 133 | + self.send(service_attributes[key.to_sym]) | ||
| 134 | + end | ||
| 135 | + | ||
| 136 | + # def sync_attribute(key) | ||
| 137 | + # return self.send(key) unless self.sync_attributes.include? key.to_sym | ||
| 138 | + # self.send(sync_attributes[key.to_sym]) | ||
| 139 | + # end | ||
| 140 | + | ||
| 141 | + def conekta_attributes | ||
| 142 | + self.sync_attributes[:conekta] || {} | ||
| 143 | + end | ||
| 144 | + | ||
| 145 | + def paypal_attributes | ||
| 146 | + self.sync_attributes[:paypal] || {} | ||
| 85 | end | 147 | end |
| 86 | end | 148 | end |
| 87 | end | 149 | end | ... | ... |
-
Please register or login to post a comment