Skip to content

Commit

Permalink
Shipping addresses
Browse files Browse the repository at this point in the history
Adds the ability to send a shipping address to Paypal (see spree-contrib#113)

Also toggles displaying the shipping address on the Paypal pages.
  • Loading branch information
fredjean committed Oct 2, 2014
1 parent 8ce4366 commit 0df9e48
Show file tree
Hide file tree
Showing 4 changed files with 221 additions and 42 deletions.
45 changes: 42 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ You will also need a "Personal" account to test the transactions on your site. C

#### Spree Setup

In Spree, go to the admin backend, click "Configuration" and then "Payment Methods" and create a new payment method. Select "Spree::Gateway::PayPalExpress" as the provider, and click "Create". Enter the email address, password and signature from the "API Credentials" tab for the **Business** account on PayPal.
In Spree, go to the admin backend, click "Configuration" and then "Payment Methods" and create a new payment method. Select "Spree::Gateway::PayPalExpress" as the provider, and click "Create". Enter the email address, password and signature from the "API Credentials" tab for the **Business** account on PayPal.

### Production setup

Expand All @@ -56,15 +56,15 @@ This Spree extension supports *some* of those. If your favourite is not here, th

### Solution Type

Determines whether or not a user needs a PayPal account to check out.
Determines whether or not a user needs a PayPal account to check out.

```ruby
payment_method.preferred_solution_type = "Mark"
# or
payment_method.preferred_solution_type = "Sole"
```

"Mark" if you do want users to have a paypal account, "Sole" otherwise.
"Mark" if you do want users to have a paypal account, "Sole" otherwise.

### Landing Page

Expand All @@ -88,6 +88,45 @@ payment_method.preferred_logourl = 'http://yoursite.com/images/checkout.jpg'

**Must** be an absolute path to the image.

### Displaying Shipping Addresses

You have the option of displaying the shipping address of an order on
the PayPal pages:

```ruby
# Displays the shipping address of the order on the Paypal page
payment_method.preferred_no_shipping = '0'

# Do not display the shipping address on the Paypal page
# This is the default configuration since this matches the pre-existing
# behavior of the gem
payment_method.preferred_no_shipping = '1'

# Display the shipping address listed in the profile if it is not
# sent along with the order
payment_method.preferred_no_shipping = '2'
```

This has no effect on what shipping address is passed to the gateway.

### Overriding the Shipping Address

By default PayPay will use the shipping address that it has on file as
the shipping address for an order.

You can configure the gateway to send up the shipping address associated
with the order through the following configuration:

```ruby
# Do not override the shipping address on file (default)
payment_method.preferred_address_override = '0'

# Override the shipping address on file
payment_method.preferred_address_override = '1'
```

The shipping address will be sent to the server if configured to do so.

## Caveats

*Caveat venditor*
Expand Down
24 changes: 14 additions & 10 deletions app/controllers/spree/paypal_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ def express_checkout_request_details order, items
:SolutionType => payment_method.preferred_solution.present? ? payment_method.preferred_solution : "Mark",
:LandingPage => payment_method.preferred_landing_page.present? ? payment_method.preferred_landing_page : "Billing",
:cppheaderimage => payment_method.preferred_logourl.present? ? payment_method.preferred_logourl : "",
:NoShipping => payment_method.preferred_no_shipping.present? ? (payment_method.preferred_no_shipping ? 1 : 0) : 1,
:NoShipping => payment_method.preferred_no_shipping.present? ? payment_method.preferred_no_shipping : '1',
:AddressOverride => payment_method.preferred_address_override.present? ? payment_method.preferred_address_override : '0',
:PaymentDetails => [payment_details(items)]
}}
end
Expand Down Expand Up @@ -156,15 +157,17 @@ def payment_details items
def address_options
return {} unless address_required?

address = current_order.use_billing ? current_order.bill_address : current_order.ship_address

{
:Name => current_order.bill_address.try(:full_name),
:Street1 => current_order.bill_address.address1,
:Street2 => current_order.bill_address.address2,
:CityName => current_order.bill_address.city,
:Phone => current_order.bill_address.phone,
:StateOrProvince => current_order.bill_address.state_text,
:Country => current_order.bill_address.country.iso,
:PostalCode => current_order.bill_address.zipcode
:Name => address.try(:full_name),
:Street1 => address.address1,
:Street2 => address.address2,
:CityName => address.city,
:Phone => address.phone,
:StateOrProvince => address.state_text,
:Country => address.country.iso,
:PostalCode => address.zipcode
}
end

Expand All @@ -173,7 +176,8 @@ def completion_route(order)
end

def address_required?
payment_method.preferred_solution.eql?('Sole')
payment_method.preferred_solution.eql?('Sole') \
|| payment_method.preferred_address_override.eql?('1')
end
end
end
14 changes: 13 additions & 1 deletion app/models/spree/gateway/pay_pal_express.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,19 @@ class Gateway::PayPalExpress < Gateway
preference :solution, :string, default: 'Mark'
preference :landing_page, :string, default: 'Billing'
preference :logourl, :string, default: ''
preference :no_shipping, :boolean, default: true
# Indicates whether to display the shipping address on the paypal checkout
# page.
#
# 0 - Paypal displays the shipping address on the page.
# 1 - Paypal does not display the shipping address on its pages. This is
# the default due to the history of this gem.
# 2 - Paypal will obtain the shipping address from the profile.
preference :no_shipping, :string, default: '1'

# Allow Address Override
# 0 - Do not display the address passed to Paypal
# 1 - Display the address sent to Paypal
preference :address_override, :string, default: '0'

def supports?(source)
true
Expand Down
Loading

0 comments on commit 0df9e48

Please sign in to comment.