Skip to content

Commit

Permalink
Add Venmo as payment option on checkout
Browse files Browse the repository at this point in the history
Resolves issues/tickets:
- solidusio#136
- solidusio#137

Setting the preference enable_venmo to true will now show a Venmo button
on checkout for customers whom it is available to.

For testing purposes, the string preference force_buyer_country was
added. You can use this to mock what country the buyer is from, allowing
you to see what funding sources are shown to customers in various
countries.

This preference does not affect checkout on production, environment.
Instead, PayPal will get this information from their ip geolocation.

More information:
https://developer.paypal.com/docs/business/javascript-sdk/javascript-sdk-configuration/#buyer-country
  • Loading branch information
RyanofWoods committed Nov 22, 2021
1 parent 35524e8 commit d6a1465
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
10 changes: 10 additions & 0 deletions app/models/solidus_paypal_commerce_platform/payment_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class PaymentMethod < SolidusSupport.payment_method_parent_class
preference :display_on_cart, :boolean, default: true
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 @@ -73,8 +75,16 @@ 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 @@ -119,6 +119,50 @@ def Struct(data) # rubocop:disable Naming/MethodName
expect(url.query.split("&")).to include("components=buttons")
end
end

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

it 'does not include the "enable-funding=venmo" parameter' do
expect(url.query.split("&")).not_to include("enable-funding=venmo")
end
end

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

it 'includes "enable-funding=venmo" as a parameter' do
paypal_payment_method.preferences.update(enable_venmo: true)
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 d6a1465

Please sign in to comment.