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/Openpay/OpenpayStrategy.php
<?php

declare(strict_types=1);

namespace App\Core\Payment\Methods\Openpay;

use App\Core\Payment\Entities\Payment;
use App\Core\Payment\Entities\PaymentIntentResponse;
use App\Core\Payment\Entities\PaymentRetrieveResponse;
use App\Core\Payment\PaymentMethodInterface;
use App\Core\Payment\PaymentStatusEnum;
use Openpay\Data\Openpay;

abstract class OpenpayStrategy implements PaymentMethodInterface
{
    const CARD_PAYMENT = 'card';
    const PSE_PAYMENT = 'bank_account';

    const PAYMENT_STATUS = [
        "completed" => PaymentStatusEnum::CONFIRMED,
        "failed" => PaymentStatusEnum::DECLINED,
        "in_progress" => PaymentStatusEnum::PENDING
    ];

    private $parameters;
    private $openpay;

    public function __construct(
        $parameters
    ) {
        $this->parameters = $parameters;
        Openpay::setProductionMode($this->parameters->is_productive);
        $this->openpay = Openpay::getInstance(
            $this->parameters->merchant_id,
            $this->parameters->client_secret,
            'CO',
            $this->getPublicIp()
        );
    }

    abstract protected function paymentMethod();

    public function pay(Payment $payment): PaymentIntentResponse
    {
        $chargeRequest = $this->createCharge($payment);
        $charge = $this->openpay->charges->create($chargeRequest);
        $payment->setPaymentGatewayTxId($charge->id);

        $paymentIntentResponse = new PaymentIntentResponse(
            PaymentIntentResponse::ACTION_REDIRECT,
            $payment->paymentGatewayTxId()
        );

        $paymentIntentResponse->setRedirectUrl($charge->payment_method->url);

        return $paymentIntentResponse;
    }

    public function retrieve(Payment $payment): PaymentRetrieveResponse
    {
        try {
            $charge = $this->openpay->charges->get($payment->paymentGatewayTxId());
            $retrieveResponse = new PaymentRetrieveResponse(
                $charge->id,
                self::PAYMENT_STATUS[$charge->status],
                $charge->status
            );

            $retrieveResponse->setRawData($this->serializeResponse($charge));
            return $retrieveResponse;
        } catch (\Throwable $th) {
            return new PaymentRetrieveResponse(
                $payment->paymentGatewayTxId(),
                PaymentStatusEnum::PENDING,
                $th->getMessage()
            );
        }
    }

    private function createCharge(Payment $payment)
    {
        $customer = [
            'name' => $payment->customer()->name(),
            'last_name' => $payment->customer()->surname(),
            'phone_number' => $payment->customer()->mobile(),
            'email' => $payment->customer()->email()
        ];

        return [
            'method' => $this->paymentMethod(),
            'amount' => $payment->amount()->total(),
            'description' => $payment->description(),
            'customer' => $customer,
            'currency' => 'COP',
            'order_id' => $this->openpayReference($payment->reference()),
            'redirect_url' => config('app.url') .
                '/store/payment?paymentGatewayId=' .
                $payment->paymentGatewayId() .
                '&reference=' . $this->openpayReference($payment->reference()),
            'send_email' => true,
            'confirm' => false
        ];
    }

    private function openpayReference($reference)
    {
        return $reference . '_' . time();
    }

    private function getPublicIp()
    {
        return request()->ip() ?: file_get_contents('http://ipecho.net/plain');
    }

    private function serializeResponse($charge)
    {
        return (object) [
            "id" => $charge->id,
            "status" => $charge->status,
            "authorization" => $charge->authorization,
            "creation_date" => $charge->creation_date,
            "currency" => $charge->currency,
            "operation_type" => $charge->operation_type,
            "transaction_type" => $charge->transaction_type,
            "card" => $charge->card,
            "customer_id" => $charge->customer_id,
            "serializableData" => $charge->serializableData,
        ];
    }
}