class FlexCommerce::PaypalExpress::Auth

@class Setup

This service authorises the payment via the Paypal gateway

Constants

DEFAULT_CURRENCY

Attributes

cart[RW]
gateway_class[RW]
payer_id[RW]
payment_transaction[RW]
token[RW]

Public Class Methods

new(cart:, token:, payer_id:, payment_transaction:, gateway_class: ::ActiveMerchant::Billing::PaypalExpressGateway) click to toggle source

@initialize

@param {String} token - Paypal token @param {String} payer_id - Paypal user id

# File lib/paypal_express/auth.rb, line 21
def initialize(cart:, token:, payer_id:, payment_transaction:, gateway_class: ::ActiveMerchant::Billing::PaypalExpressGateway)
  self.cart = cart
  self.token = token
  self.payer_id = payer_id
  self.payment_transaction = payment_transaction
  self.gateway_class = gateway_class
end

Public Instance Methods

call() click to toggle source
# File lib/paypal_express/auth.rb, line 29
def call
  process_with_gateway
end

Private Instance Methods

do_authorization(response) click to toggle source
# File lib/paypal_express/auth.rb, line 71
def do_authorization(response)
  Retry.call(no_of_retries: no_of_retires, rescue_errors: ::ActiveMerchant::ConnectionError) {
    ::NewRelic::Agent.increment_metric('Custom/Paypal/Do_Auhtorization') if defined?(NewRelic::Agent)
    gateway.authorize_transaction(response.params["transaction_id"], convert_amount(cart.total), transaction_entity: "Order", currency: DEFAULT_CURRENCY, payer_id: payer_id)
  }
end
do_express_checkout_payment() click to toggle source
# File lib/paypal_express/auth.rb, line 63
def do_express_checkout_payment
  Retry.call(no_of_retries: no_of_retires, rescue_errors: ::ActiveMerchant::ConnectionError) {
    ::NewRelic::Agent.increment_metric('Custom/Paypal/Do_Express_Checkout_Payment') if defined?(NewRelic::Agent)
    gateway.order(convert_amount(cart.total), token: token, payer_id: payer_id, currency: DEFAULT_CURRENCY)
  }
end
no_of_retires() click to toggle source
# File lib/paypal_express/auth.rb, line 78
def no_of_retires
  FlexCommerceApi.config.paypal_connection_errors_no_of_retries
end
process_with_gateway() click to toggle source
# File lib/paypal_express/auth.rb, line 37
def process_with_gateway
  # Fetch Order details from Paypal
  response = do_express_checkout_payment
  unless response.success?
    unless is_user_error?(response)
      raise ::FlexCommerce::PaypalExpress::Exception::NotAuthorized.new("Payment not authorised - #{response.message}", response: response)
    end
    return mark_transaction_with_errors!(response)
  end

  # Authorizing transaction
  auth_response = do_authorization(response)
  unless auth_response.success?
    unless is_user_error?(auth_response)
      raise ::FlexCommerce::PaypalExpress::Exception::NotAuthorized.new("Failed authorising transaction - #{auth_response.message}", response: auth_response)
    end
    return mark_transaction_with_errors!(auth_response)
  end

  payment_transaction.attributes = { gateway_response: { payer_id: payer_id, token: token, transaction_id: response.params["transaction_id"], authorization_id: auth_response.params["transaction_id"]} }
  payment_transaction.save
  payment_transaction
rescue ::ActiveMerchant::ConnectionError => ex
  raise ::FlexCommerce::PaypalExpress::Exception::ConnectionError.new("Failed authorising transaction due to a connection error.  Original message was #{ex.message}")
end