@@ -1,5 +1,5 @@ discard block |
||
1 | 1 | <?php |
2 | -if ( ! defined( 'ABSPATH' ) ) { |
|
2 | +if ( ! defined('ABSPATH')) { |
|
3 | 3 | exit; |
4 | 4 | } |
5 | 5 | |
@@ -20,12 +20,12 @@ discard block |
||
20 | 20 | public function __construct() { |
21 | 21 | self::$_this = $this; |
22 | 22 | |
23 | - add_action( 'wp', array( $this, 'maybe_process_redirect_order' ) ); |
|
24 | - add_action( 'woocommerce_order_status_on-hold_to_processing', array( $this, 'capture_payment' ) ); |
|
25 | - add_action( 'woocommerce_order_status_on-hold_to_completed', array( $this, 'capture_payment' ) ); |
|
26 | - add_action( 'woocommerce_order_status_on-hold_to_cancelled', array( $this, 'cancel_payment' ) ); |
|
27 | - add_action( 'woocommerce_order_status_on-hold_to_refunded', array( $this, 'cancel_payment' ) ); |
|
28 | - add_action( 'wc_ajax_wc_stripe_validate_checkout', array( $this, 'validate_checkout' ) ); |
|
23 | + add_action('wp', array($this, 'maybe_process_redirect_order')); |
|
24 | + add_action('woocommerce_order_status_on-hold_to_processing', array($this, 'capture_payment')); |
|
25 | + add_action('woocommerce_order_status_on-hold_to_completed', array($this, 'capture_payment')); |
|
26 | + add_action('woocommerce_order_status_on-hold_to_cancelled', array($this, 'cancel_payment')); |
|
27 | + add_action('woocommerce_order_status_on-hold_to_refunded', array($this, 'cancel_payment')); |
|
28 | + add_action('wc_ajax_wc_stripe_validate_checkout', array($this, 'validate_checkout')); |
|
29 | 29 | } |
30 | 30 | |
31 | 31 | /** |
@@ -46,25 +46,25 @@ discard block |
||
46 | 46 | * @since 4.0.0 |
47 | 47 | * @version 4.0.0 |
48 | 48 | */ |
49 | - public function process_redirect_payment( $order_id, $retry = true ) { |
|
49 | + public function process_redirect_payment($order_id, $retry = true) { |
|
50 | 50 | try { |
51 | - $source = wc_clean( $_GET['source'] ); |
|
51 | + $source = wc_clean($_GET['source']); |
|
52 | 52 | |
53 | - if ( empty( $source ) ) { |
|
53 | + if (empty($source)) { |
|
54 | 54 | return; |
55 | 55 | } |
56 | 56 | |
57 | - if ( empty( $order_id ) ) { |
|
57 | + if (empty($order_id)) { |
|
58 | 58 | return; |
59 | 59 | } |
60 | 60 | |
61 | - $order = wc_get_order( $order_id ); |
|
61 | + $order = wc_get_order($order_id); |
|
62 | 62 | |
63 | - if ( ! is_object( $order ) ) { |
|
63 | + if ( ! is_object($order)) { |
|
64 | 64 | return; |
65 | 65 | } |
66 | 66 | |
67 | - if ( 'processing' === $order->get_status() || 'completed' === $order->get_status() || 'on-hold' === $order->get_status() ) { |
|
67 | + if ('processing' === $order->get_status() || 'completed' === $order->get_status() || 'on-hold' === $order->get_status()) { |
|
68 | 68 | return; |
69 | 69 | } |
70 | 70 | |
@@ -72,108 +72,108 @@ discard block |
||
72 | 72 | $response = null; |
73 | 73 | |
74 | 74 | // This will throw exception if not valid. |
75 | - $this->validate_minimum_order_amount( $order ); |
|
75 | + $this->validate_minimum_order_amount($order); |
|
76 | 76 | |
77 | - WC_Stripe_Logger::log( "Info: (Redirect) Begin processing payment for order $order_id for the amount of {$order->get_total()}" ); |
|
77 | + WC_Stripe_Logger::log("Info: (Redirect) Begin processing payment for order $order_id for the amount of {$order->get_total()}"); |
|
78 | 78 | |
79 | 79 | /** |
80 | 80 | * First check if the source is chargeable at this time. If not, |
81 | 81 | * webhook will take care of it later. |
82 | 82 | */ |
83 | - $source_info = WC_Stripe_API::retrieve( 'sources/' . $source ); |
|
83 | + $source_info = WC_Stripe_API::retrieve('sources/' . $source); |
|
84 | 84 | |
85 | - if ( ! empty( $source_info->error ) ) { |
|
86 | - throw new WC_Stripe_Exception( print_r( $source_info, true ), $source_info->error->message ); |
|
85 | + if ( ! empty($source_info->error)) { |
|
86 | + throw new WC_Stripe_Exception(print_r($source_info, true), $source_info->error->message); |
|
87 | 87 | } |
88 | 88 | |
89 | - if ( 'failed' === $source_info->status || 'canceled' === $source_info->status ) { |
|
90 | - throw new WC_Stripe_Exception( print_r( $source_info, true ), __( 'Unable to process this payment, please try again or use alternative method.', 'woocommerce-gateway-stripe' ) ); |
|
89 | + if ('failed' === $source_info->status || 'canceled' === $source_info->status) { |
|
90 | + throw new WC_Stripe_Exception(print_r($source_info, true), __('Unable to process this payment, please try again or use alternative method.', 'woocommerce-gateway-stripe')); |
|
91 | 91 | } |
92 | 92 | |
93 | 93 | // If already consumed, then ignore request. |
94 | - if ( 'consumed' === $source_info->status ) { |
|
94 | + if ('consumed' === $source_info->status) { |
|
95 | 95 | return; |
96 | 96 | } |
97 | 97 | |
98 | 98 | // If not chargeable, then ignore request. |
99 | - if ( 'chargeable' !== $source_info->status ) { |
|
99 | + if ('chargeable' !== $source_info->status) { |
|
100 | 100 | return; |
101 | 101 | } |
102 | 102 | |
103 | 103 | // Prep source object. |
104 | 104 | $source_object = new stdClass(); |
105 | 105 | $source_object->token_id = ''; |
106 | - $source_object->customer = $this->get_stripe_customer_id( $order ); |
|
106 | + $source_object->customer = $this->get_stripe_customer_id($order); |
|
107 | 107 | $source_object->source = $source_info->id; |
108 | 108 | |
109 | 109 | // Make the request. |
110 | - $response = WC_Stripe_API::request( $this->generate_payment_request( $order, $source_object ) ); |
|
110 | + $response = WC_Stripe_API::request($this->generate_payment_request($order, $source_object)); |
|
111 | 111 | |
112 | - if ( ! empty( $response->error ) ) { |
|
112 | + if ( ! empty($response->error)) { |
|
113 | 113 | // If it is an API error such connection or server, let's retry. |
114 | - if ( 'api_connection_error' === $response->error->type || 'api_error' === $response->error->type ) { |
|
115 | - if ( $retry ) { |
|
116 | - sleep( 5 ); |
|
117 | - return $this->process_redirect_payment( $order_id, false ); |
|
114 | + if ('api_connection_error' === $response->error->type || 'api_error' === $response->error->type) { |
|
115 | + if ($retry) { |
|
116 | + sleep(5); |
|
117 | + return $this->process_redirect_payment($order_id, false); |
|
118 | 118 | } else { |
119 | 119 | $message = 'API connection error and retries exhausted.'; |
120 | - $order->add_order_note( $message ); |
|
121 | - throw new WC_Stripe_Exception( print_r( $response, true ), $message ); |
|
120 | + $order->add_order_note($message); |
|
121 | + throw new WC_Stripe_Exception(print_r($response, true), $message); |
|
122 | 122 | } |
123 | 123 | } |
124 | 124 | |
125 | 125 | // Customer param wrong? The user may have been deleted on stripe's end. Remove customer_id. Can be retried without. |
126 | - if ( preg_match( '/No such customer/i', $response->error->message ) && $retry ) { |
|
127 | - if ( WC_Stripe_Helper::is_pre_30() ) { |
|
128 | - delete_user_meta( $order->customer_user, '_stripe_customer_id' ); |
|
129 | - delete_post_meta( $order_id, '_stripe_customer_id' ); |
|
126 | + if (preg_match('/No such customer/i', $response->error->message) && $retry) { |
|
127 | + if (WC_Stripe_Helper::is_pre_30()) { |
|
128 | + delete_user_meta($order->customer_user, '_stripe_customer_id'); |
|
129 | + delete_post_meta($order_id, '_stripe_customer_id'); |
|
130 | 130 | } else { |
131 | - delete_user_meta( $order->get_customer_id(), '_stripe_customer_id' ); |
|
132 | - $order->delete_meta_data( '_stripe_customer_id' ); |
|
131 | + delete_user_meta($order->get_customer_id(), '_stripe_customer_id'); |
|
132 | + $order->delete_meta_data('_stripe_customer_id'); |
|
133 | 133 | $order->save(); |
134 | 134 | } |
135 | 135 | |
136 | - return $this->process_redirect_payment( $order_id, false ); |
|
136 | + return $this->process_redirect_payment($order_id, false); |
|
137 | 137 | |
138 | - } elseif ( preg_match( '/No such token/i', $response->error->message ) && $source_object->token_id ) { |
|
138 | + } elseif (preg_match('/No such token/i', $response->error->message) && $source_object->token_id) { |
|
139 | 139 | // Source param wrong? The CARD may have been deleted on stripe's end. Remove token and show message. |
140 | 140 | |
141 | - $wc_token = WC_Payment_Tokens::get( $source_object->token_id ); |
|
141 | + $wc_token = WC_Payment_Tokens::get($source_object->token_id); |
|
142 | 142 | $wc_token->delete(); |
143 | - $message = __( 'This card is no longer available and has been removed.', 'woocommerce-gateway-stripe' ); |
|
144 | - $order->add_order_note( $message ); |
|
145 | - throw new WC_Stripe_Exception( print_r( $response, true ), $message ); |
|
143 | + $message = __('This card is no longer available and has been removed.', 'woocommerce-gateway-stripe'); |
|
144 | + $order->add_order_note($message); |
|
145 | + throw new WC_Stripe_Exception(print_r($response, true), $message); |
|
146 | 146 | } |
147 | 147 | |
148 | 148 | $localized_messages = WC_Stripe_Helper::get_localized_messages(); |
149 | 149 | |
150 | - if ( 'card_error' === $response->error->type ) { |
|
151 | - $message = isset( $localized_messages[ $response->error->code ] ) ? $localized_messages[ $response->error->code ] : $response->error->message; |
|
150 | + if ('card_error' === $response->error->type) { |
|
151 | + $message = isset($localized_messages[$response->error->code]) ? $localized_messages[$response->error->code] : $response->error->message; |
|
152 | 152 | } else { |
153 | - $message = isset( $localized_messages[ $response->error->type ] ) ? $localized_messages[ $response->error->type ] : $response->error->message; |
|
153 | + $message = isset($localized_messages[$response->error->type]) ? $localized_messages[$response->error->type] : $response->error->message; |
|
154 | 154 | } |
155 | 155 | |
156 | - throw new WC_Stripe_Exception( print_r( $response, true ), $message ); |
|
156 | + throw new WC_Stripe_Exception(print_r($response, true), $message); |
|
157 | 157 | } |
158 | 158 | |
159 | - do_action( 'wc_gateway_stripe_process_redirect_payment', $response, $order ); |
|
159 | + do_action('wc_gateway_stripe_process_redirect_payment', $response, $order); |
|
160 | 160 | |
161 | - $this->process_response( $response, $order ); |
|
161 | + $this->process_response($response, $order); |
|
162 | 162 | |
163 | - } catch ( WC_Stripe_Exception $e ) { |
|
164 | - WC_Stripe_Logger::log( 'Error: ' . $e->getMessage() ); |
|
163 | + } catch (WC_Stripe_Exception $e) { |
|
164 | + WC_Stripe_Logger::log('Error: ' . $e->getMessage()); |
|
165 | 165 | |
166 | - do_action( 'wc_gateway_stripe_process_redirect_payment_error', $e, $order ); |
|
166 | + do_action('wc_gateway_stripe_process_redirect_payment_error', $e, $order); |
|
167 | 167 | |
168 | 168 | /* translators: error message */ |
169 | - $order->update_status( 'failed', sprintf( __( 'Stripe payment failed: %s', 'woocommerce-gateway-stripe' ), $e->getLocalizedMessage() ) ); |
|
169 | + $order->update_status('failed', sprintf(__('Stripe payment failed: %s', 'woocommerce-gateway-stripe'), $e->getLocalizedMessage())); |
|
170 | 170 | |
171 | - if ( $order->has_status( array( 'pending', 'failed' ) ) ) { |
|
172 | - $this->send_failed_order_email( $order_id ); |
|
171 | + if ($order->has_status(array('pending', 'failed'))) { |
|
172 | + $this->send_failed_order_email($order_id); |
|
173 | 173 | } |
174 | 174 | |
175 | - wc_add_notice( $e->getLocalizedMessage(), 'error' ); |
|
176 | - wp_safe_redirect( wc_get_checkout_url() ); |
|
175 | + wc_add_notice($e->getLocalizedMessage(), 'error'); |
|
176 | + wp_safe_redirect(wc_get_checkout_url()); |
|
177 | 177 | exit; |
178 | 178 | } |
179 | 179 | } |
@@ -185,13 +185,13 @@ discard block |
||
185 | 185 | * @version 4.0.0 |
186 | 186 | */ |
187 | 187 | public function maybe_process_redirect_order() { |
188 | - if ( ! is_order_received_page() || empty( $_GET['client_secret'] ) || empty( $_GET['source'] ) ) { |
|
188 | + if ( ! is_order_received_page() || empty($_GET['client_secret']) || empty($_GET['source'])) { |
|
189 | 189 | return; |
190 | 190 | } |
191 | 191 | |
192 | - $order_id = wc_clean( $_GET['order_id'] ); |
|
192 | + $order_id = wc_clean($_GET['order_id']); |
|
193 | 193 | |
194 | - $this->process_redirect_payment( $order_id ); |
|
194 | + $this->process_redirect_payment($order_id); |
|
195 | 195 | } |
196 | 196 | |
197 | 197 | /** |
@@ -201,52 +201,52 @@ discard block |
||
201 | 201 | * @version 4.0.0 |
202 | 202 | * @param int $order_id |
203 | 203 | */ |
204 | - public function capture_payment( $order_id ) { |
|
205 | - $order = wc_get_order( $order_id ); |
|
204 | + public function capture_payment($order_id) { |
|
205 | + $order = wc_get_order($order_id); |
|
206 | 206 | |
207 | - if ( 'stripe' === ( WC_Stripe_Helper::is_pre_30() ? $order->payment_method : $order->get_payment_method() ) ) { |
|
208 | - $charge = WC_Stripe_Helper::is_pre_30() ? get_post_meta( $order_id, '_transaction_id', true ) : $order->get_transaction_id(); |
|
209 | - $captured = WC_Stripe_Helper::is_pre_30() ? get_post_meta( $order_id, '_stripe_charge_captured', true ) : $order->get_meta( '_stripe_charge_captured', true ); |
|
207 | + if ('stripe' === (WC_Stripe_Helper::is_pre_30() ? $order->payment_method : $order->get_payment_method())) { |
|
208 | + $charge = WC_Stripe_Helper::is_pre_30() ? get_post_meta($order_id, '_transaction_id', true) : $order->get_transaction_id(); |
|
209 | + $captured = WC_Stripe_Helper::is_pre_30() ? get_post_meta($order_id, '_stripe_charge_captured', true) : $order->get_meta('_stripe_charge_captured', true); |
|
210 | 210 | |
211 | - if ( $charge && 'no' === $captured ) { |
|
211 | + if ($charge && 'no' === $captured) { |
|
212 | 212 | $order_total = $order->get_total(); |
213 | 213 | |
214 | - if ( 0 < $order->get_total_refunded() ) { |
|
214 | + if (0 < $order->get_total_refunded()) { |
|
215 | 215 | $order_total = $order_total - $order->get_total_refunded(); |
216 | 216 | } |
217 | 217 | |
218 | - $result = WC_Stripe_API::request( array( |
|
219 | - 'amount' => WC_Stripe_Helper::get_stripe_amount( $order_total ), |
|
218 | + $result = WC_Stripe_API::request(array( |
|
219 | + 'amount' => WC_Stripe_Helper::get_stripe_amount($order_total), |
|
220 | 220 | 'expand[]' => 'balance_transaction', |
221 | - ), 'charges/' . $charge . '/capture' ); |
|
221 | + ), 'charges/' . $charge . '/capture'); |
|
222 | 222 | |
223 | - if ( ! empty( $result->error ) ) { |
|
223 | + if ( ! empty($result->error)) { |
|
224 | 224 | /* translators: error message */ |
225 | - $order->update_status( 'failed', sprintf( __( 'Unable to capture charge! %s', 'woocommerce-gateway-stripe' ), $result->error->message ) ); |
|
225 | + $order->update_status('failed', sprintf(__('Unable to capture charge! %s', 'woocommerce-gateway-stripe'), $result->error->message)); |
|
226 | 226 | } else { |
227 | 227 | /* translators: transaction id */ |
228 | - $order->add_order_note( sprintf( __( 'Stripe charge complete (Charge ID: %s)', 'woocommerce-gateway-stripe' ), $result->id ) ); |
|
229 | - WC_Stripe_Helper::is_pre_30() ? update_post_meta( $order_id, '_stripe_charge_captured', 'yes' ) : $order->update_meta_data( '_stripe_charge_captured', 'yes' ); |
|
228 | + $order->add_order_note(sprintf(__('Stripe charge complete (Charge ID: %s)', 'woocommerce-gateway-stripe'), $result->id)); |
|
229 | + WC_Stripe_Helper::is_pre_30() ? update_post_meta($order_id, '_stripe_charge_captured', 'yes') : $order->update_meta_data('_stripe_charge_captured', 'yes'); |
|
230 | 230 | |
231 | 231 | // Store other data such as fees |
232 | - WC_Stripe_Helper::is_pre_30() ? update_post_meta( $order_id, '_transaction_id', $result->id ) : $order->set_transaction_id( $result->id ); |
|
232 | + WC_Stripe_Helper::is_pre_30() ? update_post_meta($order_id, '_transaction_id', $result->id) : $order->set_transaction_id($result->id); |
|
233 | 233 | |
234 | - if ( isset( $result->balance_transaction ) && isset( $result->balance_transaction->fee ) ) { |
|
234 | + if (isset($result->balance_transaction) && isset($result->balance_transaction->fee)) { |
|
235 | 235 | // Fees and Net needs to both come from Stripe to be accurate as the returned |
236 | 236 | // values are in the local currency of the Stripe account, not from WC. |
237 | - $fee = ! empty( $result->balance_transaction->fee ) ? WC_Stripe_Helper::format_balance_fee( $result->balance_transaction, 'fee' ) : 0; |
|
238 | - $net = ! empty( $result->balance_transaction->net ) ? WC_Stripe_Helper::format_balance_fee( $result->balance_transaction, 'net' ) : 0; |
|
239 | - WC_Stripe_Helper::is_pre_30() ? update_post_meta( $order_id, parent::META_NAME_FEE, $fee ) : $order->update_meta_data( parent::META_NAME_FEE, $fee ); |
|
240 | - WC_Stripe_Helper::is_pre_30() ? update_post_meta( $order_id, parent::META_NAME_NET, $net ) : $order->update_meta_data( parent::META_NAME_NET, $net ); |
|
237 | + $fee = ! empty($result->balance_transaction->fee) ? WC_Stripe_Helper::format_balance_fee($result->balance_transaction, 'fee') : 0; |
|
238 | + $net = ! empty($result->balance_transaction->net) ? WC_Stripe_Helper::format_balance_fee($result->balance_transaction, 'net') : 0; |
|
239 | + WC_Stripe_Helper::is_pre_30() ? update_post_meta($order_id, parent::META_NAME_FEE, $fee) : $order->update_meta_data(parent::META_NAME_FEE, $fee); |
|
240 | + WC_Stripe_Helper::is_pre_30() ? update_post_meta($order_id, parent::META_NAME_NET, $net) : $order->update_meta_data(parent::META_NAME_NET, $net); |
|
241 | 241 | } |
242 | 242 | |
243 | - if ( is_callable( array( $order, 'save' ) ) ) { |
|
243 | + if (is_callable(array($order, 'save'))) { |
|
244 | 244 | $order->save(); |
245 | 245 | } |
246 | 246 | } |
247 | 247 | |
248 | 248 | // This hook fires when admin manually changes order status to processing or completed. |
249 | - do_action( 'woocommerce_stripe_process_manual_capture', $order, $result ); |
|
249 | + do_action('woocommerce_stripe_process_manual_capture', $order, $result); |
|
250 | 250 | } |
251 | 251 | } |
252 | 252 | } |
@@ -258,14 +258,14 @@ discard block |
||
258 | 258 | * @version 4.0.0 |
259 | 259 | * @param int $order_id |
260 | 260 | */ |
261 | - public function cancel_payment( $order_id ) { |
|
262 | - $order = wc_get_order( $order_id ); |
|
261 | + public function cancel_payment($order_id) { |
|
262 | + $order = wc_get_order($order_id); |
|
263 | 263 | |
264 | - if ( 'stripe' === ( WC_Stripe_Helper::is_pre_30() ? $order->payment_method : $order->get_payment_method() ) ) { |
|
265 | - $this->process_refund( $order_id ); |
|
264 | + if ('stripe' === (WC_Stripe_Helper::is_pre_30() ? $order->payment_method : $order->get_payment_method())) { |
|
265 | + $this->process_refund($order_id); |
|
266 | 266 | |
267 | 267 | // This hook fires when admin manually changes order status to cancel. |
268 | - do_action( 'woocommerce_stripe_process_manual_cancel', $order ); |
|
268 | + do_action('woocommerce_stripe_process_manual_cancel', $order); |
|
269 | 269 | } |
270 | 270 | } |
271 | 271 | |
@@ -277,21 +277,21 @@ discard block |
||
277 | 277 | * @param string $field |
278 | 278 | * @return string $error_field |
279 | 279 | */ |
280 | - public function normalize_field( $field ) { |
|
280 | + public function normalize_field($field) { |
|
281 | 281 | $checkout_fields = WC()->checkout->get_checkout_fields(); |
282 | 282 | $org_str = array(); |
283 | 283 | $replace_str = array(); |
284 | 284 | |
285 | - if ( array_key_exists( $field, $checkout_fields['billing'] ) ) { |
|
286 | - $error_field = __( 'Billing', 'woocommerce-gateway-stripe' ) . ' ' . $checkout_fields['billing'][ $field ]['label']; |
|
287 | - } elseif ( array_key_exists( $field, $checkout_fields['shipping'] ) ) { |
|
288 | - $error_field = __( 'Shipping', 'woocommerce-gateway-stripe' ) . ' ' . $checkout_fields['shipping'][ $field ]['label']; |
|
289 | - } elseif ( array_key_exists( $field, $checkout_fields['order'] ) ) { |
|
290 | - $error_field = $checkout_fields['order'][ $field ]['label']; |
|
291 | - } elseif ( array_key_exists( $field, $checkout_fields['account'] ) ) { |
|
292 | - $error_field = $checkout_fields['account'][ $field ]['label']; |
|
285 | + if (array_key_exists($field, $checkout_fields['billing'])) { |
|
286 | + $error_field = __('Billing', 'woocommerce-gateway-stripe') . ' ' . $checkout_fields['billing'][$field]['label']; |
|
287 | + } elseif (array_key_exists($field, $checkout_fields['shipping'])) { |
|
288 | + $error_field = __('Shipping', 'woocommerce-gateway-stripe') . ' ' . $checkout_fields['shipping'][$field]['label']; |
|
289 | + } elseif (array_key_exists($field, $checkout_fields['order'])) { |
|
290 | + $error_field = $checkout_fields['order'][$field]['label']; |
|
291 | + } elseif (array_key_exists($field, $checkout_fields['account'])) { |
|
292 | + $error_field = $checkout_fields['account'][$field]['label']; |
|
293 | 293 | } else { |
294 | - $error_field = str_replace( '_', ' ', $field ); |
|
294 | + $error_field = str_replace('_', ' ', $field); |
|
295 | 295 | |
296 | 296 | $org_str[] = 'stripe'; |
297 | 297 | $replace_str[] = ''; |
@@ -306,9 +306,9 @@ discard block |
||
306 | 306 | $replace_str[] = 'SOFORT'; |
307 | 307 | |
308 | 308 | $org_str[] = 'owner'; |
309 | - $replace_str[] = __( 'Owner', 'woocommerce-gateway-stripe' ); |
|
309 | + $replace_str[] = __('Owner', 'woocommerce-gateway-stripe'); |
|
310 | 310 | |
311 | - $error_field = str_replace( $org_str, $replace_str, $error_field ); |
|
311 | + $error_field = str_replace($org_str, $replace_str, $error_field); |
|
312 | 312 | } |
313 | 313 | |
314 | 314 | return $error_field; |
@@ -321,138 +321,138 @@ discard block |
||
321 | 321 | * @version 4.0.0 |
322 | 322 | */ |
323 | 323 | public function validate_checkout() { |
324 | - if ( ! wp_verify_nonce( $_POST['nonce'], '_wc_stripe_nonce' ) ) { |
|
325 | - wp_die( __( 'Cheatin’ huh?', 'woocommerce-gateway-stripe' ) ); |
|
324 | + if ( ! wp_verify_nonce($_POST['nonce'], '_wc_stripe_nonce')) { |
|
325 | + wp_die(__('Cheatin’ huh?', 'woocommerce-gateway-stripe')); |
|
326 | 326 | } |
327 | 327 | |
328 | 328 | $errors = new WP_Error(); |
329 | - parse_str( $_POST['required_fields'], $required_fields ); |
|
330 | - parse_str( $_POST['all_fields'], $all_fields ); |
|
331 | - $source_type = isset( $_POST['source_type'] ) ? wc_clean( $_POST['source_type'] ) : ''; |
|
329 | + parse_str($_POST['required_fields'], $required_fields); |
|
330 | + parse_str($_POST['all_fields'], $all_fields); |
|
331 | + $source_type = isset($_POST['source_type']) ? wc_clean($_POST['source_type']) : ''; |
|
332 | 332 | $validate_shipping_fields = false; |
333 | 333 | $create_account = false; |
334 | 334 | |
335 | - $all_fields = apply_filters( 'wc_stripe_validate_checkout_all_fields', $all_fields ); |
|
336 | - $required_fields = apply_filters( 'wc_stripe_validate_checkout_required_fields', $required_fields ); |
|
335 | + $all_fields = apply_filters('wc_stripe_validate_checkout_all_fields', $all_fields); |
|
336 | + $required_fields = apply_filters('wc_stripe_validate_checkout_required_fields', $required_fields); |
|
337 | 337 | |
338 | - array_walk_recursive( $required_fields, 'wc_clean' ); |
|
339 | - array_walk_recursive( $all_fields, 'wc_clean' ); |
|
338 | + array_walk_recursive($required_fields, 'wc_clean'); |
|
339 | + array_walk_recursive($all_fields, 'wc_clean'); |
|
340 | 340 | |
341 | 341 | /** |
342 | 342 | * If ship to different address checkbox is checked then we need |
343 | 343 | * to validate shipping fields too. |
344 | 344 | */ |
345 | - if ( isset( $all_fields['ship_to_different_address'] ) ) { |
|
345 | + if (isset($all_fields['ship_to_different_address'])) { |
|
346 | 346 | $validate_shipping_fields = true; |
347 | 347 | } |
348 | 348 | |
349 | 349 | // Check if createaccount is checked. |
350 | - if ( isset( $all_fields['createaccount'] ) ) { |
|
350 | + if (isset($all_fields['createaccount'])) { |
|
351 | 351 | $create_account = true; |
352 | 352 | } |
353 | 353 | |
354 | 354 | // Check if required fields are empty. |
355 | - foreach ( $required_fields as $field => $field_value ) { |
|
355 | + foreach ($required_fields as $field => $field_value) { |
|
356 | 356 | // Check for shipping field. |
357 | - if ( preg_match( '/^shipping_/', $field ) && ! $validate_shipping_fields ) { |
|
357 | + if (preg_match('/^shipping_/', $field) && ! $validate_shipping_fields) { |
|
358 | 358 | continue; |
359 | 359 | } |
360 | 360 | |
361 | 361 | // Check create account name. |
362 | - if ( 'account_username' === $field && ! $create_account ) { |
|
362 | + if ('account_username' === $field && ! $create_account) { |
|
363 | 363 | continue; |
364 | 364 | } |
365 | 365 | |
366 | 366 | // Check create account password. |
367 | - if ( 'account_password' === $field && ! $create_account ) { |
|
367 | + if ('account_password' === $field && ! $create_account) { |
|
368 | 368 | continue; |
369 | 369 | } |
370 | 370 | |
371 | - if ( empty( $field_value ) || '-1' === $field_value ) { |
|
372 | - $error_field = $this->normalize_field( $field ); |
|
371 | + if (empty($field_value) || '-1' === $field_value) { |
|
372 | + $error_field = $this->normalize_field($field); |
|
373 | 373 | /* translators: error field name */ |
374 | - $errors->add( 'validation', sprintf( __( '<strong>%s</strong> cannot be empty', 'woocommerce-gateway-stripe' ), $error_field ) ); |
|
374 | + $errors->add('validation', sprintf(__('<strong>%s</strong> cannot be empty', 'woocommerce-gateway-stripe'), $error_field)); |
|
375 | 375 | } |
376 | 376 | } |
377 | 377 | |
378 | 378 | // Check if email is valid format. |
379 | - if ( ! empty( $required_fields['billing_email'] ) && ! is_email( $required_fields['billing_email'] ) ) { |
|
380 | - $errors->add( 'validation', __( 'Email is not valid', 'woocommerce-gateway-stripe' ) ); |
|
379 | + if ( ! empty($required_fields['billing_email']) && ! is_email($required_fields['billing_email'])) { |
|
380 | + $errors->add('validation', __('Email is not valid', 'woocommerce-gateway-stripe')); |
|
381 | 381 | } |
382 | 382 | |
383 | 383 | // Check if phone number is valid format. |
384 | - if ( ! empty( $required_fields['billing_phone'] ) ) { |
|
385 | - $phone = wc_format_phone_number( $required_fields['billing_phone'] ); |
|
384 | + if ( ! empty($required_fields['billing_phone'])) { |
|
385 | + $phone = wc_format_phone_number($required_fields['billing_phone']); |
|
386 | 386 | |
387 | - if ( '' !== $phone && ! WC_Validation::is_phone( $phone ) ) { |
|
387 | + if ('' !== $phone && ! WC_Validation::is_phone($phone)) { |
|
388 | 388 | /* translators: %s: phone number */ |
389 | - $errors->add( 'validation', __( 'Please enter a valid phone number.', 'woocommerce-gateway-stripe' ) ); |
|
389 | + $errors->add('validation', __('Please enter a valid phone number.', 'woocommerce-gateway-stripe')); |
|
390 | 390 | } |
391 | 391 | } |
392 | 392 | |
393 | 393 | // Check if postal code is valid format. |
394 | - if ( ! empty( $required_fields['billing_postcode'] ) ) { |
|
395 | - $country = isset( $required_fields['billing_country'] ) ? $required_fields['billing_country'] : WC()->customer->get_billing_country(); |
|
396 | - $postcode = wc_format_postcode( $required_fields['billing_postcode'], $country ); |
|
394 | + if ( ! empty($required_fields['billing_postcode'])) { |
|
395 | + $country = isset($required_fields['billing_country']) ? $required_fields['billing_country'] : WC()->customer->get_billing_country(); |
|
396 | + $postcode = wc_format_postcode($required_fields['billing_postcode'], $country); |
|
397 | 397 | |
398 | - if ( '' !== $required_fields['billing_postcode'] && ! WC_Validation::is_postcode( $postcode, $country ) ) { |
|
399 | - $errors->add( 'validation', __( 'Please enter a valid billing postcode / ZIP.', 'woocommerce-gateway-stripe' ) ); |
|
398 | + if ('' !== $required_fields['billing_postcode'] && ! WC_Validation::is_postcode($postcode, $country)) { |
|
399 | + $errors->add('validation', __('Please enter a valid billing postcode / ZIP.', 'woocommerce-gateway-stripe')); |
|
400 | 400 | } |
401 | 401 | } |
402 | 402 | |
403 | 403 | // Don't check this on add payment method page. |
404 | - if ( ( isset( $_POST['is_add_payment_page'] ) && 'no' === $_POST['is_add_payment_page'] ) ) { |
|
405 | - if ( empty( $all_fields['woocommerce_checkout_update_totals'] ) && empty( $all_fields['terms'] ) && apply_filters( 'woocommerce_checkout_show_terms', wc_get_page_id( 'terms' ) > 0 ) ) { |
|
406 | - $errors->add( 'terms', __( 'You must accept our Terms & Conditions.', 'woocommerce-gateway-stripe' ) ); |
|
404 | + if ((isset($_POST['is_add_payment_page']) && 'no' === $_POST['is_add_payment_page'])) { |
|
405 | + if (empty($all_fields['woocommerce_checkout_update_totals']) && empty($all_fields['terms']) && apply_filters('woocommerce_checkout_show_terms', wc_get_page_id('terms') > 0)) { |
|
406 | + $errors->add('terms', __('You must accept our Terms & Conditions.', 'woocommerce-gateway-stripe')); |
|
407 | 407 | } |
408 | 408 | } |
409 | 409 | |
410 | - if ( WC()->cart->needs_shipping() && $validate_shipping_fields ) { |
|
410 | + if (WC()->cart->needs_shipping() && $validate_shipping_fields) { |
|
411 | 411 | // Check if postal code is valid format. |
412 | - if ( ! empty( $required_fields['shipping_postcode'] ) ) { |
|
413 | - $country = isset( $required_fields['shipping_country'] ) ? $required_fields['shipping_country'] : WC()->customer->get_shipping_country(); |
|
414 | - $postcode = wc_format_postcode( $required_fields['shipping_postcode'], $country ); |
|
412 | + if ( ! empty($required_fields['shipping_postcode'])) { |
|
413 | + $country = isset($required_fields['shipping_country']) ? $required_fields['shipping_country'] : WC()->customer->get_shipping_country(); |
|
414 | + $postcode = wc_format_postcode($required_fields['shipping_postcode'], $country); |
|
415 | 415 | |
416 | - if ( '' !== $required_fields['shipping_postcode'] && ! WC_Validation::is_postcode( $postcode, $country ) ) { |
|
417 | - $errors->add( 'validation', __( 'Please enter a valid shipping postcode / ZIP.', 'woocommerce-gateway-stripe' ) ); |
|
416 | + if ('' !== $required_fields['shipping_postcode'] && ! WC_Validation::is_postcode($postcode, $country)) { |
|
417 | + $errors->add('validation', __('Please enter a valid shipping postcode / ZIP.', 'woocommerce-gateway-stripe')); |
|
418 | 418 | } |
419 | 419 | } |
420 | 420 | } |
421 | 421 | |
422 | - if ( WC()->cart->needs_shipping() ) { |
|
422 | + if (WC()->cart->needs_shipping()) { |
|
423 | 423 | $shipping_country = WC()->customer->get_shipping_country(); |
424 | 424 | |
425 | - if ( empty( $shipping_country ) ) { |
|
426 | - $errors->add( 'shipping', __( 'Please enter an address to continue.', 'woocommerce-gateway-stripe' ) ); |
|
427 | - } elseif ( ! in_array( WC()->customer->get_shipping_country(), array_keys( WC()->countries->get_shipping_countries() ) ) ) { |
|
425 | + if (empty($shipping_country)) { |
|
426 | + $errors->add('shipping', __('Please enter an address to continue.', 'woocommerce-gateway-stripe')); |
|
427 | + } elseif ( ! in_array(WC()->customer->get_shipping_country(), array_keys(WC()->countries->get_shipping_countries()))) { |
|
428 | 428 | /* translators: country name */ |
429 | - $errors->add( 'shipping', sprintf( __( 'Unfortunately <strong>we do not ship %s</strong>. Please enter an alternative shipping address.', 'woocommerce-gateway-stripe' ), WC()->countries->shipping_to_prefix() . ' ' . WC()->customer->get_shipping_country() ) ); |
|
429 | + $errors->add('shipping', sprintf(__('Unfortunately <strong>we do not ship %s</strong>. Please enter an alternative shipping address.', 'woocommerce-gateway-stripe'), WC()->countries->shipping_to_prefix() . ' ' . WC()->customer->get_shipping_country())); |
|
430 | 430 | } else { |
431 | - $chosen_shipping_methods = WC()->session->get( 'chosen_shipping_methods' ); |
|
431 | + $chosen_shipping_methods = WC()->session->get('chosen_shipping_methods'); |
|
432 | 432 | |
433 | - foreach ( WC()->shipping->get_packages() as $i => $package ) { |
|
434 | - if ( ! isset( $chosen_shipping_methods[ $i ], $package['rates'][ $chosen_shipping_methods[ $i ] ] ) ) { |
|
435 | - $errors->add( 'shipping', __( 'No shipping method has been selected. Please double check your address, or contact us if you need any help.', 'woocommerce-gateway-stripe' ) ); |
|
433 | + foreach (WC()->shipping->get_packages() as $i => $package) { |
|
434 | + if ( ! isset($chosen_shipping_methods[$i], $package['rates'][$chosen_shipping_methods[$i]])) { |
|
435 | + $errors->add('shipping', __('No shipping method has been selected. Please double check your address, or contact us if you need any help.', 'woocommerce-gateway-stripe')); |
|
436 | 436 | } |
437 | 437 | } |
438 | 438 | } |
439 | 439 | } |
440 | 440 | |
441 | - if ( WC()->cart->needs_payment() ) { |
|
441 | + if (WC()->cart->needs_payment()) { |
|
442 | 442 | $available_gateways = WC()->payment_gateways->get_available_payment_gateways(); |
443 | 443 | |
444 | - if ( ! isset( $available_gateways[ $all_fields['payment_method'] ] ) ) { |
|
445 | - $errors->add( 'payment', __( 'Invalid payment method.', 'woocommerce-gateway-stripe' ) ); |
|
444 | + if ( ! isset($available_gateways[$all_fields['payment_method']])) { |
|
445 | + $errors->add('payment', __('Invalid payment method.', 'woocommerce-gateway-stripe')); |
|
446 | 446 | } else { |
447 | - $available_gateways[ $all_fields['payment_method'] ]->validate_fields(); |
|
447 | + $available_gateways[$all_fields['payment_method']]->validate_fields(); |
|
448 | 448 | } |
449 | 449 | } |
450 | 450 | |
451 | - if ( 0 === count( $errors->errors ) ) { |
|
452 | - wp_send_json( 'success' ); |
|
451 | + if (0 === count($errors->errors)) { |
|
452 | + wp_send_json('success'); |
|
453 | 453 | } else { |
454 | - foreach ( $errors->get_error_messages() as $message ) { |
|
455 | - wc_add_notice( $message, 'error' ); |
|
454 | + foreach ($errors->get_error_messages() as $message) { |
|
455 | + wc_add_notice($message, 'error'); |
|
456 | 456 | } |
457 | 457 | |
458 | 458 | $this->send_ajax_failure_response(); |
@@ -466,9 +466,9 @@ discard block |
||
466 | 466 | * @version 4.0.0 |
467 | 467 | */ |
468 | 468 | public function send_ajax_failure_response() { |
469 | - if ( is_ajax() ) { |
|
469 | + if (is_ajax()) { |
|
470 | 470 | // only print notices if not reloading the checkout, otherwise they're lost in the page reload. |
471 | - if ( ! isset( WC()->session->reload_checkout ) ) { |
|
471 | + if ( ! isset(WC()->session->reload_checkout)) { |
|
472 | 472 | ob_start(); |
473 | 473 | wc_print_notices(); |
474 | 474 | $messages = ob_get_clean(); |
@@ -476,14 +476,14 @@ discard block |
||
476 | 476 | |
477 | 477 | $response = array( |
478 | 478 | 'result' => 'failure', |
479 | - 'messages' => isset( $messages ) ? $messages : '', |
|
480 | - 'refresh' => isset( WC()->session->refresh_totals ), |
|
481 | - 'reload' => isset( WC()->session->reload_checkout ), |
|
479 | + 'messages' => isset($messages) ? $messages : '', |
|
480 | + 'refresh' => isset(WC()->session->refresh_totals), |
|
481 | + 'reload' => isset(WC()->session->reload_checkout), |
|
482 | 482 | ); |
483 | 483 | |
484 | - unset( WC()->session->refresh_totals, WC()->session->reload_checkout ); |
|
484 | + unset(WC()->session->refresh_totals, WC()->session->reload_checkout); |
|
485 | 485 | |
486 | - wp_send_json( $response ); |
|
486 | + wp_send_json($response); |
|
487 | 487 | } |
488 | 488 | } |
489 | 489 | } |
@@ -1,5 +1,5 @@ discard block |
||
1 | 1 | <?php |
2 | -if ( ! defined( 'ABSPATH' ) ) { |
|
2 | +if ( ! defined('ABSPATH')) { |
|
3 | 3 | exit; |
4 | 4 | } |
5 | 5 | |
@@ -18,11 +18,11 @@ discard block |
||
18 | 18 | * Check if this gateway is enabled |
19 | 19 | */ |
20 | 20 | public function is_available() { |
21 | - if ( 'yes' === $this->enabled ) { |
|
22 | - if ( ! $this->testmode && is_checkout() && ! is_ssl() ) { |
|
21 | + if ('yes' === $this->enabled) { |
|
22 | + if ( ! $this->testmode && is_checkout() && ! is_ssl()) { |
|
23 | 23 | return false; |
24 | 24 | } |
25 | - if ( ! $this->secret_key || ! $this->publishable_key ) { |
|
25 | + if ( ! $this->secret_key || ! $this->publishable_key) { |
|
26 | 26 | return false; |
27 | 27 | } |
28 | 28 | return true; |
@@ -37,8 +37,8 @@ discard block |
||
37 | 37 | * @since 4.0.0 |
38 | 38 | * @version 4.0.0 |
39 | 39 | */ |
40 | - public function add_admin_notice( $slug, $class, $message ) { |
|
41 | - $this->notices[ $slug ] = array( |
|
40 | + public function add_admin_notice($slug, $class, $message) { |
|
41 | + $this->notices[$slug] = array( |
|
42 | 42 | 'class' => $class, |
43 | 43 | 'message' => $message, |
44 | 44 | ); |
@@ -51,8 +51,8 @@ discard block |
||
51 | 51 | * @version 4.0.0 |
52 | 52 | */ |
53 | 53 | public function remove_admin_notice() { |
54 | - if ( did_action( 'woocommerce_update_options' ) ) { |
|
55 | - remove_action( 'admin_notices', array( $this, 'check_environment' ) ); |
|
54 | + if (did_action('woocommerce_update_options')) { |
|
55 | + remove_action('admin_notices', array($this, 'check_environment')); |
|
56 | 56 | } |
57 | 57 | } |
58 | 58 | |
@@ -64,7 +64,7 @@ discard block |
||
64 | 64 | * @return array |
65 | 65 | */ |
66 | 66 | public function payment_icons() { |
67 | - return apply_filters( 'wc_stripe_payment_icons', array( |
|
67 | + return apply_filters('wc_stripe_payment_icons', array( |
|
68 | 68 | 'visa' => '<i class="stripe-pf stripe-pf-visa stripe-pf-right" alt="Visa" aria-hidden="true"></i>', |
69 | 69 | 'amex' => '<i class="stripe-pf stripe-pf-american-express stripe-pf-right" alt="Amex" aria-hidden="true"></i>', |
70 | 70 | 'mastercard' => '<i class="stripe-pf stripe-pf-mastercard stripe-pf-right" alt="Mastercard" aria-hidden="true"></i>', |
@@ -81,7 +81,7 @@ discard block |
||
81 | 81 | 'eps' => '<i class="stripe-pf stripe-pf-eps stripe-pf-right" alt="EPS" aria-hidden="true"></i>', |
82 | 82 | 'sofort' => '<i class="stripe-pf stripe-pf-sofort stripe-pf-right" alt="SOFORT" aria-hidden="true"></i>', |
83 | 83 | 'sepa' => '<i class="stripe-pf stripe-pf-sepa stripe-pf-right" alt="SEPA" aria-hidden="true"></i>', |
84 | - ) ); |
|
84 | + )); |
|
85 | 85 | } |
86 | 86 | |
87 | 87 | /** |
@@ -92,10 +92,10 @@ discard block |
||
92 | 92 | * @version 4.0.0 |
93 | 93 | * @param object $order |
94 | 94 | */ |
95 | - public function validate_minimum_order_amount( $order ) { |
|
96 | - if ( $order->get_total() * 100 < WC_Stripe_Helper::get_minimum_amount() ) { |
|
95 | + public function validate_minimum_order_amount($order) { |
|
96 | + if ($order->get_total() * 100 < WC_Stripe_Helper::get_minimum_amount()) { |
|
97 | 97 | /* translators: 1) dollar amount */ |
98 | - throw new WC_Stripe_Exception( 'Did not meet minimum amount', sprintf( __( 'Sorry, the minimum allowed order total is %1$s to use this payment method.', 'woocommerce-gateway-stripe' ), wc_price( WC_Stripe_Helper::get_minimum_amount() / 100 ) ) ); |
|
98 | + throw new WC_Stripe_Exception('Did not meet minimum amount', sprintf(__('Sorry, the minimum allowed order total is %1$s to use this payment method.', 'woocommerce-gateway-stripe'), wc_price(WC_Stripe_Helper::get_minimum_amount() / 100))); |
|
99 | 99 | } |
100 | 100 | } |
101 | 101 | |
@@ -105,14 +105,14 @@ discard block |
||
105 | 105 | * @since 4.0.0 |
106 | 106 | * @version 4.0.0 |
107 | 107 | */ |
108 | - public function get_transaction_url( $order ) { |
|
109 | - if ( $this->testmode ) { |
|
108 | + public function get_transaction_url($order) { |
|
109 | + if ($this->testmode) { |
|
110 | 110 | $this->view_transaction_url = 'https://dashboard.stripe.com/test/payments/%s'; |
111 | 111 | } else { |
112 | 112 | $this->view_transaction_url = 'https://dashboard.stripe.com/payments/%s'; |
113 | 113 | } |
114 | 114 | |
115 | - return parent::get_transaction_url( $order ); |
|
115 | + return parent::get_transaction_url($order); |
|
116 | 116 | } |
117 | 117 | |
118 | 118 | /** |
@@ -121,15 +121,15 @@ discard block |
||
121 | 121 | * @since 4.0.0 |
122 | 122 | * @version 4.0.0 |
123 | 123 | */ |
124 | - public function get_stripe_customer_id( $order ) { |
|
125 | - $customer = get_user_meta( WC_Stripe_Helper::is_pre_30() ? $order->customer_user : $order->get_customer_id(), '_stripe_customer_id', true ); |
|
124 | + public function get_stripe_customer_id($order) { |
|
125 | + $customer = get_user_meta(WC_Stripe_Helper::is_pre_30() ? $order->customer_user : $order->get_customer_id(), '_stripe_customer_id', true); |
|
126 | 126 | |
127 | - if ( empty( $customer ) ) { |
|
127 | + if (empty($customer)) { |
|
128 | 128 | // Try to get it via the order. |
129 | - if ( WC_Stripe_Helper::is_pre_30() ) { |
|
130 | - return get_post_meta( $order->id, '_stripe_customer_id', true ); |
|
129 | + if (WC_Stripe_Helper::is_pre_30()) { |
|
130 | + return get_post_meta($order->id, '_stripe_customer_id', true); |
|
131 | 131 | } else { |
132 | - return $order->get_meta( '_stripe_customer_id', true ); |
|
132 | + return $order->get_meta('_stripe_customer_id', true); |
|
133 | 133 | } |
134 | 134 | } else { |
135 | 135 | return $customer; |
@@ -146,9 +146,9 @@ discard block |
||
146 | 146 | * @param object $order |
147 | 147 | * @param int $id Stripe session id. |
148 | 148 | */ |
149 | - public function get_stripe_return_url( $order = null, $id = null ) { |
|
150 | - if ( is_object( $order ) ) { |
|
151 | - if ( empty( $id ) ) { |
|
149 | + public function get_stripe_return_url($order = null, $id = null) { |
|
150 | + if (is_object($order)) { |
|
151 | + if (empty($id)) { |
|
152 | 152 | $id = uniqid(); |
153 | 153 | } |
154 | 154 | |
@@ -159,10 +159,10 @@ discard block |
||
159 | 159 | 'order_id' => $order_id, |
160 | 160 | ); |
161 | 161 | |
162 | - return esc_url_raw( add_query_arg( $args, $this->get_return_url( $order ) ) ); |
|
162 | + return esc_url_raw(add_query_arg($args, $this->get_return_url($order))); |
|
163 | 163 | } |
164 | 164 | |
165 | - return esc_url_raw( add_query_arg( array( 'utm_nooverride' => '1' ), $this->get_return_url() ) ); |
|
165 | + return esc_url_raw(add_query_arg(array('utm_nooverride' => '1'), $this->get_return_url())); |
|
166 | 166 | } |
167 | 167 | |
168 | 168 | /** |
@@ -174,27 +174,26 @@ discard block |
||
174 | 174 | * @param object $source |
175 | 175 | * @return array() |
176 | 176 | */ |
177 | - public function generate_payment_request( $order, $source ) { |
|
178 | - $settings = get_option( 'woocommerce_stripe_settings', array() ); |
|
179 | - $statement_descriptor = ! empty( $settings['statement_descriptor'] ) ? str_replace( "'", '', $settings['statement_descriptor'] ) : ''; |
|
180 | - $capture = ! empty( $settings['capture'] ) && 'yes' === $settings['capture'] ? true : false; |
|
177 | + public function generate_payment_request($order, $source) { |
|
178 | + $settings = get_option('woocommerce_stripe_settings', array()); |
|
179 | + $statement_descriptor = ! empty($settings['statement_descriptor']) ? str_replace("'", '', $settings['statement_descriptor']) : ''; |
|
180 | + $capture = ! empty($settings['capture']) && 'yes' === $settings['capture'] ? true : false; |
|
181 | 181 | $post_data = array(); |
182 | - $post_data['currency'] = strtolower( WC_Stripe_Helper::is_pre_30() ? $order->get_order_currency() : $order->get_currency() ); |
|
183 | - $post_data['amount'] = WC_Stripe_Helper::get_stripe_amount( $order->get_total(), $post_data['currency'] ); |
|
182 | + $post_data['currency'] = strtolower(WC_Stripe_Helper::is_pre_30() ? $order->get_order_currency() : $order->get_currency()); |
|
183 | + $post_data['amount'] = WC_Stripe_Helper::get_stripe_amount($order->get_total(), $post_data['currency']); |
|
184 | 184 | /* translators: 1) blog name 2) order number */ |
185 | - $post_data['description'] = sprintf( __( '%1$s - Order %2$s', 'woocommerce-gateway-stripe' ), wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ), $order->get_order_number() ); |
|
185 | + $post_data['description'] = sprintf(__('%1$s - Order %2$s', 'woocommerce-gateway-stripe'), wp_specialchars_decode(get_bloginfo('name'), ENT_QUOTES), $order->get_order_number()); |
|
186 | 186 | $billing_email = WC_Stripe_Helper::is_pre_30() ? $order->billing_email : $order->get_billing_email(); |
187 | 187 | $billing_first_name = WC_Stripe_Helper::is_pre_30() ? $order->billing_first_name : $order->get_billing_first_name(); |
188 | 188 | $billing_last_name = WC_Stripe_Helper::is_pre_30() ? $order->billing_last_name : $order->get_billing_last_name(); |
189 | 189 | |
190 | - if ( ! empty( $billing_email ) && apply_filters( 'wc_stripe_send_stripe_receipt', false ) ) { |
|
190 | + if ( ! empty($billing_email) && apply_filters('wc_stripe_send_stripe_receipt', false)) { |
|
191 | 191 | $post_data['receipt_email'] = $billing_email; |
192 | 192 | } |
193 | 193 | |
194 | - switch ( WC_Stripe_Helper::is_pre_30() ? $order->payment_method : $order->get_payment_method() ) { |
|
195 | - case 'stripe': |
|
196 | - if ( ! empty( $statement_descriptor ) ) { |
|
197 | - $post_data['statement_descriptor'] = WC_Stripe_Helper::clean_statement_descriptor( $statement_descriptor ); |
|
194 | + switch (WC_Stripe_Helper::is_pre_30() ? $order->payment_method : $order->get_payment_method()) { |
|
195 | + case 'stripe' : if ( ! empty($statement_descriptor)) { |
|
196 | + $post_data['statement_descriptor'] = WC_Stripe_Helper::clean_statement_descriptor($statement_descriptor); |
|
198 | 197 | } |
199 | 198 | |
200 | 199 | $post_data['capture'] = $capture ? 'true' : 'false'; |
@@ -204,18 +203,18 @@ discard block |
||
204 | 203 | $post_data['expand[]'] = 'balance_transaction'; |
205 | 204 | |
206 | 205 | $metadata = array( |
207 | - __( 'customer_name', 'woocommerce-gateway-stripe' ) => sanitize_text_field( $billing_first_name ) . ' ' . sanitize_text_field( $billing_last_name ), |
|
208 | - __( 'customer_email', 'woocommerce-gateway-stripe' ) => sanitize_email( $billing_email ), |
|
206 | + __('customer_name', 'woocommerce-gateway-stripe') => sanitize_text_field($billing_first_name) . ' ' . sanitize_text_field($billing_last_name), |
|
207 | + __('customer_email', 'woocommerce-gateway-stripe') => sanitize_email($billing_email), |
|
209 | 208 | 'order_id' => WC_Stripe_Helper::is_pre_30() ? $order->id : $order->get_id(), |
210 | 209 | ); |
211 | 210 | |
212 | - $post_data['metadata'] = apply_filters( 'wc_stripe_payment_metadata', $metadata, $order, $source ); |
|
211 | + $post_data['metadata'] = apply_filters('wc_stripe_payment_metadata', $metadata, $order, $source); |
|
213 | 212 | |
214 | - if ( $source->customer ) { |
|
213 | + if ($source->customer) { |
|
215 | 214 | $post_data['customer'] = $source->customer; |
216 | 215 | } |
217 | 216 | |
218 | - if ( $source->source ) { |
|
217 | + if ($source->source) { |
|
219 | 218 | $post_data['source'] = $source->source; |
220 | 219 | } |
221 | 220 | |
@@ -227,77 +226,77 @@ discard block |
||
227 | 226 | * @param WC_Order $order |
228 | 227 | * @param object $source |
229 | 228 | */ |
230 | - return apply_filters( 'wc_stripe_generate_payment_request', $post_data, $order, $source ); |
|
229 | + return apply_filters('wc_stripe_generate_payment_request', $post_data, $order, $source); |
|
231 | 230 | } |
232 | 231 | |
233 | 232 | /** |
234 | 233 | * Store extra meta data for an order from a Stripe Response. |
235 | 234 | */ |
236 | - public function process_response( $response, $order ) { |
|
237 | - WC_Stripe_Logger::log( 'Processing response: ' . print_r( $response, true ) ); |
|
235 | + public function process_response($response, $order) { |
|
236 | + WC_Stripe_Logger::log('Processing response: ' . print_r($response, true)); |
|
238 | 237 | |
239 | 238 | $order_id = WC_Stripe_Helper::is_pre_30() ? $order->id : $order->get_id(); |
240 | 239 | |
241 | - $captured = ( isset( $response->captured ) && $response->captured ) ? 'yes' : 'no'; |
|
240 | + $captured = (isset($response->captured) && $response->captured) ? 'yes' : 'no'; |
|
242 | 241 | |
243 | 242 | // Store charge data |
244 | - WC_Stripe_Helper::is_pre_30() ? update_post_meta( $order_id, '_stripe_charge_captured', $captured ) : $order->update_meta_data( '_stripe_charge_captured', $captured ); |
|
243 | + WC_Stripe_Helper::is_pre_30() ? update_post_meta($order_id, '_stripe_charge_captured', $captured) : $order->update_meta_data('_stripe_charge_captured', $captured); |
|
245 | 244 | |
246 | 245 | // Store other data such as fees |
247 | - if ( isset( $response->balance_transaction ) && isset( $response->balance_transaction->fee ) ) { |
|
246 | + if (isset($response->balance_transaction) && isset($response->balance_transaction->fee)) { |
|
248 | 247 | // Fees and Net needs to both come from Stripe to be accurate as the returned |
249 | 248 | // values are in the local currency of the Stripe account, not from WC. |
250 | - $fee = ! empty( $response->balance_transaction->fee ) ? WC_Stripe_Helper::format_balance_fee( $response->balance_transaction, 'fee' ) : 0; |
|
251 | - $net = ! empty( $response->balance_transaction->net ) ? WC_Stripe_Helper::format_balance_fee( $response->balance_transaction, 'net' ) : 0; |
|
252 | - WC_Stripe_Helper::is_pre_30() ? update_post_meta( $order_id, self::META_NAME_FEE, $fee ) : $order->update_meta_data( self::META_NAME_FEE, $fee ); |
|
253 | - WC_Stripe_Helper::is_pre_30() ? update_post_meta( $order_id, self::META_NAME_NET, $net ) : $order->update_meta_data( self::META_NAME_NET, $net ); |
|
249 | + $fee = ! empty($response->balance_transaction->fee) ? WC_Stripe_Helper::format_balance_fee($response->balance_transaction, 'fee') : 0; |
|
250 | + $net = ! empty($response->balance_transaction->net) ? WC_Stripe_Helper::format_balance_fee($response->balance_transaction, 'net') : 0; |
|
251 | + WC_Stripe_Helper::is_pre_30() ? update_post_meta($order_id, self::META_NAME_FEE, $fee) : $order->update_meta_data(self::META_NAME_FEE, $fee); |
|
252 | + WC_Stripe_Helper::is_pre_30() ? update_post_meta($order_id, self::META_NAME_NET, $net) : $order->update_meta_data(self::META_NAME_NET, $net); |
|
254 | 253 | } |
255 | 254 | |
256 | - if ( 'yes' === $captured ) { |
|
255 | + if ('yes' === $captured) { |
|
257 | 256 | /** |
258 | 257 | * Charge can be captured but in a pending state. Payment methods |
259 | 258 | * that are asynchronous may take couple days to clear. Webhook will |
260 | 259 | * take care of the status changes. |
261 | 260 | */ |
262 | - if ( 'pending' === $response->status ) { |
|
263 | - if ( ! wc_string_to_bool( get_post_meta( $order_id, '_order_stock_reduced', true ) ) ) { |
|
264 | - WC_Stripe_Helper::is_pre_30() ? $order->reduce_order_stock() : wc_reduce_stock_levels( $order_id ); |
|
261 | + if ('pending' === $response->status) { |
|
262 | + if ( ! wc_string_to_bool(get_post_meta($order_id, '_order_stock_reduced', true))) { |
|
263 | + WC_Stripe_Helper::is_pre_30() ? $order->reduce_order_stock() : wc_reduce_stock_levels($order_id); |
|
265 | 264 | } |
266 | 265 | |
267 | - WC_Stripe_Helper::is_pre_30() ? update_post_meta( $order_id, '_transaction_id', $response->id ) : $order->set_transaction_id( $response->id ); |
|
266 | + WC_Stripe_Helper::is_pre_30() ? update_post_meta($order_id, '_transaction_id', $response->id) : $order->set_transaction_id($response->id); |
|
268 | 267 | /* translators: transaction id */ |
269 | - $order->update_status( 'on-hold', sprintf( __( 'Stripe charge awaiting payment: %s.', 'woocommerce-gateway-stripe' ), $response->id ) ); |
|
268 | + $order->update_status('on-hold', sprintf(__('Stripe charge awaiting payment: %s.', 'woocommerce-gateway-stripe'), $response->id)); |
|
270 | 269 | } |
271 | 270 | |
272 | - if ( 'succeeded' === $response->status ) { |
|
273 | - $order->payment_complete( $response->id ); |
|
271 | + if ('succeeded' === $response->status) { |
|
272 | + $order->payment_complete($response->id); |
|
274 | 273 | |
275 | 274 | /* translators: transaction id */ |
276 | - $message = sprintf( __( 'Stripe charge complete (Charge ID: %s)', 'woocommerce-gateway-stripe' ), $response->id ); |
|
277 | - $order->add_order_note( $message ); |
|
275 | + $message = sprintf(__('Stripe charge complete (Charge ID: %s)', 'woocommerce-gateway-stripe'), $response->id); |
|
276 | + $order->add_order_note($message); |
|
278 | 277 | } |
279 | 278 | |
280 | - if ( 'failed' === $response->status ) { |
|
281 | - $localized_message = __( 'Payment processing failed. Please retry.', 'woocommerce-gateway-stripe' ); |
|
282 | - $order->add_order_note( $localized_message ); |
|
283 | - throw new WC_Stripe_Exception( print_r( $response, true ), $localized_message ); |
|
279 | + if ('failed' === $response->status) { |
|
280 | + $localized_message = __('Payment processing failed. Please retry.', 'woocommerce-gateway-stripe'); |
|
281 | + $order->add_order_note($localized_message); |
|
282 | + throw new WC_Stripe_Exception(print_r($response, true), $localized_message); |
|
284 | 283 | } |
285 | 284 | } else { |
286 | - WC_Stripe_Helper::is_pre_30() ? update_post_meta( $order_id, '_transaction_id', $response->id ) : $order->set_transaction_id( $response->id ); |
|
285 | + WC_Stripe_Helper::is_pre_30() ? update_post_meta($order_id, '_transaction_id', $response->id) : $order->set_transaction_id($response->id); |
|
287 | 286 | |
288 | - if ( $order->has_status( array( 'pending', 'failed' ) ) ) { |
|
289 | - WC_Stripe_Helper::is_pre_30() ? $order->reduce_order_stock() : wc_reduce_stock_levels( $order_id ); |
|
287 | + if ($order->has_status(array('pending', 'failed'))) { |
|
288 | + WC_Stripe_Helper::is_pre_30() ? $order->reduce_order_stock() : wc_reduce_stock_levels($order_id); |
|
290 | 289 | } |
291 | 290 | |
292 | 291 | /* translators: transaction id */ |
293 | - $order->update_status( 'on-hold', sprintf( __( 'Stripe charge authorized (Charge ID: %s). Process order to take payment, or cancel to remove the pre-authorization.', 'woocommerce-gateway-stripe' ), $response->id ) ); |
|
292 | + $order->update_status('on-hold', sprintf(__('Stripe charge authorized (Charge ID: %s). Process order to take payment, or cancel to remove the pre-authorization.', 'woocommerce-gateway-stripe'), $response->id)); |
|
294 | 293 | } |
295 | 294 | |
296 | - if ( is_callable( array( $order, 'save' ) ) ) { |
|
295 | + if (is_callable(array($order, 'save'))) { |
|
297 | 296 | $order->save(); |
298 | 297 | } |
299 | 298 | |
300 | - do_action( 'wc_gateway_stripe_process_response', $response, $order ); |
|
299 | + do_action('wc_gateway_stripe_process_response', $response, $order); |
|
301 | 300 | |
302 | 301 | return $response; |
303 | 302 | } |
@@ -310,10 +309,10 @@ discard block |
||
310 | 309 | * @param int $order_id |
311 | 310 | * @return null |
312 | 311 | */ |
313 | - public function send_failed_order_email( $order_id ) { |
|
312 | + public function send_failed_order_email($order_id) { |
|
314 | 313 | $emails = WC()->mailer()->get_emails(); |
315 | - if ( ! empty( $emails ) && ! empty( $order_id ) ) { |
|
316 | - $emails['WC_Email_Failed_Order']->trigger( $order_id ); |
|
314 | + if ( ! empty($emails) && ! empty($order_id)) { |
|
315 | + $emails['WC_Email_Failed_Order']->trigger($order_id); |
|
317 | 316 | } |
318 | 317 | } |
319 | 318 | |
@@ -325,7 +324,7 @@ discard block |
||
325 | 324 | * @param object $order |
326 | 325 | * @return object $details |
327 | 326 | */ |
328 | - public function get_owner_details( $order ) { |
|
327 | + public function get_owner_details($order) { |
|
329 | 328 | $billing_first_name = WC_Stripe_Helper::is_pre_30() ? $order->billing_first_name : $order->get_billing_first_name(); |
330 | 329 | $billing_last_name = WC_Stripe_Helper::is_pre_30() ? $order->billing_last_name : $order->get_billing_last_name(); |
331 | 330 | |
@@ -336,8 +335,8 @@ discard block |
||
336 | 335 | |
337 | 336 | $phone = WC_Stripe_Helper::is_pre_30() ? $order->billing_phone : $order->get_billing_phone(); |
338 | 337 | |
339 | - if ( ! empty( $phone ) ) { |
|
340 | - $details['phone'] = $phone; |
|
338 | + if ( ! empty($phone)) { |
|
339 | + $details['phone'] = $phone; |
|
341 | 340 | } |
342 | 341 | |
343 | 342 | $details['address']['line1'] = WC_Stripe_Helper::is_pre_30() ? $order->billing_address_1 : $order->get_billing_address_1(); |
@@ -347,7 +346,7 @@ discard block |
||
347 | 346 | $details['address']['postal_code'] = WC_Stripe_Helper::is_pre_30() ? $order->billing_postcode : $order->get_billing_postcode(); |
348 | 347 | $details['address']['country'] = WC_Stripe_Helper::is_pre_30() ? $order->billing_country : $order->get_billing_country(); |
349 | 348 | |
350 | - return (object) apply_filters( 'wc_stripe_owner_details', $details, $order ); |
|
349 | + return (object) apply_filters('wc_stripe_owner_details', $details, $order); |
|
351 | 350 | } |
352 | 351 | |
353 | 352 | /** |
@@ -356,16 +355,16 @@ discard block |
||
356 | 355 | * @since 4.0.3 |
357 | 356 | */ |
358 | 357 | public function get_source_object() { |
359 | - $source = ! empty( $_POST['stripe_source'] ) ? wc_clean( $_POST['stripe_source'] ) : ''; |
|
358 | + $source = ! empty($_POST['stripe_source']) ? wc_clean($_POST['stripe_source']) : ''; |
|
360 | 359 | |
361 | - if ( empty( $source ) ) { |
|
360 | + if (empty($source)) { |
|
362 | 361 | return ''; |
363 | 362 | } |
364 | 363 | |
365 | - $source_object = WC_Stripe_API::retrieve( 'sources/' . $source ); |
|
364 | + $source_object = WC_Stripe_API::retrieve('sources/' . $source); |
|
366 | 365 | |
367 | - if ( ! empty( $source_object->error ) ) { |
|
368 | - throw new WC_Stripe_Exception( print_r( $source_object, true ), $source_object->error->message ); |
|
366 | + if ( ! empty($source_object->error)) { |
|
367 | + throw new WC_Stripe_Exception(print_r($source_object, true), $source_object->error->message); |
|
369 | 368 | } |
370 | 369 | |
371 | 370 | return $source_object; |
@@ -378,11 +377,11 @@ discard block |
||
378 | 377 | * @param object $source_object |
379 | 378 | * @return bool |
380 | 379 | */ |
381 | - public function is_3ds_required( $source_object ) { |
|
380 | + public function is_3ds_required($source_object) { |
|
382 | 381 | return ( |
383 | - $source_object && ! empty( $source_object->card ) ) && |
|
384 | - ( 'card' === $source_object->type && 'required' === $source_object->card->three_d_secure || |
|
385 | - ( $this->three_d_secure && 'optional' === $source_object->card->three_d_secure ) |
|
382 | + $source_object && ! empty($source_object->card) ) && |
|
383 | + ('card' === $source_object->type && 'required' === $source_object->card->three_d_secure || |
|
384 | + ($this->three_d_secure && 'optional' === $source_object->card->three_d_secure) |
|
386 | 385 | ); |
387 | 386 | } |
388 | 387 | |
@@ -393,8 +392,8 @@ discard block |
||
393 | 392 | * @param object $source_object |
394 | 393 | * @return bool |
395 | 394 | */ |
396 | - public function is_3ds_card( $source_object ) { |
|
397 | - return ( $source_object && 'three_d_secure' === $source_object->type ); |
|
395 | + public function is_3ds_card($source_object) { |
|
396 | + return ($source_object && 'three_d_secure' === $source_object->type); |
|
398 | 397 | } |
399 | 398 | |
400 | 399 | /** |
@@ -407,22 +406,22 @@ discard block |
||
407 | 406 | * @param string $return_url |
408 | 407 | * @return mixed |
409 | 408 | */ |
410 | - public function create_3ds_source( $order, $source_object, $return_url = '' ) { |
|
409 | + public function create_3ds_source($order, $source_object, $return_url = '') { |
|
411 | 410 | $currency = WC_Stripe_Helper::is_pre_30() ? $order->get_order_currency() : $order->get_currency(); |
412 | 411 | $order_id = WC_Stripe_Helper::is_pre_30() ? $order->id : $order->get_id(); |
413 | - $return_url = empty( $return_url ) ? $this->get_stripe_return_url( $order ) : $return_url; |
|
412 | + $return_url = empty($return_url) ? $this->get_stripe_return_url($order) : $return_url; |
|
414 | 413 | |
415 | 414 | $post_data = array(); |
416 | - $post_data['amount'] = WC_Stripe_Helper::get_stripe_amount( $order->get_total(), $currency ); |
|
417 | - $post_data['currency'] = strtolower( $currency ); |
|
415 | + $post_data['amount'] = WC_Stripe_Helper::get_stripe_amount($order->get_total(), $currency); |
|
416 | + $post_data['currency'] = strtolower($currency); |
|
418 | 417 | $post_data['type'] = 'three_d_secure'; |
419 | - $post_data['owner'] = $this->get_owner_details( $order ); |
|
420 | - $post_data['three_d_secure'] = array( 'card' => $source_object->id ); |
|
421 | - $post_data['redirect'] = array( 'return_url' => $return_url ); |
|
418 | + $post_data['owner'] = $this->get_owner_details($order); |
|
419 | + $post_data['three_d_secure'] = array('card' => $source_object->id); |
|
420 | + $post_data['redirect'] = array('return_url' => $return_url); |
|
422 | 421 | |
423 | - WC_Stripe_Logger::log( 'Info: Begin creating 3DS source...' ); |
|
422 | + WC_Stripe_Logger::log('Info: Begin creating 3DS source...'); |
|
424 | 423 | |
425 | - return WC_Stripe_API::request( apply_filters( 'wc_stripe_3ds_source', $post_data, $order ), 'sources' ); |
|
424 | + return WC_Stripe_API::request(apply_filters('wc_stripe_3ds_source', $post_data, $order), 'sources'); |
|
426 | 425 | } |
427 | 426 | |
428 | 427 | /** |
@@ -439,54 +438,54 @@ discard block |
||
439 | 438 | * @throws Exception When card was not added or for and invalid card. |
440 | 439 | * @return object |
441 | 440 | */ |
442 | - public function prepare_source( $source_object = '', $user_id, $force_save_source = false ) { |
|
443 | - $customer = new WC_Stripe_Customer( $user_id ); |
|
441 | + public function prepare_source($source_object = '', $user_id, $force_save_source = false) { |
|
442 | + $customer = new WC_Stripe_Customer($user_id); |
|
444 | 443 | $set_customer = true; |
445 | - $force_save_source = apply_filters( 'wc_stripe_force_save_source', $force_save_source, $customer ); |
|
444 | + $force_save_source = apply_filters('wc_stripe_force_save_source', $force_save_source, $customer); |
|
446 | 445 | $source_id = ''; |
447 | 446 | $wc_token_id = false; |
448 | - $payment_method = isset( $_POST['payment_method'] ) ? wc_clean( $_POST['payment_method'] ) : 'stripe'; |
|
447 | + $payment_method = isset($_POST['payment_method']) ? wc_clean($_POST['payment_method']) : 'stripe'; |
|
449 | 448 | |
450 | 449 | // New CC info was entered and we have a new source to process. |
451 | - if ( ! empty( $source_object ) ) { |
|
450 | + if ( ! empty($source_object)) { |
|
452 | 451 | $source_id = $source_object->id; |
453 | 452 | |
454 | 453 | // This checks to see if customer opted to save the payment method to file. |
455 | - $maybe_saved_card = isset( $_POST[ 'wc-' . $payment_method . '-new-payment-method' ] ) && ! empty( $_POST[ 'wc-' . $payment_method . '-new-payment-method' ] ); |
|
454 | + $maybe_saved_card = isset($_POST['wc-' . $payment_method . '-new-payment-method']) && ! empty($_POST['wc-' . $payment_method . '-new-payment-method']); |
|
456 | 455 | |
457 | 456 | /** |
458 | 457 | * This is true if the user wants to store the card to their account. |
459 | 458 | * Criteria to save to file is they are logged in, they opted to save or product requirements and the source is |
460 | 459 | * actually reusable. Either that or force_save_source is true. |
461 | 460 | */ |
462 | - if ( ( $user_id && $this->saved_cards && $maybe_saved_card && 'reusable' === $source_object->usage ) || $force_save_source ) { |
|
463 | - $response = $customer->add_source( $source_object->id ); |
|
461 | + if (($user_id && $this->saved_cards && $maybe_saved_card && 'reusable' === $source_object->usage) || $force_save_source) { |
|
462 | + $response = $customer->add_source($source_object->id); |
|
464 | 463 | |
465 | - if ( ! empty( $response->error ) ) { |
|
466 | - throw new WC_Stripe_Exception( print_r( $response, true ), $response->error->message ); |
|
464 | + if ( ! empty($response->error)) { |
|
465 | + throw new WC_Stripe_Exception(print_r($response, true), $response->error->message); |
|
467 | 466 | } |
468 | 467 | } |
469 | - } elseif ( isset( $_POST[ 'wc-' . $payment_method . '-payment-token' ] ) && 'new' !== $_POST[ 'wc-' . $payment_method . '-payment-token' ] ) { |
|
468 | + } elseif (isset($_POST['wc-' . $payment_method . '-payment-token']) && 'new' !== $_POST['wc-' . $payment_method . '-payment-token']) { |
|
470 | 469 | // Use an existing token, and then process the payment |
471 | - $wc_token_id = wc_clean( $_POST[ 'wc-' . $payment_method . '-payment-token' ] ); |
|
472 | - $wc_token = WC_Payment_Tokens::get( $wc_token_id ); |
|
470 | + $wc_token_id = wc_clean($_POST['wc-' . $payment_method . '-payment-token']); |
|
471 | + $wc_token = WC_Payment_Tokens::get($wc_token_id); |
|
473 | 472 | |
474 | - if ( ! $wc_token || $wc_token->get_user_id() !== get_current_user_id() ) { |
|
475 | - WC()->session->set( 'refresh_totals', true ); |
|
476 | - throw new WC_Stripe_Exception( 'Invalid payment method', __( 'Invalid payment method. Please input a new card number.', 'woocommerce-gateway-stripe' ) ); |
|
473 | + if ( ! $wc_token || $wc_token->get_user_id() !== get_current_user_id()) { |
|
474 | + WC()->session->set('refresh_totals', true); |
|
475 | + throw new WC_Stripe_Exception('Invalid payment method', __('Invalid payment method. Please input a new card number.', 'woocommerce-gateway-stripe')); |
|
477 | 476 | } |
478 | 477 | |
479 | 478 | $source_id = $wc_token->get_token(); |
480 | - } elseif ( isset( $_POST['stripe_token'] ) && 'new' !== $_POST['stripe_token'] ) { |
|
481 | - $stripe_token = wc_clean( $_POST['stripe_token'] ); |
|
482 | - $maybe_saved_card = isset( $_POST[ 'wc-' . $payment_method . '-new-payment-method' ] ) && ! empty( $_POST[ 'wc-' . $payment_method . '-new-payment-method' ] ); |
|
479 | + } elseif (isset($_POST['stripe_token']) && 'new' !== $_POST['stripe_token']) { |
|
480 | + $stripe_token = wc_clean($_POST['stripe_token']); |
|
481 | + $maybe_saved_card = isset($_POST['wc-' . $payment_method . '-new-payment-method']) && ! empty($_POST['wc-' . $payment_method . '-new-payment-method']); |
|
483 | 482 | |
484 | 483 | // This is true if the user wants to store the card to their account. |
485 | - if ( ( $user_id && $this->saved_cards && $maybe_saved_card ) || $force_save_source ) { |
|
486 | - $response = $customer->add_source( $stripe_token ); |
|
484 | + if (($user_id && $this->saved_cards && $maybe_saved_card) || $force_save_source) { |
|
485 | + $response = $customer->add_source($stripe_token); |
|
487 | 486 | |
488 | - if ( ! empty( $response->error ) ) { |
|
489 | - throw new WC_Stripe_Exception( print_r( $response, true ), $response->error->message ); |
|
487 | + if ( ! empty($response->error)) { |
|
488 | + throw new WC_Stripe_Exception(print_r($response, true), $response->error->message); |
|
490 | 489 | } |
491 | 490 | } else { |
492 | 491 | $set_customer = false; |
@@ -494,7 +493,7 @@ discard block |
||
494 | 493 | } |
495 | 494 | } |
496 | 495 | |
497 | - if ( ! $set_customer ) { |
|
496 | + if ( ! $set_customer) { |
|
498 | 497 | $customer_id = false; |
499 | 498 | } else { |
500 | 499 | $customer_id = $customer->get_id() ? $customer->get_id() : false; |
@@ -520,37 +519,37 @@ discard block |
||
520 | 519 | * @param object $order |
521 | 520 | * @return object |
522 | 521 | */ |
523 | - public function prepare_order_source( $order = null ) { |
|
522 | + public function prepare_order_source($order = null) { |
|
524 | 523 | $stripe_customer = new WC_Stripe_Customer(); |
525 | 524 | $stripe_source = false; |
526 | 525 | $token_id = false; |
527 | 526 | |
528 | - if ( $order ) { |
|
527 | + if ($order) { |
|
529 | 528 | $order_id = WC_Stripe_Helper::is_pre_30() ? $order->id : $order->get_id(); |
530 | 529 | |
531 | - $stripe_customer_id = get_post_meta( $order_id, '_stripe_customer_id', true ); |
|
530 | + $stripe_customer_id = get_post_meta($order_id, '_stripe_customer_id', true); |
|
532 | 531 | |
533 | - if ( $stripe_customer_id ) { |
|
534 | - $stripe_customer->set_id( $stripe_customer_id ); |
|
532 | + if ($stripe_customer_id) { |
|
533 | + $stripe_customer->set_id($stripe_customer_id); |
|
535 | 534 | } |
536 | 535 | |
537 | - $source_id = WC_Stripe_Helper::is_pre_30() ? get_post_meta( $order_id, '_stripe_source_id', true ) : $order->get_meta( '_stripe_source_id', true ); |
|
536 | + $source_id = WC_Stripe_Helper::is_pre_30() ? get_post_meta($order_id, '_stripe_source_id', true) : $order->get_meta('_stripe_source_id', true); |
|
538 | 537 | |
539 | 538 | // Since 4.0.0, we changed card to source so we need to account for that. |
540 | - if ( empty( $source_id ) ) { |
|
541 | - $source_id = WC_Stripe_Helper::is_pre_30() ? get_post_meta( $order_id, '_stripe_card_id', true ) : $order->get_meta( '_stripe_card_id', true ); |
|
539 | + if (empty($source_id)) { |
|
540 | + $source_id = WC_Stripe_Helper::is_pre_30() ? get_post_meta($order_id, '_stripe_card_id', true) : $order->get_meta('_stripe_card_id', true); |
|
542 | 541 | |
543 | 542 | // Take this opportunity to update the key name. |
544 | - WC_Stripe_Helper::is_pre_30() ? update_post_meta( $order_id, '_stripe_source_id', $source_id ) : $order->update_meta_data( '_stripe_source_id', $source_id ); |
|
543 | + WC_Stripe_Helper::is_pre_30() ? update_post_meta($order_id, '_stripe_source_id', $source_id) : $order->update_meta_data('_stripe_source_id', $source_id); |
|
545 | 544 | |
546 | - if ( is_callable( array( $order, 'save' ) ) ) { |
|
545 | + if (is_callable(array($order, 'save'))) { |
|
547 | 546 | $order->save(); |
548 | 547 | } |
549 | 548 | } |
550 | 549 | |
551 | - if ( $source_id ) { |
|
550 | + if ($source_id) { |
|
552 | 551 | $stripe_source = $source_id; |
553 | - } elseif ( apply_filters( 'wc_stripe_use_default_customer_source', true ) ) { |
|
552 | + } elseif (apply_filters('wc_stripe_use_default_customer_source', true)) { |
|
554 | 553 | /* |
555 | 554 | * We can attempt to charge the customer's default source |
556 | 555 | * by sending empty source id. |
@@ -574,27 +573,27 @@ discard block |
||
574 | 573 | * @param WC_Order $order For to which the source applies. |
575 | 574 | * @param stdClass $source Source information. |
576 | 575 | */ |
577 | - public function save_source_to_order( $order, $source ) { |
|
576 | + public function save_source_to_order($order, $source) { |
|
578 | 577 | $order_id = WC_Stripe_Helper::is_pre_30() ? $order->id : $order->get_id(); |
579 | 578 | |
580 | 579 | // Store source in the order. |
581 | - if ( $source->customer ) { |
|
582 | - if ( WC_Stripe_Helper::is_pre_30() ) { |
|
583 | - update_post_meta( $order_id, '_stripe_customer_id', $source->customer ); |
|
580 | + if ($source->customer) { |
|
581 | + if (WC_Stripe_Helper::is_pre_30()) { |
|
582 | + update_post_meta($order_id, '_stripe_customer_id', $source->customer); |
|
584 | 583 | } else { |
585 | - $order->update_meta_data( '_stripe_customer_id', $source->customer ); |
|
584 | + $order->update_meta_data('_stripe_customer_id', $source->customer); |
|
586 | 585 | } |
587 | 586 | } |
588 | 587 | |
589 | - if ( $source->source ) { |
|
590 | - if ( WC_Stripe_Helper::is_pre_30() ) { |
|
591 | - update_post_meta( $order_id, '_stripe_source_id', $source->source ); |
|
588 | + if ($source->source) { |
|
589 | + if (WC_Stripe_Helper::is_pre_30()) { |
|
590 | + update_post_meta($order_id, '_stripe_source_id', $source->source); |
|
592 | 591 | } else { |
593 | - $order->update_meta_data( '_stripe_source_id', $source->source ); |
|
592 | + $order->update_meta_data('_stripe_source_id', $source->source); |
|
594 | 593 | } |
595 | 594 | } |
596 | 595 | |
597 | - if ( is_callable( array( $order, 'save' ) ) ) { |
|
596 | + if (is_callable(array($order, 'save'))) { |
|
598 | 597 | $order->save(); |
599 | 598 | } |
600 | 599 | } |
@@ -608,27 +607,27 @@ discard block |
||
608 | 607 | * @param object $order The order object |
609 | 608 | * @param int $balance_transaction_id |
610 | 609 | */ |
611 | - public function update_fees( $order, $balance_transaction_id ) { |
|
610 | + public function update_fees($order, $balance_transaction_id) { |
|
612 | 611 | $order_id = WC_Stripe_Helper::is_pre_30() ? $order->id : $order->get_id(); |
613 | 612 | |
614 | - $balance_transaction = WC_Stripe_API::retrieve( 'balance/history/' . $balance_transaction_id ); |
|
613 | + $balance_transaction = WC_Stripe_API::retrieve('balance/history/' . $balance_transaction_id); |
|
615 | 614 | |
616 | - if ( empty( $balance_transaction->error ) ) { |
|
617 | - if ( isset( $balance_transaction ) && isset( $balance_transaction->fee ) ) { |
|
615 | + if (empty($balance_transaction->error)) { |
|
616 | + if (isset($balance_transaction) && isset($balance_transaction->fee)) { |
|
618 | 617 | // Fees and Net needs to both come from Stripe to be accurate as the returned |
619 | 618 | // values are in the local currency of the Stripe account, not from WC. |
620 | - $fee = ! empty( $balance_transaction->fee ) ? WC_Stripe_Helper::format_balance_fee( $balance_transaction, 'fee' ) : 0; |
|
621 | - $net = ! empty( $balance_transaction->net ) ? WC_Stripe_Helper::format_balance_fee( $balance_transaction, 'net' ) : 0; |
|
619 | + $fee = ! empty($balance_transaction->fee) ? WC_Stripe_Helper::format_balance_fee($balance_transaction, 'fee') : 0; |
|
620 | + $net = ! empty($balance_transaction->net) ? WC_Stripe_Helper::format_balance_fee($balance_transaction, 'net') : 0; |
|
622 | 621 | |
623 | - WC_Stripe_Helper::is_pre_30() ? update_post_meta( $order_id, self::META_NAME_FEE, $fee ) : $order->update_meta_data( self::META_NAME_FEE, $fee ); |
|
624 | - WC_Stripe_Helper::is_pre_30() ? update_post_meta( $order_id, self::META_NAME_NET, $net ) : $order->update_meta_data( self::META_NAME_NET, $net ); |
|
622 | + WC_Stripe_Helper::is_pre_30() ? update_post_meta($order_id, self::META_NAME_FEE, $fee) : $order->update_meta_data(self::META_NAME_FEE, $fee); |
|
623 | + WC_Stripe_Helper::is_pre_30() ? update_post_meta($order_id, self::META_NAME_NET, $net) : $order->update_meta_data(self::META_NAME_NET, $net); |
|
625 | 624 | |
626 | - if ( is_callable( array( $order, 'save' ) ) ) { |
|
625 | + if (is_callable(array($order, 'save'))) { |
|
627 | 626 | $order->save(); |
628 | 627 | } |
629 | 628 | } |
630 | 629 | } else { |
631 | - WC_Stripe_Logger::log( "Unable to update fees/net meta for order: {$order_id}" ); |
|
630 | + WC_Stripe_Logger::log("Unable to update fees/net meta for order: {$order_id}"); |
|
632 | 631 | } |
633 | 632 | } |
634 | 633 | |
@@ -641,33 +640,33 @@ discard block |
||
641 | 640 | * @param float $amount |
642 | 641 | * @return bool |
643 | 642 | */ |
644 | - public function process_refund( $order_id, $amount = null, $reason = '' ) { |
|
645 | - $order = wc_get_order( $order_id ); |
|
643 | + public function process_refund($order_id, $amount = null, $reason = '') { |
|
644 | + $order = wc_get_order($order_id); |
|
646 | 645 | |
647 | - if ( ! $order || ! $order->get_transaction_id() ) { |
|
646 | + if ( ! $order || ! $order->get_transaction_id()) { |
|
648 | 647 | return false; |
649 | 648 | } |
650 | 649 | |
651 | 650 | $request = array(); |
652 | 651 | |
653 | - if ( WC_Stripe_Helper::is_pre_30() ) { |
|
654 | - $order_currency = get_post_meta( $order_id, '_order_currency', true ); |
|
655 | - $captured = get_post_meta( $order_id, '_stripe_charge_captured', true ); |
|
652 | + if (WC_Stripe_Helper::is_pre_30()) { |
|
653 | + $order_currency = get_post_meta($order_id, '_order_currency', true); |
|
654 | + $captured = get_post_meta($order_id, '_stripe_charge_captured', true); |
|
656 | 655 | } else { |
657 | 656 | $order_currency = $order->get_currency(); |
658 | - $captured = $order->get_meta( '_stripe_charge_captured', true ); |
|
657 | + $captured = $order->get_meta('_stripe_charge_captured', true); |
|
659 | 658 | } |
660 | 659 | |
661 | - if ( ! is_null( $amount ) ) { |
|
662 | - $request['amount'] = WC_Stripe_Helper::get_stripe_amount( $amount, $order_currency ); |
|
660 | + if ( ! is_null($amount)) { |
|
661 | + $request['amount'] = WC_Stripe_Helper::get_stripe_amount($amount, $order_currency); |
|
663 | 662 | } |
664 | 663 | |
665 | 664 | // If order is only authorized, don't pass amount. |
666 | - if ( 'yes' !== $captured ) { |
|
667 | - unset( $request['amount'] ); |
|
665 | + if ('yes' !== $captured) { |
|
666 | + unset($request['amount']); |
|
668 | 667 | } |
669 | 668 | |
670 | - if ( $reason ) { |
|
669 | + if ($reason) { |
|
671 | 670 | $request['metadata'] = array( |
672 | 671 | 'reason' => $reason, |
673 | 672 | ); |
@@ -675,33 +674,33 @@ discard block |
||
675 | 674 | |
676 | 675 | $request['charge'] = $order->get_transaction_id(); |
677 | 676 | |
678 | - WC_Stripe_Logger::log( "Info: Beginning refund for order {$order->get_transaction_id()} for the amount of {$amount}" ); |
|
677 | + WC_Stripe_Logger::log("Info: Beginning refund for order {$order->get_transaction_id()} for the amount of {$amount}"); |
|
679 | 678 | |
680 | - $response = WC_Stripe_API::request( $request, 'refunds' ); |
|
679 | + $response = WC_Stripe_API::request($request, 'refunds'); |
|
681 | 680 | |
682 | - if ( ! empty( $response->error ) ) { |
|
683 | - WC_Stripe_Logger::log( 'Error: ' . $response->error->message ); |
|
681 | + if ( ! empty($response->error)) { |
|
682 | + WC_Stripe_Logger::log('Error: ' . $response->error->message); |
|
684 | 683 | |
685 | 684 | return $response; |
686 | 685 | |
687 | - } elseif ( ! empty( $response->id ) ) { |
|
688 | - WC_Stripe_Helper::is_pre_30() ? update_post_meta( $order_id, '_stripe_refund_id', $response->id ) : $order->update_meta_data( '_stripe_refund_id', $response->id ); |
|
686 | + } elseif ( ! empty($response->id)) { |
|
687 | + WC_Stripe_Helper::is_pre_30() ? update_post_meta($order_id, '_stripe_refund_id', $response->id) : $order->update_meta_data('_stripe_refund_id', $response->id); |
|
689 | 688 | |
690 | - $amount = wc_price( $response->amount / 100 ); |
|
689 | + $amount = wc_price($response->amount / 100); |
|
691 | 690 | |
692 | - if ( in_array( strtolower( $order->get_currency() ), WC_Stripe_Helper::no_decimal_currencies() ) ) { |
|
693 | - $amount = wc_price( $response->amount ); |
|
691 | + if (in_array(strtolower($order->get_currency()), WC_Stripe_Helper::no_decimal_currencies())) { |
|
692 | + $amount = wc_price($response->amount); |
|
694 | 693 | } |
695 | 694 | |
696 | - if ( isset( $response->balance_transaction ) ) { |
|
697 | - $this->update_fees( $order, $response->balance_transaction ); |
|
695 | + if (isset($response->balance_transaction)) { |
|
696 | + $this->update_fees($order, $response->balance_transaction); |
|
698 | 697 | } |
699 | 698 | |
700 | 699 | /* translators: 1) dollar amount 2) transaction id 3) refund message */ |
701 | - $refund_message = ( isset( $captured ) && 'yes' === $captured ) ? sprintf( __( 'Refunded %1$s - Refund ID: %2$s - Reason: %3$s', 'woocommerce-gateway-stripe' ), $amount, $response->id, $reason ) : __( 'Pre-Authorization Released', 'woocommerce-gateway-stripe' ); |
|
700 | + $refund_message = (isset($captured) && 'yes' === $captured) ? sprintf(__('Refunded %1$s - Refund ID: %2$s - Reason: %3$s', 'woocommerce-gateway-stripe'), $amount, $response->id, $reason) : __('Pre-Authorization Released', 'woocommerce-gateway-stripe'); |
|
702 | 701 | |
703 | - $order->add_order_note( $refund_message ); |
|
704 | - WC_Stripe_Logger::log( 'Success: ' . html_entity_decode( strip_tags( $refund_message ) ) ); |
|
702 | + $order->add_order_note($refund_message); |
|
703 | + WC_Stripe_Logger::log('Success: ' . html_entity_decode(strip_tags($refund_message))); |
|
705 | 704 | |
706 | 705 | return true; |
707 | 706 | } |
@@ -716,44 +715,44 @@ discard block |
||
716 | 715 | */ |
717 | 716 | public function add_payment_method() { |
718 | 717 | $error = false; |
719 | - $error_msg = __( 'There was a problem adding the card.', 'woocommerce-gateway-stripe' ); |
|
718 | + $error_msg = __('There was a problem adding the card.', 'woocommerce-gateway-stripe'); |
|
720 | 719 | $source_id = ''; |
721 | 720 | |
722 | - if ( empty( $_POST['stripe_source'] ) && empty( $_POST['stripe_token'] ) || ! is_user_logged_in() ) { |
|
721 | + if (empty($_POST['stripe_source']) && empty($_POST['stripe_token']) || ! is_user_logged_in()) { |
|
723 | 722 | $error = true; |
724 | 723 | } |
725 | 724 | |
726 | - $stripe_customer = new WC_Stripe_Customer( get_current_user_id() ); |
|
725 | + $stripe_customer = new WC_Stripe_Customer(get_current_user_id()); |
|
727 | 726 | |
728 | - $source = ! empty( $_POST['stripe_source'] ) ? wc_clean( $_POST['stripe_source'] ) : ''; |
|
727 | + $source = ! empty($_POST['stripe_source']) ? wc_clean($_POST['stripe_source']) : ''; |
|
729 | 728 | |
730 | - $source_object = WC_Stripe_API::retrieve( 'sources/' . $source ); |
|
729 | + $source_object = WC_Stripe_API::retrieve('sources/' . $source); |
|
731 | 730 | |
732 | - if ( isset( $source_object ) ) { |
|
733 | - if ( ! empty( $source_object->error ) ) { |
|
731 | + if (isset($source_object)) { |
|
732 | + if ( ! empty($source_object->error)) { |
|
734 | 733 | $error = true; |
735 | 734 | } |
736 | 735 | |
737 | 736 | $source_id = $source_object->id; |
738 | - } elseif ( isset( $_POST['stripe_token'] ) ) { |
|
739 | - $source_id = wc_clean( $_POST['stripe_token'] ); |
|
737 | + } elseif (isset($_POST['stripe_token'])) { |
|
738 | + $source_id = wc_clean($_POST['stripe_token']); |
|
740 | 739 | } |
741 | 740 | |
742 | - $response = $stripe_customer->add_source( $source_id ); |
|
741 | + $response = $stripe_customer->add_source($source_id); |
|
743 | 742 | |
744 | - if ( ! $response || is_wp_error( $response ) || ! empty( $response->error ) ) { |
|
743 | + if ( ! $response || is_wp_error($response) || ! empty($response->error)) { |
|
745 | 744 | $error = true; |
746 | 745 | } |
747 | 746 | |
748 | - if ( $error ) { |
|
749 | - wc_add_notice( $error_msg, 'error' ); |
|
750 | - WC_Stripe_Logger::log( 'Add payment method Error: ' . $error_msg ); |
|
747 | + if ($error) { |
|
748 | + wc_add_notice($error_msg, 'error'); |
|
749 | + WC_Stripe_Logger::log('Add payment method Error: ' . $error_msg); |
|
751 | 750 | return; |
752 | 751 | } |
753 | 752 | |
754 | 753 | return array( |
755 | 754 | 'result' => 'success', |
756 | - 'redirect' => wc_get_endpoint_url( 'payment-methods' ), |
|
755 | + 'redirect' => wc_get_endpoint_url('payment-methods'), |
|
757 | 756 | ); |
758 | 757 | } |
759 | 758 | } |
@@ -1,5 +1,5 @@ discard block |
||
1 | 1 | <?php |
2 | -if ( ! defined( 'ABSPATH' ) ) { |
|
2 | +if ( ! defined('ABSPATH')) { |
|
3 | 3 | exit; |
4 | 4 | } |
5 | 5 | |
@@ -17,7 +17,7 @@ discard block |
||
17 | 17 | * @version 4.0.0 |
18 | 18 | */ |
19 | 19 | public function __construct() { |
20 | - add_action( 'woocommerce_api_wc_stripe', array( $this, 'check_for_webhook' ) ); |
|
20 | + add_action('woocommerce_api_wc_stripe', array($this, 'check_for_webhook')); |
|
21 | 21 | } |
22 | 22 | |
23 | 23 | /** |
@@ -27,24 +27,24 @@ discard block |
||
27 | 27 | * @version 4.0.0 |
28 | 28 | */ |
29 | 29 | public function check_for_webhook() { |
30 | - if ( ( 'POST' !== $_SERVER['REQUEST_METHOD'] ) |
|
31 | - || ! isset( $_GET['wc-api'] ) |
|
32 | - || ( 'wc_stripe' !== $_GET['wc-api'] ) |
|
30 | + if (('POST' !== $_SERVER['REQUEST_METHOD']) |
|
31 | + || ! isset($_GET['wc-api']) |
|
32 | + || ('wc_stripe' !== $_GET['wc-api']) |
|
33 | 33 | ) { |
34 | 34 | return; |
35 | 35 | } |
36 | 36 | |
37 | - $request_body = file_get_contents( 'php://input' ); |
|
38 | - $request_headers = array_change_key_case( $this->get_request_headers(), CASE_UPPER ); |
|
37 | + $request_body = file_get_contents('php://input'); |
|
38 | + $request_headers = array_change_key_case($this->get_request_headers(), CASE_UPPER); |
|
39 | 39 | |
40 | 40 | // Validate it to make sure it is legit. |
41 | - if ( $this->is_valid_request( $request_headers, $request_body ) ) { |
|
42 | - $this->process_webhook( $request_body ); |
|
43 | - status_header( 200 ); |
|
41 | + if ($this->is_valid_request($request_headers, $request_body)) { |
|
42 | + $this->process_webhook($request_body); |
|
43 | + status_header(200); |
|
44 | 44 | exit; |
45 | 45 | } else { |
46 | - WC_Stripe_Logger::log( 'Incoming webhook failed validation: ' . print_r( $request_body, true ) ); |
|
47 | - status_header( 400 ); |
|
46 | + WC_Stripe_Logger::log('Incoming webhook failed validation: ' . print_r($request_body, true)); |
|
47 | + status_header(400); |
|
48 | 48 | exit; |
49 | 49 | } |
50 | 50 | } |
@@ -59,12 +59,12 @@ discard block |
||
59 | 59 | * @param string $request_body The request body from Stripe. |
60 | 60 | * @return bool |
61 | 61 | */ |
62 | - public function is_valid_request( $request_headers = null, $request_body = null ) { |
|
63 | - if ( null === $request_headers || null === $request_body ) { |
|
62 | + public function is_valid_request($request_headers = null, $request_body = null) { |
|
63 | + if (null === $request_headers || null === $request_body) { |
|
64 | 64 | return false; |
65 | 65 | } |
66 | 66 | |
67 | - if ( ! empty( $request_headers['USER-AGENT'] ) && ! preg_match( '/Stripe/', $request_headers['USER-AGENT'] ) ) { |
|
67 | + if ( ! empty($request_headers['USER-AGENT']) && ! preg_match('/Stripe/', $request_headers['USER-AGENT'])) { |
|
68 | 68 | return false; |
69 | 69 | } |
70 | 70 | |
@@ -80,11 +80,11 @@ discard block |
||
80 | 80 | * @version 4.0.0 |
81 | 81 | */ |
82 | 82 | public function get_request_headers() { |
83 | - if ( ! function_exists( 'getallheaders' ) ) { |
|
83 | + if ( ! function_exists('getallheaders')) { |
|
84 | 84 | $headers = []; |
85 | - foreach ( $_SERVER as $name => $value ) { |
|
86 | - if ( 'HTTP_' === substr( $name, 0, 5 ) ) { |
|
87 | - $headers[ str_replace( ' ', '-', ucwords( strtolower( str_replace( '_', ' ', substr( $name, 5 ) ) ) ) ) ] = $value; |
|
85 | + foreach ($_SERVER as $name => $value) { |
|
86 | + if ('HTTP_' === substr($name, 0, 5)) { |
|
87 | + $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value; |
|
88 | 88 | } |
89 | 89 | } |
90 | 90 | |
@@ -103,30 +103,30 @@ discard block |
||
103 | 103 | * @param object $notification |
104 | 104 | * @param bool $retry |
105 | 105 | */ |
106 | - public function process_webhook_payment( $notification, $retry = true ) { |
|
106 | + public function process_webhook_payment($notification, $retry = true) { |
|
107 | 107 | // The following 2 payment methods are synchronous so does not need to be handle via webhook. |
108 | - if ( 'card' === $notification->data->object->type || 'sepa_debit' === $notification->data->object->type ) { |
|
108 | + if ('card' === $notification->data->object->type || 'sepa_debit' === $notification->data->object->type) { |
|
109 | 109 | return; |
110 | 110 | } |
111 | 111 | |
112 | - $order = WC_Stripe_Helper::get_order_by_source_id( $notification->data->object->id ); |
|
112 | + $order = WC_Stripe_Helper::get_order_by_source_id($notification->data->object->id); |
|
113 | 113 | |
114 | - if ( ! $order ) { |
|
115 | - WC_Stripe_Logger::log( 'Could not find order via source ID: ' . $notification->data->object->id ); |
|
114 | + if ( ! $order) { |
|
115 | + WC_Stripe_Logger::log('Could not find order via source ID: ' . $notification->data->object->id); |
|
116 | 116 | return; |
117 | 117 | } |
118 | 118 | |
119 | 119 | $order_id = WC_Stripe_Helper::is_pre_30() ? $order->id : $order->get_id(); |
120 | 120 | $source_id = $notification->data->object->id; |
121 | 121 | |
122 | - $is_pending_receiver = ( 'receiver' === $notification->data->object->flow ); |
|
122 | + $is_pending_receiver = ('receiver' === $notification->data->object->flow); |
|
123 | 123 | |
124 | 124 | try { |
125 | - if ( 'processing' === $order->get_status() || 'completed' === $order->get_status() ) { |
|
125 | + if ('processing' === $order->get_status() || 'completed' === $order->get_status()) { |
|
126 | 126 | return; |
127 | 127 | } |
128 | 128 | |
129 | - if ( 'on-hold' === $order->get_status() && ! $is_pending_receiver ) { |
|
129 | + if ('on-hold' === $order->get_status() && ! $is_pending_receiver) { |
|
130 | 130 | return; |
131 | 131 | } |
132 | 132 | |
@@ -134,80 +134,80 @@ discard block |
||
134 | 134 | $response = null; |
135 | 135 | |
136 | 136 | // This will throw exception if not valid. |
137 | - $this->validate_minimum_order_amount( $order ); |
|
137 | + $this->validate_minimum_order_amount($order); |
|
138 | 138 | |
139 | - WC_Stripe_Logger::log( "Info: (Webhook) Begin processing payment for order $order_id for the amount of {$order->get_total()}" ); |
|
139 | + WC_Stripe_Logger::log("Info: (Webhook) Begin processing payment for order $order_id for the amount of {$order->get_total()}"); |
|
140 | 140 | |
141 | 141 | // Prep source object. |
142 | 142 | $source_object = new stdClass(); |
143 | 143 | $source_object->token_id = ''; |
144 | - $source_object->customer = $this->get_stripe_customer_id( $order ); |
|
144 | + $source_object->customer = $this->get_stripe_customer_id($order); |
|
145 | 145 | $source_object->source = $source_id; |
146 | 146 | |
147 | 147 | // Make the request. |
148 | - $response = WC_Stripe_API::request( $this->generate_payment_request( $order, $source_object ) ); |
|
148 | + $response = WC_Stripe_API::request($this->generate_payment_request($order, $source_object)); |
|
149 | 149 | |
150 | - if ( ! empty( $response->error ) ) { |
|
150 | + if ( ! empty($response->error)) { |
|
151 | 151 | // If it is an API error such connection or server, let's retry. |
152 | - if ( 'api_connection_error' === $response->error->type || 'api_error' === $response->error->type ) { |
|
153 | - if ( $retry ) { |
|
154 | - sleep( 5 ); |
|
155 | - return $this->process_payment( $order_id, false ); |
|
152 | + if ('api_connection_error' === $response->error->type || 'api_error' === $response->error->type) { |
|
153 | + if ($retry) { |
|
154 | + sleep(5); |
|
155 | + return $this->process_payment($order_id, false); |
|
156 | 156 | } else { |
157 | 157 | $localized_message = 'API connection error and retries exhausted.'; |
158 | - $order->add_order_note( $localized_message ); |
|
159 | - throw new WC_Stripe_Exception( print_r( $response, true ), $localized_message ); |
|
158 | + $order->add_order_note($localized_message); |
|
159 | + throw new WC_Stripe_Exception(print_r($response, true), $localized_message); |
|
160 | 160 | } |
161 | 161 | } |
162 | 162 | |
163 | 163 | // Customer param wrong? The user may have been deleted on stripe's end. Remove customer_id. Can be retried without. |
164 | - if ( preg_match( '/No such customer/i', $response->error->message ) && $retry ) { |
|
165 | - if ( WC_Stripe_Helper::is_pre_30() ) { |
|
166 | - delete_user_meta( $order->customer_user, '_stripe_customer_id' ); |
|
167 | - delete_post_meta( $order_id, '_stripe_customer_id' ); |
|
164 | + if (preg_match('/No such customer/i', $response->error->message) && $retry) { |
|
165 | + if (WC_Stripe_Helper::is_pre_30()) { |
|
166 | + delete_user_meta($order->customer_user, '_stripe_customer_id'); |
|
167 | + delete_post_meta($order_id, '_stripe_customer_id'); |
|
168 | 168 | } else { |
169 | - delete_user_meta( $order->get_customer_id(), '_stripe_customer_id' ); |
|
170 | - $order->delete_meta_data( '_stripe_customer_id' ); |
|
169 | + delete_user_meta($order->get_customer_id(), '_stripe_customer_id'); |
|
170 | + $order->delete_meta_data('_stripe_customer_id'); |
|
171 | 171 | $order->save(); |
172 | 172 | } |
173 | 173 | |
174 | - return $this->process_payment( $order_id, false ); |
|
174 | + return $this->process_payment($order_id, false); |
|
175 | 175 | |
176 | - } elseif ( preg_match( '/No such token/i', $response->error->message ) && $source_object->token_id ) { |
|
176 | + } elseif (preg_match('/No such token/i', $response->error->message) && $source_object->token_id) { |
|
177 | 177 | // Source param wrong? The CARD may have been deleted on stripe's end. Remove token and show message. |
178 | - $wc_token = WC_Payment_Tokens::get( $source_object->token_id ); |
|
178 | + $wc_token = WC_Payment_Tokens::get($source_object->token_id); |
|
179 | 179 | $wc_token->delete(); |
180 | - $message = __( 'This card is no longer available and has been removed.', 'woocommerce-gateway-stripe' ); |
|
181 | - $order->add_order_note( $message ); |
|
182 | - throw new WC_Stripe_Exception( print_r( $response, true ), $message ); |
|
180 | + $message = __('This card is no longer available and has been removed.', 'woocommerce-gateway-stripe'); |
|
181 | + $order->add_order_note($message); |
|
182 | + throw new WC_Stripe_Exception(print_r($response, true), $message); |
|
183 | 183 | } |
184 | 184 | |
185 | 185 | $localized_messages = WC_Stripe_Helper::get_localized_messages(); |
186 | 186 | |
187 | - if ( 'card_error' === $response->error->type ) { |
|
188 | - $localized_message = isset( $localized_messages[ $response->error->code ] ) ? $localized_messages[ $response->error->code ] : $response->error->message; |
|
187 | + if ('card_error' === $response->error->type) { |
|
188 | + $localized_message = isset($localized_messages[$response->error->code]) ? $localized_messages[$response->error->code] : $response->error->message; |
|
189 | 189 | } else { |
190 | - $localized_message = isset( $localized_messages[ $response->error->type ] ) ? $localized_messages[ $response->error->type ] : $response->error->message; |
|
190 | + $localized_message = isset($localized_messages[$response->error->type]) ? $localized_messages[$response->error->type] : $response->error->message; |
|
191 | 191 | } |
192 | 192 | |
193 | - $order->add_order_note( $localized_message ); |
|
193 | + $order->add_order_note($localized_message); |
|
194 | 194 | |
195 | - throw new WC_Stripe_Exception( print_r( $response, true ), $localized_message ); |
|
195 | + throw new WC_Stripe_Exception(print_r($response, true), $localized_message); |
|
196 | 196 | } |
197 | 197 | |
198 | - do_action( 'wc_gateway_stripe_process_webhook_payment', $response, $order ); |
|
198 | + do_action('wc_gateway_stripe_process_webhook_payment', $response, $order); |
|
199 | 199 | |
200 | - $this->process_response( $response, $order ); |
|
200 | + $this->process_response($response, $order); |
|
201 | 201 | |
202 | - } catch ( WC_Stripe_Exception $e ) { |
|
203 | - WC_Stripe_Logger::log( 'Error: ' . $e->getMessage() ); |
|
202 | + } catch (WC_Stripe_Exception $e) { |
|
203 | + WC_Stripe_Logger::log('Error: ' . $e->getMessage()); |
|
204 | 204 | |
205 | - do_action( 'wc_gateway_stripe_process_webhook_payment_error', $e, $order ); |
|
205 | + do_action('wc_gateway_stripe_process_webhook_payment_error', $e, $order); |
|
206 | 206 | |
207 | - $statuses = array( 'pending', 'failed' ); |
|
207 | + $statuses = array('pending', 'failed'); |
|
208 | 208 | |
209 | - if ( $order->has_status( $statuses ) ) { |
|
210 | - $this->send_failed_order_email( $order_id ); |
|
209 | + if ($order->has_status($statuses)) { |
|
210 | + $this->send_failed_order_email($order_id); |
|
211 | 211 | } |
212 | 212 | } |
213 | 213 | } |
@@ -220,20 +220,20 @@ discard block |
||
220 | 220 | * @since 4.0.0 |
221 | 221 | * @param object $notification |
222 | 222 | */ |
223 | - public function process_webhook_dispute( $notification ) { |
|
224 | - $order = WC_Stripe_Helper::get_order_by_charge_id( $notification->data->object->charge ); |
|
223 | + public function process_webhook_dispute($notification) { |
|
224 | + $order = WC_Stripe_Helper::get_order_by_charge_id($notification->data->object->charge); |
|
225 | 225 | |
226 | - if ( ! $order ) { |
|
227 | - WC_Stripe_Logger::log( 'Could not find order via charge ID: ' . $notification->data->object->charge ); |
|
226 | + if ( ! $order) { |
|
227 | + WC_Stripe_Logger::log('Could not find order via charge ID: ' . $notification->data->object->charge); |
|
228 | 228 | return; |
229 | 229 | } |
230 | 230 | |
231 | - $order->update_status( 'on-hold', __( 'A dispute was created for this order. Response is needed. Please go to your Stripe Dashboard to review this dispute.', 'woocommerce-gateway-stripe' ) ); |
|
231 | + $order->update_status('on-hold', __('A dispute was created for this order. Response is needed. Please go to your Stripe Dashboard to review this dispute.', 'woocommerce-gateway-stripe')); |
|
232 | 232 | |
233 | - do_action( 'wc_gateway_stripe_process_webhook_payment_error', $order, $notification ); |
|
233 | + do_action('wc_gateway_stripe_process_webhook_payment_error', $order, $notification); |
|
234 | 234 | |
235 | 235 | $order_id = WC_Stripe_Helper::is_pre_30() ? $order->id : $order->get_id(); |
236 | - $this->send_failed_order_email( $order_id ); |
|
236 | + $this->send_failed_order_email($order_id); |
|
237 | 237 | } |
238 | 238 | |
239 | 239 | /** |
@@ -244,41 +244,41 @@ discard block |
||
244 | 244 | * @version 4.0.0 |
245 | 245 | * @param object $notification |
246 | 246 | */ |
247 | - public function process_webhook_capture( $notification ) { |
|
248 | - $order = WC_Stripe_Helper::get_order_by_charge_id( $notification->data->object->id ); |
|
247 | + public function process_webhook_capture($notification) { |
|
248 | + $order = WC_Stripe_Helper::get_order_by_charge_id($notification->data->object->id); |
|
249 | 249 | |
250 | - if ( ! $order ) { |
|
251 | - WC_Stripe_Logger::log( 'Could not find order via charge ID: ' . $notification->data->object->id ); |
|
250 | + if ( ! $order) { |
|
251 | + WC_Stripe_Logger::log('Could not find order via charge ID: ' . $notification->data->object->id); |
|
252 | 252 | return; |
253 | 253 | } |
254 | 254 | |
255 | 255 | $order_id = WC_Stripe_Helper::is_pre_30() ? $order->id : $order->get_id(); |
256 | 256 | |
257 | - if ( 'stripe' === ( WC_Stripe_Helper::is_pre_30() ? $order->payment_method : $order->get_payment_method() ) ) { |
|
258 | - $charge = WC_Stripe_Helper::is_pre_30() ? get_post_meta( $order_id, '_transaction_id', true ) : $order->get_transaction_id(); |
|
259 | - $captured = WC_Stripe_Helper::is_pre_30() ? get_post_meta( $order_id, '_stripe_charge_captured', true ) : $order->get_meta( '_stripe_charge_captured', true ); |
|
257 | + if ('stripe' === (WC_Stripe_Helper::is_pre_30() ? $order->payment_method : $order->get_payment_method())) { |
|
258 | + $charge = WC_Stripe_Helper::is_pre_30() ? get_post_meta($order_id, '_transaction_id', true) : $order->get_transaction_id(); |
|
259 | + $captured = WC_Stripe_Helper::is_pre_30() ? get_post_meta($order_id, '_stripe_charge_captured', true) : $order->get_meta('_stripe_charge_captured', true); |
|
260 | 260 | |
261 | - if ( $charge && 'no' === $captured ) { |
|
262 | - WC_Stripe_Helper::is_pre_30() ? update_post_meta( $order_id, '_stripe_charge_captured', 'yes' ) : $order->update_meta_data( '_stripe_charge_captured', 'yes' ); |
|
261 | + if ($charge && 'no' === $captured) { |
|
262 | + WC_Stripe_Helper::is_pre_30() ? update_post_meta($order_id, '_stripe_charge_captured', 'yes') : $order->update_meta_data('_stripe_charge_captured', 'yes'); |
|
263 | 263 | |
264 | 264 | // Store other data such as fees |
265 | - WC_Stripe_Helper::is_pre_30() ? update_post_meta( $order_id, '_transaction_id', $notification->data->object->id ) : $order->set_transaction_id( $notification->data->object->id ); |
|
265 | + WC_Stripe_Helper::is_pre_30() ? update_post_meta($order_id, '_transaction_id', $notification->data->object->id) : $order->set_transaction_id($notification->data->object->id); |
|
266 | 266 | |
267 | - if ( isset( $notification->data->object->balance_transaction ) ) { |
|
268 | - $this->update_fees( $order, $notification->data->object->balance_transaction ); |
|
267 | + if (isset($notification->data->object->balance_transaction)) { |
|
268 | + $this->update_fees($order, $notification->data->object->balance_transaction); |
|
269 | 269 | } |
270 | 270 | |
271 | - if ( is_callable( array( $order, 'save' ) ) ) { |
|
271 | + if (is_callable(array($order, 'save'))) { |
|
272 | 272 | $order->save(); |
273 | 273 | } |
274 | 274 | |
275 | 275 | /* translators: transaction id */ |
276 | - $order->update_status( $order->needs_processing() ? 'processing' : 'completed', sprintf( __( 'Stripe charge complete (Charge ID: %s)', 'woocommerce-gateway-stripe' ), $notification->data->object->id ) ); |
|
276 | + $order->update_status($order->needs_processing() ? 'processing' : 'completed', sprintf(__('Stripe charge complete (Charge ID: %s)', 'woocommerce-gateway-stripe'), $notification->data->object->id)); |
|
277 | 277 | |
278 | 278 | // Check and see if capture is partial. |
279 | - if ( $this->is_partial_capture( $notification ) ) { |
|
280 | - $order->set_total( $this->get_partial_amount_to_charge( $notification ) ); |
|
281 | - $order->add_note( __( 'This charge was partially captured via Stripe Dashboard', 'woocommerce-gateway-stripe' ) ); |
|
279 | + if ($this->is_partial_capture($notification)) { |
|
280 | + $order->set_total($this->get_partial_amount_to_charge($notification)); |
|
281 | + $order->add_note(__('This charge was partially captured via Stripe Dashboard', 'woocommerce-gateway-stripe')); |
|
282 | 282 | $order->save(); |
283 | 283 | } |
284 | 284 | } |
@@ -293,38 +293,38 @@ discard block |
||
293 | 293 | * @version 4.0.0 |
294 | 294 | * @param object $notification |
295 | 295 | */ |
296 | - public function process_webhook_charge_succeeded( $notification ) { |
|
296 | + public function process_webhook_charge_succeeded($notification) { |
|
297 | 297 | // The following payment methods are synchronous so does not need to be handle via webhook. |
298 | - if ( ( isset( $notification->data->object->source->type ) && 'card' === $notification->data->object->source->type ) || ( isset( $notification->data->object->source->type ) && 'three_d_secure' === $notification->data->object->source->type ) ) { |
|
298 | + if ((isset($notification->data->object->source->type) && 'card' === $notification->data->object->source->type) || (isset($notification->data->object->source->type) && 'three_d_secure' === $notification->data->object->source->type)) { |
|
299 | 299 | return; |
300 | 300 | } |
301 | 301 | |
302 | - $order = WC_Stripe_Helper::get_order_by_charge_id( $notification->data->object->id ); |
|
302 | + $order = WC_Stripe_Helper::get_order_by_charge_id($notification->data->object->id); |
|
303 | 303 | |
304 | - if ( ! $order ) { |
|
305 | - WC_Stripe_Logger::log( 'Could not find order via charge ID: ' . $notification->data->object->id ); |
|
304 | + if ( ! $order) { |
|
305 | + WC_Stripe_Logger::log('Could not find order via charge ID: ' . $notification->data->object->id); |
|
306 | 306 | return; |
307 | 307 | } |
308 | 308 | |
309 | 309 | $order_id = WC_Stripe_Helper::is_pre_30() ? $order->id : $order->get_id(); |
310 | 310 | |
311 | - if ( 'on-hold' !== $order->get_status() ) { |
|
311 | + if ('on-hold' !== $order->get_status()) { |
|
312 | 312 | return; |
313 | 313 | } |
314 | 314 | |
315 | 315 | // Store other data such as fees |
316 | - WC_Stripe_Helper::is_pre_30() ? update_post_meta( $order_id, '_transaction_id', $notification->data->object->id ) : $order->set_transaction_id( $notification->data->object->id ); |
|
316 | + WC_Stripe_Helper::is_pre_30() ? update_post_meta($order_id, '_transaction_id', $notification->data->object->id) : $order->set_transaction_id($notification->data->object->id); |
|
317 | 317 | |
318 | - if ( isset( $notification->data->object->balance_transaction ) ) { |
|
319 | - $this->update_fees( $order, $notification->data->object->balance_transaction ); |
|
318 | + if (isset($notification->data->object->balance_transaction)) { |
|
319 | + $this->update_fees($order, $notification->data->object->balance_transaction); |
|
320 | 320 | } |
321 | 321 | |
322 | - if ( is_callable( array( $order, 'save' ) ) ) { |
|
322 | + if (is_callable(array($order, 'save'))) { |
|
323 | 323 | $order->save(); |
324 | 324 | } |
325 | 325 | |
326 | 326 | /* translators: transaction id */ |
327 | - $order->update_status( $order->needs_processing() ? 'processing' : 'completed', sprintf( __( 'Stripe charge complete (Charge ID: %s)', 'woocommerce-gateway-stripe' ), $notification->data->object->id ) ); |
|
327 | + $order->update_status($order->needs_processing() ? 'processing' : 'completed', sprintf(__('Stripe charge complete (Charge ID: %s)', 'woocommerce-gateway-stripe'), $notification->data->object->id)); |
|
328 | 328 | } |
329 | 329 | |
330 | 330 | /** |
@@ -335,23 +335,23 @@ discard block |
||
335 | 335 | * @version 4.0.0 |
336 | 336 | * @param object $notification |
337 | 337 | */ |
338 | - public function process_webhook_charge_failed( $notification ) { |
|
339 | - $order = WC_Stripe_Helper::get_order_by_charge_id( $notification->data->object->id ); |
|
338 | + public function process_webhook_charge_failed($notification) { |
|
339 | + $order = WC_Stripe_Helper::get_order_by_charge_id($notification->data->object->id); |
|
340 | 340 | |
341 | - if ( ! $order ) { |
|
342 | - WC_Stripe_Logger::log( 'Could not find order via charge ID: ' . $notification->data->object->id ); |
|
341 | + if ( ! $order) { |
|
342 | + WC_Stripe_Logger::log('Could not find order via charge ID: ' . $notification->data->object->id); |
|
343 | 343 | return; |
344 | 344 | } |
345 | 345 | |
346 | 346 | $order_id = WC_Stripe_Helper::is_pre_30() ? $order->id : $order->get_id(); |
347 | 347 | |
348 | - if ( 'on-hold' !== $order->get_status() ) { |
|
348 | + if ('on-hold' !== $order->get_status()) { |
|
349 | 349 | return; |
350 | 350 | } |
351 | 351 | |
352 | - $order->update_status( 'failed', __( 'This payment failed to clear.', 'woocommerce-gateway-stripe' ) ); |
|
352 | + $order->update_status('failed', __('This payment failed to clear.', 'woocommerce-gateway-stripe')); |
|
353 | 353 | |
354 | - do_action( 'wc_gateway_stripe_process_webhook_payment_error', $order, $notification ); |
|
354 | + do_action('wc_gateway_stripe_process_webhook_payment_error', $order, $notification); |
|
355 | 355 | } |
356 | 356 | |
357 | 357 | /** |
@@ -362,23 +362,23 @@ discard block |
||
362 | 362 | * @version 4.0.0 |
363 | 363 | * @param object $notification |
364 | 364 | */ |
365 | - public function process_webhook_source_canceled( $notification ) { |
|
366 | - $order = WC_Stripe_Helper::get_order_by_charge_id( $notification->data->object->id ); |
|
365 | + public function process_webhook_source_canceled($notification) { |
|
366 | + $order = WC_Stripe_Helper::get_order_by_charge_id($notification->data->object->id); |
|
367 | 367 | |
368 | - if ( ! $order ) { |
|
369 | - WC_Stripe_Logger::log( 'Could not find order via charge ID: ' . $notification->data->object->id ); |
|
368 | + if ( ! $order) { |
|
369 | + WC_Stripe_Logger::log('Could not find order via charge ID: ' . $notification->data->object->id); |
|
370 | 370 | return; |
371 | 371 | } |
372 | 372 | |
373 | 373 | $order_id = WC_Stripe_Helper::is_pre_30() ? $order->id : $order->get_id(); |
374 | 374 | |
375 | - if ( 'on-hold' !== $order->get_status() || 'cancelled' !== $order->get_status() ) { |
|
375 | + if ('on-hold' !== $order->get_status() || 'cancelled' !== $order->get_status()) { |
|
376 | 376 | return; |
377 | 377 | } |
378 | 378 | |
379 | - $order->update_status( 'cancelled', __( 'This payment has cancelled.', 'woocommerce-gateway-stripe' ) ); |
|
379 | + $order->update_status('cancelled', __('This payment has cancelled.', 'woocommerce-gateway-stripe')); |
|
380 | 380 | |
381 | - do_action( 'wc_gateway_stripe_process_webhook_payment_error', $order, $notification ); |
|
381 | + do_action('wc_gateway_stripe_process_webhook_payment_error', $order, $notification); |
|
382 | 382 | } |
383 | 383 | |
384 | 384 | /** |
@@ -389,42 +389,42 @@ discard block |
||
389 | 389 | * @version 4.0.0 |
390 | 390 | * @param object $notification |
391 | 391 | */ |
392 | - public function process_webhook_refund( $notification ) { |
|
393 | - $order = WC_Stripe_Helper::get_order_by_charge_id( $notification->data->object->id ); |
|
392 | + public function process_webhook_refund($notification) { |
|
393 | + $order = WC_Stripe_Helper::get_order_by_charge_id($notification->data->object->id); |
|
394 | 394 | |
395 | - if ( ! $order ) { |
|
396 | - WC_Stripe_Logger::log( 'Could not find order via charge ID: ' . $notification->data->object->id ); |
|
395 | + if ( ! $order) { |
|
396 | + WC_Stripe_Logger::log('Could not find order via charge ID: ' . $notification->data->object->id); |
|
397 | 397 | return; |
398 | 398 | } |
399 | 399 | |
400 | 400 | $order_id = WC_Stripe_Helper::is_pre_30() ? $order->id : $order->get_id(); |
401 | 401 | |
402 | - if ( 'stripe' === ( WC_Stripe_Helper::is_pre_30() ? $order->payment_method : $order->get_payment_method() ) ) { |
|
403 | - $charge = WC_Stripe_Helper::is_pre_30() ? get_post_meta( $order_id, '_transaction_id', true ) : $order->get_transaction_id(); |
|
404 | - $captured = WC_Stripe_Helper::is_pre_30() ? get_post_meta( $order_id, '_stripe_charge_captured', true ) : $order->get_meta( '_stripe_charge_captured', true ); |
|
405 | - $refund_id = WC_Stripe_Helper::is_pre_30() ? get_post_meta( $order_id, '_stripe_refund_id', true ) : $order->get_meta( '_stripe_refund_id', true ); |
|
402 | + if ('stripe' === (WC_Stripe_Helper::is_pre_30() ? $order->payment_method : $order->get_payment_method())) { |
|
403 | + $charge = WC_Stripe_Helper::is_pre_30() ? get_post_meta($order_id, '_transaction_id', true) : $order->get_transaction_id(); |
|
404 | + $captured = WC_Stripe_Helper::is_pre_30() ? get_post_meta($order_id, '_stripe_charge_captured', true) : $order->get_meta('_stripe_charge_captured', true); |
|
405 | + $refund_id = WC_Stripe_Helper::is_pre_30() ? get_post_meta($order_id, '_stripe_refund_id', true) : $order->get_meta('_stripe_refund_id', true); |
|
406 | 406 | |
407 | 407 | // If the refund ID matches, don't continue to prevent double refunding. |
408 | - if ( $notification->data->object->refunds->data[0]->id === $refund_id ) { |
|
408 | + if ($notification->data->object->refunds->data[0]->id === $refund_id) { |
|
409 | 409 | return; |
410 | 410 | } |
411 | 411 | |
412 | 412 | // Only refund captured charge. |
413 | - if ( $charge ) { |
|
414 | - $reason = ( isset( $captured ) && 'yes' === $captured ) ? __( 'Refunded via Stripe Dashboard', 'woocommerce-gateway-stripe' ) : __( 'Pre-Authorization Released via Stripe Dashboard', 'woocommerce-gateway-stripe' ); |
|
413 | + if ($charge) { |
|
414 | + $reason = (isset($captured) && 'yes' === $captured) ? __('Refunded via Stripe Dashboard', 'woocommerce-gateway-stripe') : __('Pre-Authorization Released via Stripe Dashboard', 'woocommerce-gateway-stripe'); |
|
415 | 415 | |
416 | 416 | // Create the refund. |
417 | - $refund = wc_create_refund( array( |
|
417 | + $refund = wc_create_refund(array( |
|
418 | 418 | 'order_id' => $order_id, |
419 | - 'amount' => $this->get_refund_amount( $notification ), |
|
419 | + 'amount' => $this->get_refund_amount($notification), |
|
420 | 420 | 'reason' => $reason, |
421 | - ) ); |
|
421 | + )); |
|
422 | 422 | |
423 | - if ( is_wp_error( $refund ) ) { |
|
424 | - WC_Stripe_Logger::log( $refund->get_error_message() ); |
|
423 | + if (is_wp_error($refund)) { |
|
424 | + WC_Stripe_Logger::log($refund->get_error_message()); |
|
425 | 425 | } |
426 | 426 | |
427 | - $order->add_order_note( $reason ); |
|
427 | + $order->add_order_note($reason); |
|
428 | 428 | } |
429 | 429 | } |
430 | 430 | } |
@@ -436,7 +436,7 @@ discard block |
||
436 | 436 | * @version 4.0.0 |
437 | 437 | * @param object $notification |
438 | 438 | */ |
439 | - public function is_partial_capture( $notification ) { |
|
439 | + public function is_partial_capture($notification) { |
|
440 | 440 | return 0 < $notification->data->object->amount_refunded; |
441 | 441 | } |
442 | 442 | |
@@ -447,11 +447,11 @@ discard block |
||
447 | 447 | * @version 4.0.0 |
448 | 448 | * @param object $notification |
449 | 449 | */ |
450 | - public function get_refund_amount( $notification ) { |
|
451 | - if ( $this->is_partial_capture( $notification ) ) { |
|
450 | + public function get_refund_amount($notification) { |
|
451 | + if ($this->is_partial_capture($notification)) { |
|
452 | 452 | $amount = $notification->data->object->amount_refunded / 100; |
453 | 453 | |
454 | - if ( in_array( strtolower( $notification->data->object->currency ), WC_Stripe_Helper::no_decimal_currencies() ) ) { |
|
454 | + if (in_array(strtolower($notification->data->object->currency), WC_Stripe_Helper::no_decimal_currencies())) { |
|
455 | 455 | $amount = $notification->data->object->amount_refunded; |
456 | 456 | } |
457 | 457 | |
@@ -468,12 +468,12 @@ discard block |
||
468 | 468 | * @version 4.0.0 |
469 | 469 | * @param object $notification |
470 | 470 | */ |
471 | - public function get_partial_amount_to_charge( $notification ) { |
|
472 | - if ( $this->is_partial_capture( $notification ) ) { |
|
473 | - $amount = ( $notification->data->object->amount - $notification->data->object->amount_refunded ) / 100; |
|
471 | + public function get_partial_amount_to_charge($notification) { |
|
472 | + if ($this->is_partial_capture($notification)) { |
|
473 | + $amount = ($notification->data->object->amount - $notification->data->object->amount_refunded) / 100; |
|
474 | 474 | |
475 | - if ( in_array( strtolower( $notification->data->object->currency ), WC_Stripe_Helper::no_decimal_currencies() ) ) { |
|
476 | - $amount = ( $notification->data->object->amount - $notification->data->object->amount_refunded ); |
|
475 | + if (in_array(strtolower($notification->data->object->currency), WC_Stripe_Helper::no_decimal_currencies())) { |
|
476 | + $amount = ($notification->data->object->amount - $notification->data->object->amount_refunded); |
|
477 | 477 | } |
478 | 478 | |
479 | 479 | return $amount; |
@@ -489,43 +489,43 @@ discard block |
||
489 | 489 | * @version 4.0.0 |
490 | 490 | * @param string $request_body |
491 | 491 | */ |
492 | - public function process_webhook( $request_body ) { |
|
493 | - $notification = json_decode( $request_body ); |
|
492 | + public function process_webhook($request_body) { |
|
493 | + $notification = json_decode($request_body); |
|
494 | 494 | |
495 | 495 | /* |
496 | 496 | * Hacky way to possibly prevent duplicate requests due to |
497 | 497 | * frontend request and webhook payment firing at the same |
498 | 498 | * time. |
499 | 499 | */ |
500 | - sleep( 10 ); |
|
500 | + sleep(10); |
|
501 | 501 | |
502 | - switch ( $notification->type ) { |
|
502 | + switch ($notification->type) { |
|
503 | 503 | case 'source.chargeable': |
504 | - $this->process_webhook_payment( $notification ); |
|
504 | + $this->process_webhook_payment($notification); |
|
505 | 505 | break; |
506 | 506 | |
507 | 507 | case 'source.canceled': |
508 | - $this->process_webhook_source_canceled( $notification ); |
|
508 | + $this->process_webhook_source_canceled($notification); |
|
509 | 509 | break; |
510 | 510 | |
511 | 511 | case 'charge.succeeded': |
512 | - $this->process_webhook_charge_succeeded( $notification ); |
|
512 | + $this->process_webhook_charge_succeeded($notification); |
|
513 | 513 | break; |
514 | 514 | |
515 | 515 | case 'charge.failed': |
516 | - $this->process_webhook_charge_failed( $notification ); |
|
516 | + $this->process_webhook_charge_failed($notification); |
|
517 | 517 | break; |
518 | 518 | |
519 | 519 | case 'charge.captured': |
520 | - $this->process_webhook_capture( $notification ); |
|
520 | + $this->process_webhook_capture($notification); |
|
521 | 521 | break; |
522 | 522 | |
523 | 523 | case 'charge.dispute.created': |
524 | - $this->process_webhook_dispute( $notification ); |
|
524 | + $this->process_webhook_dispute($notification); |
|
525 | 525 | break; |
526 | 526 | |
527 | 527 | case 'charge.refunded': |
528 | - $this->process_webhook_refund( $notification ); |
|
528 | + $this->process_webhook_refund($notification); |
|
529 | 529 | break; |
530 | 530 | |
531 | 531 | } |