Skip to content

Commit

Permalink
Resolving failed recurrent charge from actual failed payment time
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
rootpd authored and markoph committed May 29, 2020
1 parent 3b39835 commit a175bb1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
13 changes: 5 additions & 8 deletions src/model/RecurrentPaymentsResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Crm\SubscriptionsModule\Repository\SubscriptionTypesRepository;
use Nette\Database\Table\ActiveRow;
use Nette\Utils\DateTime;
use Tracy\Debugger;

class RecurrentPaymentsResolver
{
Expand Down Expand Up @@ -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()`?');
Expand Down
16 changes: 9 additions & 7 deletions src/templates/Payments/my.latte
Original file line number Diff line number Diff line change
Expand Up @@ -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}
<span class="label label-success">{_payments.frontend.my.success_recurrent}</span>
{case \Crm\PaymentsModule\Repository\RecurrentPaymentsRepository::STATE_ACTIVE}
{if isset($lastChargeFailDate)}
{if isset($lastFailedChargeDate)}
<span class="label label-danger">{_payments.frontend.my.charge_failed.title}</span>
<ul style="font-style: italic; margin-left: 1em;">
<li>{_payments.frontend.my.charge_failed.last_try}: {$lastChargeFailDate}</li>
<li>{_payments.frontend.my.charge_failed.next_try}: {$recurrent->charge_at}</li>
</ul>
<div class="text-muted" style="margin: 1em 0">
<small>
{_payments.frontend.my.charge_failed.last_try}: {$lastFailedChargeDate|userDate}<br>
{_payments.frontend.my.charge_failed.next_try}: {$recurrent->charge_at|userDate}
</small>
</div>
{/if}
<a class="btn btn-sm btn-primary" n:href="recurrentStop $recurrent->id">{_payments.frontend.my.stop_recurrent}</a>
<a class="btn btn-sm btn-default" n:href="recurrentStop $recurrent->id">{_payments.frontend.my.stop_recurrent}</a>
{case \Crm\PaymentsModule\Repository\RecurrentPaymentsRepository::STATE_USER_STOP}
<span class="label label-info">{_payments.frontend.my.user_stopped}</span>
<span n:if="$recurrent->cid && $recurrent->charge_at > new \DateTime()">
Expand Down

0 comments on commit a175bb1

Please sign in to comment.