Class: FlexCommerce::PaypalExpress::Setup

Inherits:
Object
  • Object
show all
Includes:
Api
Defined in:
lib/paypal_express/setup.rb

Overview

This is the main class, which talks to ActiveMerchant gem to initiate a transaction using Paypal

Constant Summary

Constants included from Api

Api::USER_ERRORS

Instance Method Summary collapse

Constructor Details

#initialize(cart:, gateway_class: ::ActiveMerchant::Billing::PaypalExpressGateway, success_url:, cancel_url:, ip_address:, allow_shipping_change: true, callback_url:, shipping_method_model: FlexCommerce::ShippingMethod, use_mobile_payments: false, description: nil) ⇒ Setup

@note: For ‘::ActiveMerchant::Billing::PaypalExpressGateway` to work rails-site should include active merchant gem. Ideally this gem should be included in the gemspec. But as we are using custom gem, which is not published to ruby gems, there is no way of including it within this gem dependency

Parameters:

  • payment_provider_setup (FlexCommerce::PaymentProviderSetup)
  • cart (FlexCommerce::Cart)
  • [gateway_class (Paypal Gateway)

    ::ActiveMerchant::Billing::PaypalExpressGateway]

  • success_url (URL)
    • Generally Paypal confirmation page

  • cancel_url (URL)
    • Generally new transaction page

  • ip_address (IP)
    • User ip address

  • [allow_shipping_change (boolean)

    true] - true: display shipping options, false: dont display shipping options

  • callback_url (URL)
    • Generally cart show page

  • shipping_method_model (FlexCommerce::ShippingMethod) (defaults to: FlexCommerce::ShippingMethod)

    FlexCommerce::ShippingMethod

  • [use_mobile_payments (boolean)

    false]

  • [description (String)

    nil]



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/paypal_express/setup.rb', line 31

def initialize(cart:, gateway_class: ::ActiveMerchant::Billing::PaypalExpressGateway, success_url:, cancel_url:, ip_address:, allow_shipping_change: true, callback_url:, shipping_method_model: FlexCommerce::ShippingMethod, use_mobile_payments: false, description: nil)
  self.gateway_class = gateway_class
  self.cart = cart
  self.allow_shipping_change = allow_shipping_change
  self.success_url = success_url
  self.cancel_url = cancel_url
  self.ip_address = ip_address
  self.callback_url = callback_url
  self.shipping_method_model = shipping_method_model
  self.use_mobile_payments = use_mobile_payments
  self.description = description
end

Instance Method Details

#callObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/paypal_express/setup.rb', line 44

def call
  validate_shipping_method
  
  response = gateway.setup_order(convert_amount(cart.total), paypal_params)
  # If paypal setup went fine, redirect to the paypal page
  if response.success?
    PaypalSetup.new(setup_type:  "redirect", redirect_url: gateway.redirect_url_for(response.token, mobile: use_mobile_payments))
  else
    # @TODO Find out where to get the message from and add it
    error = "An error occured communicating with paypal #{response.message} \n\n#{response.params.to_json}. Total sent was #{convert_amount(cart.total)} Parameters sent were \n\n#{paypal_params}"
    raise ::FlexCommerce::PaypalExpress::Exception::AccessDenied.new(error)
  end
rescue ::FlexCommerce::PaypalExpress::Exception::AccessDenied => exception
  PaypalSetup.new(errors: exception)
end