From 5bcb62b21e835cd46991cca108ec121fbd77051a Mon Sep 17 00:00:00 2001 From: Ryan Woods Date: Fri, 17 Dec 2021 14:41:32 +0100 Subject: [PATCH 1/3] Update Venmo integration The Venmo integration now adds a standalone button for Venmo, rather than enabling Venmo as a financing option for the normal checkout flow. This is because Venmo does not currently support shipping callbacks, so we cannot use this. The Venmo integration adds a Venmo button to payment page. It cannot be rendered on the cart or product page, because it is not possible in Venmo to get shipping costs from Solidus and update for the payment total. --- README.md | 11 ++- .../buttons.js | 10 +++ .../button_options_helper.rb | 4 ++ .../payment_method.rb | 17 +++-- .../testing_support/factories.rb | 3 +- .../_paypal_commerce_platform.html.erb | 8 +++ .../payment_method_spec.rb | 67 ++++++------------- 7 files changed, 57 insertions(+), 63 deletions(-) diff --git a/README.md b/README.md index 4fe9c080..c1e19b19 100644 --- a/README.md +++ b/README.md @@ -123,14 +123,11 @@ PayPals API does not allow for admin-side payments. Instead, backend users takin ## Venmo Venmo is currently available to US merchants and buyers. There are also other [prequisites](https://developer.paypal.com/docs/business/checkout/pay-with-venmo/#eligibility). -If the transaction supports Venmo then a button should appear for it on checkout, cart and product page, depending on your `Payment Method` preferences. +If the transaction supports Venmo and it is enabled by the following, then a button should appear for it on checkout payment page. Note, Venmo cannot currently be rendered on the product or cart pages. -By default, the extension and PayPal will try to render a Venmo button to buyers when prequisites are met. - -Set the PaypalCommercePlatform `PaymentMethod` `venmo_control` preference to: -- `enforced`, disregard buyer's location and show button (if other prequisites are met); or -- `enabled` (default), available as a PayPal funding option (if other prequisites are met); or -- `disabled`, disable Venmo as a funding option. +Set the PaypalCommercePlatform `PaymentMethod` `venmo_standalone` preference to: +- `enabled`, show Venmo on the payment page; or +- `disabled` (default), do not show the Venmo button. See more about preferences([Configuration](#configuration)) below. diff --git a/app/assets/javascripts/spree/frontend/solidus_paypal_commerce_platform/buttons.js b/app/assets/javascripts/spree/frontend/solidus_paypal_commerce_platform/buttons.js index 5d020bd6..32a90ea7 100644 --- a/app/assets/javascripts/spree/frontend/solidus_paypal_commerce_platform/buttons.js +++ b/app/assets/javascripts/spree/frontend/solidus_paypal_commerce_platform/buttons.js @@ -27,3 +27,13 @@ SolidusPaypalCommercePlatform.renderProductButton = function(payment_method_id, onError: SolidusPaypalCommercePlatform.handleError }).render('#paypal-button-container') } + +SolidusPaypalCommercePlatform.renderVenmoStandalone = function(payment_method_id, style) { + paypal.Buttons({ + style: style, + fundingSource: paypal.FUNDING.VENMO, + createOrder: SolidusPaypalCommercePlatform.sendOrder.bind(null, payment_method_id), + onApprove: SolidusPaypalCommercePlatform.approveOrder, + onError: SolidusPaypalCommercePlatform.handleError + }).render('#paypal-button-container') +} diff --git a/app/helpers/solidus_paypal_commerce_platform/button_options_helper.rb b/app/helpers/solidus_paypal_commerce_platform/button_options_helper.rb index a437aefc..f2112602 100644 --- a/app/helpers/solidus_paypal_commerce_platform/button_options_helper.rb +++ b/app/helpers/solidus_paypal_commerce_platform/button_options_helper.rb @@ -17,5 +17,9 @@ def preferred_paypal_button_shape_options def preferred_paypal_button_layout_options [["Vertical", "vertical"], ["Horizontal", "horizontal"]] end + + def preferred_venmo_standalone_options + [['Enabled', 'Enabled'], ['Disabled', 'disabled']] + end end end diff --git a/app/models/solidus_paypal_commerce_platform/payment_method.rb b/app/models/solidus_paypal_commerce_platform/payment_method.rb index 22f5acf1..07ab2c2c 100644 --- a/app/models/solidus_paypal_commerce_platform/payment_method.rb +++ b/app/models/solidus_paypal_commerce_platform/payment_method.rb @@ -12,13 +12,17 @@ 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 :venmo_control, :string, default: 'enabled' + preference :venmo_standalone, :paypal_select, default: 'disabled' preference :force_buyer_country, :string - validates :preferred_venmo_control, inclusion: { - in: %w[enforced enabled disabled], - message: "must be 'enforced', 'enabled' or 'disabled'." - } + validates :preferred_paypal_button_color, exclusion: { + in: %w[gold], + message: "cannot be 'gold' when Venmo standalone is enabled." + }, if: :venmo_standalone_enabled? + + def venmo_standalone_enabled? + options[:venmo_standalone] != 'disabled' + end def partial_name "paypal_commerce_platform" @@ -80,8 +84,7 @@ def javascript_sdk_url(order: nil, currency: nil) } parameters[:shipping_preference] = 'NO_SHIPPING' if step_names.exclude? 'delivery' - parameters['enable-funding'] = 'venmo' if options[:venmo_control] == 'enforced' - parameters['disable-funding'] = 'venmo' if options[:venmo_control] == 'disabled' + parameters['enable-funding'] = 'venmo' if venmo_standalone_enabled? if !Rails.env.production? && options[:force_buyer_country].present? parameters['buyer-country'] = options[:force_buyer_country] diff --git a/lib/solidus_paypal_commerce_platform/testing_support/factories.rb b/lib/solidus_paypal_commerce_platform/testing_support/factories.rb index 6918a610..708573a5 100644 --- a/lib/solidus_paypal_commerce_platform/testing_support/factories.rb +++ b/lib/solidus_paypal_commerce_platform/testing_support/factories.rb @@ -7,7 +7,8 @@ preferences { { client_id: SecureRandom.hex(8), - client_secret: SecureRandom.hex(10) + client_secret: SecureRandom.hex(10), + venmo_standalone: 'disabled' } } end diff --git a/lib/views/frontend/spree/checkout/payment/_paypal_commerce_platform.html.erb b/lib/views/frontend/spree/checkout/payment/_paypal_commerce_platform.html.erb index b9fdbb38..893351c2 100644 --- a/lib/views/frontend/spree/checkout/payment/_paypal_commerce_platform.html.erb +++ b/lib/views/frontend/spree/checkout/payment/_paypal_commerce_platform.html.erb @@ -15,4 +15,12 @@ }) +<% if payment_method.venmo_standalone_enabled? %> + +<% end %> +
diff --git a/spec/models/solidus_paypal_commerce_platform/payment_method_spec.rb b/spec/models/solidus_paypal_commerce_platform/payment_method_spec.rb index 2eb546c5..f6a88c48 100644 --- a/spec/models/solidus_paypal_commerce_platform/payment_method_spec.rb +++ b/spec/models/solidus_paypal_commerce_platform/payment_method_spec.rb @@ -15,6 +15,21 @@ def Struct(data) # rubocop:disable Naming/MethodName before { allow_any_instance_of(PayPal::PayPalHttpClient).to receive(:execute) { response } } + describe 'preferences' do + context 'with paypal_button_color' do + before do + paypal_payment_method.preferences.update(paypal_button_color: 'gold') + paypal_payment_method.save + end + + it 'cannot be gold when Venmo standalone is enabled' do + expect(paypal_payment_method).to be_valid + paypal_payment_method.preferences.update(venmo_standalone: 'enabled') + expect(paypal_payment_method).to be_invalid + end + end + end + describe "#purchase" do let(:result) { Struct(purchase_units: [Struct(payments: payments)]) } let(:payments) { Struct(captures: [Struct(id: SecureRandom.hex(4))]) } @@ -114,64 +129,20 @@ def Struct(data) # rubocop:disable Naming/MethodName end end - describe 'preferences' do - context 'with venmo_control' do - it 'is "default" by default' do - expect(paypal_payment_method.preferred_venmo_control).to eq('enabled') - end - - it 'only allows "enforced", "enabled" and "disabled" as values', :aggregate_failures do - expect(paypal_payment_method).to be_valid - valid_values = %w[enforced enabled disabled] - invalid_values = %w[foo bar] - - valid_values.each do |value| - paypal_payment_method.preferred_venmo_control = value - expect(paypal_payment_method).to be_valid - end - - invalid_values.each do |value| - paypal_payment_method.preferred_venmo_control = value - expect(paypal_payment_method).to be_invalid - end - end - end - end - - context 'when venmo_control is "enforced"' do - before { paypal_payment_method.preferences.update(venmo_control: 'enforced') } + context 'when venmo_standalone is "enabled"' do + before { paypal_payment_method.preferences.update(venmo_standalone: 'enabled') } it 'includes "enable-funding=venmo" as a parameter' do expect(url.query.split('&')).to include('enable-funding=venmo') end - - it 'does not include the "disable-funding" parameter' do - expect(url.query.split('&')).not_to include(match 'disable-funding') - end end - context 'when venmo_control is "enabled"' do - before { paypal_payment_method.preferences.update(venmo_control: 'enabled') } - - it 'include the "enable-funding" parameter' do - expect(url.query.split('&')).not_to include(match 'enable-funding') - end - - it 'does not include the "disable-funding" parameter' do - expect(url.query.split('&')).not_to include(match 'disable-funding') - end - end - - context 'when venmo_control is "disabled"' do - before { paypal_payment_method.preferences.update(venmo_control: 'disabled') } + context 'when venmo_standalone is "disabled"' do + before { paypal_payment_method.preferences.update(venmo_standalone: 'disabled') } it 'does not include the "enable-funding" parameter' do expect(url.query.split('&')).not_to include(match 'enable-funding') end - - it 'includes "disable-funding=venmo" as a parameter' do - expect(url.query.split('&')).to include('disable-funding=venmo') - end end context 'when force_buyer_country is an empty string' do From 9af876102831b84dd75065136da3e041dcbe060e Mon Sep 17 00:00:00 2001 From: Ryan Woods Date: Mon, 20 Dec 2021 14:24:39 +0100 Subject: [PATCH 2/3] Add 'render only standalone' option to venmo_standalone_control preference --- README.md | 1 + .../button_options_helper.rb | 2 +- .../payment_method.rb | 4 ++++ .../payment/_paypal_commerce_platform.html.erb | 11 ++++++++--- .../payment_method_spec.rb | 10 ++++++++++ 5 files changed, 24 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index c1e19b19..a3c22125 100644 --- a/README.md +++ b/README.md @@ -126,6 +126,7 @@ Venmo is currently available to US merchants and buyers. There are also other [p If the transaction supports Venmo and it is enabled by the following, then a button should appear for it on checkout payment page. Note, Venmo cannot currently be rendered on the product or cart pages. Set the PaypalCommercePlatform `PaymentMethod` `venmo_standalone` preference to: +- `render only standalone`, show Venmo on the payment page and do not show the other funding sources (i.e. PayPal, Credit); or - `enabled`, show Venmo on the payment page; or - `disabled` (default), do not show the Venmo button. diff --git a/app/helpers/solidus_paypal_commerce_platform/button_options_helper.rb b/app/helpers/solidus_paypal_commerce_platform/button_options_helper.rb index f2112602..647c24b8 100644 --- a/app/helpers/solidus_paypal_commerce_platform/button_options_helper.rb +++ b/app/helpers/solidus_paypal_commerce_platform/button_options_helper.rb @@ -19,7 +19,7 @@ def preferred_paypal_button_layout_options end def preferred_venmo_standalone_options - [['Enabled', 'Enabled'], ['Disabled', 'disabled']] + [['Render only standalone', 'render only standalone'], ['Enabled', 'Enabled'], ['Disabled', 'disabled']] end end end diff --git a/app/models/solidus_paypal_commerce_platform/payment_method.rb b/app/models/solidus_paypal_commerce_platform/payment_method.rb index 07ab2c2c..584f277c 100644 --- a/app/models/solidus_paypal_commerce_platform/payment_method.rb +++ b/app/models/solidus_paypal_commerce_platform/payment_method.rb @@ -24,6 +24,10 @@ def venmo_standalone_enabled? options[:venmo_standalone] != 'disabled' end + def render_only_venmo_standalone? + options[:venmo_standalone] == 'render only standalone' + end + def partial_name "paypal_commerce_platform" end diff --git a/lib/views/frontend/spree/checkout/payment/_paypal_commerce_platform.html.erb b/lib/views/frontend/spree/checkout/payment/_paypal_commerce_platform.html.erb index 893351c2..d03ce922 100644 --- a/lib/views/frontend/spree/checkout/payment/_paypal_commerce_platform.html.erb +++ b/lib/views/frontend/spree/checkout/payment/_paypal_commerce_platform.html.erb @@ -10,11 +10,16 @@ +<% unless payment_method.render_only_venmo_standalone? %> + +<% end %> + <% if payment_method.venmo_standalone_enabled? %>