Conditions | 5 |
Paths | 4 |
Total Lines | 18 |
Code Lines | 7 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Metric | Value |
---|---|
cc | 5 |
c | 2 |
b | 0 |
f | 0 |
dl | 0 |
loc | 18 |
rs | 8.8571 |
eloc | 7 |
nc | 4 |
nop | 0 |
1 | <?php |
||
0 ignored issues
–
show
|
|||
2 | |||
3 | use Omnipay\Omnipay; |
||
4 | use Omnipay\Common\Exception\InvalidResponseException; |
||
5 | use Omnipay\Common\Exception\OmnipayException; |
||
6 | |||
7 | if ( !defined('ABSPATH') ) exit; |
||
0 ignored issues
–
show
|
|||
8 | |||
9 | /** |
||
10 | * WC_Gateway_FirstAtlanticCommerce class |
||
11 | * |
||
12 | * @extends WC_Payment_Gateway |
||
13 | */ |
||
14 | class WC_Gateway_FirstAtlanticCommerce extends WC_Payment_Gateway |
||
0 ignored issues
–
show
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.
You can fix this by adding a namespace to your class: namespace YourVendor;
class YourClass { }
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries. ![]() |
|||
15 | { |
||
16 | /** |
||
0 ignored issues
–
show
|
|||
17 | * Constructor |
||
0 ignored issues
–
show
|
|||
18 | */ |
||
0 ignored issues
–
show
|
|||
19 | public function __construct() |
||
0 ignored issues
–
show
|
|||
20 | { |
||
0 ignored issues
–
show
|
|||
21 | $this->id = 'fac'; |
||
0 ignored issues
–
show
|
|||
22 | $this->method_title = __('First Atlantic Commerce', 'wc-gateway-fac'); |
||
0 ignored issues
–
show
|
|||
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'); |
||
0 ignored issues
–
show
|
|||
24 | $this->has_fields = true; |
||
0 ignored issues
–
show
|
|||
25 | $this->supports = [ |
||
0 ignored issues
–
show
|
|||
26 | //'subscriptions', |
||
0 ignored issues
–
show
|
|||
27 | 'products', |
||
0 ignored issues
–
show
|
|||
28 | 'refunds', |
||
0 ignored issues
–
show
|
|||
29 | //'subscription_cancellation', |
||
0 ignored issues
–
show
|
|||
30 | //'subscription_reactivation', |
||
0 ignored issues
–
show
|
|||
31 | //'subscription_suspension', |
||
0 ignored issues
–
show
|
|||
32 | //'subscription_amount_changes', |
||
0 ignored issues
–
show
|
|||
33 | //'subscription_payment_method_change', |
||
0 ignored issues
–
show
|
|||
34 | //'subscription_date_changes', |
||
0 ignored issues
–
show
|
|||
35 | //'pre-orders', |
||
0 ignored issues
–
show
|
|||
36 | 'default_credit_card_form' |
||
0 ignored issues
–
show
|
|||
37 | ]; |
||
0 ignored issues
–
show
|
|||
38 | |||
39 | // Load the form fields |
||
0 ignored issues
–
show
|
|||
40 | $this->init_form_fields(); |
||
0 ignored issues
–
show
|
|||
41 | |||
42 | // Load the settings |
||
0 ignored issues
–
show
|
|||
43 | $this->init_settings(); |
||
0 ignored issues
–
show
|
|||
44 | |||
45 | // User defined settings |
||
0 ignored issues
–
show
|
|||
46 | $this->title = $this->get_option('title'); |
||
0 ignored issues
–
show
|
|||
47 | $this->description = $this->get_option('description'); |
||
0 ignored issues
–
show
|
|||
48 | $this->enabled = $this->get_option('enabled'); |
||
0 ignored issues
–
show
|
|||
49 | $this->testmode = 'yes' === $this->get_option('testmode', 'no'); |
||
0 ignored issues
–
show
|
|||
50 | $this->capture = $this->get_option('capture', "yes") === "yes" ? true : false; |
||
0 ignored issues
–
show
The string literal
yes does not require double quotes, as per coding-style, please use single quotes.
PHP provides two ways to mark string literals. Either with single quotes String literals in single quotes on the other hand are evaluated very literally and the only two
characters that needs escaping in the literal are the single quote itself ( Double quoted string literals may contain other variables or more complex escape sequences. <?php
$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";
print $doubleQuoted;
will print an indented: If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear. For more information on PHP string literals and available escape sequences see the PHP core documentation. ![]() |
|||
51 | //$this->saved_cards = $this->get_option( 'saved_cards' ) === "yes" ? true : false; |
||
0 ignored issues
–
show
49% of this comment could be valid code. Did you maybe forget this after debugging?
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it. The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production. This check looks for comments that seem to be mostly valid code and reports them. ![]() |
|||
52 | $this->merchant_id = $this->testmode ? $this->get_option('test_merchant_id') : $this->get_option('merchant_id'); |
||
0 ignored issues
–
show
|
|||
53 | $this->merchant_password = $this->testmode ? $this->get_option('test_merchant_password') : $this->get_option('merchant_password'); |
||
0 ignored issues
–
show
|
|||
54 | |||
55 | // Hooks |
||
0 ignored issues
–
show
|
|||
56 | add_action('admin_notices', [$this, 'admin_notices']); |
||
0 ignored issues
–
show
|
|||
57 | add_action('woocommerce_update_options_payment_gateways_' . $this->id, [$this, 'process_admin_options']); |
||
0 ignored issues
–
show
|
|||
58 | } |
||
0 ignored issues
–
show
|
|||
59 | |||
60 | /** |
||
0 ignored issues
–
show
|
|||
61 | * Notify of issues in wp-admin |
||
0 ignored issues
–
show
|
|||
62 | */ |
||
0 ignored issues
–
show
|
|||
63 | public function admin_notices() |
||
0 ignored issues
–
show
|
|||
64 | { |
||
0 ignored issues
–
show
|
|||
65 | if ($this->enabled == 'no') |
||
0 ignored issues
–
show
|
|||
66 | { |
||
0 ignored issues
–
show
|
|||
67 | return; |
||
0 ignored issues
–
show
|
|||
68 | } |
||
0 ignored issues
–
show
|
|||
69 | |||
70 | // Check required fields |
||
0 ignored issues
–
show
|
|||
71 | if (!$this->merchant_id) |
||
0 ignored issues
–
show
|
|||
72 | { |
||
0 ignored issues
–
show
|
|||
73 | 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>'; |
||
0 ignored issues
–
show
|
|||
74 | return; |
||
0 ignored issues
–
show
|
|||
75 | } |
||
0 ignored issues
–
show
|
|||
76 | elseif (!$this->merchant_password) |
||
0 ignored issues
–
show
|
|||
77 | { |
||
0 ignored issues
–
show
|
|||
78 | 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>'; |
||
0 ignored issues
–
show
|
|||
79 | return; |
||
0 ignored issues
–
show
|
|||
80 | } |
||
0 ignored issues
–
show
|
|||
81 | |||
82 | // Check if enabled and force SSL is disabled |
||
0 ignored issues
–
show
|
|||
83 | if ( get_option('woocommerce_force_ssl_checkout') == 'no' ) { |
||
0 ignored issues
–
show
|
|||
84 | echo '<div class="error"><p>' . sprintf( __( 'First Atlantic Commerce is enabled, but the <a href="%s">force SSL option</a> is disabled; your checkout may not be secure! Please enable SSL and ensure your server has a valid SSL certificate - First Atlantic Commerce will only work in test mode.', 'woocommerce-gateway-fac' ), admin_url( 'admin.php?page=wc-settings&tab=checkout' ) ) . '</p></div>'; |
||
0 ignored issues
–
show
|
|||
85 | return; |
||
0 ignored issues
–
show
|
|||
86 | } |
||
0 ignored issues
–
show
|
|||
87 | } |
||
0 ignored issues
–
show
|
|||
88 | |||
89 | /** |
||
0 ignored issues
–
show
|
|||
90 | * Logging method |
||
0 ignored issues
–
show
|
|||
91 | * |
||
0 ignored issues
–
show
|
|||
92 | * @param string $message |
||
0 ignored issues
–
show
|
|||
93 | * |
||
0 ignored issues
–
show
|
|||
94 | * @return void |
||
0 ignored issues
–
show
|
|||
95 | */ |
||
0 ignored issues
–
show
|
|||
96 | public function log($message) |
||
0 ignored issues
–
show
|
|||
97 | { |
||
0 ignored issues
–
show
|
|||
98 | if ( empty($this->log) ) |
||
0 ignored issues
–
show
|
|||
99 | { |
||
0 ignored issues
–
show
|
|||
100 | $this->log = new WC_Logger(); |
||
0 ignored issues
–
show
|
|||
101 | } |
||
0 ignored issues
–
show
|
|||
102 | |||
103 | $this->log->add($this->id, $message); |
||
0 ignored issues
–
show
|
|||
104 | } |
||
0 ignored issues
–
show
|
|||
105 | |||
106 | /** |
||
0 ignored issues
–
show
|
|||
107 | * Check if the gateway is available for use |
||
0 ignored issues
–
show
|
|||
108 | * |
||
0 ignored issues
–
show
|
|||
109 | * @return bool |
||
0 ignored issues
–
show
|
|||
110 | */ |
||
0 ignored issues
–
show
|
|||
111 | public function is_available() |
||
0 ignored issues
–
show
|
|||
112 | { |
||
0 ignored issues
–
show
|
|||
113 | $is_available = parent::is_available(); |
||
0 ignored issues
–
show
|
|||
114 | |||
115 | // Only allow unencrypted connections when testing |
||
0 ignored issues
–
show
|
|||
116 | if (!is_ssl() && !$this->testmode) |
||
0 ignored issues
–
show
|
|||
117 | { |
||
0 ignored issues
–
show
|
|||
118 | $is_available = false; |
||
0 ignored issues
–
show
|
|||
119 | } |
||
0 ignored issues
–
show
|
|||
120 | |||
121 | // Required fields check |
||
0 ignored issues
–
show
|
|||
122 | if (!$this->merchant_id || !$this->merchant_password) |
||
0 ignored issues
–
show
|
|||
123 | { |
||
0 ignored issues
–
show
|
|||
124 | $is_available = false; |
||
0 ignored issues
–
show
|
|||
125 | } |
||
0 ignored issues
–
show
|
|||
126 | |||
127 | return $is_available; |
||
0 ignored issues
–
show
|
|||
128 | } |
||
0 ignored issues
–
show
|
|||
129 | |||
130 | /** |
||
0 ignored issues
–
show
|
|||
131 | * Initialise Gateway Settings Form Fields |
||
0 ignored issues
–
show
|
|||
132 | */ |
||
0 ignored issues
–
show
|
|||
133 | public function init_form_fields() |
||
0 ignored issues
–
show
|
|||
134 | { |
||
0 ignored issues
–
show
|
|||
135 | $this->form_fields = apply_filters('wc_fac_settings', [ |
||
0 ignored issues
–
show
|
|||
136 | 'enabled' => [ |
||
0 ignored issues
–
show
|
|||
137 | 'title' => __('Enable/Disable', 'woocommerce-gateway-fac'), |
||
0 ignored issues
–
show
|
|||
138 | 'label' => __('Enable FAC', 'woocommerce-gateway-fac'), |
||
0 ignored issues
–
show
|
|||
139 | 'type' => 'checkbox', |
||
0 ignored issues
–
show
|
|||
140 | 'description' => '', |
||
0 ignored issues
–
show
|
|||
141 | 'default' => 'no' |
||
0 ignored issues
–
show
|
|||
142 | ], |
||
0 ignored issues
–
show
|
|||
143 | 'title' => [ |
||
0 ignored issues
–
show
|
|||
144 | 'title' => __('Title', 'woocommerce-gateway-fac'), |
||
0 ignored issues
–
show
|
|||
145 | 'type' => 'text', |
||
0 ignored issues
–
show
|
|||
146 | 'description' => __('This controls the title which the user sees during checkout.', 'woocommerce-gateway-fac'), |
||
0 ignored issues
–
show
|
|||
147 | 'default' => __('Credit card', 'woocommerce-gateway-fac') |
||
0 ignored issues
–
show
|
|||
148 | ], |
||
0 ignored issues
–
show
|
|||
149 | 'description' => [ |
||
0 ignored issues
–
show
|
|||
150 | 'title' => __('Description', 'woocommerce-gateway-fac'), |
||
0 ignored issues
–
show
|
|||
151 | 'type' => 'textarea', |
||
0 ignored issues
–
show
|
|||
152 | 'description' => __('This controls the description which the user sees during checkout.', 'woocommerce-gateway-fac'), |
||
0 ignored issues
–
show
|
|||
153 | 'default' => __('Pay with your credit card.', 'woocommerce-gateway-fac') |
||
0 ignored issues
–
show
|
|||
154 | ], |
||
0 ignored issues
–
show
|
|||
155 | 'testmode' => [ |
||
0 ignored issues
–
show
|
|||
156 | 'title' => __('Test mode', 'woocommerce-gateway-fac'), |
||
0 ignored issues
–
show
|
|||
157 | 'label' => __('Enable Test Mode', 'woocommerce-gateway-fac'), |
||
0 ignored issues
–
show
|
|||
158 | 'type' => 'checkbox', |
||
0 ignored issues
–
show
|
|||
159 | 'description' => __('Place the payment gateway in test mode using test API credentials.', 'woocommerce-gateway-fac'), |
||
0 ignored issues
–
show
|
|||
160 | 'default' => 'yes' |
||
0 ignored issues
–
show
|
|||
161 | ], |
||
0 ignored issues
–
show
|
|||
162 | 'merchant_id' => [ |
||
0 ignored issues
–
show
|
|||
163 | 'title' => __('Live Merchant ID', 'woocommerce-gateway-fac'), |
||
0 ignored issues
–
show
|
|||
164 | 'type' => 'text', |
||
0 ignored issues
–
show
|
|||
165 | 'description' => __('Get your API credentials from your merchant account.', 'woocommerce-gateway-fac'), |
||
0 ignored issues
–
show
|
|||
166 | 'default' => '' |
||
0 ignored issues
–
show
|
|||
167 | ], |
||
0 ignored issues
–
show
|
|||
168 | 'merchant_password' => [ |
||
0 ignored issues
–
show
|
|||
169 | 'title' => __('Live Merchant Password', 'woocommerce-gateway-fac'), |
||
0 ignored issues
–
show
|
|||
170 | 'type' => 'text', |
||
0 ignored issues
–
show
|
|||
171 | 'description' => __('Get your API credentials from your merchant account.', 'woocommerce-gateway-fac'), |
||
0 ignored issues
–
show
|
|||
172 | 'default' => '' |
||
0 ignored issues
–
show
|
|||
173 | ], |
||
0 ignored issues
–
show
|
|||
174 | 'test_merchant_id' => [ |
||
0 ignored issues
–
show
|
|||
175 | 'title' => __('Test Merchant ID', 'woocommerce-gateway-fac'), |
||
0 ignored issues
–
show
|
|||
176 | 'type' => 'text', |
||
0 ignored issues
–
show
|
|||
177 | 'description' => __('Get your API credentials from your merchant account.', 'woocommerce-gateway-fac'), |
||
0 ignored issues
–
show
|
|||
178 | 'default' => '' |
||
0 ignored issues
–
show
|
|||
179 | ], |
||
0 ignored issues
–
show
|
|||
180 | 'test_merchant_password' => [ |
||
0 ignored issues
–
show
|
|||
181 | 'title' => __('Test Merchant Password', 'woocommerce-gateway-fac'), |
||
0 ignored issues
–
show
|
|||
182 | 'type' => 'text', |
||
0 ignored issues
–
show
|
|||
183 | 'description' => __('Get your API credentials from your merchant account.', 'woocommerce-gateway-fac'), |
||
0 ignored issues
–
show
|
|||
184 | 'default' => '' |
||
0 ignored issues
–
show
|
|||
185 | ], |
||
0 ignored issues
–
show
|
|||
186 | 'capture' => [ |
||
0 ignored issues
–
show
|
|||
187 | 'title' => __('Capture', 'woocommerce-gateway-fac'), |
||
0 ignored issues
–
show
|
|||
188 | 'label' => __('Capture charge immediately', 'woocommerce-gateway-fac'), |
||
0 ignored issues
–
show
|
|||
189 | 'type' => 'checkbox', |
||
0 ignored issues
–
show
|
|||
190 | 'description' => __('Whether or not to immediately capture the charge. When unchecked, the charge issues an authorization and will need to be captured later. Uncaptured charges expire in 7 days.', 'woocommerce-gateway-fac'), |
||
0 ignored issues
–
show
|
|||
191 | 'default' => 'yes' |
||
0 ignored issues
–
show
|
|||
192 | ]/*, |
||
0 ignored issues
–
show
60% of this comment could be valid code. Did you maybe forget this after debugging?
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it. The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production. This check looks for comments that seem to be mostly valid code and reports them. ![]() |
|||
193 | 'saved_cards' => [ |
||
194 | 'title' => __('Saved cards', 'woocommerce-gateway-fac'), |
||
195 | 'label' => __('Enable saved cards', 'woocommerce-gateway-fac'), |
||
196 | 'type' => 'checkbox', |
||
197 | 'description' => __('If enabled, users will be able to pay with a saved card during checkout. Card details are saved on FAC servers, not on your store.', 'woocommerce-gateway-fac'), |
||
198 | 'default' => 'no' |
||
199 | ]*/ |
||
200 | ]); |
||
0 ignored issues
–
show
|
|||
201 | } |
||
0 ignored issues
–
show
|
|||
202 | |||
203 | /** |
||
0 ignored issues
–
show
|
|||
204 | * Setup the gateway object |
||
0 ignored issues
–
show
|
|||
205 | */ |
||
0 ignored issues
–
show
|
|||
206 | public function setup_gateway() |
||
0 ignored issues
–
show
|
|||
207 | { |
||
0 ignored issues
–
show
|
|||
208 | $gateway = Omnipay::create('FirstAtlanticCommerce'); |
||
0 ignored issues
–
show
|
|||
209 | |||
210 | $gateway->setMerchantId($this->merchant_id); |
||
0 ignored issues
–
show
|
|||
211 | $gateway->setMerchantPassword($this->merchant_password); |
||
0 ignored issues
–
show
|
|||
212 | |||
213 | if ($this->testmode) |
||
0 ignored issues
–
show
|
|||
214 | { |
||
0 ignored issues
–
show
|
|||
215 | $gateway->setTestMode(true); |
||
0 ignored issues
–
show
|
|||
216 | } |
||
0 ignored issues
–
show
|
|||
217 | |||
218 | return $gateway; |
||
0 ignored issues
–
show
|
|||
219 | } |
||
0 ignored issues
–
show
|
|||
220 | |||
221 | /** |
||
0 ignored issues
–
show
|
|||
222 | * Output payment fields |
||
0 ignored issues
–
show
|
|||
223 | * |
||
0 ignored issues
–
show
|
|||
224 | * @return void |
||
0 ignored issues
–
show
|
|||
225 | */ |
||
0 ignored issues
–
show
|
|||
226 | public function payment_fields() |
||
0 ignored issues
–
show
|
|||
227 | { |
||
0 ignored issues
–
show
|
|||
228 | // Default credit card form |
||
0 ignored issues
–
show
|
|||
229 | $this->credit_card_form(); |
||
0 ignored issues
–
show
|
|||
230 | } |
||
0 ignored issues
–
show
|
|||
231 | |||
232 | /** |
||
0 ignored issues
–
show
|
|||
233 | * Validate form fields |
||
0 ignored issues
–
show
|
|||
234 | * |
||
0 ignored issues
–
show
|
|||
235 | * @return bool |
||
0 ignored issues
–
show
|
|||
236 | */ |
||
0 ignored issues
–
show
|
|||
237 | public function validate_fields() |
||
0 ignored issues
–
show
validate_fields uses the super-global variable $_POST which is generally not recommended.
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: // Bad
class Router
{
public function generate($path)
{
return $_SERVER['HOST'].$path;
}
}
// Better
class Router
{
private $host;
public function __construct($host)
{
$this->host = $host;
}
public function generate($path)
{
return $this->host.$path;
}
}
class Controller
{
public function myAction(Request $request)
{
// Instead of
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
// Better (assuming you use the Symfony2 request)
$page = $request->query->get('page', 1);
}
}
![]() |
|||
238 | { |
||
0 ignored issues
–
show
|
|||
239 | $validated = true; |
||
0 ignored issues
–
show
|
|||
240 | |||
241 | View Code Duplication | if ( empty($_POST['fac-card-number']) ) |
|
0 ignored issues
–
show
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. ![]() |
|||
242 | { |
||
0 ignored issues
–
show
|
|||
243 | wc_add_notice( $this->get_validation_error( __('Card Number', 'woocommerce-gateway-fac'), $_POST['fac-card-number'] ), 'error' ); |
||
0 ignored issues
–
show
|
|||
244 | $validated = false; |
||
0 ignored issues
–
show
|
|||
245 | } |
||
0 ignored issues
–
show
|
|||
246 | View Code Duplication | if ( empty($_POST['fac-card-expiry']) ) |
|
0 ignored issues
–
show
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. ![]() |
|||
247 | { |
||
0 ignored issues
–
show
|
|||
248 | wc_add_notice( $this->get_validation_error( __('Card Expiry', 'woocommerce-gateway-fac'), $_POST['fac-card-number'] ), 'error' ); |
||
0 ignored issues
–
show
|
|||
249 | $validated = false; |
||
0 ignored issues
–
show
|
|||
250 | } |
||
0 ignored issues
–
show
|
|||
251 | View Code Duplication | if ( empty($_POST['fac-card-cvc']) ) |
|
0 ignored issues
–
show
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. ![]() |
|||
252 | { |
||
0 ignored issues
–
show
|
|||
253 | wc_add_notice( $this->get_validation_error( __('Card Code', 'woocommerce-gateway-fac'), $_POST['fac-card-number'] ), 'error' ); |
||
0 ignored issues
–
show
|
|||
254 | $validated = false; |
||
0 ignored issues
–
show
|
|||
255 | } |
||
0 ignored issues
–
show
|
|||
256 | |||
257 | return $validated; |
||
0 ignored issues
–
show
|
|||
258 | } |
||
0 ignored issues
–
show
|
|||
259 | |||
260 | /** |
||
0 ignored issues
–
show
|
|||
261 | * Get error message for form fields |
||
0 ignored issues
–
show
|
|||
262 | * |
||
0 ignored issues
–
show
|
|||
263 | * @param string $field |
||
0 ignored issues
–
show
|
|||
264 | * @param string $type |
||
0 ignored issues
–
show
|
|||
265 | * @return string |
||
0 ignored issues
–
show
|
|||
266 | */ |
||
0 ignored issues
–
show
|
|||
267 | protected function get_validation_error($field, $type = 'undefined') |
||
0 ignored issues
–
show
|
|||
268 | { |
||
0 ignored issues
–
show
|
|||
269 | if ( $type === 'invalid' ) |
||
0 ignored issues
–
show
|
|||
270 | { |
||
0 ignored issues
–
show
|
|||
271 | return sprintf( __( 'Please enter a valid %s.', 'woocommerce-gateway-fac' ), "<strong>$field</strong>" ); |
||
0 ignored issues
–
show
|
|||
272 | } |
||
0 ignored issues
–
show
|
|||
273 | else |
||
0 ignored issues
–
show
|
|||
274 | { |
||
0 ignored issues
–
show
|
|||
275 | return sprintf( __( '%s is a required field.', 'woocommerce-gateway-fac' ), "<strong>$field</strong>" ); |
||
0 ignored issues
–
show
|
|||
276 | } |
||
0 ignored issues
–
show
|
|||
277 | } |
||
0 ignored issues
–
show
|
|||
278 | |||
279 | /** |
||
0 ignored issues
–
show
|
|||
280 | * Can the order be processed? |
||
0 ignored issues
–
show
|
|||
281 | * |
||
0 ignored issues
–
show
|
|||
282 | * @param WC_Order $order |
||
0 ignored issues
–
show
|
|||
283 | * |
||
0 ignored issues
–
show
|
|||
284 | * @return bool |
||
0 ignored issues
–
show
|
|||
285 | */ |
||
0 ignored issues
–
show
|
|||
286 | public function can_process_order($order) |
||
0 ignored issues
–
show
|
|||
287 | { |
||
0 ignored issues
–
show
|
|||
288 | return $order && $order->payment_method == 'fac'; |
||
0 ignored issues
–
show
|
|||
289 | } |
||
0 ignored issues
–
show
|
|||
290 | |||
291 | /** |
||
0 ignored issues
–
show
|
|||
292 | * Process the payment and return the result |
||
0 ignored issues
–
show
|
|||
293 | * |
||
0 ignored issues
–
show
|
|||
294 | * @param int $order_id |
||
0 ignored issues
–
show
|
|||
295 | * |
||
0 ignored issues
–
show
|
|||
296 | * @return array |
||
0 ignored issues
–
show
|
|||
297 | */ |
||
0 ignored issues
–
show
|
|||
298 | public function process_payment($order_id) |
||
0 ignored issues
–
show
process_payment uses the super-global variable $_POST which is generally not recommended.
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: // Bad
class Router
{
public function generate($path)
{
return $_SERVER['HOST'].$path;
}
}
// Better
class Router
{
private $host;
public function __construct($host)
{
$this->host = $host;
}
public function generate($path)
{
return $this->host.$path;
}
}
class Controller
{
public function myAction(Request $request)
{
// Instead of
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
// Better (assuming you use the Symfony2 request)
$page = $request->query->get('page', 1);
}
}
![]() |
|||
299 | { |
||
0 ignored issues
–
show
|
|||
300 | $order = new WC_Order($order_id); |
||
0 ignored issues
–
show
|
|||
301 | |||
302 | if ( !$this->can_process_order($order) ) return; |
||
0 ignored issues
–
show
|
|||
303 | |||
304 | $transaction = $order->get_transaction_id(); |
||
0 ignored issues
–
show
|
|||
305 | $captured = get_post_meta($order_id, '_fac_captured', true); |
||
0 ignored issues
–
show
|
|||
306 | |||
307 | // Skip already captured transactions |
||
0 ignored issues
–
show
|
|||
308 | if ($captured) return; |
||
0 ignored issues
–
show
|
|||
309 | |||
310 | try |
||
0 ignored issues
–
show
|
|||
311 | { |
||
0 ignored issues
–
show
|
|||
312 | $gateway = $this->setup_gateway(); |
||
0 ignored issues
–
show
|
|||
313 | |||
314 | $data = [ |
||
0 ignored issues
–
show
|
|||
315 | 'transactionId' => $order->get_order_number(), |
||
0 ignored issues
–
show
|
|||
316 | 'amount' => $this->get_order_total(), |
||
0 ignored issues
–
show
|
|||
317 | 'currency' => $order->order_currency |
||
0 ignored issues
–
show
|
|||
318 | ]; |
||
0 ignored issues
–
show
|
|||
319 | |||
320 | // Already authorized transactions should be captured |
||
0 ignored issues
–
show
|
|||
321 | if ( $transaction && !$captured ) |
||
0 ignored issues
–
show
|
|||
322 | { |
||
0 ignored issues
–
show
|
|||
323 | $response = $gateway->capture($data)->send(); |
||
0 ignored issues
–
show
|
|||
324 | } |
||
0 ignored issues
–
show
|
|||
325 | else |
||
0 ignored issues
–
show
|
|||
326 | { |
||
0 ignored issues
–
show
|
|||
327 | $card_number = str_replace( [' ', '-'], '', wc_clean($_POST['fac-card-number']) ); |
||
0 ignored issues
–
show
|
|||
328 | $card_cvv = wc_clean($_POST['fac-card-cvc']); |
||
0 ignored issues
–
show
|
|||
329 | $card_expiry = preg_split('/\s?\/\s?/', wc_clean($_POST['fac-card-expiry']), 2); |
||
0 ignored issues
–
show
|
|||
330 | |||
331 | $data['card'] = [ |
||
0 ignored issues
–
show
|
|||
332 | 'firstName' => $order->billing_first_name, |
||
0 ignored issues
–
show
|
|||
333 | 'lastName' => $order->billing_last_name, |
||
0 ignored issues
–
show
|
|||
334 | 'number' => $card_number, |
||
0 ignored issues
–
show
|
|||
335 | 'expiryMonth' => $card_expiry[0], |
||
0 ignored issues
–
show
|
|||
336 | 'expiryYear' => $card_expiry[1], |
||
0 ignored issues
–
show
|
|||
337 | 'cvv' => $card_cvv, |
||
0 ignored issues
–
show
|
|||
338 | 'billingAddress1' => $order->billing_address_1, |
||
0 ignored issues
–
show
|
|||
339 | 'billingAddress2' => $order->billing_address_2, |
||
0 ignored issues
–
show
|
|||
340 | 'billingCity' => $order->billing_city, |
||
0 ignored issues
–
show
|
|||
341 | 'billingPostcode' => $order->billing_postcode, |
||
0 ignored issues
–
show
|
|||
342 | 'billingState' => $order->billing_state, |
||
0 ignored issues
–
show
|
|||
343 | 'billingCountry' => $order->billing_country, |
||
0 ignored issues
–
show
|
|||
344 | 'email' => $order->billing_email |
||
0 ignored issues
–
show
|
|||
345 | ]; |
||
0 ignored issues
–
show
|
|||
346 | |||
347 | // Capture in one pass if enabled, otherwise authorize |
||
0 ignored issues
–
show
|
|||
348 | if ($this->capture) |
||
0 ignored issues
–
show
|
|||
349 | { |
||
0 ignored issues
–
show
|
|||
350 | $response = $gateway->purchase($data)->send(); |
||
0 ignored issues
–
show
|
|||
351 | } |
||
0 ignored issues
–
show
|
|||
352 | else |
||
0 ignored issues
–
show
|
|||
353 | { |
||
0 ignored issues
–
show
|
|||
354 | $response = $gateway->authorize($data)->send(); |
||
0 ignored issues
–
show
|
|||
355 | } |
||
0 ignored issues
–
show
|
|||
356 | } |
||
0 ignored issues
–
show
|
|||
357 | |||
358 | if ( $response->isSuccessful() ) |
||
0 ignored issues
–
show
|
|||
359 | { |
||
0 ignored issues
–
show
|
|||
360 | $reference = $response->getTransactionReference(); |
||
0 ignored issues
–
show
|
|||
361 | |||
362 | // Captured transaction |
||
0 ignored issues
–
show
|
|||
363 | if ( ($transaction && !$captured) || (!$transaction && $this->capture) ) |
||
0 ignored issues
–
show
|
|||
364 | { |
||
0 ignored issues
–
show
|
|||
365 | // Store captured |
||
0 ignored issues
–
show
|
|||
366 | update_post_meta($order_id, '_fac_captured', true); |
||
0 ignored issues
–
show
|
|||
367 | |||
368 | // Complete payment |
||
0 ignored issues
–
show
|
|||
369 | $order->payment_complete($reference); |
||
0 ignored issues
–
show
|
|||
370 | |||
371 | // Add note to order |
||
0 ignored issues
–
show
|
|||
372 | $order->add_order_note( sprintf( __('FAC transaction complete (ID: %s)', 'woocommerce-gateway-fac'), $reference ) ); |
||
0 ignored issues
–
show
|
|||
373 | } |
||
0 ignored issues
–
show
|
|||
374 | // Authorized transaction |
||
0 ignored issues
–
show
|
|||
375 | else |
||
0 ignored issues
–
show
|
|||
376 | { |
||
0 ignored issues
–
show
|
|||
377 | // Store captured |
||
0 ignored issues
–
show
|
|||
378 | update_post_meta($order_id, '_transaction_id', $reference, true); |
||
0 ignored issues
–
show
|
|||
379 | update_post_meta($order_id, '_fac_captured', false); |
||
0 ignored issues
–
show
|
|||
380 | |||
381 | // Mark order as on-hold and add note |
||
0 ignored issues
–
show
|
|||
382 | $order->update_status( 'on-hold', sprintf( __('FAC charge authorized (ID: %s). Process the order to take payment, or cancel to remove the pre-authorization.', 'woocommerce-gateway-fac'), $reference ) ); |
||
0 ignored issues
–
show
|
|||
383 | |||
384 | // Reduce stock level |
||
0 ignored issues
–
show
|
|||
385 | $order->reduce_order_stock(); |
||
0 ignored issues
–
show
|
|||
386 | } |
||
0 ignored issues
–
show
|
|||
387 | |||
388 | // Clear cart |
||
0 ignored issues
–
show
|
|||
389 | WC()->cart->empty_cart(); |
||
0 ignored issues
–
show
|
|||
390 | |||
391 | // Return thank you page redirect |
||
0 ignored issues
–
show
|
|||
392 | return [ |
||
0 ignored issues
–
show
|
|||
393 | 'result' => 'success', |
||
0 ignored issues
–
show
|
|||
394 | 'redirect' => $this->get_return_url($order) |
||
0 ignored issues
–
show
|
|||
395 | ]; |
||
0 ignored issues
–
show
|
|||
396 | } |
||
0 ignored issues
–
show
|
|||
397 | else |
||
0 ignored issues
–
show
|
|||
398 | { |
||
0 ignored issues
–
show
|
|||
399 | throw new InvalidResponseException( $response->getMessage(), $response->getCode() ); |
||
0 ignored issues
–
show
|
|||
400 | } |
||
0 ignored issues
–
show
|
|||
401 | } |
||
0 ignored issues
–
show
|
|||
402 | catch (OmnipayException $e) |
||
0 ignored issues
–
show
|
|||
403 | { |
||
0 ignored issues
–
show
|
|||
404 | $message = 'Transaction Failed: '. $e->getCode() .' – '. $e->getMessage(); |
||
0 ignored issues
–
show
|
|||
405 | |||
406 | $this->log($message); |
||
0 ignored issues
–
show
|
|||
407 | $order->add_order_note( __($message, 'woocommerce-gateway-fac') ); |
||
0 ignored issues
–
show
|
|||
408 | |||
409 | // Friendly declined message |
||
0 ignored issues
–
show
|
|||
410 | if ( in_array( $e->getCode(), [2, 3, 4, 35, 38, 39] ) ) |
||
0 ignored issues
–
show
|
|||
411 | { |
||
0 ignored issues
–
show
|
|||
412 | $message = __('Unfortunately your order cannot be processed as the originating bank/merchant has declined your transaction.', 'woocommerce') .' '. __('Please attempt your purchase again.', 'woocommerce'); |
||
0 ignored issues
–
show
|
|||
413 | } |
||
0 ignored issues
–
show
|
|||
414 | |||
415 | // Friendly error message |
||
0 ignored issues
–
show
|
|||
416 | else |
||
0 ignored issues
–
show
|
|||
417 | { |
||
0 ignored issues
–
show
|
|||
418 | $message = __('Unfortunately your order cannot be processed as an error has occured.', 'woocommerce') .' '. __('Please attempt your purchase again.', 'woocommerce'); |
||
0 ignored issues
–
show
|
|||
419 | } |
||
0 ignored issues
–
show
|
|||
420 | |||
421 | if ( !is_admin() || ( defined('DOING_AJAX') && DOING_AJAX ) ) |
||
0 ignored issues
–
show
|
|||
422 | { |
||
0 ignored issues
–
show
|
|||
423 | wc_add_notice($message, 'error'); |
||
0 ignored issues
–
show
|
|||
424 | } |
||
0 ignored issues
–
show
|
|||
425 | |||
426 | return; |
||
0 ignored issues
–
show
|
|||
427 | } |
||
0 ignored issues
–
show
|
|||
428 | } |
||
0 ignored issues
–
show
|
|||
429 | |||
430 | /** |
||
0 ignored issues
–
show
|
|||
431 | * Can the order be refunded? |
||
0 ignored issues
–
show
|
|||
432 | * |
||
0 ignored issues
–
show
|
|||
433 | * @param WC_Order $order |
||
0 ignored issues
–
show
|
|||
434 | * |
||
0 ignored issues
–
show
|
|||
435 | * @return bool |
||
0 ignored issues
–
show
|
|||
436 | */ |
||
0 ignored issues
–
show
|
|||
437 | public function can_refund_order($order) |
||
0 ignored issues
–
show
|
|||
438 | { |
||
0 ignored issues
–
show
|
|||
439 | return $order && $order->payment_method == 'fac' && $order->get_transaction_id(); |
||
0 ignored issues
–
show
|
|||
440 | } |
||
0 ignored issues
–
show
|
|||
441 | |||
442 | /** |
||
0 ignored issues
–
show
|
|||
443 | * Refund a charge |
||
0 ignored issues
–
show
|
|||
444 | * |
||
0 ignored issues
–
show
|
|||
445 | * @param int $order_id |
||
0 ignored issues
–
show
|
|||
446 | * @param float $amount |
||
0 ignored issues
–
show
Should the type for parameter
$amount not be double|null ?
This check looks for It makes a suggestion as to what type it considers more descriptive. Most often this is a case of a parameter that can be null in addition to its declared types. ![]() |
|||
447 | * |
||
0 ignored issues
–
show
|
|||
448 | * @return bool |
||
0 ignored issues
–
show
|
|||
449 | */ |
||
0 ignored issues
–
show
|
|||
450 | public function process_refund($order_id, $amount = null, $reason = '') |
||
0 ignored issues
–
show
|
|||
451 | { |
||
0 ignored issues
–
show
|
|||
452 | $order = wc_get_order($order_id); |
||
0 ignored issues
–
show
|
|||
453 | |||
454 | if ( !$this->can_refund_order($order) ) |
||
0 ignored issues
–
show
|
|||
455 | { |
||
0 ignored issues
–
show
|
|||
456 | $this->log('Refund Failed: No transaction ID for FAC'); |
||
0 ignored issues
–
show
|
|||
457 | return false; |
||
0 ignored issues
–
show
|
|||
458 | } |
||
0 ignored issues
–
show
|
|||
459 | |||
460 | $transaction = $order->get_transaction_id(); |
||
0 ignored issues
–
show
$transaction is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the ![]() |
|||
461 | $captured = get_post_meta($order_id, '_fac_captured', true); |
||
0 ignored issues
–
show
|
|||
462 | |||
463 | if ( is_null($amount) ) |
||
0 ignored issues
–
show
|
|||
464 | { |
||
0 ignored issues
–
show
|
|||
465 | $amount = $order->get_total(); |
||
0 ignored issues
–
show
|
|||
466 | } |
||
0 ignored issues
–
show
|
|||
467 | |||
468 | try |
||
0 ignored issues
–
show
|
|||
469 | { |
||
0 ignored issues
–
show
|
|||
470 | $gateway = $this->setup_gateway(); |
||
0 ignored issues
–
show
|
|||
471 | |||
472 | $data = [ |
||
0 ignored issues
–
show
|
|||
473 | 'transactionId' => $order->get_order_number(), |
||
0 ignored issues
–
show
|
|||
474 | 'amount' => $amount |
||
0 ignored issues
–
show
|
|||
475 | ]; |
||
0 ignored issues
–
show
|
|||
476 | |||
477 | if ($captured) |
||
0 ignored issues
–
show
|
|||
478 | { |
||
0 ignored issues
–
show
|
|||
479 | $response = $gateway->refund($data)->send(); |
||
0 ignored issues
–
show
|
|||
480 | } |
||
0 ignored issues
–
show
|
|||
481 | else |
||
0 ignored issues
–
show
|
|||
482 | { |
||
0 ignored issues
–
show
|
|||
483 | $response = $gateway->void($data)->send(); |
||
0 ignored issues
–
show
|
|||
484 | } |
||
0 ignored issues
–
show
|
|||
485 | |||
486 | if ( $response->isSuccessful() ) |
||
0 ignored issues
–
show
|
|||
487 | { |
||
0 ignored issues
–
show
|
|||
488 | $order->add_order_note( sprintf( __('Refunded %s', 'woocommerce-gateway-fac'), $data['amount'] ) ); |
||
0 ignored issues
–
show
|
|||
489 | return true; |
||
0 ignored issues
–
show
|
|||
490 | } |
||
0 ignored issues
–
show
|
|||
491 | else |
||
0 ignored issues
–
show
|
|||
492 | { |
||
0 ignored issues
–
show
|
|||
493 | throw new InvalidResponseException( $response->getMessage(), $response->getCode() ); |
||
0 ignored issues
–
show
|
|||
494 | } |
||
0 ignored issues
–
show
|
|||
495 | } |
||
0 ignored issues
–
show
|
|||
496 | catch (OmnipayException $e) |
||
0 ignored issues
–
show
|
|||
497 | { |
||
0 ignored issues
–
show
|
|||
498 | $message = 'Refund Failed: '. $e->getCode() .' – '. $e->getMessage(); |
||
0 ignored issues
–
show
|
|||
499 | |||
500 | $this->log($message); |
||
0 ignored issues
–
show
|
|||
501 | $order->add_order_note( __($message, 'woocommerce-gateway-fac') ); |
||
0 ignored issues
–
show
|
|||
502 | |||
503 | return new WP_Error( 'fac-refund', __($e->getCode() .' – '.$e->getMessage(), 'woocommerce-gateway-fac') ); |
||
0 ignored issues
–
show
|
|||
504 | } |
||
0 ignored issues
–
show
|
|||
505 | } |
||
0 ignored issues
–
show
|
|||
506 | } |
||
507 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.