From a6e44bd368b566bee7b5000311a7c36a93f0ec2e Mon Sep 17 00:00:00 2001 From: Flavio Auciello Date: Fri, 9 Oct 2020 14:53:54 +0200 Subject: [PATCH 1/2] Move `paypal_button_preference` helper method Ref. https://github.com/solidusio/solidus_paypal_braintree/issues/251 Using `Spree::CheckoutController.helper` and `Spree::OrdersController.helper` in `config/initializers/braintree.rb` to include helper modules does not work: at `checkout/payement` step an exception is raised ``` ActionView::Template::Error (undefined method `paypal_button_preference`) ``` This commit move `paypal_button_preferences` (introduced with [1]) in the already present `SolidusPaypalBraintree::BraintreeCheckoutHelper` module, and adds the same helper method to the `Spree::OrdersController` (in the same way done here [2]) to enable the button also in the cart page. [1] https://github.com/solidusio/solidus_paypal_braintree/pull/236 [2] https://github.com/solidusio/solidus_paypal_braintree/pull/232/commits/28d0de963a5e8bf3f8f539dd8bb30d80717b4154 --- .../orders_controller_decorator.rb | 10 ++++++++++ app/helpers/braintree_checkout_helper.rb | 5 ----- .../braintree_checkout_helper.rb | 3 +++ config/initializers/braintree.rb | 5 ----- 4 files changed, 13 insertions(+), 10 deletions(-) create mode 100644 app/controllers/solidus_paypal_braintree/orders_controller_decorator.rb delete mode 100644 app/helpers/braintree_checkout_helper.rb diff --git a/app/controllers/solidus_paypal_braintree/orders_controller_decorator.rb b/app/controllers/solidus_paypal_braintree/orders_controller_decorator.rb new file mode 100644 index 00000000..67791bf3 --- /dev/null +++ b/app/controllers/solidus_paypal_braintree/orders_controller_decorator.rb @@ -0,0 +1,10 @@ +module SolidusPaypalBraintree + module OrdersControllerDecorator + + def self.prepended(base) + base.helper ::SolidusPaypalBraintree::BraintreeCheckoutHelper + end + + ::Spree::OrdersController.prepend(self) + end +end diff --git a/app/helpers/braintree_checkout_helper.rb b/app/helpers/braintree_checkout_helper.rb deleted file mode 100644 index d481dc58..00000000 --- a/app/helpers/braintree_checkout_helper.rb +++ /dev/null @@ -1,5 +0,0 @@ -module BraintreeCheckoutHelper - def paypal_button_preference(key, store:) - store.braintree_configuration.preferences[key] - end -end diff --git a/app/helpers/solidus_paypal_braintree/braintree_checkout_helper.rb b/app/helpers/solidus_paypal_braintree/braintree_checkout_helper.rb index 52dd21ab..12535d9b 100644 --- a/app/helpers/solidus_paypal_braintree/braintree_checkout_helper.rb +++ b/app/helpers/solidus_paypal_braintree/braintree_checkout_helper.rb @@ -40,5 +40,8 @@ def braintree_3ds_options_for(order) } end + def paypal_button_preference(key, store:) + store.braintree_configuration.preferences[key] + end end end diff --git a/config/initializers/braintree.rb b/config/initializers/braintree.rb index 1f370736..3cd773ae 100644 --- a/config/initializers/braintree.rb +++ b/config/initializers/braintree.rb @@ -1,8 +1,3 @@ if SolidusSupport.backend_available? Spree::Admin::PaymentsController.helper :braintree_admin end - -if SolidusSupport.frontend_available? - Spree::CheckoutController.helper :braintree_checkout - Spree::OrdersController.helper :braintree_checkout -end From 69923ed9168474a9511f11e8df3e7e292e98ec97 Mon Sep 17 00:00:00 2001 From: Flavio Auciello Date: Fri, 9 Oct 2020 18:48:27 +0200 Subject: [PATCH 2/2] Inject admin helper methods via ad-hoc decorator As for the frontend helper methods in the previous commit, using `Spree::Admin::PaymentsController.helper` in `config/initializers/braintree.rb` does not work: at `checkout/payement` step an exception is raised. ``` ActionView::Template::Error (undefined method `braintree_transaction_link`) ``` This commit firstly move the helper module in the appropriate namespace (i.e. `SolidusPaypalBraintree`), then inject into the controller via a custom decorator. This way the exception is no longer raised. --- .../admin_payments_controller_decorator.rb | 10 ++++++++++ app/helpers/braintree_admin_helper.rb | 18 ----------------- .../braintree_admin_helper.rb | 20 +++++++++++++++++++ config/initializers/braintree.rb | 3 --- spec/helpers/braintree_admin_helper_spec.rb | 2 +- 5 files changed, 31 insertions(+), 22 deletions(-) create mode 100644 app/controllers/solidus_paypal_braintree/admin_payments_controller_decorator.rb delete mode 100644 app/helpers/braintree_admin_helper.rb create mode 100644 app/helpers/solidus_paypal_braintree/braintree_admin_helper.rb delete mode 100644 config/initializers/braintree.rb diff --git a/app/controllers/solidus_paypal_braintree/admin_payments_controller_decorator.rb b/app/controllers/solidus_paypal_braintree/admin_payments_controller_decorator.rb new file mode 100644 index 00000000..eefe2392 --- /dev/null +++ b/app/controllers/solidus_paypal_braintree/admin_payments_controller_decorator.rb @@ -0,0 +1,10 @@ +module SolidusPaypalBraintree + module AdminPaymentsControllerDecorator + + def self.prepended(base) + base.helper ::SolidusPaypalBraintree::BraintreeAdminHelper + end + + ::Spree::Admin::PaymentsController.prepend(self) + end +end diff --git a/app/helpers/braintree_admin_helper.rb b/app/helpers/braintree_admin_helper.rb deleted file mode 100644 index 51e57353..00000000 --- a/app/helpers/braintree_admin_helper.rb +++ /dev/null @@ -1,18 +0,0 @@ -module BraintreeAdminHelper - # Returns a link to the Braintree web UI for the given Braintree payment - def braintree_transaction_link(payment) - environment = payment.payment_method.preferred_environment == 'sandbox' ? 'sandbox' : 'www' - merchant_id = payment.payment_method.preferred_merchant_id - response_code = payment.response_code - - return unless response_code.present? - return response_code unless merchant_id.present? - - link_to( - response_code, - "https://#{environment}.braintreegateway.com/merchants/#{merchant_id}/transactions/#{response_code}", - title: 'Show payment on Braintree', - target: '_blank' - ) - end -end diff --git a/app/helpers/solidus_paypal_braintree/braintree_admin_helper.rb b/app/helpers/solidus_paypal_braintree/braintree_admin_helper.rb new file mode 100644 index 00000000..7063c5b0 --- /dev/null +++ b/app/helpers/solidus_paypal_braintree/braintree_admin_helper.rb @@ -0,0 +1,20 @@ +module SolidusPaypalBraintree + module BraintreeAdminHelper + # Returns a link to the Braintree web UI for the given Braintree payment + def braintree_transaction_link(payment) + environment = payment.payment_method.preferred_environment == 'sandbox' ? 'sandbox' : 'www' + merchant_id = payment.payment_method.preferred_merchant_id + response_code = payment.response_code + + return unless response_code.present? + return response_code unless merchant_id.present? + + link_to( + response_code, + "https://#{environment}.braintreegateway.com/merchants/#{merchant_id}/transactions/#{response_code}", + title: 'Show payment on Braintree', + target: '_blank' + ) + end + end +end diff --git a/config/initializers/braintree.rb b/config/initializers/braintree.rb deleted file mode 100644 index 3cd773ae..00000000 --- a/config/initializers/braintree.rb +++ /dev/null @@ -1,3 +0,0 @@ -if SolidusSupport.backend_available? - Spree::Admin::PaymentsController.helper :braintree_admin -end diff --git a/spec/helpers/braintree_admin_helper_spec.rb b/spec/helpers/braintree_admin_helper_spec.rb index 4e769285..19798593 100644 --- a/spec/helpers/braintree_admin_helper_spec.rb +++ b/spec/helpers/braintree_admin_helper_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -RSpec.describe BraintreeAdminHelper do +RSpec.describe SolidusPaypalBraintree::BraintreeAdminHelper do describe '#braintree_transaction_link' do let(:payment_method) { create_gateway } let(:payment) do