File: /var/www/vhost/disk-apps/alq-cali.bikenow.co/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,
];
}
}