daily pastebin goal
11%
SHARE
TWEET

Untitled

a guest May 26th, 2018 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class PaymentCard < ActiveRecord::Base
  2.   class CIMPaymentProfileCreationError < StandardError; end
  3.   include SharedGateway
  4.   include ActiveMerchant::RequiresParameters
  5.  
  6.   belongs_to :user
  7.  
  8.   attr_accessor :full_number, :cvs
  9.  
  10.   VALID_MONTHS = (1..12).collect{|m| [m, m]}
  11.   VALID_YEARS = (Time.now.year..Time.now.year + 10).collect{|y| [y, y]}
  12.   VALID_CARD_TYPES = %w(visa mastercard discover).collect{|c| [c.capitalize, c]}
  13.  
  14.  
  15.   validates_presence_of :first_name, :last_name
  16.   validates_presence_of :full_number, :on => :create
  17.  
  18.   after_save :deactivate_other_payment_cards
  19.   after_create :create_payment_profile_id
  20.   after_create :create_customer_profile_transaction
  21.  
  22.   def full_number_before_typecast
  23.     self.full_number
  24.   end
  25.  
  26.   def self.create_or_activate_payment_card(user, params)
  27.       # If we have an existing card, activate it and return it
  28.       logger.debug {"message here #{:number}"}
  29.       existing_card = PaymentCard.find(:first, :conditions => {:user_id => user.id,
  30.         :month => params[:month], :year => params[:year], :number => params[:full_number].to_s.last(4)
  31.         })
  32.       existing_card.activate! and return existing_card if existing_card
  33.     else
  34.       # Otherwise, create one and generate the CIM payment profile id for it
  35.       new_card = user.payment_cards.build(params)
  36.       begin
  37.         new_card.save
  38.       rescue CIMPaymentProfileCreationError => e
  39.         Rails.logger.error e
  40.         new_card.errors.add_to_base("Credit card is invalid.")
  41.       end
  42.       new_card
  43.   end
  44.  
  45.   def activate!
  46.     self.is_active = true
  47.     self.save
  48.   end
  49.  
  50.   private
  51.   def deactivate_other_payment_cards
  52.     if self.is_active == true
  53.       self.user.payment_cards.reject{|c| c.id == self.id}.each do |card|
  54.         card.update_attribute(:is_active, false)
  55.       end
  56.     end
  57.   end
  58.  
  59.   def create_payment_profile_id
  60.     # More magical dwarven knowledge here
  61.    
  62.     card = CreditCard.new(:year => self.year, :month => self.month, :number => self.full_number)
  63.     profile = {
  64.         :customer_type => 'individual', # Optional
  65.         :payment => { :credit_card => card }
  66.       }
  67.       response = gateway.create_customer_payment_profile(:customer_profile_id => self.user.customer_profile_id, :payment_profile => profile)
  68.       self.customer_payment_profile_id = response.params['customer_payment_profile_id']
  69.       raise CIMPaymentProfileCreationError, "could not create a customer payment profile id for user #{self.inspect}" unless self.customer_payment_profile_id
  70.   end
  71.  
  72.   def create_customer_profile_transaction
  73.     creditcard = ActiveMerchant::Billing::CreditCard.new({
  74.       :type => self.type,
  75.       :number => self.full_number,
  76.       :month => self.month,
  77.       :year => self._year,
  78.       :first_name => self.first_name,
  79.       :last_name => self.last_name
  80.       })
  81.       if creditcard.valid?
  82.          
  83.           response = gateway.purchase(amount, creditcard, options)
  84.           if response.success?
  85.             self.status = 'processed'
  86.           else
  87.             self.status = 'failed'
  88.             self.error_message = response.message
  89.           end
  90.         else
  91.           self.status = 'failed'
  92.           self.error_message = 'Invalid credit card'
  93.         end
  94.       end
  95.     end
  96. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top