ldc-boutique-cnc/app/Lib/Payments/PaymentAdapter.php
2023-04-25 21:52:27 +02:00

117 lines
3.9 KiB
PHP

<?php
namespace App\Lib\Payments;
use App\Models\Customer\Customer;
use App\Models\Payment\Transaction;
use App\Models\Shop\Order;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\URL;
use Symfony\Component\HttpFoundation\Response;
abstract class PaymentAdapter
{
// * - Checkout system
/**
*
* @param \Closure $action
* @param \Closure(): void $success
* @param \Closure(\Exception): void $failure
* @return \Illuminate\Http\Response
* @phpcs:disable Squiz.Commenting.FunctionComment.IncorrectTypeHint
*/
protected function wrapAction(\Closure $action, \Closure $success, \Closure $failure): Response
{
// phpcs:enable
try {
$out = DB::transaction(function () use ($action) {
return $action();
});
$success();
return $out;
} 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(
'order-tunnel.checkout',
\now()->addMinutes(1),
$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 \Symfony\Component\HttpFoundation\Response
*/
abstract public function checkout(
Customer $customer,
Order $order,
Transaction $transaction,
\Closure $succeed,
\Closure $failed
): Response;
// 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
* @param object $items
* @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,
object $items
): Transaction;
}