payment::checkout_initialization_method()   C
last analyzed

Complexity

Conditions 8
Paths 2

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 13
nc 2
nop 0
dl 0
loc 22
rs 6.6037
c 0
b 0
f 0
1
<?php
2
/**
3
  * osCommerce Online Merchant
4
  *
5
  * @copyright (c) 2016 osCommerce; https://www.oscommerce.com
6
  * @license MIT; https://www.oscommerce.com/license/mit.txt
7
  */
8
9
  use OSC\OM\Apps;
10
  use OSC\OM\OSCOM;
11
  use OSC\OM\Registry;
12
13
  class payment {
14
    var $modules, $selected_module;
15
16
    protected $lang;
17
18
    function __construct($module = '') {
19
      global $PHP_SELF;
20
21
      $this->lang = Registry::get('Language');
22
23
      if (defined('MODULE_PAYMENT_INSTALLED') && tep_not_null(MODULE_PAYMENT_INSTALLED)) {
24
        $this->modules = explode(';', MODULE_PAYMENT_INSTALLED);
25
26
        $include_modules = array();
27
28
        if ( (tep_not_null($module)) && (in_array($module . '.' . substr($PHP_SELF, (strrpos($PHP_SELF, '.')+1)), $this->modules) || in_array($module, $this->modules)) ) {
29
          $this->selected_module = $module;
30
31
          if (strpos($module, '\\') !== false) {
32
            $class = Apps::getModuleClass($module, 'Payment');
33
            $include_modules[] = [
34
              'class' => $module,
35
              'file' => $class
36
            ];
37
          } else {
38
            $include_modules[] = array('class' => $module, 'file' => $module . '.php');
39
          }
40 View Code Duplication
        } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
41
          foreach($this->modules as $value) {
42
            if (strpos($value, '\\') !== false) {
43
              $class = Apps::getModuleClass($value, 'Payment');
44
              $include_modules[] = [
45
                'class' => $value,
46
                'file' => $class
47
              ];
48
            } else {
49
              $class = basename($value, '.php');
50
              $include_modules[] = array('class' => $class, 'file' => $value);
51
            }
52
          }
53
        }
54
55 View Code Duplication
        for ($i=0, $n=sizeof($include_modules); $i<$n; $i++) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56
          if (strpos($include_modules[$i]['class'], '\\') !== false) {
57
            Registry::set('Payment_' . str_replace('\\', '_', $include_modules[$i]['class']), new $include_modules[$i]['file']);
58
          } else {
59
            $this->lang->loadDefinitions('modules/payment/' . pathinfo($include_modules[$i]['file'], PATHINFO_FILENAME));
60
            include('includes/modules/payment/' . $include_modules[$i]['file']);
61
62
            $GLOBALS[$include_modules[$i]['class']] = new $include_modules[$i]['class'];
63
          }
64
        }
65
66
// if there is only one payment method, select it as default because in
67
// checkout_confirmation.php the $_SESSION['payment'] variable is being assigned the
68
// $_POST['payment'] value which will be empty (no radio button selection possible)
69
        if ( (tep_count_payment_modules() == 1) && (!isset($_SESSION['payment']) || ($_SESSION['payment'] != $include_modules[0]['class'])) ) {
70
          $_SESSION['payment'] = $include_modules[0]['class'];
71
        }
72
73
        if ( (tep_not_null($module)) && (in_array($module . '.' . substr($PHP_SELF, (strrpos($PHP_SELF, '.')+1)), $this->modules) || in_array($module, $this->modules)) ) {
74
          if (strpos($module, '\\') !== false) {
75
            $OSCOM_PM = Registry::get('Payment_' . str_replace('\\', '_', $module));
76
77
            if (isset($OSCOM_PM->form_action_url)) {
78
              $this->form_action_url = $OSCOM_PM->form_action_url;
0 ignored issues
show
Bug introduced by
The property form_action_url does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
79
            }
80
          } elseif (isset($GLOBALS[$module]->form_action_url)) {
81
            $this->form_action_url = $GLOBALS[$module]->form_action_url;
82
          }
83
        }
84
      }
85
    }
86
87
// class methods
88
/* The following method is needed in the checkout_confirmation.php page
89
   due to a chicken and egg problem with the payment class and order class.
90
   The payment modules needs the order destination data for the dynamic status
91
   feature, and the order class needs the payment module title.
92
   The following method is a work-around to implementing the method in all
93
   payment modules available which would break the modules in the contributions
94
   section. This should be looked into again post 2.2.
95
*/
96
    function update_status() {
97
      if (is_array($this->modules)) {
98
        if (strpos($this->selected_module, '\\') !== false) {
99
          $code = 'Payment_' . str_replace('\\', '_', $this->selected_module);
100
101
          if (Registry::exists($code)) {
102
            $OSCOM_PM = Registry::get($code);
103
104
            if (method_exists($OSCOM_PM, 'update_status')) {
105
              $OSCOM_PM->update_status();
106
            }
107
          }
108
        } else {
109
          if (is_object($GLOBALS[$this->selected_module])) {
110
            if (method_exists($GLOBALS[$this->selected_module], 'update_status')) {
111
              $GLOBALS[$this->selected_module]->update_status();
112
            }
113
          }
114
        }
115
      }
116
    }
117
118
    function javascript_validation() {
119
      $js = '';
120
      if (is_array($this->modules)) {
121
        $js = '<script><!-- ' . "\n" .
122
              'function check_form() {' . "\n" .
123
              '  var error = 0;' . "\n" .
124
              '  var error_message = ' . json_encode(OSCOM::getDef('js_error') . "\n\n") . ';' . "\n" .
125
              '  var payment_value = null;' . "\n" .
126
              '  if (document.checkout_payment.payment.length) {' . "\n" .
127
              '    for (var i=0; i<document.checkout_payment.payment.length; i++) {' . "\n" .
128
              '      if (document.checkout_payment.payment[i].checked) {' . "\n" .
129
              '        payment_value = document.checkout_payment.payment[i].value;' . "\n" .
130
              '      }' . "\n" .
131
              '    }' . "\n" .
132
              '  } else if (document.checkout_payment.payment.checked) {' . "\n" .
133
              '    payment_value = document.checkout_payment.payment.value;' . "\n" .
134
              '  } else if (document.checkout_payment.payment.value) {' . "\n" .
135
              '    payment_value = document.checkout_payment.payment.value;' . "\n" .
136
              '  }' . "\n\n";
137
138
        foreach($this->modules as $value) {
139
          if (strpos($value, '\\') !== false) {
140
            $OSCOM_PM = Registry::get('Payment_' . str_replace('\\', '_', $value));
141
142
            if ($OSCOM_PM->enabled) {
143
              $js .= $OSCOM_PM->javascript_validation();
144
            }
145
          } else {
146
            $class = basename($value, '.php');
147
            if ($GLOBALS[$class]->enabled) {
148
              $js .= $GLOBALS[$class]->javascript_validation();
149
            }
150
          }
151
        }
152
153
        $js .= "\n" . '  if (payment_value == null) {' . "\n" .
154
               '    error_message = error_message + ' . json_encode(OSCOM::getDef('js_error_no_payment_module_selected') . "\n") . ';' . "\n" .
155
               '    error = 1;' . "\n" .
156
               '  }' . "\n\n" .
157
               '  if (error == 1) {' . "\n" .
158
               '    alert(error_message);' . "\n" .
159
               '    return false;' . "\n" .
160
               '  } else {' . "\n" .
161
               '    return true;' . "\n" .
162
               '  }' . "\n" .
163
               '}' . "\n" .
164
               '//--></script>' . "\n";
165
      }
166
167
      return $js;
168
    }
169
170
    function checkout_initialization_method() {
171
      $initialize_array = array();
172
173
      if (is_array($this->modules)) {
174
        foreach($this->modules as $value) {
175
          if (strpos($value, '\\') !== false) {
176
            $OSCOM_PM = Registry::get('Payment_' . str_replace('\\', '_', $value));
177
178
            if ($OSCOM_PM->enabled && method_exists($OSCOM_PM, 'checkout_initialization_method')) {
179
              $initialize_array[] = $OSCOM_PM->checkout_initialization_method();
180
            }
181
          } else {
182
            $class = basename($value, '.php');
183
            if ($GLOBALS[$class]->enabled && method_exists($GLOBALS[$class], 'checkout_initialization_method')) {
184
              $initialize_array[] = $GLOBALS[$class]->checkout_initialization_method();
185
            }
186
          }
187
        }
188
      }
189
190
      return $initialize_array;
191
    }
192
193
    function selection() {
194
      $selection_array = array();
195
196
      if (is_array($this->modules)) {
197
        foreach($this->modules as $value) {
198
          if (strpos($value, '\\') !== false) {
199
            $OSCOM_PM = Registry::get('Payment_' . str_replace('\\', '_', $value));
200
201
            if ($OSCOM_PM->enabled) {
202
              $selection = $OSCOM_PM->selection();
203
              if (is_array($selection)) $selection_array[] = $selection;
204
            }
205
          } else {
206
            $class = basename($value, '.php');
207
            if ($GLOBALS[$class]->enabled) {
208
              $selection = $GLOBALS[$class]->selection();
209
              if (is_array($selection)) $selection_array[] = $selection;
210
            }
211
          }
212
        }
213
      }
214
215
      return $selection_array;
216
    }
217
218 View Code Duplication
    function pre_confirmation_check() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
219
      if (is_array($this->modules)) {
220
        if (strpos($this->selected_module, '\\') !== false) {
221
          $OSCOM_PM = Registry::get('Payment_' . str_replace('\\', '_', $this->selected_module));
222
223
          if ($OSCOM_PM->enabled) {
224
            $OSCOM_PM->pre_confirmation_check();
225
          }
226
        } else {
227
          if (is_object($GLOBALS[$this->selected_module]) && ($GLOBALS[$this->selected_module]->enabled) ) {
228
            $GLOBALS[$this->selected_module]->pre_confirmation_check();
229
          }
230
        }
231
      }
232
    }
233
234 View Code Duplication
    function confirmation() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
235
      if (is_array($this->modules)) {
236
        if (strpos($this->selected_module, '\\') !== false) {
237
          $OSCOM_PM = Registry::get('Payment_' . str_replace('\\', '_', $this->selected_module));
238
239
          if ($OSCOM_PM->enabled) {
240
            return $OSCOM_PM->confirmation();
241
          }
242
        } else {
243
          if (is_object($GLOBALS[$this->selected_module]) && ($GLOBALS[$this->selected_module]->enabled) ) {
244
            return $GLOBALS[$this->selected_module]->confirmation();
245
          }
246
        }
247
      }
248
    }
249
250 View Code Duplication
    function process_button() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
251
      if (is_array($this->modules)) {
252
        if (strpos($this->selected_module, '\\') !== false) {
253
          $OSCOM_PM = Registry::get('Payment_' . str_replace('\\', '_', $this->selected_module));
254
255
          if ($OSCOM_PM->enabled) {
256
            return $OSCOM_PM->process_button();
257
          }
258
        } else {
259
          if (is_object($GLOBALS[$this->selected_module]) && ($GLOBALS[$this->selected_module]->enabled) ) {
260
            return $GLOBALS[$this->selected_module]->process_button();
261
          }
262
        }
263
      }
264
    }
265
266 View Code Duplication
    function before_process() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
267
      if (is_array($this->modules)) {
268
        if (strpos($this->selected_module, '\\') !== false) {
269
          $OSCOM_PM = Registry::get('Payment_' . str_replace('\\', '_', $this->selected_module));
270
271
          if ($OSCOM_PM->enabled) {
272
            return $OSCOM_PM->before_process();
273
          }
274
        } else {
275
          if (is_object($GLOBALS[$this->selected_module]) && ($GLOBALS[$this->selected_module]->enabled) ) {
276
            return $GLOBALS[$this->selected_module]->before_process();
277
          }
278
        }
279
      }
280
    }
281
282 View Code Duplication
    function after_process() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
283
      if (is_array($this->modules)) {
284
        if (strpos($this->selected_module, '\\') !== false) {
285
          $OSCOM_PM = Registry::get('Payment_' . str_replace('\\', '_', $this->selected_module));
286
287
          if ($OSCOM_PM->enabled) {
288
            return $OSCOM_PM->after_process();
289
          }
290
        } else {
291
          if (is_object($GLOBALS[$this->selected_module]) && ($GLOBALS[$this->selected_module]->enabled) ) {
292
            return $GLOBALS[$this->selected_module]->after_process();
293
          }
294
        }
295
      }
296
    }
297
298 View Code Duplication
    function get_error() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
299
      if (is_array($this->modules)) {
300
        if (strpos($this->selected_module, '\\') !== false) {
301
          $OSCOM_PM = Registry::get('Payment_' . str_replace('\\', '_', $this->selected_module));
302
303
          if ($OSCOM_PM->enabled) {
304
            return $OSCOM_PM->get_error();
305
          }
306
        } else {
307
          if (is_object($GLOBALS[$this->selected_module]) && ($GLOBALS[$this->selected_module]->enabled) ) {
308
            return $GLOBALS[$this->selected_module]->get_error();
309
          }
310
        }
311
      }
312
    }
313
  }
314
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
315