2017-09-14 22:21:00 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Utilities;
|
|
|
|
|
|
|
|
use Cache;
|
|
|
|
use Date;
|
|
|
|
use Module;
|
|
|
|
|
|
|
|
use App\Events\PaymentGatewayListing;
|
|
|
|
|
|
|
|
class Modules
|
|
|
|
{
|
|
|
|
|
2017-11-29 19:37:16 +03:00
|
|
|
public static function getPaymentMethods($type = null)
|
2017-09-14 22:21:00 +03:00
|
|
|
{
|
2018-05-05 13:09:48 +03:00
|
|
|
$company_id = session('company_id');
|
2017-11-18 15:23:20 +03:00
|
|
|
|
2018-05-05 13:09:48 +03:00
|
|
|
$cache_admin = 'payment_methods.' . $company_id . '.admin';
|
|
|
|
$cache_customer = 'payment_methods.' . $company_id . '.customer';
|
|
|
|
|
|
|
|
$payment_methods = Cache::get($cache_admin);
|
2017-11-18 15:23:20 +03:00
|
|
|
|
|
|
|
$customer = auth()->user()->customer;
|
|
|
|
|
2017-11-29 19:37:16 +03:00
|
|
|
if ($customer && $type != 'all') {
|
2018-05-05 13:09:48 +03:00
|
|
|
$payment_methods = Cache::get($cache_customer);
|
2017-11-18 15:23:20 +03:00
|
|
|
}
|
2017-09-14 22:21:00 +03:00
|
|
|
|
|
|
|
if (!empty($payment_methods)) {
|
|
|
|
return $payment_methods;
|
|
|
|
}
|
|
|
|
|
2017-11-18 15:23:20 +03:00
|
|
|
$gateways = [];
|
|
|
|
$methods = [];
|
2017-09-14 22:21:00 +03:00
|
|
|
|
|
|
|
// Fire the event to extend the menu
|
|
|
|
$results = event(new PaymentGatewayListing($gateways));
|
|
|
|
|
|
|
|
foreach ($results as $gateways) {
|
|
|
|
foreach ($gateways as $gateway) {
|
2017-11-22 19:51:38 +03:00
|
|
|
if (!isset($gateway['name']) || !isset($gateway['code'])) {
|
2017-11-20 18:57:13 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-11-29 19:37:16 +03:00
|
|
|
if (($customer && empty($gateway['customer'])) && $type != 'all') {
|
2017-11-18 15:23:20 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-09-14 22:21:00 +03:00
|
|
|
$methods[] = $gateway;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-18 15:23:20 +03:00
|
|
|
$sort_order = [];
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2017-11-18 15:23:20 +03:00
|
|
|
if ($methods) {
|
|
|
|
foreach ($methods as $key => $value) {
|
2017-11-20 18:57:13 +03:00
|
|
|
$sort_order[$key] = !empty($value['order']) ? $value['order'] : 0;
|
2017-11-18 15:23:20 +03:00
|
|
|
}
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2017-11-18 15:23:20 +03:00
|
|
|
array_multisort($sort_order, SORT_ASC, $methods);
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2017-11-18 15:23:20 +03:00
|
|
|
foreach ($methods as $method) {
|
|
|
|
$payment_methods[$method['code']] = $method['name'];
|
|
|
|
}
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
|
2017-11-18 15:23:20 +03:00
|
|
|
if ($customer) {
|
2018-05-05 13:09:48 +03:00
|
|
|
Cache::put($cache_customer, $payment_methods, Date::now()->addHour(6));
|
2017-11-18 15:23:20 +03:00
|
|
|
} else {
|
2018-05-05 13:09:48 +03:00
|
|
|
Cache::put($cache_admin, $payment_methods, Date::now()->addHour(6));
|
2017-11-18 15:23:20 +03:00
|
|
|
}
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2017-11-20 19:01:34 +03:00
|
|
|
return ($payment_methods) ? $payment_methods : [];
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
}
|