File: /var/www/vhost/disk-apps/alq-cali.bikenow.co/app/Core/Payment/Methods/Pagoplux/PagopluxStrategy.php
<?php
declare(strict_types=1);
namespace App\Core\Payment\Methods\Pagoplux;
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 GuzzleHttp\Client;
use Illuminate\Http\Request;
class PagopluxStrategy implements PaymentMethodInterface
{
const PAYMENT_STATUS = [
"success" => PaymentStatusEnum::CONFIRMED,
"failed" => PaymentStatusEnum::DECLINED,
"in_progress" => PaymentStatusEnum::PENDING
];
private $parameters;
public function __construct(
$parameters
) {
$this->parameters = $parameters;
}
public function pay(Payment $payment): PaymentIntentResponse
{
$paymentIntentResponse = new PaymentIntentResponse(
PaymentIntentResponse::ACTION_SHOW,
$payment->paymentGatewayTxId()
);
$data = [
'is_productive' => $this->parameters->is_productive,
'payboxRemail' => $this->parameters->user,
'payboxRename' => config('app.name'),
'payment' => $payment,
'confirm_url' => config('app.url') .
'/store/payment?paymentGatewayId=' .
$payment->paymentGatewayId() .
'&reference=' . $payment->reference()
];
$paymentIntentResponse->setView('pagoplux.webcheckout');
$paymentIntentResponse->setData($data);
return $paymentIntentResponse;
}
public function retrieve(Payment $payment): PaymentRetrieveResponse
{
try {
$baseUrl = "https://apipre.pagoplux.com";
if ($this->parameters->is_productive) {
$baseUrl = "https://api.pagoplux.com";
}
$baseUrl .= '/intv1/integrations/getTransactionByIdStateResource';
$queryString = [
'consumptionCode' => $payment->reference(),
'idEstablishment' => $this->parameters->account_id,
];
$url = Request::create($baseUrl)->fullUrlWithQuery($queryString);
$client = new Client();
$response = $client->get(
$url,
[
'auth' => [
$this->parameters->client_id,
$this->parameters->api_key,
]
]
);
$body = $response->getBody()->getContents();
$data = json_decode($body);
foreach ($data->detail->respuest as $match) {
if ($match->valor_catalogo_estado == 'PAG') {
$payment->setPaymentGatewayTxId($match->id_transaccion);
$isConfirmed = true;
}
}
$retrieveResponse = new PaymentRetrieveResponse(
$payment->paymentGatewayTxId(),
$isConfirmed ? PaymentStatusEnum::CONFIRMED : PaymentStatusEnum::PENDING,
"Transacción procesada correctamente"
);
$retrieveResponse->setRawData($data);
return $retrieveResponse;
} catch (\Throwable $th) {
return new PaymentRetrieveResponse(
$payment->paymentGatewayTxId(),
PaymentStatusEnum::PENDING,
$th->getMessage()
);
}
}
}