2017-09-14 22:21:00 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Utilities;
|
|
|
|
|
2020-08-28 10:34:51 +03:00
|
|
|
use App\Events\Module\PaymentMethodShowing;
|
2023-04-29 00:52:12 +03:00
|
|
|
use App\Utilities\Date;
|
|
|
|
use Illuminate\Support\Facades\Cache;
|
2017-09-14 22:21:00 +03:00
|
|
|
|
|
|
|
class Modules
|
|
|
|
{
|
2017-11-29 19:37:16 +03:00
|
|
|
public static function getPaymentMethods($type = null)
|
2017-09-14 22:21:00 +03:00
|
|
|
{
|
2020-05-23 15:34:54 +03:00
|
|
|
$cache_admin = static::getPaymentMethodsCacheKey('admin');
|
|
|
|
$cache_customer = static::getPaymentMethodsCacheKey('customer');
|
2018-05-05 13:09:48 +03:00
|
|
|
|
|
|
|
$payment_methods = Cache::get($cache_admin);
|
2017-11-18 15:23:20 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
$contact = true;
|
2018-10-15 16:10:49 +03:00
|
|
|
|
2020-08-28 10:34:51 +03:00
|
|
|
if ($user = user()) {
|
|
|
|
$contact = $user->contact;
|
2018-10-15 16:10:49 +03:00
|
|
|
}
|
2017-11-18 15:23:20 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
if ($contact && ($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;
|
|
|
|
}
|
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
$list = [];
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
$modules = new \stdClass();
|
|
|
|
$modules->payment_methods = [];
|
2017-11-20 18:57:13 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
// Fire the event to get the list of payment methods
|
2020-08-28 10:34:51 +03:00
|
|
|
event(new PaymentMethodShowing($modules));
|
2017-11-18 15:23:20 +03:00
|
|
|
|
2020-08-28 10:34:51 +03:00
|
|
|
foreach ((array) $modules->payment_methods as $method) {
|
2019-11-16 10:21:14 +03:00
|
|
|
if (!isset($method['name']) || !isset($method['code'])) {
|
|
|
|
continue;
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
if (($contact && empty($method['customer'])) && ($type != 'all')) {
|
|
|
|
continue;
|
2017-11-18 15:23:20 +03:00
|
|
|
}
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
$list[] = $method;
|
|
|
|
}
|
|
|
|
|
|
|
|
static::sortPaymentMethods($list);
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
foreach ($list as $method) {
|
|
|
|
$payment_methods[$method['code']] = $method['name'];
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
if ($contact) {
|
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
|
|
|
}
|
2019-11-16 10:21:14 +03:00
|
|
|
|
2020-05-23 15:34:54 +03:00
|
|
|
public static function clearPaymentMethodsCache()
|
|
|
|
{
|
|
|
|
Cache::forget(static::getPaymentMethodsCacheKey('admin'));
|
|
|
|
Cache::forget(static::getPaymentMethodsCacheKey('customer'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getPaymentMethodsCacheKey($type)
|
|
|
|
{
|
2021-04-16 00:59:43 +03:00
|
|
|
return 'payment_methods.' . company_id() . '.' . $type;
|
2020-05-23 15:34:54 +03:00
|
|
|
}
|
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
protected static function sortPaymentMethods(&$list)
|
|
|
|
{
|
|
|
|
$sort_order = [];
|
|
|
|
|
|
|
|
foreach ($list as $key => $value) {
|
|
|
|
$sort_order[$key] = !empty($value['order']) ? $value['order'] : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($sort_order)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
array_multisort($sort_order, SORT_ASC, $list);
|
|
|
|
}
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|