![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/job-board.corals.io/Corals/modules/Reservation/Classes/ |
<?php namespace Corals\Modules\Reservation\Classes; use Corals\Modules\Payment\Common\Models\Transaction; use Corals\Modules\Payment\Facades\Payments; use Corals\Modules\Payment\Payment; use Corals\User\Models\User; class ReservationPayment { public $gateway; /** * ReservationPayment constructor. * @param null $gateway_key * @param array $params * @throws \Exception */ function __construct($gateway_key = null, array $params = []) { if ($gateway_key) { $gateway = Payment::create($gateway_key); $config = config('payment_' . strtolower($gateway_key)); if (!$config) { throw new \Exception(trans('Reservation::exception.misc.invalid_gateway')); } $this->gateway = $gateway; $this->gateway->setAuthentication(); foreach ($params as $key => $value) { $this->gateway->setParameter($key, $value); } } } /** * @return mixed */ public function getReservationAvailableGateway() { $availableGateways = Payments::getAvailableGateways(); foreach ($availableGateways as $gatewayKey => $gateway_title) { $paymentGateway = Payment::create($gatewayKey); if (!$paymentGateway->getConfig('support_reservation')) { unset($availableGateways[$gatewayKey]); } } return $availableGateways; } /** * @param $request * @param $reservation * @return bool * @throws \Exception */ public function doPayment($request, $reservation): bool { $invoice = $reservation->invoice; $amount = $invoice->total; $order = (object)[ 'id' => $reservation->id, 'amount' => $amount, 'currency' => $invoice->currency, 'billing' => [ 'billing_address' => [ 'email' => $request->get('email') ] ] ]; $checkoutDetails = [ 'token' => $request->get('checkoutToken'), 'gateway' => $this->gateway->getName(), ]; $user = user() ?? new User; $response = $this->gateway->createCharge( $parameters = $this->gateway->prepareCreateChargeParameters($order, $user, $checkoutDetails) )->send(); if ($response->isSuccessful()) { Transaction::query()->create([ 'code' => Transaction::getCode('RES'), 'owner_type' => getMorphAlias($user), 'owner_id' => $user->id ?? 0, 'sourcable_type' => getMorphAlias($reservation), 'sourcable_id' => $reservation->id, 'paid_currency' => $invoice->currency, 'paid_amount' => $amount, 'amount' => $amount, 'transaction_date' => now(), 'status' => 'completed', 'type' => 'reservation_payment', 'reference' => $response->getChargeReference(), 'notes' => "Payment for reservation #$reservation->id" ]); return true; } else { $message = 'pay Gateway Order Failed. ' . $response->getMessage(); logger($response->getMessage()); throw new \Exception($message); } } /** * @param $paymentGateway * @param $reservation * @param $params * @return mixed * @throws \Exception */ public function createPaymentToken($reservation, $params) { $invoice = $reservation->invoice; $amount = $invoice->total; $currency = $invoice->currency; $description = "Payment for Reservation#" . $reservation->id; $parameters = $this->gateway->preparePaymentTokenParameters($amount, $currency, $description, $params); $request = $this->gateway->purchase($parameters); $response = $request->send(); if ($response->isSuccessful()) { return $response->getPaymentTokenReference(); } else { throw new \Exception($response->getDataText()); } } /** * @param $gateway * @param $params * @return mixed * @throws \Exception */ public function checkPaymentToken($params) { $parameters = $this->gateway->prepareCheckPaymentTokenParameters($params); if ($this->gateway->getConfig('require_token_confirm')) { $request = $this->gateway->confirmPaymentToken($parameters); } else { $request = $this->gateway->checkPaymentToken($parameters); } $response = $request->send(); if ($response->isSuccessful()) { return $response->getPaymentTokenReference(); } else { throw new \Exception(trans($response->getDataText())); } } public function getPaymentView($reservation) { $gateway = $this->gateway; return view($this->gateway->getPaymentViewName('reservation')) ->with(compact('gateway', 'reservation')); } }