diff --git a/app/models/billing_integration/paypal_express.rb b/app/models/billing_integration/paypal_express.rb index 8a902c7..c26e72a 100644 --- a/app/models/billing_integration/paypal_express.rb +++ b/app/models/billing_integration/paypal_express.rb @@ -3,6 +3,7 @@ class BillingIntegration::PaypalExpress < BillingIntegration preference :password, :password preference :signature, :string preference :review, :boolean, :default => false + preference :no_shipping, :boolean, :default => false def provider_class ActiveMerchant::Billing::PaypalExpressGateway diff --git a/app/models/billing_integration/paypal_express_uk.rb b/app/models/billing_integration/paypal_express_uk.rb index 1d87baf..d3c9fbd 100644 --- a/app/models/billing_integration/paypal_express_uk.rb +++ b/app/models/billing_integration/paypal_express_uk.rb @@ -3,6 +3,7 @@ class BillingIntegration::PaypalExpressUk < BillingIntegration preference :password, :password preference :signature, :string preference :review, :boolean, :default => false + preference :no_shipping, :boolean, :default => false def provider_class ActiveMerchant::Billing::PaypalExpressUkGateway diff --git a/app/models/paypal_account.rb b/app/models/paypal_account.rb index 7927bed..5605dd1 100644 --- a/app/models/paypal_account.rb +++ b/app/models/paypal_account.rb @@ -66,6 +66,11 @@ class PaypalAccount < ActiveRecord::Base def can_credit?(payment) !find_capture(payment).nil? end + + # fix for Payment#payment_profiles_supported? + def payment_gateway + false + end private def find_authorization(payment) diff --git a/app/views/shared/_paypal_express_confirm.html.erb b/app/views/shared/_paypal_express_confirm.html.erb index 9c9c708..26a44f8 100644 --- a/app/views/shared/_paypal_express_confirm.html.erb +++ b/app/views/shared/_paypal_express_confirm.html.erb @@ -5,5 +5,6 @@ <%= render :partial => 'shared/order_details', :locals => {:order => @order} -%>
- <%= button_to t('place_order'), paypal_finish_order_checkout_url(@checkout.order, {:token => params[:token] , :PayerID => params[:PayerID] } ), :class => "button primary" %> + <%= button_to t('place_order'), paypal_finish_order_checkout_url(@checkout.order, {:token => params[:token] , :PayerID => params[:PayerID], :payment_method_id => + params[:payment_method_id] } ), :class => "button primary" %>
diff --git a/config/locales/en-GB.yml b/config/locales/en-GB.yml index c7eb886..17c9cd5 100644 --- a/config/locales/en-GB.yml +++ b/config/locales/en-GB.yml @@ -10,6 +10,14 @@ en-GB: paypal_payment_id: PayPal Payment ID pending_reason: Pending Reason result: Result + review: Review + no_shipping: No Shipping + paypal_account: PayPal Account + payer_id: Payer ID + payer_country: Country + payer_status: Status + comment: Comment + account_details: Account Details finalize: paypalexpress: Capture Payment activerecord: @@ -19,4 +27,4 @@ en-GB: models: paypal_payment: one: PayPal Payment - other: PayPal Payments \ No newline at end of file + other: PayPal Payments diff --git a/config/locales/en-US.yml b/config/locales/en-US.yml index c3f59be..93c46a9 100644 --- a/config/locales/en-US.yml +++ b/config/locales/en-US.yml @@ -11,6 +11,7 @@ en-US: pending_reason: Pending Reason result: Result review: Review + no_shipping: No Shipping paypal_account: PayPal Account payer_id: Payer ID payer_country: Country diff --git a/lib/active_merchant/billing/gateways/paypal/paypal_common_api.rb b/lib/active_merchant/billing/gateways/paypal/paypal_common_api.rb index 5bfd594..e5f863b 100644 --- a/lib/active_merchant/billing/gateways/paypal/paypal_common_api.rb +++ b/lib/active_merchant/billing/gateways/paypal/paypal_common_api.rb @@ -10,40 +10,6 @@ module ActiveMerchant #:nodoc: silence_warnings do API_VERSION = '60.0' - - URLS = { - :test => { :certificate => 'https://api.sandbox.paypal.com/2.0/', - :signature => 'https://api-3t.sandbox.paypal.com/2.0/' }, - :live => { :certificate => 'https://api-aa.paypal.com/2.0/', - :signature => 'https://api-3t.paypal.com/2.0/' } - } - - PAYPAL_NAMESPACE = 'urn:ebay:api:PayPalAPI' - EBAY_NAMESPACE = 'urn:ebay:apis:eBLBaseComponents' - - ENVELOPE_NAMESPACES = { 'xmlns:xsd' => 'http://www.w3.org/2001/XMLSchema', - 'xmlns:env' => 'http://schemas.xmlsoap.org/soap/envelope/', - 'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance' - } - CREDENTIALS_NAMESPACES = { 'xmlns' => PAYPAL_NAMESPACE, - 'xmlns:n1' => EBAY_NAMESPACE, - 'env:mustUnderstand' => '0' - } - - AUSTRALIAN_STATES = { - 'ACT' => 'Australian Capital Territory', - 'NSW' => 'New South Wales', - 'NT' => 'Northern Territory', - 'QLD' => 'Queensland', - 'SA' => 'South Australia', - 'TAS' => 'Tasmania', - 'VIC' => 'Victoria', - 'WA' => 'Western Australia' - } - - SUCCESS_CODES = [ 'Success', 'SuccessWithWarning' ] - - FRAUD_REVIEW_CODE = "11610" end # The gateway must be configured with either your PayPal PEM file @@ -291,7 +257,7 @@ module ActiveMerchant #:nodoc: end end - def add_payment_detail_item(xml, item) + def add_payment_detail_item(xml, item, options) currency_code = options[:currency] || currency(item[:amount]) xml.tag! 'n2:PaymentDetailsItem' do xml.tag! 'n2:Name', item[:name] unless item[:name].blank? @@ -335,7 +301,7 @@ module ActiveMerchant #:nodoc: xml.tag! 'n2:ButtonSource', application_id.to_s.slice(0,32) unless application_id.blank? xml.tag! 'n2:NotifyURL', options[:notify_url] unless options[:notify_url].blank? add_address(xml, 'n2:ShipToAddress', options[:shipping_address] || options[:address]) - options[:items].each {|i| add_payment_detail_item xml, i } if options[:items] + options[:items].each {|i| add_payment_detail_item xml, i, options } if options[:items] end end diff --git a/lib/spree/paypal_express.rb b/lib/spree/paypal_express.rb index 7d0ad90..778edbb 100644 --- a/lib/spree/paypal_express.rb +++ b/lib/spree/paypal_express.rb @@ -54,27 +54,28 @@ module Spree::PaypalExpress @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)" - 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 - if (state = State.find_by_abbr(ship_address["state"])) - order_ship_address.state = state - else - order_ship_address.state_name = ship_address["state"] + order_ship_address.save! + + @order.checkout.ship_address = order_ship_address end - - order_ship_address.save! - - @order.checkout.ship_address = order_ship_address @order.checkout.save if payment_method.preferred_review @@ -90,7 +91,7 @@ module Spree::PaypalExpress def paypal_finish load_object - opts = { :token => params[:token], :payer_id => params[:PayerID] }.merge all_opts(@order, params[:payment_method_id]) + opts = { :token => params[:token], :payer_id => params[:PayerID] }.merge all_opts(@order, params[:payment_method_id], 'checkout' ) gateway = paypal_gateway if Spree::Config[:auto_capture] @@ -169,6 +170,11 @@ module Spree::PaypalExpress # see http://www.pdncommunity.com/t5/PayPal-Developer-Blog/Displaying-Order-Details-in-Express-Checkout/bc-p/92902#C851 } end + + # hook to override paypal site options + def paypal_site_opts + {} + end def order_opts(order, payment_method, stage) items = order.line_items.map do |item| @@ -201,6 +207,8 @@ module Spree::PaypalExpress # get the main totals from the items (already *100) opts[:subtotal] = opts[:items].map {|i| i[:amount] * i[:qty] }.sum opts[:tax] = opts[:items].map {|i| i[:tax] * i[:qty] }.sum + opts[:handling] = 0 # MJM Added to force elements to be generated + opts[:shipping] = (order.ship_total*100).to_i # overall total opts[:money] = opts.slice(:subtotal, :tax, :shipping, :handling).values.sum @@ -234,24 +242,28 @@ module Spree::PaypalExpress end def address_options(order) - { - :no_shipping => false, - :address_override => true, - :address => { - :name => "#{order.ship_address.firstname} #{order.ship_address.lastname}", - :address1 => order.ship_address.address1, - :address2 => order.ship_address.address2, - :city => order.ship_address.city, - :state => order.ship_address.state.nil? ? order.ship_address.state_name.to_s : order.ship_address.state.abbr, - :country => order.ship_address.country.iso, - :zip => order.ship_address.zipcode, - :phone => order.ship_address.phone + if payment_method.preferred_no_shipping + { :no_shipping => true } + else + { + :no_shipping => false, + :address_override => true, + :address => { + :name => "#{order.ship_address.firstname} #{order.ship_address.lastname}", + :address1 => order.ship_address.address1, + :address2 => order.ship_address.address2, + :city => order.ship_address.city, + :state => order.ship_address.state.nil? ? order.ship_address.state_name.to_s : order.ship_address.state.abbr, + :country => order.ship_address.country.iso, + :zip => order.ship_address.zipcode, + :phone => order.ship_address.phone + } } - } + end end def all_opts(order, payment_method, stage=nil) - opts = fixed_opts.merge(order_opts(order, payment_method, stage)) + opts = fixed_opts.merge(order_opts(order, payment_method, stage)).merge(paypal_site_opts) if stage == "payment" opts.merge! flat_rate_shipping_and_handling_options(order, stage) diff --git a/paypal_express_extension.rb b/paypal_express_extension.rb index 23009e1..177c231 100644 --- a/paypal_express_extension.rb +++ b/paypal_express_extension.rb @@ -14,12 +14,11 @@ class PaypalExpressExtension < Spree::Extension # these will be submitted to ActiveMerchant some time... require File.join(PaypalExpressExtension.root, "lib", "active_merchant", "billing", "gateways", "paypal", "paypal_common_api.rb") require File.join(PaypalExpressExtension.root, "lib", "active_merchant", "billing", "gateways", "paypal_express_uk.rb") - require File.join(PaypalExpressExtension.root, "lib", "active_merchant", "billing", "gateways", "paypal_express_uk.rb") - # inject paypal code into orders controller CheckoutsController.class_eval do include Spree::PaypalExpress end + end end