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_action { |
18
|
|
|
var $server = 1; |
19
|
|
|
|
20
|
|
View Code Duplication |
function braintree_hook_admin_orders_action() { |
|
|
|
|
21
|
|
|
global $OSCOM_Braintree; |
22
|
|
|
|
23
|
|
|
if ( !isset($OSCOM_Braintree) || !is_object($OSCOM_Braintree) || (get_class($OSCOM_Braintree) != 'OSCOM_Braintree') ) { |
24
|
|
|
$OSCOM_Braintree = new OSCOM_Braintree(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
$this->_app = $OSCOM_Braintree; |
|
|
|
|
28
|
|
|
|
29
|
|
|
$this->_app->loadLanguageFile('hooks/admin/orders/action.php'); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
function execute() { |
33
|
|
|
global $HTTP_GET_VARS, $HTTP_POST_VARS; |
34
|
|
|
|
35
|
|
|
if ( isset($HTTP_GET_VARS['tabaction']) ) { |
36
|
|
|
$btstatus_query = tep_db_query("select comments from " . TABLE_ORDERS_STATUS_HISTORY . " where orders_id = '" . (int)$HTTP_GET_VARS['oID'] . "' and orders_status_id = '" . (int)OSCOM_APP_PAYPAL_BRAINTREE_TRANSACTIONS_ORDER_STATUS_ID . "' and comments like 'Transaction ID:%' order by date_added limit 1"); |
37
|
|
|
if ( tep_db_num_rows($btstatus_query) ) { |
38
|
|
|
$btstatus = tep_db_fetch_array($btstatus_query); |
39
|
|
|
|
40
|
|
|
$bt = array(); |
41
|
|
|
|
42
|
|
View Code Duplication |
foreach ( explode("\n", $btstatus['comments']) as $s ) { |
|
|
|
|
43
|
|
|
if ( !empty($s) && (strpos($s, ':') !== false) ) { |
44
|
|
|
$entry = explode(':', $s, 2); |
45
|
|
|
|
46
|
|
|
$bt[trim($entry[0])] = trim($entry[1]); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if ( isset($bt['Transaction ID']) ) { |
51
|
|
|
$o_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)$HTTP_GET_VARS['oID'] . "' and o.orders_id = ot.orders_id and ot.class = 'ot_total'"); |
52
|
|
|
$o = tep_db_fetch_array($o_query); |
53
|
|
|
|
54
|
|
|
if ((isset($bt['Server']) && ($bt['Server'] !== 'production')) || (strpos($o['payment_method'], 'Sandbox') !== false)) { |
55
|
|
|
$this->server = 0; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
View Code Duplication |
switch ( $HTTP_GET_VARS['tabaction'] ) { |
|
|
|
|
59
|
|
|
case 'getTransactionDetails': |
60
|
|
|
$this->getTransactionDetails($bt, $o); |
61
|
|
|
break; |
62
|
|
|
|
63
|
|
|
case 'doCapture': |
64
|
|
|
$this->doCapture($bt, $o); |
65
|
|
|
break; |
66
|
|
|
|
67
|
|
|
case 'doVoid': |
68
|
|
|
$this->doVoid($bt, $o); |
69
|
|
|
break; |
70
|
|
|
|
71
|
|
|
case 'refundTransaction': |
72
|
|
|
$this->refundTransaction($bt, $o); |
73
|
|
|
break; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
tep_redirect(tep_href_link(FILENAME_ORDERS, 'page=' . $HTTP_GET_VARS['page'] . '&oID=' . $HTTP_GET_VARS['oID'] . '&action=edit#section_status_history_content')); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
function getTransactionDetails($comments, $order) { |
83
|
|
|
global $messageStack; |
84
|
|
|
|
85
|
|
|
$result = null; |
86
|
|
|
|
87
|
|
|
$this->_app->setupCredentials($this->server === 1 ? 'live' : 'sandbox'); |
88
|
|
|
|
89
|
|
|
$error = false; |
90
|
|
|
|
91
|
|
|
try { |
92
|
|
|
$response = Braintree_Transaction::find($comments['Transaction ID']); |
93
|
|
|
} catch (Exception $e) { |
94
|
|
|
$error = true; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
if (($error === false) && is_object($response) && (get_class($response) == 'Braintree\\Transaction') && isset($response->id) && ($response->id == $comments['Transaction ID'])) { |
98
|
|
|
$result = 'Transaction ID: ' . tep_db_prepare_input($response->id) . "\n"; |
99
|
|
|
|
100
|
|
|
if (($response->paymentInstrumentType == 'credit_card') && isset($comments['3D Secure'])) { |
|
|
|
|
101
|
|
|
if (isset($response->threeDSecureInfo) && is_object($response->threeDSecureInfo)) { |
|
|
|
|
102
|
|
|
$result .= '3D Secure: ' . tep_db_prepare_input($response->threeDSecureInfo->status . ' (Liability Shifted: ' . ($response->threeDSecureInfo->liabilityShifted === true ? 'true' : 'false') . ')') . "\n"; |
|
|
|
|
103
|
|
|
} else { |
104
|
|
|
$result .= '3D Secure: ** MISSING **' . "\n"; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$result .= 'Payment Status: ' . tep_db_prepare_input($response->status) . "\n" . |
109
|
|
|
'Payment Type: ' . tep_db_prepare_input($response->paymentInstrumentType) . "\n"; |
|
|
|
|
110
|
|
|
|
111
|
|
|
if ($this->server === 0) { |
112
|
|
|
$result .= 'Server: sandbox' . "\n"; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$result .= 'Status History:'; |
116
|
|
|
|
117
|
|
View Code Duplication |
foreach ($response->statusHistory as $sh) { |
|
|
|
|
118
|
|
|
$sh->timestamp->setTimezone(new DateTimeZone(date_default_timezone_get())); |
119
|
|
|
|
120
|
|
|
$result .= "\n" . tep_db_prepare_input('[' . $sh->timestamp->format('Y-m-d H:i:s T') . '] ' . $sh->status . ' ' . $sh->amount . ' ' . $response->currencyIsoCode); |
|
|
|
|
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
View Code Duplication |
if (!empty($result)) { |
|
|
|
|
125
|
|
|
$sql_data_array = array( |
126
|
|
|
'orders_id' => (int)$order['orders_id'], |
127
|
|
|
'orders_status_id' => OSCOM_APP_PAYPAL_BRAINTREE_TRANSACTIONS_ORDER_STATUS_ID, |
128
|
|
|
'date_added' => 'now()', |
129
|
|
|
'customer_notified' => '0', |
130
|
|
|
'comments' => $result |
131
|
|
|
); |
132
|
|
|
|
133
|
|
|
tep_db_perform('orders_status_history', $sql_data_array); |
134
|
|
|
|
135
|
|
|
$messageStack->add_session($this->_app->getDef('ms_success_getTransactionDetails'), 'success'); |
136
|
|
|
} else { |
137
|
|
|
$messageStack->add_session($this->_app->getDef('ms_error_getTransactionDetails'), 'error'); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
function doCapture($comments, $order) { |
142
|
|
|
global $HTTP_POST_VARS, $messageStack; |
143
|
|
|
|
144
|
|
|
$capture_value = $this->_app->formatCurrencyRaw($order['total'], $order['currency'], $order['currency_value']); |
145
|
|
|
|
146
|
|
|
if ($this->_app->formatCurrencyRaw($_POST['btCaptureAmount'], $order['currency'], 1) < $capture_value) { |
147
|
|
|
$capture_value = $this->_app->formatCurrencyRaw($_POST['btCaptureAmount'], $order['currency'], 1); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$this->_app->setupCredentials($this->server === 1 ? 'live' : 'sandbox'); |
151
|
|
|
|
152
|
|
|
$error = false; |
153
|
|
|
|
154
|
|
|
try { |
155
|
|
|
$response = Braintree_Transaction::submitForSettlement($comments['Transaction ID'], $capture_value); |
156
|
|
|
} catch (Exception $e) { |
157
|
|
|
$error = true; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
if (($error === false) && is_object($response) && (get_class($response) == 'Braintree\\Result\\Successful') && ($response->success === true) && (get_class($response->transaction) == 'Braintree\\Transaction') && isset($response->transaction->id) && ($response->transaction->id == $comments['Transaction ID'])) { |
|
|
|
|
161
|
|
|
$result = 'Braintree App: Capture (' . $capture_value . ')' . "\n" . |
162
|
|
|
'Transaction ID: ' . tep_db_prepare_input($response->transaction->id) . "\n" . |
|
|
|
|
163
|
|
|
'Payment Status: ' . tep_db_prepare_input($response->transaction->status) . "\n" . |
|
|
|
|
164
|
|
|
'Status History:'; |
165
|
|
|
|
166
|
|
View Code Duplication |
foreach ($response->transaction->statusHistory as $sh) { |
|
|
|
|
167
|
|
|
$sh->timestamp->setTimezone(new DateTimeZone(date_default_timezone_get())); |
168
|
|
|
|
169
|
|
|
$result .= "\n" . tep_db_prepare_input('[' . $sh->timestamp->format('Y-m-d H:i:s T') . '] ' . $sh->status . ' ' . $sh->amount . ' ' . $response->transaction->currencyIsoCode); |
|
|
|
|
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
$sql_data_array = array( |
173
|
|
|
'orders_id' => (int)$order['orders_id'], |
174
|
|
|
'orders_status_id' => OSCOM_APP_PAYPAL_BRAINTREE_TRANSACTIONS_ORDER_STATUS_ID, |
175
|
|
|
'date_added' => 'now()', |
176
|
|
|
'customer_notified' => '0', |
177
|
|
|
'comments' => $result |
178
|
|
|
); |
179
|
|
|
|
180
|
|
|
tep_db_perform('orders_status_history', $sql_data_array); |
181
|
|
|
|
182
|
|
|
// immediately settle sandbox transactions |
183
|
|
|
if (strpos($order['payment_method'], 'Sandbox') !== false) { |
184
|
|
|
$error = false; |
185
|
|
|
|
186
|
|
|
try { |
187
|
|
|
$response = Braintree_Test_Transaction::settle($comments['Transaction ID']); |
188
|
|
|
} catch (Exception $e) { |
189
|
|
|
$error = true; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
if (($error === false) && is_object($response) && (get_class($response) == 'Braintree\\Transaction') && isset($response->id) && ($response->id == $comments['Transaction ID'])) { |
193
|
|
|
$result = 'Braintree App: Settled (' . tep_db_prepare_input($response->amount) . ')' . "\n" . |
194
|
|
|
'Transaction ID: ' . tep_db_prepare_input($response->id) . "\n" . |
195
|
|
|
'Payment Status: ' . tep_db_prepare_input($response->status) . "\n" . |
196
|
|
|
'Status History:'; |
197
|
|
|
|
198
|
|
View Code Duplication |
foreach ($response->statusHistory as $sh) { |
|
|
|
|
199
|
|
|
$sh->timestamp->setTimezone(new DateTimeZone(date_default_timezone_get())); |
200
|
|
|
|
201
|
|
|
$result .= "\n" . tep_db_prepare_input('[' . $sh->timestamp->format('Y-m-d H:i:s T') . '] ' . $sh->status . ' ' . $sh->amount . ' ' . $response->currencyIsoCode); |
|
|
|
|
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
$sql_data_array = array( |
205
|
|
|
'orders_id' => (int)$order['orders_id'], |
206
|
|
|
'orders_status_id' => OSCOM_APP_PAYPAL_BRAINTREE_TRANSACTIONS_ORDER_STATUS_ID, |
207
|
|
|
'date_added' => 'now()', |
208
|
|
|
'customer_notified' => '0', |
209
|
|
|
'comments' => $result |
210
|
|
|
); |
211
|
|
|
|
212
|
|
|
tep_db_perform('orders_status_history', $sql_data_array); |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
$messageStack->add_session($this->_app->getDef('ms_success_doCapture'), 'success'); |
217
|
|
|
} else { |
218
|
|
|
$messageStack->add_session($this->_app->getDef('ms_error_doCapture'), 'error'); |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
function doVoid($comments, $order) { |
223
|
|
|
global $messageStack; |
224
|
|
|
|
225
|
|
|
$this->_app->setupCredentials($this->server === 1 ? 'live' : 'sandbox'); |
226
|
|
|
|
227
|
|
|
$error = false; |
228
|
|
|
|
229
|
|
|
try { |
230
|
|
|
$response = Braintree_Transaction::void($comments['Transaction ID']); |
231
|
|
|
} catch (Exception $e) { |
232
|
|
|
$error = true; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
if (($error === false) && is_object($response) && (get_class($response) == 'Braintree\\Result\\Successful') && ($response->success === true) && (get_class($response->transaction) == 'Braintree\\Transaction') && isset($response->transaction->id) && ($response->transaction->id == $comments['Transaction ID'])) { |
|
|
|
|
236
|
|
|
$result = 'Braintree App: Void (' . tep_db_prepare_input($response->transaction->amount) . ')' . "\n" . |
|
|
|
|
237
|
|
|
'Transaction ID: ' . tep_db_prepare_input($response->transaction->id) . "\n" . |
|
|
|
|
238
|
|
|
'Payment Status: ' . tep_db_prepare_input($response->transaction->status) . "\n" . |
|
|
|
|
239
|
|
|
'Status History:'; |
240
|
|
|
|
241
|
|
View Code Duplication |
foreach ($response->transaction->statusHistory as $sh) { |
|
|
|
|
242
|
|
|
$sh->timestamp->setTimezone(new DateTimeZone(date_default_timezone_get())); |
243
|
|
|
|
244
|
|
|
$result .= "\n" . tep_db_prepare_input('[' . $sh->timestamp->format('Y-m-d H:i:s T') . '] ' . $sh->status . ' ' . $sh->amount . ' ' . $response->transaction->currencyIsoCode); |
|
|
|
|
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
$sql_data_array = array('orders_id' => (int)$order['orders_id'], |
248
|
|
|
'orders_status_id' => OSCOM_APP_PAYPAL_BRAINTREE_TRANSACTIONS_ORDER_STATUS_ID, |
249
|
|
|
'date_added' => 'now()', |
250
|
|
|
'customer_notified' => '0', |
251
|
|
|
'comments' => $result); |
252
|
|
|
|
253
|
|
|
tep_db_perform(TABLE_ORDERS_STATUS_HISTORY, $sql_data_array); |
254
|
|
|
|
255
|
|
|
$messageStack->add_session($this->_app->getDef('ms_success_doVoid'), 'success'); |
256
|
|
|
} else { |
257
|
|
|
$messageStack->add($this->_app->getDef('ms_error_doVoid'), 'error'); |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
function refundTransaction($comments, $order) { |
262
|
|
|
global $HTTP_POST_VARS, $messageStack; |
263
|
|
|
|
264
|
|
|
$refund_value = (isset($_POST['btRefundAmount']) && !empty($_POST['btRefundAmount'])) ? $this->_app->formatCurrencyRaw($_POST['btRefundAmount'], $order['currency'], 1) : null; |
265
|
|
|
|
266
|
|
|
$this->_app->setupCredentials($this->server === 1 ? 'live' : 'sandbox'); |
267
|
|
|
|
268
|
|
|
$error = false; |
269
|
|
|
|
270
|
|
|
try { |
271
|
|
|
$response = Braintree_Transaction::refund($comments['Transaction ID'], $refund_value); |
272
|
|
|
} catch (Exception $e) { |
273
|
|
|
$error = true; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
if (($error === false) && is_object($response) && (get_class($response) == 'Braintree\\Result\\Successful') && ($response->success === true) && (get_class($response->transaction) == 'Braintree\\Transaction') && isset($response->transaction->refundedTransactionId) && ($response->transaction->refundedTransactionId == $comments['Transaction ID'])) { |
|
|
|
|
277
|
|
|
$result = 'Braintree App: Refund (' . tep_db_prepare_input($response->transaction->amount) . ')' . "\n" . |
|
|
|
|
278
|
|
|
'Credit Transaction ID: ' . tep_db_prepare_input($response->transaction->id) . "\n" . |
|
|
|
|
279
|
|
|
'Transaction ID: ' . tep_db_prepare_input($response->transaction->refundedTransactionId) . "\n" . |
|
|
|
|
280
|
|
|
'Payment Status: ' . tep_db_prepare_input($response->transaction->status) . "\n" . |
|
|
|
|
281
|
|
|
'Status History:'; |
282
|
|
|
|
283
|
|
View Code Duplication |
foreach ($response->transaction->statusHistory as $sh) { |
|
|
|
|
284
|
|
|
$sh->timestamp->setTimezone(new DateTimeZone(date_default_timezone_get())); |
285
|
|
|
|
286
|
|
|
$result .= "\n" . tep_db_prepare_input('[' . $sh->timestamp->format('Y-m-d H:i:s T') . '] ' . $sh->status . ' ' . $sh->amount . ' ' . $response->transaction->currencyIsoCode); |
|
|
|
|
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
$sql_data_array = array('orders_id' => (int)$order['orders_id'], |
290
|
|
|
'orders_status_id' => OSCOM_APP_PAYPAL_BRAINTREE_TRANSACTIONS_ORDER_STATUS_ID, |
291
|
|
|
'date_added' => 'now()', |
292
|
|
|
'customer_notified' => '0', |
293
|
|
|
'comments' => $result); |
294
|
|
|
|
295
|
|
|
tep_db_perform(TABLE_ORDERS_STATUS_HISTORY, $sql_data_array); |
296
|
|
|
|
297
|
|
|
$messageStack->add_session($this->_app->getDef('ms_success_doRefund', array( |
298
|
|
|
'refund_amount' => tep_db_prepare_input($response->transaction->amount) |
|
|
|
|
299
|
|
|
)), 'success'); |
300
|
|
|
} else { |
301
|
|
|
$messageStack->add_session($this->_app->getDef('ms_error_doRefund', array( |
302
|
|
|
'refund_amount' => tep_db_prepare_input($response->transaction->amount) |
303
|
|
|
)), 'error'); |
304
|
|
|
} |
305
|
|
|
} |
306
|
|
|
} |
307
|
|
|
?> |
|
|
|
|
308
|
|
|
|
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.