Skip to content

Commit

Permalink
Add force_country preference for testing purposes
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 the different
funding sources for different customers and their integration.

More information:
https://developer.paypal.com/docs/business/javascript-sdk/javascript-sdk-configuration/#buyer-country
  • Loading branch information
RyanofWoods committed Nov 23, 2021
1 parent 1abd542 commit 7b3fe5c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
8 changes: 8 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,8 +76,15 @@ 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]
parameters['buyer-country'] = options[:force_buyer_country] if add_buyer_country_to_url?

"https://www.paypal.com/sdk/js?#{parameters.to_query}"
end

private

def add_buyer_country_to_url?
options[:force_buyer_country].present? && !Rails.env.production?
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,33 @@ 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
let(:order) { instance_double(Spree::Order, checkout_steps: { "foo" => "bar" }) }

it 'does not include the "buyer-country" parameter' do
expect(url.query.split("&")).not_to include("buyer-country")
end
end

context 'when force_buyer_country is "US"' do
let(:order) { instance_double(Spree::Order, checkout_steps: { "foo" => "bar" }) }

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

context 'when force_buyer_country is "US" and environment is production' do
let(:order) { instance_double(Spree::Order, checkout_steps: { "foo" => "bar" }) }

it 'does not include the "buyer-country" parameter' do
paypal_payment_method.preferences.update(force_buyer_country: 'US')
allow(Rails.env).to receive(:production?).and_return(true)
expect(url.query.split("&")).not_to include("buyer-country")
end
end
end

private
Expand Down

0 comments on commit 7b3fe5c

Please sign in to comment.