Blame view

app/Utilities/Modules.php 1.89 KB
b7c7a5f6   Alexey Boroda   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
  <?php
  
  namespace App\Utilities;
  
  use Cache;
  use Date;
  use Module;
  
  use App\Events\PaymentGatewayListing;
  
  class Modules
  {
  
      public static function getPaymentMethods($type = null)
      {
          $company_id = session('company_id');
  
          $cache_admin = 'payment_methods.' . $company_id . '.admin';
          $cache_customer = 'payment_methods.' . $company_id . '.customer';
  
          $payment_methods = Cache::get($cache_admin);
  
          $customer = auth()->user()->customer;
  
          if ($customer && $type != 'all') {
              $payment_methods = Cache::get($cache_customer);
          }
  
          if (!empty($payment_methods)) {
              return $payment_methods;
          }
  
          $gateways = [];
          $methods = [];
  
          // Fire the event to extend the menu
          $results = event(new PaymentGatewayListing($gateways));
  
          foreach ($results as $gateways) {
              foreach ($gateways as $gateway) {
                  if (!isset($gateway['name']) || !isset($gateway['code'])) {
                      continue;
                  }
  
                  if (($customer && empty($gateway['customer'])) && $type != 'all') {
                      continue;
                  }
  
                  $methods[] = $gateway;
              }
          }
  
          $sort_order = [];
  
          if ($methods) {
              foreach ($methods as $key => $value) {
                  $sort_order[$key] = !empty($value['order']) ? $value['order'] : 0;
              }
  
              array_multisort($sort_order, SORT_ASC, $methods);
  
              foreach ($methods as $method) {
                  $payment_methods[$method['code']] = $method['name'];
              }
          }
  
          if ($customer) {
              Cache::put($cache_customer, $payment_methods, Date::now()->addHour(6));
          } else {
              Cache::put($cache_admin, $payment_methods, Date::now()->addHour(6));
          }
  
          return ($payment_methods) ? $payment_methods : [];
      }
  }