Class: ActivePayment::Gateways::PaypalExpressCheckout

Inherits:
Object
  • Object
show all
Includes:
ActionDispatch::Routing, ActionView::Helpers
Defined in:
lib/active_payment/gateways/paypal_express_checkout.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePaypalExpressCheckout

Returns a new instance of PaypalExpressCheckout.



12
13
14
# File 'lib/active_payment/gateways/paypal_express_checkout.rb', line 12

def initialize
  @gateway = ActiveMerchant::Billing::PaypalExpressGateway.new(paypal_options)
end

Instance Attribute Details

#purchase_tokenObject

Returns the value of attribute purchase_token.



10
11
12
# File 'lib/active_payment/gateways/paypal_express_checkout.rb', line 10

def purchase_token
  @purchase_token
end

#salesObject

Returns the value of attribute sales.



10
11
12
# File 'lib/active_payment/gateways/paypal_express_checkout.rb', line 10

def sales
  @sales
end

Instance Method Details

#external_id_from_request(request) ⇒ Object



47
48
49
# File 'lib/active_payment/gateways/paypal_express_checkout.rb', line 47

def external_id_from_request(request)
  request.params[:token]
end

#livemode?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/active_payment/gateways/paypal_express_checkout.rb', line 51

def livemode?
  ActiveMerchant::Billing::Base.mode != :test
end

#setup_purchase(sales) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/active_payment/gateways/paypal_express_checkout.rb', line 16

def setup_purchase(sales)
  @sales = sales

  payables = @sales.map(&:payable)
  amount = @sales.amount_in_cents.to_i

  response = @gateway.setup_purchase(amount, paypal_data(payables))
  raise ActivePayment::InvalidGatewayResponseError.new(response) unless response.success?

  @purchase_token = response.token
  @gateway.redirect_url_for(response.token)
end

#verify_purchase(params) ⇒ Object

return boolean



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/active_payment/gateways/paypal_express_checkout.rb', line 30

def verify_purchase(params)
  token = params[:token]

  begin
    response = @gateway.details_for(token)
    fail ActivePayment::InvalidGatewayResponseError.new(response) unless response.success?

    amount = params[:amount]
    purchase_response = @gateway.purchase(amount, params.merge(payer_id: response.payer_id))
    fail ActivePayment::InvalidGatewayResponseError.new(purchase_response) unless purchase_response.success?
  rescue ActivePayment::InvalidGatewayResponseError
    return false
  end

  true
end