File: /var/www/vhost/disk-apps/comfama.sports-crowd.com/app/Core/Payment/PaymentMethodContext.php
<?php
declare(strict_types=1);
namespace App\Core\Payment;
use App\Core\Academy\Application\AcademyPurchaseService;
use App\Core\Academy\Application\AcademyTournamentPaymentService;
use App\Core\Academy\Application\ExperiencePaymentService;
use App\Core\Academy\Application\MembershipPaymentService;
use App\Core\Order\Application\OrderService;
use App\Core\Payment\Entities\Payment;
use App\Core\Payment\Entities\PaymentRetrieveResponse;
use App\Core\Payment\Exceptions\PaymentMethodException;
use App\Core\Payment\Methods\Nmi\NmiStrategy;
use App\Core\Payment\Methods\Nuvei\NuveiStrategy;
use App\Core\Payment\Methods\Onvo\OnvoStrategy;
use App\Core\Payment\Methods\Openpay\OpenpayCardStrategy;
use App\Core\Payment\Methods\Openpay\OpenpayPseStrategy;
use App\Core\Payment\Methods\PlaceToPay\PlaceToPayStrategy;
use App\Core\Payment\Methods\Stripe\StripeStrategy;
use App\Core\Payment\Methods\Webpay\WebpayStrategy;
use App\Core\Payment\Methods\Wompi\WompiStrategy;
use App\Core\Payment\Methods\WompiApi\WompiApiStrategy;
use App\Core\Payment\Methods\Yappy\YappyStrategy;
use App\Core\Payment\Methods\ZonaPagos\ZonaPagosStrategy;
use App\Core\Ticket\Application\TicketService;
use App\GatewayPayment;
class PaymentMethodContext
{
private $strategy;
private $gatewayParameters;
private $orderService;
private $ticketService;
private $academyPurchaseService;
private $academyTournamentPaymentService;
private $membershipPaymentService;
private $experiencePaymentService;
public function __construct(
array $gatewayParameters
) {
$this->orderService = new OrderService();
$this->ticketService = new TicketService();
$this->academyPurchaseService = new AcademyPurchaseService();
$this->academyTournamentPaymentService = new AcademyTournamentPaymentService();
$this->membershipPaymentService = new MembershipPaymentService();
$this->experiencePaymentService = new ExperiencePaymentService();
$this->gatewayParameters = (object) $gatewayParameters;
$this->gatewayParameters->extraConfirmUrlData = [];
switch ($this->gatewayParameters->page_response_for_gateway) {
case 'wompi':
$this->strategy = new WompiStrategy($this->gatewayParameters);
break;
case 'placetopay':
$this->strategy = new PlaceToPayStrategy($this->gatewayParameters);
break;
case 'stripe':
$this->strategy = new StripeStrategy($this->gatewayParameters);
break;
case 'zonapagos':
$this->strategy = new ZonaPagosStrategy($this->gatewayParameters);
break;
case 'onvo':
$this->strategy = new OnvoStrategy($this->gatewayParameters);
break;
case 'wompiapi':
$this->strategy = new WompiApiStrategy($this->gatewayParameters);
break;
case 'openpaycard':
$this->strategy = new OpenpayCardStrategy($this->gatewayParameters);
break;
case 'openpaypse':
$this->strategy = new OpenpayPseStrategy($this->gatewayParameters);
break;
case 'nmi':
$this->strategy = new NmiStrategy($this->gatewayParameters);
break;
case 'yappy':
$this->strategy = new YappyStrategy($this->gatewayParameters);
break;
case 'webpay':
$this->strategy = new WebpayStrategy($this->gatewayParameters);
break;
case 'nuvei':
$this->strategy = new NuveiStrategy($this->gatewayParameters);
break;
default:
throw new PaymentMethodException("The payment method does not exist or is not implemented.");
break;
}
}
public static function init(int $paymentGatewayId)
{
$gatewayData = GatewayPayment::findOrFail($paymentGatewayId);
return new self($gatewayData->toArray());
}
public function parameters()
{
return clone $this->gatewayParameters;
}
public function payOrder($order)
{
$payment = $this->orderService->buildPayment($order);
return $this->strategy->pay($payment);
}
public function payTicket($ticket)
{
$payment = $this->ticketService->buildPayment($ticket);
return $this->strategy->pay($payment);
}
public function payAcademyPurchase($academyPurchase)
{
$payment = $this->academyPurchaseService->buildPayment($academyPurchase);
return $this->strategy->pay($payment);
}
public function payAcademyTournament($academyTournament)
{
$payment = $this->academyTournamentPaymentService->buildPayment($academyTournament);
return $this->strategy->pay($payment);
}
public function payMembership($membership)
{
$payment = $this->membershipPaymentService->buildPayment($membership);
return $this->strategy->pay($payment);
}
public function payExperience($experience)
{
$payment = $this->experiencePaymentService->buildPayment($experience);
return $this->strategy->pay($payment);
}
public function retrieve(Payment $payment)
{
if (is_null($payment->paymentGatewayId())) {
return PaymentRetrieveResponse::createPending();
}
return $this->strategy->retrieve($payment);
}
public function retrieveOrderPayment($order)
{
if (is_null($order->gateway_payments_id)) {
return PaymentRetrieveResponse::createPending();
}
$payment = $this->orderService->buildPayment($order);
return $this->strategy->retrieve($payment);
}
public function retrieveTicketPayment($ticket)
{
if (is_null($ticket->gateway_payments_id)) {
return PaymentRetrieveResponse::createPending();
}
$payment = $this->ticketService->buildPayment($ticket);
return $this->strategy->retrieve($payment);
}
public function setExtraConfirmUrlData(array $extraConfirmUrlData): void
{
$this->gatewayParameters->extraConfirmUrlData = $extraConfirmUrlData;
}
}