File: /var/www/vhost/disk-apps/comfama.sports-crowd.com/app/Core/Payment/Methods/Onvo/OnvoStrategy.php
<?php
declare(strict_types=1);
namespace App\Core\Payment\Methods\Onvo;
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 GuzzleHttp\RequestOptions;
use Illuminate\Http\Request;
class OnvoStrategy implements PaymentMethodInterface
{
const PAYMENT_STATUS = [
"succeeded" => PaymentStatusEnum::CONFIRMED,
"payment_failed" => PaymentStatusEnum::DECLINED,
"requires_payment_method" => PaymentStatusEnum::PENDING
];
private $parameters;
public function __construct(
$parameters
) {
$this->parameters = $parameters;
}
public function pay(Payment $payment): PaymentIntentResponse
{
$paymentIntent = $this->createPaymentIntent($payment);
$paymentIntentResponse = new PaymentIntentResponse(
PaymentIntentResponse::ACTION_SHOW,
$paymentIntent->id
);
$returnUrl = $this->buildReturnUrl($paymentIntent);
$data = [
'paymentIntentId' => $paymentIntent->id,
"pubKey" => $this->parameters->client_public,
"returnUrl" => $returnUrl
];
$paymentIntentResponse->setView("onvo.webcheckout");
$paymentIntentResponse->setData($data);
return $paymentIntentResponse;
}
public function retrieve(Payment $payment): PaymentRetrieveResponse
{
try {
$client = new Client();
$response = $client->get(
$this->parameters->gw_url_prd . '/payment-intents/' . $payment->paymentGatewayTxId(),
[
'headers' => [
'Authorization' => 'Bearer ' . $this->parameters->client_secret
]
]
);
$body = $response->getBody()->getContents();
$data = json_decode($body);
return new PaymentRetrieveResponse(
$data->id,
self::PAYMENT_STATUS[$data->status],
$data->status
);
} catch (\Throwable $th) {
return new PaymentRetrieveResponse(
$payment->paymentGatewayTxId(),
PaymentStatusEnum::PENDING,
$th->getMessage()
);
}
}
private function createPaymentIntent(Payment $payment)
{
$client = new Client();
$response = $client->post(
$this->parameters->gw_url_prd . '/payment-intents',
[
'headers' => [
'Authorization' => 'Bearer ' . $this->parameters->client_secret
],
RequestOptions::JSON => [
'amount' => (int) ($payment->amount()->total() * 100),
'currency' => $this->parameters->currency,
"metadata" => [
"reference" => $payment->reference()
]
]
]
);
$body = $response->getBody()->getContents();
return json_decode($body);
}
private function buildReturnUrl($paymentIntent)
{
$url = config('app.url') . '/store/payment';
$queryString = [
'paymentGatewayId' => $this->parameters->id,
'reference' => $paymentIntent->metadata->reference
];
$queryString = array_merge($queryString, $this->parameters->extraConfirmUrlData);
return Request::create($url)->fullUrlWithQuery($queryString);
}
}