From 3796b8f698e670ac1459ccb63104ad3592dc7ea4 Mon Sep 17 00:00:00 2001 From: Rene van Lieshout Date: Mon, 13 Apr 2020 10:57:12 +0200 Subject: [PATCH 1/6] Don't create a Mollie customer when the gateway is inactive A client had invalid credentials in the Gateway settings which resulted in a Mollie::RequestError. Let's only create a Mollie customer when the gateway is active. Using an ensure_mollie_customer after_save should also create Mollie customers for users that have been created in the time that is was inactive --- app/models/spree/user_decorator.rb | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/app/models/spree/user_decorator.rb b/app/models/spree/user_decorator.rb index bda25e5..5fad9a7 100644 --- a/app/models/spree/user_decorator.rb +++ b/app/models/spree/user_decorator.rb @@ -1,20 +1,23 @@ module Spree::UserDecorator def self.prepended(base) - base.after_create :create_mollie_customer + base.after_save :ensure_mollie_customer end - def create_mollie_customer + def ensure_mollie_customer + # Don't create a Mollie customer if it has been created before + return if mollie_customer_id.present? + # Don't create Mollie customers if spree_auth_devise is not installed. return unless defined? Spree::User mollie_gateway = Spree::PaymentMethod.find_by_type 'Spree::Gateway::MollieGateway' - return unless mollie_gateway.present? + return unless mollie_gateway.present? && mollie_gateway.active? mollie_customer = mollie_gateway.create_customer(self) update mollie_customer_id: mollie_customer.id end end -Spree.user_class.prepend(Spree::UserDecorator) \ No newline at end of file +Spree.user_class.prepend(Spree::UserDecorator) From 674be36d9c076af1c7b85511c993494f4a9b6781 Mon Sep 17 00:00:00 2001 From: Rene van Lieshout Date: Mon, 10 Aug 2020 07:44:44 +0200 Subject: [PATCH 2/6] Use after_commit to ensure mollie customer and use a safe operator --- app/models/spree/user_decorator.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/app/models/spree/user_decorator.rb b/app/models/spree/user_decorator.rb index 5fad9a7..ca7f748 100644 --- a/app/models/spree/user_decorator.rb +++ b/app/models/spree/user_decorator.rb @@ -1,19 +1,18 @@ module Spree::UserDecorator def self.prepended(base) - base.after_save :ensure_mollie_customer + base.after_commit :ensure_mollie_customer end def ensure_mollie_customer - # Don't create a Mollie customer if it has been created before return if mollie_customer_id.present? # Don't create Mollie customers if spree_auth_devise is not installed. return unless defined? Spree::User mollie_gateway = Spree::PaymentMethod.find_by_type 'Spree::Gateway::MollieGateway' - return unless mollie_gateway.present? && mollie_gateway.active? + return unless mollie_gateway&.active? mollie_customer = mollie_gateway.create_customer(self) update mollie_customer_id: mollie_customer.id From ec80d42b73c50d076447096b21dd766e7cf76965 Mon Sep 17 00:00:00 2001 From: Rene van Lieshout Date: Mon, 10 Aug 2020 09:48:26 +0200 Subject: [PATCH 3/6] Only ensure mollie customer on create and update --- app/models/spree/user_decorator.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/spree/user_decorator.rb b/app/models/spree/user_decorator.rb index ca7f748..32c9f3f 100644 --- a/app/models/spree/user_decorator.rb +++ b/app/models/spree/user_decorator.rb @@ -1,7 +1,7 @@ module Spree::UserDecorator def self.prepended(base) - base.after_commit :ensure_mollie_customer + base.after_commit :ensure_mollie_customer, on: %i[create update] end From fde5616f42095a53b2a041b0f1ba94110600fe93 Mon Sep 17 00:00:00 2001 From: Rene van Lieshout Date: Mon, 10 Aug 2020 10:16:24 +0200 Subject: [PATCH 4/6] Check for mollie customer presence only if gateway is active --- app/models/spree/user_decorator.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/spree/user_decorator.rb b/app/models/spree/user_decorator.rb index 32c9f3f..b9f5689 100644 --- a/app/models/spree/user_decorator.rb +++ b/app/models/spree/user_decorator.rb @@ -6,14 +6,14 @@ def self.prepended(base) def ensure_mollie_customer - return if mollie_customer_id.present? - # Don't create Mollie customers if spree_auth_devise is not installed. return unless defined? Spree::User mollie_gateway = Spree::PaymentMethod.find_by_type 'Spree::Gateway::MollieGateway' return unless mollie_gateway&.active? + return if mollie_customer_id.present? + mollie_customer = mollie_gateway.create_customer(self) update mollie_customer_id: mollie_customer.id end From 78a7fc10fa1b3f11cf66e648a0698bc08782d981 Mon Sep 17 00:00:00 2001 From: Rene van Lieshout Date: Mon, 10 Aug 2020 10:26:13 +0200 Subject: [PATCH 5/6] mollie_customer_id could not exist --- app/models/spree/user_decorator.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/app/models/spree/user_decorator.rb b/app/models/spree/user_decorator.rb index b9f5689..bfcd15e 100644 --- a/app/models/spree/user_decorator.rb +++ b/app/models/spree/user_decorator.rb @@ -4,16 +4,15 @@ def self.prepended(base) base.after_commit :ensure_mollie_customer, on: %i[create update] end - def ensure_mollie_customer + return if mollie_customer_id&.present? + # Don't create Mollie customers if spree_auth_devise is not installed. return unless defined? Spree::User mollie_gateway = Spree::PaymentMethod.find_by_type 'Spree::Gateway::MollieGateway' return unless mollie_gateway&.active? - return if mollie_customer_id.present? - mollie_customer = mollie_gateway.create_customer(self) update mollie_customer_id: mollie_customer.id end From 38d1c7f3c60df93b8e8dc61a1dd28fd0657bc0f0 Mon Sep 17 00:00:00 2001 From: Fabian Oudhaarlem Date: Mon, 10 Aug 2020 10:35:09 +0200 Subject: [PATCH 6/6] Use try instead of safe navigation operator on possible undefined attribute Since the safe navigation operator does raise on undefined methods we should use try instead. --- app/models/spree/user_decorator.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/spree/user_decorator.rb b/app/models/spree/user_decorator.rb index bfcd15e..5965e35 100644 --- a/app/models/spree/user_decorator.rb +++ b/app/models/spree/user_decorator.rb @@ -5,7 +5,7 @@ def self.prepended(base) end def ensure_mollie_customer - return if mollie_customer_id&.present? + return if try(:mollie_customer_id).present? # Don't create Mollie customers if spree_auth_devise is not installed. return unless defined? Spree::User