File: /var/www/vhost/disk-apps/demo.sports-crowd.com/app/Core/Payment/Methods/Webpay/WebpayStrategy.php
<?php
declare(strict_types=1);
namespace App\Core\Payment\Methods\Webpay;
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 Transbank\Webpay\WebpayPlus;
use Transbank\Webpay\WebpayPlus\Transaction;
class WebpayStrategy implements PaymentMethodInterface
{
private $parameters;
public function __construct(
$parameters
) {
$this->parameters = $parameters;
}
public function pay(Payment $payment): PaymentIntentResponse
{
$return_url = config('app.url') .
'/store/payment?paymentGatewayId=' .
$payment->paymentGatewayId() .
'&reference=' .$payment->reference();
$this->configureGateway();
$transaction = new Transaction();
$response = $transaction->create(
$payment->pin(),
$payment->customer()->id(),
$payment->amount()->total(),
$return_url
);
$paymentIntentResponse = new PaymentIntentResponse(
PaymentIntentResponse::ACTION_SHOW,
$response->getToken()
);
$data = [
'url_action' => $response->getUrl(),
'token' => $response->getToken()
];
$paymentIntentResponse->setView('webpay.webpaycheckout');
$paymentIntentResponse->setData($data);
return $paymentIntentResponse;
}
public function retrieve(Payment $payment): PaymentRetrieveResponse
{
$this->configureGateway();
$response = (new Transaction())->commit($payment->paymentGatewayTxId());
if ($response->isApproved()) {
return new PaymentRetrieveResponse(
$payment->paymentGatewayTxId(),
PaymentStatusEnum::CONFIRMED,
'Pago confirmado'
);
} else {
return new PaymentRetrieveResponse(
$payment->paymentGatewayTxId(),
PaymentStatusEnum::REJECTED,
'Pago rechazado'
);
}
return new PaymentRetrieveResponse(
$payment->paymentGatewayTxId(),
PaymentStatusEnum::PENDING,
''
);
}
private function configureGateway()
{
if ($this->parameters->is_productive) {
WebpayPlus::configureForProduction($this->parameters->merchant_id, $this->parameters->api_key);
} else {
WebpayPlus::configureForIntegration($this->parameters->merchant_id, $this->parameters->api_key);
}
}
}