![]() 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/inventory.corals.io/Corals/modules/Inventory/Classes/ |
<?php namespace Corals\Modules\Inventory\Classes; use Carbon\Carbon; use Corals\Modules\Inventory\Models\Order; use Corals\Modules\Payment\Common\Models\Transaction; use Corals\Modules\Payment\Payment; use Corals\User\Models\User; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; class InventoryPayments { public $gateway; /** * 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) { abort(404); } $this->gateway = $gateway; $this->gateway->setAuthentication(); foreach ($params as $key => $value) { $this->gateway->setParameter($key, $value); } } } /** * @param Request $request * @param Order $order * @return \Illuminate\Foundation\Application|JsonResponse|mixed * @throws \Illuminate\Validation\ValidationException * @throws \Exception */ public function payOrder(Order $order, $checkoutDetails) { $parameters = $this->gateway->prepareCreateChargeParameters($checkoutDetails); $request = $this->gateway->createCharge($parameters); $response = $request->send(); if ($response->isSuccessful()) { $payment_reference = $response->getChargeReference(); $data = $response->getData(); $transactionData = [ 'code' => Transaction::getCode('TR'), 'status' => data_get($data, 'transaction_status'), 'amount' => data_get($data, 'amount'), 'owner_type' => getMorphAlias(User::class), 'paid_currency' => $order->currency, 'paid_amount' => data_get($data, 'paid_amount'), 'sourcable_id' => $order->id, 'sourcable_type' => getMorphAlias($order), 'reference' => $payment_reference, 'transaction_date' => Carbon::now(), 'notes' => 'order# ' . $order->id, 'extra' => $data ]; if ($order->type == 'inventory_order') { $transactionData = array_merge($transactionData, [ 'owner_id' => auth()->user()->id, 'type' => 'expense', ]); } else { $transactionData = array_merge($transactionData, [ 'owner_id' => $order->user->id ?? '', 'invoice_id' => $order->invoice->id, 'type' => 'order_revenue', ]); } Transaction::create($transactionData); if ($order->paymentTransactions() ->where('status', 'completed') ->sum('paid_amount') >= $order->total) { $status = 'paid'; $billing = $order->billing; $billing['payment_status'] = $status; $order->billing = $billing; $order->save(); if ($order->type == 'sales_order') { $order->invoice->status = $status; $order->invoice->save(); } } return true; } else { $message = 'pay Gateway Order Failed. ' . $response->getMessage(); throw new \Exception($message); } } }