From 6b2126710ff92db90916eb18abe6d5c390547964 Mon Sep 17 00:00:00 2001 From: Alberto Vena Date: Tue, 17 Jan 2023 12:05:25 +0100 Subject: [PATCH] Allow to use the auto_capture global preference In the Payment Method edit UI, we can choose the autocapture preference we want to be applied for their payments. The choices are: "Yes", "No", "Use App Default". When the last option is picked, the value for this boolean is set to nil and we were not considering this last option through the codebase. Luckily, a convenient method in Solidus [1] allows us to return the App Default value. [1]: https://github.com/solidusio/solidus/blob/929035c957f75ebd7ae745af69bf2a93a3727a87/core/app/models/spree/payment_method.rb#L156-L158 --- .../paypal_orders_controller.rb | 2 +- .../payment_method.rb | 2 +- .../payment_method_spec.rb | 25 +++++++++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/app/controllers/solidus_paypal_commerce_platform/paypal_orders_controller.rb b/app/controllers/solidus_paypal_commerce_platform/paypal_orders_controller.rb index bb9147a3..9ee112e1 100644 --- a/app/controllers/solidus_paypal_commerce_platform/paypal_orders_controller.rb +++ b/app/controllers/solidus_paypal_commerce_platform/paypal_orders_controller.rb @@ -7,7 +7,7 @@ class PaypalOrdersController < ::Spree::Api::BaseController def show authorize! :show, @order, order_token - order_request = @payment_method.gateway.create_order(@order, @payment_method.auto_capture) + order_request = @payment_method.gateway.create_order(@order, @payment_method.auto_capture?) render json: order_request, status: order_request.status_code end diff --git a/app/models/solidus_paypal_commerce_platform/payment_method.rb b/app/models/solidus_paypal_commerce_platform/payment_method.rb index 45bc55e1..9964b353 100644 --- a/app/models/solidus_paypal_commerce_platform/payment_method.rb +++ b/app/models/solidus_paypal_commerce_platform/payment_method.rb @@ -83,7 +83,7 @@ def javascript_sdk_url(order: nil, currency: nil) parameters = { 'client-id': client_id, - intent: auto_capture ? "capture" : "authorize", + intent: auto_capture? ? "capture" : "authorize", commit: commit_immediately ? "false" : "true", components: options[:display_credit_messaging] ? "buttons,messages" : "buttons", currency: currency, 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 17d4149c..dfd38195 100644 --- a/spec/models/solidus_paypal_commerce_platform/payment_method_spec.rb +++ b/spec/models/solidus_paypal_commerce_platform/payment_method_spec.rb @@ -153,6 +153,31 @@ def Struct(data) # rubocop:disable Naming/MethodName end end + context 'when autocapture value is true' do + it 'sets the intent to capture' do + paypal_payment_method.update(auto_capture: true) + + expect(url.query.split("&")).to include("intent=capture") + end + end + + context 'when autocapture value is false' do + it 'sets the intent to capture' do + paypal_payment_method.update(auto_capture: false) + + expect(url.query.split("&")).to include("intent=authorize") + end + end + + context 'when autocapture value is nil' do + it 'sets the intent to the global auto_capture value' do + paypal_payment_method.update(auto_capture: nil) + stub_spree_preferences(auto_capture: true) + + expect(url.query.split("&")).to include("intent=capture") + end + end + context 'when messaging is turned on' do it 'includes messaging component' do paypal_payment_method.preferences.update(display_credit_messaging: true)