Skip to content

Commit

Permalink
Add force_buyer_country payment method preference for testing
Browse files Browse the repository at this point in the history
Resolves issue/ticket:
- #137

Usually on checkout, PayPal will look at the buyer's location by their
ip and determine which funding sources are available to them. For
example, allow Venmo as an option for US buyers, but not for others.

With this new preference, you can set a country to override the buyer
location. Allowing you on non-production environments to see the
different funding sources for different customers and its integration.

This preference is still available on production but has no effect.
It was kept on production to not have differences between the
environments. Otherwise, errors could easily occur on production if
for example, preferred_force_buyer_country method was incorrectly
called.

More information:
https://developer.paypal.com/docs/business/javascript-sdk/javascript-sdk-configuration/#buyer-country
  • Loading branch information
RyanofWoods committed Dec 2, 2021
1 parent 46a4b77 commit cc2e8cd
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,12 @@ $ bin/rails server
Use Ctrl-C to stop
```

### Mocking your buyer country
PayPal normally looks at your IP geolocation to see where you are located to determine what funding sources are available to you. For example, Venmo is currently only available to US buyers.
Because of this, you may want to pretend you are from US check that that Venmo is correctly integrated for these customers. To do this, set the payment method's preference of `force_buyer_country` to "US". See more information about preferences above.

This preference has no effect on production.

### Updating the changelog

Before and after releases the changelog should be updated to reflect the up-to-date status of
Expand Down
4 changes: 4 additions & 0 deletions app/models/solidus_paypal_commerce_platform/payment_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class PaymentMethod < SolidusSupport.payment_method_parent_class
preference :display_on_product_page, :boolean, default: true
preference :display_credit_messaging, :boolean, default: true
preference :enable_venmo, :boolean, default: false
preference :force_buyer_country, :string

def partial_name
"paypal_commerce_platform"
Expand Down Expand Up @@ -75,6 +76,9 @@ def javascript_sdk_url(order: nil, currency: nil)

parameters[:shipping_preference] = 'NO_SHIPPING' if step_names.exclude? 'delivery'
parameters['enable-funding'] = 'venmo' if options[:enable_venmo]
if !Rails.env.production? && options[:force_buyer_country].present?
parameters['buyer-country'] = options[:force_buyer_country]
end

"https://www.paypal.com/sdk/js?#{parameters.to_query}"
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,31 @@ def Struct(data) # rubocop:disable Naming/MethodName
expect(url.query.split('&')).to include('enable-funding=venmo')
end
end

context 'when force_buyer_country is an empty string' do
it 'does not include the "buyer-country" parameter' do
expect(url.query.split('&')).not_to include(match 'buyer-country')
end
end

context 'when force_buyer_country is "US"' do
before { paypal_payment_method.preferences.update(force_buyer_country: 'US') }

it 'includes "buyer-country=US" as a parameter' do
expect(url.query.split('&')).to include('buyer-country=US')
end
end

context 'when force_buyer_country is "US" but the environment is production' do
before {
allow(Rails.env).to receive(:production?).and_return(true)
paypal_payment_method.preferences.update(force_buyer_country: 'US')
}

it 'includes "buyer-country=US" as a parameter' do
expect(url.query.split('&')).not_to include(match 'buyer-country')
end
end
end

private
Expand Down

0 comments on commit cc2e8cd

Please sign in to comment.