Completed
Push — 23 ( 09d773...16ee8e )
by Harald
11s
created

braintree_hook_admin_orders_tab::getVoidButton()   C

Complexity

Conditions 11
Paths 4

Size

Total Lines 72
Code Lines 31

Duplication

Lines 51
Ratio 70.83 %

Importance

Changes 0
Metric Value
cc 11
eloc 31
nc 4
nop 2
dl 51
loc 72
rs 5.6331
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/*
3
  $Id$
4
5
  osCommerce, Open Source E-Commerce Solutions
6
  http://www.oscommerce.com
7
8
  Copyright (c) 2016 osCommerce
9
10
  Released under the GNU General Public License
11
*/
12
13
  if ( !class_exists('OSCOM_Braintree') ) {
14
    include(DIR_FS_CATALOG . 'includes/apps/braintree/OSCOM_Braintree.php');
15
  }
16
17
  class braintree_hook_admin_orders_tab {
18 View Code Duplication
    function braintree_hook_admin_orders_tab() {
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...
19
      global $OSCOM_Braintree;
20
21
      if ( !isset($OSCOM_Braintree) || !is_object($OSCOM_Braintree) || (get_class($OSCOM_Braintree) != 'OSCOM_Braintree') ) {
22
        $OSCOM_Braintree = new OSCOM_Braintree();
23
      }
24
25
      $this->_app = $OSCOM_Braintree;
0 ignored issues
show
Bug introduced by
The property _app 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...
26
27
      $this->_app->loadLanguageFile('hooks/admin/orders/tab.php');
28
    }
29
30
    function execute() {
31
      global $HTTP_GET_VARS, $oID, $base_url;
32
33
      if (!defined('OSCOM_APP_PAYPAL_BRAINTREE_TRANSACTIONS_ORDER_STATUS_ID')) {
34
        return false;
35
      }
36
37
      $output = '';
38
39
      $status = array();
40
41
      $btstatus_query = tep_db_query("select comments from " . TABLE_ORDERS_STATUS_HISTORY . " where orders_id = '" . (int)$oID . "' and orders_status_id = '" . (int)OSCOM_APP_PAYPAL_BRAINTREE_TRANSACTIONS_ORDER_STATUS_ID . "' and comments like 'Transaction ID:%' order by date_added desc limit 1");
42
      if ( tep_db_num_rows($btstatus_query) ) {
43
        $btstatus = tep_db_fetch_array($btstatus_query);
44
45 View Code Duplication
        foreach ( explode("\n", $btstatus['comments']) as $s ) {
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...
46
          if ( !empty($s) && (strpos($s, ':') !== false) ) {
47
            $entry = explode(':', $s, 2);
48
49
            $status[trim($entry[0])] = trim($entry[1]);
50
          }
51
        }
52
53
        if ( isset($status['Transaction ID']) ) {
54
          $order_query = tep_db_query("select o.orders_id, o.payment_method, o.currency, o.currency_value, ot.value as total from " . TABLE_ORDERS . " o, " . TABLE_ORDERS_TOTAL . " ot where o.orders_id = '" . (int)$oID . "' and o.orders_id = ot.orders_id and ot.class = 'ot_total'");
55
          $order = tep_db_fetch_array($order_query);
56
57
          $bt_server = (strpos(strtolower($order['payment_method']), 'sandbox') !== false) ? 'sandbox' : 'live';
58
59
          $info_button = $this->_app->drawButton($this->_app->getDef('button_details'), tep_href_link(FILENAME_ORDERS, 'page=' . $HTTP_GET_VARS['page'] . '&oID=' . $oID . '&action=edit&tabaction=getTransactionDetails'), 'primary', null, true);
60
          $capture_button = $this->getCaptureButton($status, $order);
61
          $void_button = $this->getVoidButton($status, $order);
62
          $refund_button = $this->getRefundButton($status, $order);
63
          $braintree_button = $this->_app->drawButton($this->_app->getDef('button_view_at_braintree'), 'https://www.' . ($bt_server == 'sandbox' ? 'sandbox.' : '') . 'braintreegateway.com/merchants/' . ($bt_server == 'sandbox' ? OSCOM_APP_PAYPAL_BRAINTREE_SANDBOX_MERCHANT_ID : OSCOM_APP_PAYPAL_BRAINTREE_MERCHANT_ID) . '/transactions/' . $status['Transaction ID'], 'info', 'target="_blank"', true);
64
65
          $tab_title = addslashes($this->_app->getDef('tab_title'));
66
          $tab_link = substr(tep_href_link(FILENAME_ORDERS, tep_get_all_get_params()), strlen($base_url)) . '#section_braintree_content';
67
68
          $output = <<<EOD
69
<script>
70
$(function() {
71
  $('#orderTabs ul').append('<li><a href="{$tab_link}">{$tab_title}</a></li>');
72
});
73
</script>
74
75
<div id="section_braintree_content" style="padding: 10px;">
76
  {$info_button} {$capture_button} {$void_button} {$refund_button} {$braintree_button}
77
</div>
78
EOD;
79
80
        }
81
      }
82
83
      return $output;
84
    }
85
86
    function getCaptureButton($status, $order) {
87
      global $HTTP_GET_VARS;
88
89
      $output = '';
90
91
      if ($status['Payment Status'] == 'authorized') {
92
        $v_query = tep_db_query("select comments from " . TABLE_ORDERS_STATUS_HISTORY . " where orders_id = '" . (int)$order['orders_id'] . "' and orders_status_id = '" . (int)OSCOM_APP_PAYPAL_BRAINTREE_TRANSACTIONS_ORDER_STATUS_ID . "' and comments like 'Braintree App: Void (%' limit 1");
93
94
        if ( !tep_db_num_rows($v_query) ) {
95
          $c_query = tep_db_query("select comments from " . TABLE_ORDERS_STATUS_HISTORY . " where orders_id = '" . (int)$order['orders_id'] . "' and orders_status_id = '" . (int)OSCOM_APP_PAYPAL_BRAINTREE_TRANSACTIONS_ORDER_STATUS_ID . "' and comments like 'Braintree App: Capture (%' limit 1");
96
97
          if ( !tep_db_num_rows($c_query) ) {
98
            $output .= $this->_app->drawButton($this->_app->getDef('button_dialog_capture'), '#', 'success', 'data-button="braintreeButtonDoCapture"', true);
99
100
            $dialog_title = tep_output_string_protected($this->_app->getDef('dialog_capture_title'));
101
            $dialog_body = $this->_app->getDef('dialog_capture_body');
102
            $field_amount_title = $this->_app->getDef('dialog_capture_amount_field_title');
103
            $capture_link = tep_href_link(FILENAME_ORDERS, 'page=' . $HTTP_GET_VARS['page'] . '&oID=' . $order['orders_id'] . '&action=edit&tabaction=doCapture');
104
            $capture_currency = $order['currency'];
105
            $capture_total = $this->_app->formatCurrencyRaw($order['total'], $order['currency'], $order['currency_value']);
106
            $dialog_button_capture = addslashes($this->_app->getDef('dialog_capture_button_capture'));
107
            $dialog_button_cancel = addslashes($this->_app->getDef('dialog_capture_button_cancel'));
108
109
            $output .= <<<EOD
110
<div id="braintree-dialog-capture" title="{$dialog_title}">
111
  <form id="btCaptureForm" action="{$capture_link}" method="post">
112
    <p>{$dialog_body}</p>
113
114
    <p>
115
      <label for="btCaptureAmount"><strong>{$field_amount_title}</strong></label>
116
      <input type="text" name="btCaptureAmount" value="{$capture_total}" id="btCaptureAmount" style="text-align: right;" />
117
      {$capture_currency}
118
    </p>
119
  </form>
120
</div>
121
122
<script>
123
$(function() {
124
  $('#braintree-dialog-capture').dialog({
125
    autoOpen: false,
126
    resizable: false,
127
    modal: true,
128
    buttons: {
129
      "{$dialog_button_capture}": function() {
130
        $('#btCaptureForm').submit();
131
      },
132
      "{$dialog_button_cancel}": function() {
133
        $(this).dialog('close');
134
      }
135
    }
136
  });
137
138
  $('a[data-button="braintreeButtonDoCapture"]').click(function(e) {
139
    e.preventDefault();
140
141
    $('#braintree-dialog-capture').dialog('open');
142
  });
143
});
144
</script>
145
EOD;
146
          }
147
        }
148
      }
149
150
      return $output;
151
    }
152
153
    function getVoidButton($status, $order) {
0 ignored issues
show
Unused Code introduced by
The parameter $status is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
154
      global $HTTP_GET_VARS;
155
156
      $output = '';
157
158
      $s_query = tep_db_query("select comments from " . TABLE_ORDERS_STATUS_HISTORY . " where orders_id = '" . (int)$order['orders_id'] . "' and orders_status_id = '" . (int)OSCOM_APP_PAYPAL_BRAINTREE_TRANSACTIONS_ORDER_STATUS_ID . "' and comments like '%Payment Status:%' order by date_added desc limit 1");
159
160
      if (tep_db_num_rows($s_query)) {
161
        $s = tep_db_fetch_array($s_query);
162
163
        $last_status = array();
164
165 View Code Duplication
        foreach (explode("\n", $s['comments']) as $status) {
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...
166
          if (!empty($status) && (strpos($status, ':') !== false) && (substr($status, 0, 1) !== '[')) {
167
            $entry = explode(':', $status, 2);
168
169
            $key = trim($entry[0]);
170
            $value = trim($entry[1]);
171
172
            if ((strlen($key) > 0) && (strlen($value) > 0)) {
173
              $last_status[$key] = $value;
174
            }
175
          }
176
        }
177
178
        if (($last_status['Payment Status'] == 'authorized') || ($last_status['Payment Status'] == 'submitted_for_settlement')) {
179
          $v_query = tep_db_query("select comments from " . TABLE_ORDERS_STATUS_HISTORY . " where orders_id = '" . (int)$order['orders_id'] . "' and orders_status_id = '" . (int)OSCOM_APP_PAYPAL_TRANSACTIONS_ORDER_STATUS_ID . "' and (comments like 'Braintree App: Void (%' or comments like 'Braintree App: Refund (%') limit 1");
180
181 View Code Duplication
          if ( !tep_db_num_rows($v_query) ) {
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...
182
            $output .= $this->_app->drawButton($this->_app->getDef('button_dialog_void'), '#', 'warning', 'data-button="braintreeButtonDoVoid"', true);
183
184
            $dialog_title = tep_output_string_protected($this->_app->getDef('dialog_void_title'));
185
            $dialog_body = $this->_app->getDef('dialog_void_body');
186
            $void_link = tep_href_link(FILENAME_ORDERS, 'page=' . $HTTP_GET_VARS['page'] . '&oID=' . $order['orders_id'] . '&action=edit&tabaction=doVoid');
187
            $dialog_button_void = addslashes($this->_app->getDef('dialog_void_button_void'));
188
            $dialog_button_cancel = addslashes($this->_app->getDef('dialog_void_button_cancel'));
189
190
            $output .= <<<EOD
191
<div id="braintree-dialog-void" title="{$dialog_title}">
192
  <p>{$dialog_body}</p>
193
</div>
194
195
<script>
196
$(function() {
197
  $('#braintree-dialog-void').dialog({
198
    autoOpen: false,
199
    resizable: false,
200
    modal: true,
201
    buttons: {
202
      "{$dialog_button_void}": function() {
203
        window.location = '{$void_link}';
204
      },
205
      "{$dialog_button_cancel}": function() {
206
        $(this).dialog('close');
207
      }
208
    }
209
  });
210
211
  $('a[data-button="braintreeButtonDoVoid"]').click(function(e) {
212
    e.preventDefault();
213
214
    $('#braintree-dialog-void').dialog('open');
215
  });
216
});
217
</script>
218
EOD;
219
          }
220
        }
221
      }
222
223
      return $output;
224
    }
225
226
    function getRefundButton($status, $order) {
0 ignored issues
show
Unused Code introduced by
The parameter $status is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
227
      global $HTTP_GET_VARS;
228
229
      $output = '';
230
231
      $s_query = tep_db_query("select comments from " . TABLE_ORDERS_STATUS_HISTORY . " where orders_id = '" . (int)$order['orders_id'] . "' and orders_status_id = '" . (int)OSCOM_APP_PAYPAL_BRAINTREE_TRANSACTIONS_ORDER_STATUS_ID . "' and comments not like 'Braintree App: Refund (%' and comments like '%Payment Status:%' order by date_added desc limit 1");
232
233
      if ( tep_db_num_rows($s_query) ) {
234
        $s = tep_db_fetch_array($s_query);
235
236
        $last_status = array();
237
238 View Code Duplication
        foreach (explode("\n", $s['comments']) as $status) {
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...
239
          if (!empty($status) && (strpos($status, ':') !== false) && (substr($status, 0, 1) !== '[')) {
240
            $entry = explode(':', $status, 2);
241
242
            $key = trim($entry[0]);
243
            $value = trim($entry[1]);
244
245
            if ((strlen($key) > 0) && (strlen($value) > 0)) {
246
              $last_status[$key] = $value;
247
            }
248
          }
249
        }
250
251
        if (($last_status['Payment Status'] == 'settled') || ($last_status['Payment Status'] == 'settling')) {
252
          $refund_total = $this->_app->formatCurrencyRaw($order['total'], $order['currency'], $order['currency_value']);
253
254
          $r_query = tep_db_query("select comments from " . TABLE_ORDERS_STATUS_HISTORY . " where orders_id = '" . (int)$order['orders_id'] . "' and orders_status_id = '" . (int)OSCOM_APP_PAYPAL_BRAINTREE_TRANSACTIONS_ORDER_STATUS_ID . "' and comments like 'Braintree App: Refund (%'");
255
256 View Code Duplication
          while ($r = tep_db_fetch_array($r_query)) {
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...
257
            if (preg_match('/^Braintree App\: Refund \(([0-9\.]+)\)\n/', $r['comments'], $r_matches)) {
258
              $refund_total = $this->_app->formatCurrencyRaw($refund_total - $r_matches[1], $order['currency'], 1);
259
            }
260
          }
261
262
          if ($refund_total > 0) {
263
            $output .= $this->_app->drawButton($this->_app->getDef('button_dialog_refund'), '#', 'error', 'data-button="braintreeButtonRefundTransaction"', true);
264
265
            $dialog_title = tep_output_string_protected($this->_app->getDef('dialog_refund_title'));
266
            $dialog_body = $this->_app->getDef('dialog_refund_body');
267
            $field_amount_title = $this->_app->getDef('dialog_refund_amount_field_title');
268
            $refund_link = tep_href_link(FILENAME_ORDERS, 'page=' . $HTTP_GET_VARS['page'] . '&oID=' . $order['orders_id'] . '&action=edit&tabaction=refundTransaction');
269
            $refund_currency = $order['currency'];
270
            $dialog_button_refund = addslashes($this->_app->getDef('dialog_refund_button_refund'));
271
            $dialog_button_cancel = addslashes($this->_app->getDef('dialog_refund_button_cancel'));
272
273
            $output .= <<<EOD
274
<div id="braintree-dialog-refund" title="{$dialog_title}">
275
  <form id="btRefundForm" action="{$refund_link}" method="post">
276
    <p>{$dialog_body}</p>
277
278
    <p>
279
      <label for="btRefundAmount"><strong>{$field_amount_title}</strong></label>
280
      <input type="text" name="btRefundAmount" value="{$refund_total}" id="btRefundAmount" style="text-align: right;" />
281
      {$refund_currency}
282
    </p>
283
  </form>
284
</div>
285
286
<script>
287
$(function() {
288
  $('#braintree-dialog-refund').dialog({
289
    autoOpen: false,
290
    resizable: false,
291
    modal: true,
292
    buttons: {
293
      "{$dialog_button_refund}": function() {
294
        $('#btRefundForm').submit();
295
      },
296
      "{$dialog_button_cancel}": function() {
297
        $(this).dialog('close');
298
      }
299
    }
300
  });
301
302
  $('a[data-button="braintreeButtonRefundTransaction"]').click(function(e) {
303
    e.preventDefault();
304
305
    $('#braintree-dialog-refund').dialog('open');
306
  });
307
});
308
</script>
309
EOD;
310
          }
311
        }
312
      }
313
314
      return $output;
315
    }
316
  }
317
?>
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...
318