@@ -10,40 +10,44 @@ |
||
10 | 10 | * License URI: https://github.com/Strikewood/woocommerce-first-atlantic-commerce/blob/master/LICENSE |
11 | 11 | */ |
12 | 12 | |
13 | -if ( !defined('ABSPATH') ) exit; |
|
13 | +if ( !defined('ABSPATH') ) { |
|
14 | + exit; |
|
15 | +} |
|
14 | 16 | |
15 | -function woocommerce_init_fac_gateway() |
|
16 | -{ |
|
17 | +function woocommerce_init_fac_gateway() { |
|
17 | 18 | // Make sure WooCommerce is available |
18 | - if ( !class_exists('WC_Payment_Gateway') ) return; |
|
19 | + if ( !class_exists('WC_Payment_Gateway') ) { |
|
20 | + return; |
|
21 | + } |
|
19 | 22 | |
20 | 23 | // Localisation |
21 | 24 | load_plugin_textdomain('wc-gateway-fac', false, dirname( plugin_basename(__FILE__) ) . '/languages'); |
22 | 25 | |
23 | 26 | // Our classes and depdencies (if not using composer) |
24 | - if ( is_file( dirname(__FILE__) . DIRECTORY_SEPARATOR . 'vendor/autoload.php' ) ) require_once('vendor/autoload.php'); |
|
27 | + if ( is_file( dirname(__FILE__) . DIRECTORY_SEPARATOR . 'vendor/autoload.php' ) ) { |
|
28 | + require_once('vendor/autoload.php'); |
|
29 | + } |
|
25 | 30 | |
26 | 31 | // Make sure the FAC class was autoloaded |
27 | - if ( !class_exists('WC_Gateway_FirstAtlanticCommerce') ) return; |
|
32 | + if ( !class_exists('WC_Gateway_FirstAtlanticCommerce') ) { |
|
33 | + return; |
|
34 | + } |
|
28 | 35 | |
29 | 36 | // Register the gateway in WC |
30 | - function woocommerce_register_fac_gateway($methods) |
|
31 | - { |
|
37 | + function woocommerce_register_fac_gateway($methods) { |
|
32 | 38 | $methods[] = 'WC_Gateway_FirstAtlanticCommerce'; |
33 | 39 | |
34 | 40 | return $methods; |
35 | 41 | } |
36 | 42 | add_filter('woocommerce_payment_gateways', 'woocommerce_register_fac_gateway'); |
37 | 43 | |
38 | - function woocommerce_fac_process_payment($order_id) |
|
39 | - { |
|
44 | + function woocommerce_fac_process_payment($order_id) { |
|
40 | 45 | $fac = new WC_Gateway_FirstAtlanticCommerce; |
41 | 46 | |
42 | 47 | $fac->process_payment($order_id); |
43 | 48 | } |
44 | 49 | |
45 | - function woocommerce_fac_process_refund($order_id) |
|
46 | - { |
|
50 | + function woocommerce_fac_process_refund($order_id) { |
|
47 | 51 | $fac = new WC_Gateway_FirstAtlanticCommerce; |
48 | 52 | |
49 | 53 | $fac->process_refund($order_id); |
@@ -4,20 +4,20 @@ discard block |
||
4 | 4 | use Omnipay\Common\Exception\InvalidResponseException; |
5 | 5 | use Omnipay\Common\Exception\OmnipayException; |
6 | 6 | |
7 | -if ( !defined('ABSPATH') ) exit; |
|
7 | +if ( !defined('ABSPATH') ) { |
|
8 | + exit; |
|
9 | +} |
|
8 | 10 | |
9 | 11 | /** |
10 | 12 | * WC_Gateway_FirstAtlanticCommerce class |
11 | 13 | * |
12 | 14 | * @extends WC_Payment_Gateway |
13 | 15 | */ |
14 | -class WC_Gateway_FirstAtlanticCommerce extends WC_Payment_Gateway |
|
15 | -{ |
|
16 | +class WC_Gateway_FirstAtlanticCommerce extends WC_Payment_Gateway { |
|
16 | 17 | /** |
17 | 18 | * Constructor |
18 | 19 | */ |
19 | - public function __construct() |
|
20 | - { |
|
20 | + public function __construct() { |
|
21 | 21 | $this->id = 'fac'; |
22 | 22 | $this->method_title = __('First Atlantic Commerce', 'wc-gateway-fac'); |
23 | 23 | $this->method_description = __('First Atlantic Commerce works by adding credit card fields on the checkout and then sending the details to First Atlantic Commerce for verification.', 'wc-gateway-fac'); |
@@ -60,21 +60,16 @@ discard block |
||
60 | 60 | /** |
61 | 61 | * Notify of issues in wp-admin |
62 | 62 | */ |
63 | - public function admin_notices() |
|
64 | - { |
|
65 | - if ($this->enabled == 'no') |
|
66 | - { |
|
63 | + public function admin_notices() { |
|
64 | + if ($this->enabled == 'no') { |
|
67 | 65 | return; |
68 | 66 | } |
69 | 67 | |
70 | 68 | // Check required fields |
71 | - if (!$this->merchant_id) |
|
72 | - { |
|
69 | + if (!$this->merchant_id) { |
|
73 | 70 | echo '<div class="error"><p>' . sprintf( __( 'First Atlantic Commerce error: Please enter your merchant id <a href="%s">here</a>', 'woocommerce-gateway-fac' ), admin_url( 'admin.php?page=wc-settings&tab=checkout§ion=wc_gateway_fac' ) ) . '</p></div>'; |
74 | 71 | return; |
75 | - } |
|
76 | - elseif (!$this->merchant_password) |
|
77 | - { |
|
72 | + } elseif (!$this->merchant_password) { |
|
78 | 73 | echo '<div class="error"><p>' . sprintf( __( 'First Atlantic Commerce error: Please enter your merchant password <a href="%s">here</a>', 'woocommerce-gateway-fac' ), admin_url( 'admin.php?page=wc-settings&tab=checkout§ion=wc_gateway_fac' ) ) . '</p></div>'; |
79 | 74 | return; |
80 | 75 | } |
@@ -93,10 +88,8 @@ discard block |
||
93 | 88 | * |
94 | 89 | * @return void |
95 | 90 | */ |
96 | - public function log($message) |
|
97 | - { |
|
98 | - if ( empty($this->log) ) |
|
99 | - { |
|
91 | + public function log($message) { |
|
92 | + if ( empty($this->log) ) { |
|
100 | 93 | $this->log = new WC_Logger(); |
101 | 94 | } |
102 | 95 | |
@@ -108,19 +101,16 @@ discard block |
||
108 | 101 | * |
109 | 102 | * @return bool |
110 | 103 | */ |
111 | - public function is_available() |
|
112 | - { |
|
104 | + public function is_available() { |
|
113 | 105 | $is_available = parent::is_available(); |
114 | 106 | |
115 | 107 | // Only allow unencrypted connections when testing |
116 | - if (!is_ssl() && !$this->testmode) |
|
117 | - { |
|
108 | + if (!is_ssl() && !$this->testmode) { |
|
118 | 109 | $is_available = false; |
119 | 110 | } |
120 | 111 | |
121 | 112 | // Required fields check |
122 | - if (!$this->merchant_id || !$this->merchant_password) |
|
123 | - { |
|
113 | + if (!$this->merchant_id || !$this->merchant_password) { |
|
124 | 114 | $is_available = false; |
125 | 115 | } |
126 | 116 | |
@@ -130,8 +120,7 @@ discard block |
||
130 | 120 | /** |
131 | 121 | * Initialise Gateway Settings Form Fields |
132 | 122 | */ |
133 | - public function init_form_fields() |
|
134 | - { |
|
123 | + public function init_form_fields() { |
|
135 | 124 | $this->form_fields = apply_filters('wc_fac_settings', [ |
136 | 125 | 'enabled' => [ |
137 | 126 | 'title' => __('Enable/Disable', 'woocommerce-gateway-fac'), |
@@ -203,15 +192,13 @@ discard block |
||
203 | 192 | /** |
204 | 193 | * Setup the gateway object |
205 | 194 | */ |
206 | - public function setup_gateway() |
|
207 | - { |
|
195 | + public function setup_gateway() { |
|
208 | 196 | $gateway = Omnipay::create('FirstAtlanticCommerce'); |
209 | 197 | |
210 | 198 | $gateway->setMerchantId($this->merchant_id); |
211 | 199 | $gateway->setMerchantPassword($this->merchant_password); |
212 | 200 | |
213 | - if ($this->testmode) |
|
214 | - { |
|
201 | + if ($this->testmode) { |
|
215 | 202 | $gateway->setTestMode(true); |
216 | 203 | } |
217 | 204 | |
@@ -223,8 +210,7 @@ discard block |
||
223 | 210 | * |
224 | 211 | * @return void |
225 | 212 | */ |
226 | - public function payment_fields() |
|
227 | - { |
|
213 | + public function payment_fields() { |
|
228 | 214 | // Default credit card form |
229 | 215 | $this->credit_card_form(); |
230 | 216 | } |
@@ -234,22 +220,18 @@ discard block |
||
234 | 220 | * |
235 | 221 | * @return bool |
236 | 222 | */ |
237 | - public function validate_fields() |
|
238 | - { |
|
223 | + public function validate_fields() { |
|
239 | 224 | $validated = true; |
240 | 225 | |
241 | - if ( empty($_POST['fac-card-number']) ) |
|
242 | - { |
|
226 | + if ( empty($_POST['fac-card-number']) ) { |
|
243 | 227 | wc_add_notice( $this->get_validation_error( __('Card Number', 'woocommerce-gateway-fac'), $_POST['fac-card-number'] ), 'error' ); |
244 | 228 | $validated = false; |
245 | 229 | } |
246 | - if ( empty($_POST['fac-card-expiry']) ) |
|
247 | - { |
|
230 | + if ( empty($_POST['fac-card-expiry']) ) { |
|
248 | 231 | wc_add_notice( $this->get_validation_error( __('Card Expiry', 'woocommerce-gateway-fac'), $_POST['fac-card-number'] ), 'error' ); |
249 | 232 | $validated = false; |
250 | 233 | } |
251 | - if ( empty($_POST['fac-card-cvc']) ) |
|
252 | - { |
|
234 | + if ( empty($_POST['fac-card-cvc']) ) { |
|
253 | 235 | wc_add_notice( $this->get_validation_error( __('Card Code', 'woocommerce-gateway-fac'), $_POST['fac-card-number'] ), 'error' ); |
254 | 236 | $validated = false; |
255 | 237 | } |
@@ -264,14 +246,10 @@ discard block |
||
264 | 246 | * @param string $type |
265 | 247 | * @return string |
266 | 248 | */ |
267 | - protected function get_validation_error($field, $type = 'undefined') |
|
268 | - { |
|
269 | - if ( $type === 'invalid' ) |
|
270 | - { |
|
249 | + protected function get_validation_error($field, $type = 'undefined') { |
|
250 | + if ( $type === 'invalid' ) { |
|
271 | 251 | return sprintf( __( 'Please enter a valid %s.', 'woocommerce-gateway-fac' ), "<strong>$field</strong>" ); |
272 | - } |
|
273 | - else |
|
274 | - { |
|
252 | + } else { |
|
275 | 253 | return sprintf( __( '%s is a required field.', 'woocommerce-gateway-fac' ), "<strong>$field</strong>" ); |
276 | 254 | } |
277 | 255 | } |
@@ -283,8 +261,7 @@ discard block |
||
283 | 261 | * |
284 | 262 | * @return bool |
285 | 263 | */ |
286 | - public function can_process_order($order) |
|
287 | - { |
|
264 | + public function can_process_order($order) { |
|
288 | 265 | return $order && $order->payment_method == 'fac'; |
289 | 266 | } |
290 | 267 | |
@@ -295,20 +272,22 @@ discard block |
||
295 | 272 | * |
296 | 273 | * @return array |
297 | 274 | */ |
298 | - public function process_payment($order_id) |
|
299 | - { |
|
275 | + public function process_payment($order_id) { |
|
300 | 276 | $order = new WC_Order($order_id); |
301 | 277 | |
302 | - if ( !$this->can_process_order($order) ) return; |
|
278 | + if ( !$this->can_process_order($order) ) { |
|
279 | + return; |
|
280 | + } |
|
303 | 281 | |
304 | 282 | $transaction = $order->get_transaction_id(); |
305 | 283 | $captured = get_post_meta($order_id, '_fac_captured', true); |
306 | 284 | |
307 | 285 | // Skip already captured transactions |
308 | - if ($captured) return; |
|
286 | + if ($captured) { |
|
287 | + return; |
|
288 | + } |
|
309 | 289 | |
310 | - try |
|
311 | - { |
|
290 | + try { |
|
312 | 291 | $gateway = $this->setup_gateway(); |
313 | 292 | |
314 | 293 | $data = [ |
@@ -318,12 +297,9 @@ discard block |
||
318 | 297 | ]; |
319 | 298 | |
320 | 299 | // Already authorized transactions should be captured |
321 | - if ( $transaction && !$captured ) |
|
322 | - { |
|
300 | + if ( $transaction && !$captured ) { |
|
323 | 301 | $response = $gateway->capture($data)->send(); |
324 | - } |
|
325 | - else |
|
326 | - { |
|
302 | + } else { |
|
327 | 303 | $card_number = str_replace( [' ', '-'], '', wc_clean($_POST['fac-card-number']) ); |
328 | 304 | $card_cvv = wc_clean($_POST['fac-card-cvc']); |
329 | 305 | $card_expiry = preg_split('/\s?\/\s?/', wc_clean($_POST['fac-card-expiry']), 2); |
@@ -345,23 +321,18 @@ discard block |
||
345 | 321 | ]; |
346 | 322 | |
347 | 323 | // Capture in one pass if enabled, otherwise authorize |
348 | - if ($this->capture) |
|
349 | - { |
|
324 | + if ($this->capture) { |
|
350 | 325 | $response = $gateway->purchase($data)->send(); |
351 | - } |
|
352 | - else |
|
353 | - { |
|
326 | + } else { |
|
354 | 327 | $response = $gateway->authorize($data)->send(); |
355 | 328 | } |
356 | 329 | } |
357 | 330 | |
358 | - if ( $response->isSuccessful() ) |
|
359 | - { |
|
331 | + if ( $response->isSuccessful() ) { |
|
360 | 332 | $reference = $response->getTransactionReference(); |
361 | 333 | |
362 | 334 | // Captured transaction |
363 | - if ( ($transaction && !$captured) || (!$transaction && $this->capture) ) |
|
364 | - { |
|
335 | + if ( ($transaction && !$captured) || (!$transaction && $this->capture) ) { |
|
365 | 336 | // Store captured |
366 | 337 | update_post_meta($order_id, '_fac_captured', true); |
367 | 338 | |
@@ -372,8 +343,7 @@ discard block |
||
372 | 343 | $order->add_order_note( sprintf( __('FAC transaction complete (ID: %s)', 'woocommerce-gateway-fac'), $reference ) ); |
373 | 344 | } |
374 | 345 | // Authorized transaction |
375 | - else |
|
376 | - { |
|
346 | + else { |
|
377 | 347 | // Store captured |
378 | 348 | update_post_meta($order_id, '_transaction_id', $reference, true); |
379 | 349 | update_post_meta($order_id, '_fac_captured', false); |
@@ -393,33 +363,26 @@ discard block |
||
393 | 363 | 'result' => 'success', |
394 | 364 | 'redirect' => $this->get_return_url($order) |
395 | 365 | ]; |
396 | - } |
|
397 | - else |
|
398 | - { |
|
366 | + } else { |
|
399 | 367 | throw new InvalidResponseException( $response->getMessage(), $response->getCode() ); |
400 | 368 | } |
401 | - } |
|
402 | - catch (OmnipayException $e) |
|
403 | - { |
|
369 | + } catch (OmnipayException $e) { |
|
404 | 370 | $message = 'Transaction Failed: '. $e->getCode() .' – '. $e->getMessage(); |
405 | 371 | |
406 | 372 | $this->log($message); |
407 | 373 | $order->add_order_note( __($message, 'woocommerce-gateway-fac') ); |
408 | 374 | |
409 | 375 | // Friendly declined message |
410 | - if ( in_array( $e->getCode(), [2, 3, 4, 35, 38, 39] ) ) |
|
411 | - { |
|
376 | + if ( in_array( $e->getCode(), [2, 3, 4, 35, 38, 39] ) ) { |
|
412 | 377 | $message = __('Unfortunately your order cannot be processed as the originating bank/merchant has declined your transaction.', 'woocommerce') .' '. __('Please attempt your purchase again.', 'woocommerce'); |
413 | 378 | } |
414 | 379 | |
415 | 380 | // Friendly error message |
416 | - else |
|
417 | - { |
|
381 | + else { |
|
418 | 382 | $message = __('Unfortunately your order cannot be processed as an error has occured.', 'woocommerce') .' '. __('Please attempt your purchase again.', 'woocommerce'); |
419 | 383 | } |
420 | 384 | |
421 | - if ( !is_admin() || ( defined('DOING_AJAX') && DOING_AJAX ) ) |
|
422 | - { |
|
385 | + if ( !is_admin() || ( defined('DOING_AJAX') && DOING_AJAX ) ) { |
|
423 | 386 | wc_add_notice($message, 'error'); |
424 | 387 | } |
425 | 388 | |
@@ -434,8 +397,7 @@ discard block |
||
434 | 397 | * |
435 | 398 | * @return bool |
436 | 399 | */ |
437 | - public function can_refund_order($order) |
|
438 | - { |
|
400 | + public function can_refund_order($order) { |
|
439 | 401 | return $order && $order->payment_method == 'fac' && $order->get_transaction_id(); |
440 | 402 | } |
441 | 403 | |
@@ -447,12 +409,10 @@ discard block |
||
447 | 409 | * |
448 | 410 | * @return bool |
449 | 411 | */ |
450 | - public function process_refund($order_id, $amount = null, $reason = '') |
|
451 | - { |
|
412 | + public function process_refund($order_id, $amount = null, $reason = '') { |
|
452 | 413 | $order = wc_get_order($order_id); |
453 | 414 | |
454 | - if ( !$this->can_refund_order($order) ) |
|
455 | - { |
|
415 | + if ( !$this->can_refund_order($order) ) { |
|
456 | 416 | $this->log('Refund Failed: No transaction ID for FAC'); |
457 | 417 | return false; |
458 | 418 | } |
@@ -460,13 +420,11 @@ discard block |
||
460 | 420 | $transaction = $order->get_transaction_id(); |
461 | 421 | $captured = get_post_meta($order_id, '_fac_captured', true); |
462 | 422 | |
463 | - if ( is_null($amount) ) |
|
464 | - { |
|
423 | + if ( is_null($amount) ) { |
|
465 | 424 | $amount = $order->get_total(); |
466 | 425 | } |
467 | 426 | |
468 | - try |
|
469 | - { |
|
427 | + try { |
|
470 | 428 | $gateway = $this->setup_gateway(); |
471 | 429 | |
472 | 430 | $data = [ |
@@ -474,27 +432,19 @@ discard block |
||
474 | 432 | 'amount' => $amount |
475 | 433 | ]; |
476 | 434 | |
477 | - if ($captured) |
|
478 | - { |
|
435 | + if ($captured) { |
|
479 | 436 | $response = $gateway->refund($data)->send(); |
480 | - } |
|
481 | - else |
|
482 | - { |
|
437 | + } else { |
|
483 | 438 | $response = $gateway->void($data)->send(); |
484 | 439 | } |
485 | 440 | |
486 | - if ( $response->isSuccessful() ) |
|
487 | - { |
|
441 | + if ( $response->isSuccessful() ) { |
|
488 | 442 | $order->add_order_note( sprintf( __('Refunded %s', 'woocommerce-gateway-fac'), $data['amount'] ) ); |
489 | 443 | return true; |
490 | - } |
|
491 | - else |
|
492 | - { |
|
444 | + } else { |
|
493 | 445 | throw new InvalidResponseException( $response->getMessage(), $response->getCode() ); |
494 | 446 | } |
495 | - } |
|
496 | - catch (OmnipayException $e) |
|
497 | - { |
|
447 | + } catch (OmnipayException $e) { |
|
498 | 448 | $message = 'Refund Failed: '. $e->getCode() .' – '. $e->getMessage(); |
499 | 449 | |
500 | 450 | $this->log($message); |