Module: Spree::PaypalStandard

Includes:
ActiveMerchant::RequiresParameters, ERB::Util
Defined in:
lib/spree/paypal_standard.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(target) ⇒ Object



5
6
7
# File 'lib/spree/paypal_standard.rb', line 5

def self.included(target)
  target.before_filter :redirect_to_paypal_standard_form, :only => [:update]
end

Instance Method Details

#paypal_standard_checkoutObject

Outbound redirect to PayPal from Cart Page Not fully implmented or tested.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/spree/paypal_standard.rb', line 12

def paypal_standard_checkout
  load_object
  opts = all_opts(@order, params[:payment_method_id], 'checkout')
  opts.merge!(address_options(@order))
  gateway = paypal_standard_gateway

  if Spree::Config[:auto_capture]
    response = gateway.setup_purchase(opts[:money], opts)
  else
    response = gateway.setup_authorization(opts[:money], opts)
  end

  unless response.success?
    gateway_error(response)
    redirect_to edit_order_url(@order)
    return
  end

  redirect_to (gateway.redirect_url_for response.token, :review => payment_method.preferred_review)
rescue ActiveMerchant::ConnectionError => e
  gateway_error I18n.t(:unable_to_connect_to_gateway)
  redirect_to :back
end

#paypal_standard_confirmObject

Inbound post from PayPal after (possible) successful completion



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/spree/paypal_standard.rb', line 45

def paypal_standard_confirm
  load_object

  opts = { :token => params[:token], :payer_id => params[:PayerID] }.merge all_opts(@order, params[:payment_method_id],  'payment')
  gateway = paypal_gateway

  @ppx_details = gateway.details_for params[:token]

  if @ppx_details.success?
    # now save the updated order info

    PaypalAccount.create(:email => @ppx_details.params["payer"],
                         :payer_id => @ppx_details.params["payer_id"],
                         :payer_country => @ppx_details.params["payer_country"],
                         :payer_status => @ppx_details.params["payer_status"])

    @order.checkout.special_instructions = @ppx_details.params["note"]

    #@order.update_attribute(:user, current_user)
    unless payment_method.preferred_no_shipping
      ship_address = @ppx_details.address
      order_ship_address = Address.new :firstname  => @ppx_details.params["first_name"],
                                       :lastname   => @ppx_details.params["last_name"],
                                       :address1   => ship_address["address1"],
                                       :address2   => ship_address["address2"],
                                       :city       => ship_address["city"],
                                       :country    => Country.find_by_iso(ship_address["country"]),
                                       :zipcode    => ship_address["zip"],
                                       # phone is currently blanked in AM's PPX response lib
                                       :phone      => @ppx_details.params["phone"] || "(not given)"

      if (state = State.find_by_abbr(ship_address["state"]))
        order_ship_address.state = state
      else
        order_ship_address.state_name = ship_address["state"]
      end

      order_ship_address.save!

      @order.checkout.ship_address = order_ship_address
    end
    @order.checkout.save

    if payment_method.preferred_review
      render :partial => "shared/paypal_express_confirm", :layout => true
    else
      paypal_finish
    end

  else
    gateway_error(@ppx_details)

    #Failed trying to get payment details from PPX
    redirect_to edit_order_checkout_url(@order, :step => "payment")
  end
rescue ActiveMerchant::ConnectionError => e
  gateway_error I18n.t(:unable_to_connect_to_gateway)
  redirect_to edit_order_url(@order)
end

#paypal_standard_finishObject

Local call from A) Order Review Screen, or B) Automatically after paypal_confirm (no review). Completes checkout & order



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/spree/paypal_standard.rb', line 108

def paypal_standard_finish
  load_object

  opts = { :token => params[:token], :payer_id => params[:PayerID] }.merge all_opts(@order, params[:payment_method_id], 'payment' )
  gateway = paypal_gateway

  if Spree::Config[:auto_capture]
    ppx_auth_response = gateway.purchase((@order.total*100).to_i, opts)
  else
    ppx_auth_response = gateway.authorize((@order.total*100).to_i, opts)
  end

  if ppx_auth_response.success?
    #confirm status
    case ppx_auth_response.params["payment_status"]
      when "Completed"
        txn_type = PaypalTxn::TxnType::CAPTURE
      when "Pending"
        txn_type = PaypalTxn::TxnType::AUTHORIZE
      else
        txn_type = PaypalTxn::TxnType::UNKNOWN
        Rails.logger.error "Unexpected response from PayPal Express"
        Rails.logger.error ppx_auth_response.to_yaml
    end

     = PaypalAccount.find_by_payer_id(params[:PayerID])

    payment = @order.checkout.payments.create(:amount => ppx_auth_response.params["gross_amount"].to_f,
                                              :source => ,
                                              :payment_method_id => params[:payment_method_id])

    PaypalTxn.create(:payment => payment,
                     :txn_type => txn_type,
                     :amount => ppx_auth_response.params["gross_amount"].to_f,
                     :message => ppx_auth_response.params["message"],
                     :payment_status => ppx_auth_response.params["payment_status"],
                     :pending_reason => ppx_auth_response.params["pending_reason"],
                     :transaction_id => ppx_auth_response.params["transaction_id"],
                     :transaction_type => ppx_auth_response.params["transaction_type"],
                     :payment_type => ppx_auth_response.params["payment_type"],
                     :response_code => ppx_auth_response.params["ack"],
                     :token => ppx_auth_response.params["token"],
                     :avs_response => ppx_auth_response.avs_result["code"],
                     :cvv_response => ppx_auth_response.cvv_result["code"])


    @order.save!
    @checkout.reload
    #need to force checkout to complete state
    until @checkout.state == "complete"
      @checkout.next!
    end
    complete_checkout

    # even with auto_capture , an Auth might be returned / forced by PPX
    if Spree::Config[:auto_capture] && txn_type == PaypalTxn::TxnType::CAPTURE
      payment.finalize!
    end

  else
    order_params = {}
    gateway_error(ppx_auth_response)

    #Failed trying to complete pending payment!
    redirect_to edit_order_checkout_url(@order, :step => "payment")
  end
rescue ActiveMerchant::ConnectionError => e
  gateway_error I18n.t(:unable_to_connect_to_gateway)
  redirect_to edit_order_url(@order)
end

#paypal_standard_paymentObject

Outbound redirect to Paypoint via a form



38
39
40
41
# File 'lib/spree/paypal_standard.rb', line 38

def paypal_standard_payment
  debugme=1
  
end