Skip to content

Commit

Permalink
Adding support for redirect resolvers in bank transfer process
Browse files Browse the repository at this point in the history
The default return handler was moved and flow is redirected
via redirect resolver to `BankTransferPresenter`.
CRM users can now register their own redirect resolvers and
direct bank transfer users to their own landing pages.

remp/crm#1095
  • Loading branch information
rootpd committed Feb 24, 2020
1 parent 5584a0b commit 297efa6
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 19 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,22 @@ services:
- registerRedirectResolver(Crm\FooModule\Model\FooPaymentCompleteRedirectResolver(), 400)
```

#### Bank Transfer landing page

When `bank_transfer` is used, user isn't redirected to external payment gateway provider, but CRM displays payment information that user should use to complete the payment manually.

By default, this is handled by [`BankTransferPresenter`](src/presenters/BankTransferPresenter.php), but you can use your own custom screen with transfer information by using your own redirect resolver instead of bank transfers' [default resolver](src/model/SuccessPageResolver/BankTransferPaymentCompleteRedirectResolver.php).

Create the resolver and register it with priority >10 in your `config.neon`.

```neon
services:
# ...
paymentCompleteRedirect:
setup:
- registerRedirectResolver(Crm\FooModule\BankTransferPaymentCompleteRedirectResolver(), 50)
```

## Bank email processing

Sometimes user doesn't finish the whole payment process and quits after the payment was made but before returning to the CRM
Expand Down
4 changes: 3 additions & 1 deletion src/config/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ services:
class: Crm\PaymentsModule\Components\SubscribersWithPaymentWidgetFactory

paymentCompleteRedirect:
class: Crm\PaymentsModule\Model\PaymentCompleteRedirectManager
class: Crm\PaymentsModule\Model\PaymentCompleteRedirectManager
setup:
- registerRedirectResolver(Crm\PaymentsModule\BankTransferPaymentCompleteRedirectResolver(), 10)

paymentInvoiceProvider:
class: Crm\PaymentsModule\DataProvider\PaymentInvoiceProviderManager
Expand Down
11 changes: 4 additions & 7 deletions src/model/Gateway/BankTransfer.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,15 @@ protected function initialize()

public function begin($payment)
{
$url = $this->linkGenerator->link(
'Payments:Return:bankTransfer',
[
'VS' => $payment->variable_symbol,
]
);
$url = $this->generateReturnUrl($payment, [
'VS' => $payment->variable_symbol,
]);
$this->httpResponse->redirect($url);
exit();
}

public function complete($payment): ?bool
{
return true;
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Crm\PaymentsModule;

use Crm\PaymentsModule\Model\PaymentCompleteRedirectResolver;
use Nette\Database\Table\ActiveRow;

class BankTransferPaymentCompleteRedirectResolver implements PaymentCompleteRedirectResolver
{
public function wantsToRedirect(?ActiveRow $payment, string $status): bool
{
if ($payment && $payment->payment_gateway->code === 'bank_transfer') {
return true;
}
return false;
}

public function redirectArgs(?ActiveRow $payment, string $status): array
{
if (!$payment || $payment->payment_gateway->code !== 'bank_transfer') {
throw new \Exception('unhandled status when requesting redirectArgs (did you check wantsToRedirect first?): ' . $status);
}

return [
':Payments:BankTransfer:info',
[
'id' => $payment->variable_symbol,
],
];
}
}
19 changes: 19 additions & 0 deletions src/model/SuccessPageResolver/PaymentCompleteRedirectResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,33 @@

interface PaymentCompleteRedirectResolver
{
/**
* PAID represents valid paid payment, that ended up successfully.
*/
const PAID = 'paid';

/**
* NOT_SETTLED represents state where payment will most likely be confirmed in the near future,
* but the gateway provider didn't make the final confirmation yet.
*/
const NOT_SETTLED = 'not_settled';

/**
* CANCELLED is used when user intentionally cancels the payment process on the payment gateway provider site.
*/
const CANCELLED = 'cancelled';

/**
* ERROR is used if there's any kind of unexpected error during payment processing.
*/
const ERROR = 'error';

/**
* FORM is used for payments, that didn't really go through external gateway and will be confirmed offline later
* (e.g. manually, by importing bank statements or by reading notification emails from gateway provider)
*/
const FORM = 'form';

/**
* shouldRedirect decides whether the implementation should be used to redirect user after successful payment to
* custom location based on arbitrary condition using $payment instance.
Expand Down
28 changes: 28 additions & 0 deletions src/presenters/BankTransferPresenter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Crm\PaymentsModule\Presenters;

use Crm\ApplicationModule\Presenters\FrontendPresenter;
use Crm\PaymentsModule\Repository\PaymentsRepository;
use Nette\Application\BadRequestException;

class BankTransferPresenter extends FrontendPresenter
{
/** @var PaymentsRepository @inject */
public $paymentsRepository;

public function renderInfo($id)
{
$payment = $this->paymentsRepository->findLastByVS($id);
if (!$payment) {
throw new BadRequestException('Payment with variable symbol not found: ' . $id);
}

$this->template->bankNumber = $this->applicationConfig->get('supplier_bank_account_number');
$this->template->bankIban = $this->applicationConfig->get('supplier_iban');
$this->template->bankSwift = $this->applicationConfig->get('supplier_swift');

$this->template->payment = $payment;
$this->template->note = 'VS' . $payment->variable_symbol;
}
}
13 changes: 2 additions & 11 deletions src/presenters/ReturnPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,6 @@ public function renderGateway($gatewayCode)
return $this->returnPayment($gatewayCode);
}

public function renderBankTransfer()
{
$this->template->bankNumber = $this->applicationConfig->get('supplier_bank_account_number');
$this->template->bankIban = $this->applicationConfig->get('supplier_iban');
$this->template->bankSwift = $this->applicationConfig->get('supplier_swift');

$payment = $this->getPayment();
$this->template->payment = $payment;
$this->template->note = 'VS' . $payment->variable_symbol;
}

public function renderGoPay($id)
{
if ($id === null) {
Expand Down Expand Up @@ -224,6 +213,8 @@ private function processPayment($payment)
$this->resolveRedirect($payment, PaymentCompleteRedirectResolver::ERROR);
}
});

$this->resolveRedirect($payment, PaymentCompleteRedirectResolver::FORM);
}

public function getPayment()
Expand Down
File renamed without changes.

0 comments on commit 297efa6

Please sign in to comment.