app/models/order.rb
def process_payment(request)
    if payment_method == 'PAYPAL'

      gateway = ActiveMerchant::Billing::PaypalExpressGateway.new(
          :login => Cache.setting(domain_id, 'eCommerce', 'PayPal API Username'),
          :password => Cache.setting(domain_id, 'eCommerce', 'PayPal API Password'),
          :signature => Cache.setting(domain_id, 'eCommerce', 'PayPal Signature'),
      )

      total_as_cents, purchase_params = get_purchase_params(self, request)
      response = gateway.purchase(total_as_cents, purchase_params)

      raise "PayPal processing failed: #{response.message}" unless response.success?

      self.update_columns(status: 'submitted', submitted: Time.now, received: Time.now)

      Payment.create(payable_id: id, payable_type: :order, amount: total, memo: 'PayPal Payment',
                     user_id: user_id, transaction_id: response.authorization)

      # add order history row
      OrderHistory.create order_id: id, user_id: user_id, amount: total,
                          event_type: 'paypal_payment', system_name: 'PayPal', identifier: response.authorization,
                          comment: notify_email

    elsif payment_method == "CREDIT_CARD"

      customer_name = billing_name
      customer_name = user.name unless user_id.nil?

      purchase_options = {
          :ip => request.remote_ip,
          :order_id => id,
          :customer => customer_name,
          :billing_address => {
              :name => billing_name,
              :address1 => billing_street1,
              :city => billing_street2,
              :state => billing_state,
              :zip => billing_zip
          }}

      # active_gateway is defined in billing/models/concerns
      response = active_gateway(domain_id).purchase(total_cents, credit_card, purchase_options)

      # credit cart authorization failed?
      raise response.message unless response.success?

      # save CC?
      if items.any? { |x| x.autoship_months > 0 }
        unless PaymentMethod.exists?(user_id: user_id, card_display: "x-#{cc_number.last(4)}")
          PaymentMethod.create(
              user_id: user_id,
              default: true,
              cardholder_name: billing_name,
              number: cc_number,
              expiration_month: cc_expiration_month,
              expiration_year: cc_expiration_year,
              billing_street1: billing_street1,
              billing_street2: billing_street2,
              billing_city: billing_city,
              billing_state: billing_state,
              billing_zip: billing_zip,
              billing_country: billing_country,
              status: "A",
              last_transaction_date: DateTime.now,
              last_transaction_result: response.message)
        end
      end

      self.update_columns(status: 'submitted',
                          submitted: Time.now,
                          received: Time.now,
                          cc_code: nil,
                          cc_number: credit_card.display_number)

      Payment.create(payable_id: id, payable_type: :order, amount: total, memo: cc_number,
                     user_id: user_id, transaction_id: response.authorization)

      # add order history row
      OrderHistory.create order_id: id, user_id: user_id, amount: total,
                          event_type: 'cc_authorization', system_name: :active_merchant, identifier: response.authorization,
                          comment: "Successfully charged #{cc_number}"

    else
      # NO_BILLING
      self.update_columns(status: 'submitted', submitted: Time.now)
    end

  end
# process payment, this is called when order is being submitted on website
process payment, this is called when order is being submitted on website
process payment this is called when order is being submitted on website
def process_payment(request)
    if payment_method == 'PAYPAL'
      gateway = ActiveMerchant::Billing::PaypalExpressGateway.new(
          :login => Cache.setting(domain_id, 'eCommerce', 'PayPal API Username'),
          :password => Cache.setting(domain_id, 'eCommerce', 'PayPal API Password'),
          :signature => Cache.setting(domain_id, 'eCommerce', 'PayPal Signature'),
      )
      total_as_cents, purchase_params = get_purchase_params(self, request)
      response = gateway.purchase(total_as_cents, purchase_params)
      raise "PayPal processing failed: #{response.message}" unless response.success?
      self.update_columns(status: 'submitted', submitted: Time.now, received: Time.now)
      Payment.create(payable_id: id, payable_type: :order, amount: total, memo: 'PayPal Payment',
                     user_id: user_id, transaction_id: response.authorization)
      OrderHistory.create order_id: id, user_id: user_id, amount: total,
                          event_type: 'paypal_payment', system_name: 'PayPal', identifier: response.authorization,
                          comment: notify_email
    elsif payment_method == "CREDIT_CARD"
      customer_name = billing_name
      customer_name = user.name unless user_id.nil?
      purchase_options = {
          :ip => request.remote_ip,
          :order_id => id,
          :customer => customer_name,
          :billing_address => {
              :name => billing_name,
              :address1 => billing_street1,
              :city => billing_street2,
              :state => billing_state,
              :zip => billing_zip
          }}
      response = active_gateway(domain_id).purchase(total_cents, credit_card, purchase_options)
      raise response.message unless response.success?
      if items.any? { |x| x.autoship_months > 0 }
        unless PaymentMethod.exists?(user_id: user_id, card_display: "x-#{cc_number.last(4)}")
          PaymentMethod.create(
              user_id: user_id,
              default: true,
              cardholder_name: billing_name,
              number: cc_number,
              expiration_month: cc_expiration_month,
              expiration_year: cc_expiration_year,
              billing_street1: billing_street1,
              billing_street2: billing_street2,
              billing_city: billing_city,
              billing_state: billing_state,
              billing_zip: billing_zip,
              billing_country: billing_country,
              status: "A",
              last_transaction_date: DateTime.now,
              last_transaction_result: response.message)
        end
      end
      self.update_columns(status: 'submitted',
                          submitted: Time.now,
                          received: Time.now,
                          cc_code: nil,
                          cc_number: credit_card.display_number)
      Payment.create(payable_id: id, payable_type: :order, amount: total, memo: cc_number,
                     user_id: user_id, transaction_id: response.authorization)
      OrderHistory.create order_id: id, user_id: user_id, amount: total,
                          event_type: 'cc_authorization', system_name: :active_merchant, identifier: response.authorization,
                          comment: "Successfully charged #{cc_number}"
    else
      self.update_columns(status: 'submitted', submitted: Time.now)
    end
  end
def process_payment ( request ) if payment_method == 'PAYPAL' gateway = ActiveMerchant :: Billing :: PaypalExpressGateway . new ( :login => Cache . setting ( domain_id , 'eCommerce' , 'PayPal API Username' ) , :password => Cache . setting ( domain_id , 'eCommerce' , 'PayPal API Password' ) , :signature => Cache . setting ( domain_id , 'eCommerce' , 'PayPal Signature' ) , ) total_as_cents , purchase_params = get_purchase_params ( self , request ) response = gateway . purchase ( total_as_cents , purchase_params ) raise "PayPal processing failed: #{response.message}" unless response . success? self . update_columns ( status : 'submitted' , submitted : Time . now , received : Time . now ) Payment . create ( payable_id : id , payable_type : :order , amount : total , memo : 'PayPal Payment' , user_id : user_id , transaction_id : response . authorization ) OrderHistory . create order_id : id , user_id : user_id , amount : total , event_type : 'paypal_payment' , system_name : 'PayPal' , identifier : response . authorization , comment : notify_email elsif payment_method == "CREDIT_CARD" customer_name = billing_name customer_name = user . name unless user_id . nil? purchase_options = { :ip => request . remote_ip , :order_id => id , :customer => customer_name , :billing_address => { :name => billing_name , :address1 => billing_street1 , :city => billing_street2 , :state => billing_state , :zip => billing_zip } } response = active_gateway ( domain_id ) . purchase ( total_cents , credit_card , purchase_options ) raise response . message unless response . success? if items . any? { | x | x . autoship_months > 0 } unless PaymentMethod . exists? ( user_id : user_id , card_display : "x-#{cc_number.last(4)}" ) PaymentMethod . create ( user_id : user_id , default : true , cardholder_name : billing_name , number : cc_number , expiration_month : cc_expiration_month , expiration_year : cc_expiration_year , billing_street1 : billing_street1 , billing_street2 : billing_street2 , billing_city : billing_city , billing_state : billing_state , billing_zip : billing_zip , billing_country : billing_country , status : "A" , last_transaction_date : DateTime . now , last_transaction_result : response . message ) end end self . update_columns ( status : 'submitted' , submitted : Time . now , received : Time . now , cc_code : nil , cc_number : credit_card . display_number ) Payment . create ( payable_id : id , payable_type : :order , amount : total , memo : cc_number , user_id : user_id , transaction_id : response . authorization ) OrderHistory . create order_id : id , user_id : user_id , amount : total , event_type : 'cc_authorization' , system_name : :active_merchant , identifier : response . authorization , comment : "Successfully charged #{cc_number}" else self . update_columns ( status : 'submitted' , submitted : Time . now ) end end
process payment, this is called when order is being submitted on website
process payment this is called when order is being submitted on website
# add order history row # active_gateway is defined in billing/models/concerns # credit cart authorization failed? # save CC? # add order history row # NO_BILLING
request