From a175bb1c7beda06434a8c832b6a0374db3897e0a Mon Sep 17 00:00:00 2001 From: Peter Dulacka Date: Fri, 29 May 2020 14:02:50 +0200 Subject: [PATCH] Resolving failed recurrent charge from actual failed payment time - There always have to be failed payment to get failed recurrent payment. Removing extra checks and using payment's time to get actual date of attempt (instead of planned date of failed attempt). - Renamed resolving function to be consistent with variable namings. - Minor UI changes to clean things up. remp/crm#1163 --- src/model/RecurrentPaymentsResolver.php | 13 +++++-------- src/templates/Payments/my.latte | 16 +++++++++------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/model/RecurrentPaymentsResolver.php b/src/model/RecurrentPaymentsResolver.php index d9522ac..1bb1a33 100644 --- a/src/model/RecurrentPaymentsResolver.php +++ b/src/model/RecurrentPaymentsResolver.php @@ -8,7 +8,6 @@ use Crm\SubscriptionsModule\Repository\SubscriptionTypesRepository; use Nette\Database\Table\ActiveRow; use Nette\Utils\DateTime; -use Tracy\Debugger; class RecurrentPaymentsResolver { @@ -95,17 +94,15 @@ public function resolveChargeAmount(ActiveRow $recurrentPayment) : float public function resolveFailedRecurrent(ActiveRow $recurrentPayment): ActiveRow { if ($recurrentPayment->state === RecurrentPaymentsRepository::STATE_CHARGE_FAILED) { - $this->lastFailedChargeAt = $recurrentPayment->charge_at; - if (isset($recurrentPayment->payment) && ($nextRecurrent = $this->recurrentPaymentsRepository->recurrent($recurrentPayment->payment))) { - $recurrentPayment = $this->resolveFailedRecurrent($nextRecurrent); - } else { - Debugger::log('Unable to find next payment for failed recurrent ID [' . $recurrentPayment->id . ']', Debugger::ERROR); - } + $this->lastFailedChargeAt = $recurrentPayment->payment->created_at; + + $nextRecurrent = $this->recurrentPaymentsRepository->recurrent($recurrentPayment->payment); + $recurrentPayment = $this->resolveFailedRecurrent($nextRecurrent); } return $recurrentPayment; } - public function getLastChargeFailedDateTime(): DateTime + public function getLastFailedChargeDateTime(): DateTime { if ($this->lastFailedChargeAt === null) { throw new \Exception('No last charge_failed date is set. Did you call `resolveFailedRecurrent()`?'); diff --git a/src/templates/Payments/my.latte b/src/templates/Payments/my.latte index c2b4a2c..1bc432a 100644 --- a/src/templates/Payments/my.latte +++ b/src/templates/Payments/my.latte @@ -42,21 +42,23 @@ {* if recurrent charge for this payment failed, try to locate following recurring profile for this payment *} {if $recurrent->state === \Crm\PaymentsModule\Repository\RecurrentPaymentsRepository::STATE_CHARGE_FAILED} {php $recurrent = $resolver->resolveFailedRecurrent($recurrent); } - {php $lastChargeFailDate = $resolver->getLastChargeFailedDateTime(); } + {php $lastFailedChargeDate = $resolver->getLastFailedChargeDateTime(); } {/if} {switch $recurrent->state} {case \Crm\PaymentsModule\Repository\RecurrentPaymentsRepository::STATE_CHARGED} {_payments.frontend.my.success_recurrent} {case \Crm\PaymentsModule\Repository\RecurrentPaymentsRepository::STATE_ACTIVE} - {if isset($lastChargeFailDate)} + {if isset($lastFailedChargeDate)} {_payments.frontend.my.charge_failed.title} - +
+ + {_payments.frontend.my.charge_failed.last_try}: {$lastFailedChargeDate|userDate}
+ {_payments.frontend.my.charge_failed.next_try}: {$recurrent->charge_at|userDate} +
+
{/if} - {_payments.frontend.my.stop_recurrent} + {_payments.frontend.my.stop_recurrent} {case \Crm\PaymentsModule\Repository\RecurrentPaymentsRepository::STATE_USER_STOP} {_payments.frontend.my.user_stopped}