File: /var/www/vhost/disk-apps/demo.sports-crowd.com/app/Core/Payment/Methods/Onepay/OnepayStrategy.php
<?php
declare(strict_types=1);
namespace App\Core\Payment\Methods\Onepay;
use App\Core\Payment\Entities\Payment;
use App\Core\Payment\Entities\PaymentIntentResponse;
use App\Core\Payment\Entities\PaymentRetrieveResponse;
use App\Core\Payment\Methods\Onepay\OnepayClient;
use App\Core\Payment\PaymentMethodInterface;
use App\Core\Payment\PaymentStatusEnum;
use Illuminate\Http\Request;
class OnepayStrategy implements PaymentMethodInterface
{
const PAYMENT_STATUS = [
"approved" => PaymentStatusEnum::CONFIRMED,
"failed" => PaymentStatusEnum::DECLINED,
"pending" => PaymentStatusEnum::PENDING
];
private $parameters;
private $onepayClient;
public function __construct(
$parameters
) {
$this->parameters = $parameters;
$this->onepayClient = new OnepayClient($this->parameters);
}
public function pay(Payment $payment): PaymentIntentResponse
{
$onepayPaymentIntent = $this->onepayClient->createPayment(
$this->buildPaymentIntent($payment)
);
$paymentIntentResponse = new PaymentIntentResponse(
PaymentIntentResponse::ACTION_REDIRECT,
$onepayPaymentIntent->id
);
$paymentIntentResponse->setRedirectUrl($onepayPaymentIntent->payment_link);
return $paymentIntentResponse;
}
public function retrieve(Payment $payment): PaymentRetrieveResponse
{
try {
$onepayRetrieve = $this->onepayClient->retrieve($payment);
$retrieveResponse = new PaymentRetrieveResponse(
$payment->paymentGatewayTxId(),
self::PAYMENT_STATUS[$onepayRetrieve->status],
"Transacción procesada correctamente"
);
$retrieveResponse->setRawData($onepayRetrieve);
return $retrieveResponse;
} catch (\Throwable $th) {
return new PaymentRetrieveResponse(
$payment->paymentGatewayTxId(),
PaymentStatusEnum::PENDING,
$th->getMessage()
);
}
}
private function buildPaymentIntent(Payment $payment)
{
return [
'title' => 'Orden de pago',
'currency' => $this->parameters->currency,
'amount' => $payment->amount()->total(),
'external_id' => $payment->reference(),
'redirect_url' => $this->buildConfirmUrl($payment),
'allows' => [
'cards' => true,
'accounts' => true,
'card_extra' => false,
'realtime' => false,
'pse' => true,
'transfiya' => false
]
];
}
private function buildConfirmUrl($payment)
{
$url = config('app.url') . '/store/payment';
$queryString = [
'paymentGatewayId' => $this->parameters->id,
'reference' => $payment->reference()
];
$queryString = array_merge($queryString, $this->parameters->extraConfirmUrlData);
return Request::create($url)->fullUrlWithQuery($queryString);
}
}