Skip to content

Commit

Permalink
Add ability to walkthrough PayPal Express Checkout
Browse files Browse the repository at this point in the history
Please see issues for what else needs doing.
  • Loading branch information
radar committed Jul 18, 2013
1 parent acdd874 commit 81bb4aa
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 13 deletions.
69 changes: 69 additions & 0 deletions app/controllers/spree/paypal_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
module Spree
class PaypalController < Spree::StoreController
def express
# TODO: support adjustments
# Check out the old spree_paypal_express for how it does this
# Specifically, look for "credits" in CheckoutController decorator
items = current_order.line_items.map do |item|
{ name: item.variant.name,
description: item.variant.description[0..120],
quantity: item.quantity,
# TODO: For 2.1.0, make this just item.money.cents
amount: item.money.money.cents }
end

# TODO: For 2.1.0, make this display_item_total.cents
response = provider.setup_purchase(current_order.display_item_total.money.cents,
ip: request.remote_ip,
return_url: confirm_paypal_url(:payment_method_id => params[:payment_method_id]),
cancel_return_url: cancel_paypal_url,
currency: Spree::Config[:currency],
locale: I18n.locale.to_s.sub(/-/, '_'),
brand_name: Spree::Config[:site_name],
address: address_options,
header_image: 'http://teclacolorida.com/assets/images/logos/schoooools.png',
allow_guest_checkout: 'true', #payment with credit card for non PayPal users
items: items
)

redirect_to provider.redirect_url_for(response.token)
end

def confirm
details = provider.details_for(params[:token])
payer_id = details.payer_id
# TODO: For 2.1.0, make this display_item_total.cents
response = provider.purchase(current_order.display_item_total.money.cents, {
ip: request.remote_addr,
token: params[:token],
payer_id: payer_id,
currency: Spree::Config[:currency]
})
# TODO: Add payment to order
end

def cancel
binding.pry

end

private

def provider
Spree::PaymentMethod.find(params[:payment_method_id]).provider
end

def address_options
{
:name => current_order.bill_address.try(:full_name),
:zip => current_order.bill_address.zipcode,
:address1 => current_order.bill_address.address1,
:address2 => current_order.bill_address.address2,
:city => current_order.bill_address.city,
:phone => current_order.bill_address.phone,
:state => current_order.bill_address.state_text,
:country => current_order.bill_address.country.iso
}
end
end
end
10 changes: 0 additions & 10 deletions app/models/gateway/pay_pal_gateway.rb

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ class Gateway::PayPalExpress < Gateway
attr_accessible :preferred_login, :preferred_password, :preferred_signature

def provider_class
ActiveMerchant::Billing::PaypalGateway
ActiveMerchant::Billing::PaypalExpressGateway
end

def method_type
'paypal'
end
end
end
1 change: 1 addition & 0 deletions app/views/spree/checkout/payment/_paypal.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= link_to(image_tag("https://www.paypal.com/en_US/i/btn/btn_xpressCheckout.gif"), paypal_express_url(:payment_method_id => payment_method.id), :method => :post) %>
5 changes: 4 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
Spree::Core::Engine.routes.draw do
# Add your extension routes here
post '/paypal', :to => "paypal#express", :as => :paypal_express
get '/paypal/confirm', :to => "paypal#confirm", :as => :confirm_paypal
get '/paypal/cancel', :to => "paypal#cancel", :as => :cancel_paypal

end
2 changes: 1 addition & 1 deletion lib/spree_paypal_express/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def self.activate
config.to_prepare &method(:activate).to_proc

initializer "spree.paypal_express.payment_methods", :after => "spree.register.payment_methods" do |app|
app.config.spree.payment_methods << Spree::Gateway::PaypalExpress
app.config.spree.payment_methods << Spree::Gateway::PayPalExpress
end
end
end

0 comments on commit 81bb4aa

Please sign in to comment.