HEX
Server: Apache/2.4.41 (Ubuntu)
System: Linux ip-172-31-42-149 5.15.0-1084-aws #91~20.04.1-Ubuntu SMP Fri May 2 07:00:04 UTC 2025 aarch64
User: ubuntu (1000)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
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);
    }
}