Showing
2 changed files
with
26 additions
and
2 deletions
| ... | @@ -5,19 +5,42 @@ require 'paypal-sdk-rest' | ... | @@ -5,19 +5,42 @@ require 'paypal-sdk-rest' |
| 5 | # Main Module | 5 | # Main Module |
| 6 | module Var | 6 | module Var |
| 7 | # TODO: add paypal | 7 | # TODO: add paypal |
| 8 | - # @@valid_services = [:conekta, :paypal] | ||
| 9 | VALID_SERVICES = [:conekta] | 8 | VALID_SERVICES = [:conekta] |
| 9 | + @@var_classes = [] | ||
| 10 | 10 | ||
| 11 | def self.valid_services | 11 | def self.valid_services |
| 12 | VALID_SERVICES | 12 | VALID_SERVICES |
| 13 | end | 13 | end |
| 14 | 14 | ||
| 15 | + def self.var_classes | ||
| 16 | + @@var_classes | ||
| 17 | + end | ||
| 18 | + | ||
| 19 | + def self.add_var_class(class_name) | ||
| 20 | + @@var_classes << class_name unless @@var_classes.include? class_name | ||
| 21 | + end | ||
| 22 | + | ||
| 15 | def self.create_charge(service, object, options = {}) | 23 | def self.create_charge(service, object, options = {}) |
| 16 | return { error_message: 'Service is not supported' } unless VALID_SERVICES.include? service | 24 | return { error_message: 'Service is not supported' } unless VALID_SERVICES.include? service |
| 17 | return { error_message: "#{object.class} doesn't support charges" } unless object.respond_to?(:charge_with) | 25 | return { error_message: "#{object.class} doesn't support charges" } unless object.respond_to?(:charge_with) |
| 18 | charge = object.charge_with(service, options) | 26 | charge = object.charge_with(service, options) |
| 19 | charge | 27 | charge |
| 20 | end | 28 | end |
| 29 | + | ||
| 30 | + def self.conekta_webhook(params) | ||
| 31 | + payment = params[:data][:object] | ||
| 32 | + object = find_charge payment[:id] | ||
| 33 | + object.update_columns(var_status: payment[:status]) | ||
| 34 | + true | ||
| 35 | + rescue Exception => exception | ||
| 36 | + false | ||
| 37 | + end | ||
| 38 | + | ||
| 39 | + def find_charge(id) | ||
| 40 | + @@var_classes.map do |class_name| | ||
| 41 | + class_name.where(var_id: id) | ||
| 42 | + end.flatten.first | ||
| 43 | + end | ||
| 21 | end | 44 | end |
| 22 | # Module for models | 45 | # Module for models |
| 23 | module ActsAsChargeable | 46 | module ActsAsChargeable |
| ... | @@ -28,6 +51,7 @@ module ActsAsChargeable | ... | @@ -28,6 +51,7 @@ module ActsAsChargeable |
| 28 | include ChargeableInstanceMethods | 51 | include ChargeableInstanceMethods |
| 29 | cattr_accessor :sync_attributes | 52 | cattr_accessor :sync_attributes |
| 30 | self.sync_attributes = keys | 53 | self.sync_attributes = keys |
| 54 | + Var.add_var_class(self) | ||
| 31 | end | 55 | end |
| 32 | end | 56 | end |
| 33 | # Instance Methods | 57 | # Instance Methods | ... | ... |
-
Please register or login to post a comment