Class: PaypalExpressController

Inherits:
ApplicationController
  • Object
show all
Includes:
PayPalHelper
Defined in:
lib/generators/templates/paypal_express_controller.rb

Instance Method Summary collapse

Methods included from PayPalHelper

#get_purchase_params, #order_info, #purchase_params, #to_cents

Instance Method Details

#checkoutObject



5
6
7
8
9
# File 'lib/generators/templates/paypal_express_controller.rb', line 5

def checkout
  total, setup_purchase_params = purchase_params cart, request
  setup_response = EXPRESS_GATEWAY.setup_purchase(total, setup_purchase_params)
  redirect_to EXPRESS_GATEWAY.redirect_url_for(setup_response.token)
end

#purchaseObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/generators/templates/paypal_express_controller.rb', line 27

def purchase
  if params[:token].nil? or params[:payer_id].nil?
      redirect_to orders_url, :notice => "Sorry but something went wrong"
    return
  end
  
  total_in_cents, purchase_params = get_purchase_params cart, request, params
  purchase = EXPRESS_GATEWAY.purchase total_in_cents, purchase_params

  if purchase.success?
    session[:cart] = nil
    # you might want to destroy your cart here if you have a shopping cart
    notice = "Thanks!, your purchase was successfull"
  else
    notice = "Something wrong happened when trying to complete the purchase with Paypal. Paypal said: #{purchase.message}"
  end

  redirect_to products_url, :notice => notice
end

#reviewObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/generators/templates/paypal_express_controller.rb', line 11

def review
  if params[:token].nil?
    redirect_to products_url, :notice => 'Something went wrong!'
    return
  end

  response = EXPRESS_GATEWAY.details_for(params[:token])

  unless response.success?
    redirect_to products_url, :notice => "Something went wrong with the Paypal purchase. Paypal said: #{response.message}"
    return
  end

  @order = order_info response, cart
end