121 lines
4 KiB
PHP
121 lines
4 KiB
PHP
<?php
|
|
|
|
namespace App\Lib\Payments;
|
|
|
|
use App\Lib\Helpers\ToolboxHelper;
|
|
use App\Models\Customer\Customer;
|
|
use App\Models\Payment\Transaction;
|
|
use App\Models\Shop\Order;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\URL;
|
|
|
|
abstract class PaymentAdapter
|
|
{
|
|
/**
|
|
* Get an unique transaction reference
|
|
* This can be overriden by any adapter.
|
|
*
|
|
* @return string
|
|
*/
|
|
public static function genUniqueTransactionReference(): string
|
|
{
|
|
return ToolboxHelper::getUniqueReference(Transaction::class);
|
|
}
|
|
|
|
// * - Checkout system
|
|
|
|
/**
|
|
*
|
|
* @param \Closure $action
|
|
* @param \Closure(\Exception): void $failure
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
* @phpcs:disable Squiz.Commenting.FunctionComment.IncorrectTypeHint
|
|
*/
|
|
protected function wrapAction(\Closure $action, \Closure $failure): RedirectResponse
|
|
{
|
|
// phpcs:enable
|
|
try {
|
|
return DB::transaction(function () use ($action) {
|
|
return $action();
|
|
});
|
|
} catch (\Exception $e) {
|
|
// Sentry report failed action.
|
|
\report($e);
|
|
return $failure($e);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get success route
|
|
*
|
|
* @param \App\Models\Shop\Transaction $transaction
|
|
* @param array $parameters
|
|
* @return string
|
|
*/
|
|
protected function successRoute(Transaction $transaction, array $parameters = []): string
|
|
{
|
|
$parameters['transaction'] = $transaction;
|
|
return \url(URL::temporarySignedRoute(
|
|
'verification.verify',
|
|
\now()->addMinutes(3),
|
|
$parameters,
|
|
false
|
|
));
|
|
}
|
|
|
|
/**
|
|
* Get cancel route
|
|
*
|
|
* @param \App\Models\Shop\Order $order
|
|
* @param array $parameters
|
|
* @return string
|
|
*/
|
|
protected function cancelRoute(Order $order, array $parameters = []): string
|
|
{
|
|
$parameters['order'] = $order;
|
|
return route('order-tunnel.review', $order);
|
|
}
|
|
|
|
/**
|
|
* Generate the checkout reponse to initiate a transaction.
|
|
*
|
|
* @phpcs:disable Squiz.Commenting.FunctionComment.IncorrectTypeHint
|
|
* @phpcs:disable Generic.Files.LineLength.TooLong
|
|
* @param \App\Models\Customer\Customer $customer
|
|
* @param \App\Models\Shop\Order $order
|
|
* @param \App\Models\Payment\Transaction $transaction
|
|
* @param \Closure(\App\Models\Customer\Customer,\App\Models\Shop\Order,\App\Models\Payment\Transaction): void $succeed
|
|
* @param \Closure(\Exception): void $failed
|
|
* @return \Illuminate\Http\Response|Illuminate\View\View
|
|
*/
|
|
abstract public function checkout(
|
|
Customer $customer,
|
|
Order $order,
|
|
Transaction $transaction,
|
|
\Closure $succeed,
|
|
\Closure $failed
|
|
): RedirectResponse;
|
|
// phpcs:enable
|
|
|
|
/**
|
|
* Check if return from payment method is valid.
|
|
*
|
|
* ! No save operation must be done here !
|
|
*
|
|
* @param \App\Models\Customer\Customer $customer
|
|
* @param \App\Models\Payment\Transaction $transaction
|
|
* @return \App\Models\Payment\Transaction|null
|
|
*/
|
|
abstract public function checkoutIsResponseIsValid(Customer $customer, Transaction $transaction): ?Transaction;
|
|
|
|
/**
|
|
* Do a refund using the same payment method as the transaction.
|
|
*
|
|
* @param \App\Models\Payment\Transaction $transaction
|
|
* @param integer $amount
|
|
* @return \App\Models\Payment\Transaction The new refund transaction.
|
|
* @throws \Illuminate\Validation\ValidationException If doing the refund fails.
|
|
*/
|
|
abstract public function doRefundCustomer(Transaction $transaction, int $amount): Transaction;
|
|
}
|