@@ -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,37 +389,37 @@ 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 && 'yes' === $captured ) { |
|
| 413 | + if ($charge && 'yes' === $captured) { |
|
| 414 | 414 | // Create the refund. |
| 415 | - $refund = wc_create_refund( array( |
|
| 415 | + $refund = wc_create_refund(array( |
|
| 416 | 416 | 'order_id' => $order_id, |
| 417 | - 'amount' => $this->get_refund_amount( $notification ), |
|
| 418 | - 'reason' => __( 'Refunded via Stripe Dashboard', 'woocommerce-gateway-stripe' ), |
|
| 419 | - ) ); |
|
| 417 | + 'amount' => $this->get_refund_amount($notification), |
|
| 418 | + 'reason' => __('Refunded via Stripe Dashboard', 'woocommerce-gateway-stripe'), |
|
| 419 | + )); |
|
| 420 | 420 | |
| 421 | - if ( is_wp_error( $refund ) ) { |
|
| 422 | - WC_Stripe_Logger::log( $refund->get_error_message() ); |
|
| 421 | + if (is_wp_error($refund)) { |
|
| 422 | + WC_Stripe_Logger::log($refund->get_error_message()); |
|
| 423 | 423 | } |
| 424 | 424 | } |
| 425 | 425 | } |
@@ -432,7 +432,7 @@ discard block |
||
| 432 | 432 | * @version 4.0.0 |
| 433 | 433 | * @param object $notification |
| 434 | 434 | */ |
| 435 | - public function is_partial_capture( $notification ) { |
|
| 435 | + public function is_partial_capture($notification) { |
|
| 436 | 436 | return 0 < $notification->data->object->amount_refunded; |
| 437 | 437 | } |
| 438 | 438 | |
@@ -443,11 +443,11 @@ discard block |
||
| 443 | 443 | * @version 4.0.0 |
| 444 | 444 | * @param object $notification |
| 445 | 445 | */ |
| 446 | - public function get_refund_amount( $notification ) { |
|
| 447 | - if ( $this->is_partial_capture( $notification ) ) { |
|
| 446 | + public function get_refund_amount($notification) { |
|
| 447 | + if ($this->is_partial_capture($notification)) { |
|
| 448 | 448 | $amount = $notification->data->object->amount_refunded / 100; |
| 449 | 449 | |
| 450 | - if ( in_array( strtolower( $notification->data->object->currency ), WC_Stripe_Helper::no_decimal_currencies() ) ) { |
|
| 450 | + if (in_array(strtolower($notification->data->object->currency), WC_Stripe_Helper::no_decimal_currencies())) { |
|
| 451 | 451 | $amount = $notification->data->object->amount_refunded; |
| 452 | 452 | } |
| 453 | 453 | |
@@ -464,12 +464,12 @@ discard block |
||
| 464 | 464 | * @version 4.0.0 |
| 465 | 465 | * @param object $notification |
| 466 | 466 | */ |
| 467 | - public function get_partial_amount_to_charge( $notification ) { |
|
| 468 | - if ( $this->is_partial_capture( $notification ) ) { |
|
| 469 | - $amount = ( $notification->data->object->amount - $notification->data->object->amount_refunded ) / 100; |
|
| 467 | + public function get_partial_amount_to_charge($notification) { |
|
| 468 | + if ($this->is_partial_capture($notification)) { |
|
| 469 | + $amount = ($notification->data->object->amount - $notification->data->object->amount_refunded) / 100; |
|
| 470 | 470 | |
| 471 | - if ( in_array( strtolower( $notification->data->object->currency ), WC_Stripe_Helper::no_decimal_currencies() ) ) { |
|
| 472 | - $amount = ( $notification->data->object->amount - $notification->data->object->amount_refunded ); |
|
| 471 | + if (in_array(strtolower($notification->data->object->currency), WC_Stripe_Helper::no_decimal_currencies())) { |
|
| 472 | + $amount = ($notification->data->object->amount - $notification->data->object->amount_refunded); |
|
| 473 | 473 | } |
| 474 | 474 | |
| 475 | 475 | return $amount; |
@@ -485,43 +485,43 @@ discard block |
||
| 485 | 485 | * @version 4.0.0 |
| 486 | 486 | * @param string $request_body |
| 487 | 487 | */ |
| 488 | - public function process_webhook( $request_body ) { |
|
| 489 | - $notification = json_decode( $request_body ); |
|
| 488 | + public function process_webhook($request_body) { |
|
| 489 | + $notification = json_decode($request_body); |
|
| 490 | 490 | |
| 491 | 491 | /* |
| 492 | 492 | * Hacky way to possibly prevent duplicate requests due to |
| 493 | 493 | * frontend request and webhook payment firing at the same |
| 494 | 494 | * time. |
| 495 | 495 | */ |
| 496 | - sleep( 10 ); |
|
| 496 | + sleep(10); |
|
| 497 | 497 | |
| 498 | - switch ( $notification->type ) { |
|
| 498 | + switch ($notification->type) { |
|
| 499 | 499 | case 'source.chargeable': |
| 500 | - $this->process_webhook_payment( $notification ); |
|
| 500 | + $this->process_webhook_payment($notification); |
|
| 501 | 501 | break; |
| 502 | 502 | |
| 503 | 503 | case 'source.canceled': |
| 504 | - $this->process_webhook_source_canceled( $notification ); |
|
| 504 | + $this->process_webhook_source_canceled($notification); |
|
| 505 | 505 | break; |
| 506 | 506 | |
| 507 | 507 | case 'charge.succeeded': |
| 508 | - $this->process_webhook_charge_succeeded( $notification ); |
|
| 508 | + $this->process_webhook_charge_succeeded($notification); |
|
| 509 | 509 | break; |
| 510 | 510 | |
| 511 | 511 | case 'charge.failed': |
| 512 | - $this->process_webhook_charge_failed( $notification ); |
|
| 512 | + $this->process_webhook_charge_failed($notification); |
|
| 513 | 513 | break; |
| 514 | 514 | |
| 515 | 515 | case 'charge.captured': |
| 516 | - $this->process_webhook_capture( $notification ); |
|
| 516 | + $this->process_webhook_capture($notification); |
|
| 517 | 517 | break; |
| 518 | 518 | |
| 519 | 519 | case 'charge.dispute.created': |
| 520 | - $this->process_webhook_dispute( $notification ); |
|
| 520 | + $this->process_webhook_dispute($notification); |
|
| 521 | 521 | break; |
| 522 | 522 | |
| 523 | 523 | case 'charge.refunded': |
| 524 | - $this->process_webhook_refund( $notification ); |
|
| 524 | + $this->process_webhook_refund($notification); |
|
| 525 | 525 | break; |
| 526 | 526 | |
| 527 | 527 | } |
@@ -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,57 +640,57 @@ 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 | $body = array(); |
| 652 | 651 | |
| 653 | - if ( WC_Stripe_Helper::is_pre_30() ) { |
|
| 654 | - $order_currency = get_post_meta( $order_id, '_order_currency', true ); |
|
| 652 | + if (WC_Stripe_Helper::is_pre_30()) { |
|
| 653 | + $order_currency = get_post_meta($order_id, '_order_currency', true); |
|
| 655 | 654 | } else { |
| 656 | 655 | $order_currency = $order->get_currency(); |
| 657 | 656 | } |
| 658 | 657 | |
| 659 | - if ( ! is_null( $amount ) ) { |
|
| 660 | - $body['amount'] = WC_Stripe_Helper::get_stripe_amount( $amount, $order_currency ); |
|
| 658 | + if ( ! is_null($amount)) { |
|
| 659 | + $body['amount'] = WC_Stripe_Helper::get_stripe_amount($amount, $order_currency); |
|
| 661 | 660 | } |
| 662 | 661 | |
| 663 | - if ( $reason ) { |
|
| 662 | + if ($reason) { |
|
| 664 | 663 | $body['metadata'] = array( |
| 665 | 664 | 'reason' => $reason, |
| 666 | 665 | ); |
| 667 | 666 | } |
| 668 | 667 | |
| 669 | - WC_Stripe_Logger::log( "Info: Beginning refund for order {$order->get_transaction_id()} for the amount of {$amount}" ); |
|
| 668 | + WC_Stripe_Logger::log("Info: Beginning refund for order {$order->get_transaction_id()} for the amount of {$amount}"); |
|
| 670 | 669 | |
| 671 | - $response = WC_Stripe_API::request( $body, 'charges/' . $order->get_transaction_id() . '/refunds' ); |
|
| 670 | + $response = WC_Stripe_API::request($body, 'charges/' . $order->get_transaction_id() . '/refunds'); |
|
| 672 | 671 | |
| 673 | - if ( ! empty( $response->error ) ) { |
|
| 674 | - WC_Stripe_Logger::log( 'Error: ' . $response->error->message ); |
|
| 672 | + if ( ! empty($response->error)) { |
|
| 673 | + WC_Stripe_Logger::log('Error: ' . $response->error->message); |
|
| 675 | 674 | |
| 676 | 675 | return $response; |
| 677 | 676 | |
| 678 | - } elseif ( ! empty( $response->id ) ) { |
|
| 679 | - 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 ); |
|
| 677 | + } elseif ( ! empty($response->id)) { |
|
| 678 | + 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); |
|
| 680 | 679 | |
| 681 | - $amount = wc_price( $response->amount / 100 ); |
|
| 680 | + $amount = wc_price($response->amount / 100); |
|
| 682 | 681 | |
| 683 | - if ( in_array( strtolower( $order->get_currency() ), WC_Stripe_Helper::no_decimal_currencies() ) ) { |
|
| 684 | - $amount = wc_price( $response->amount ); |
|
| 682 | + if (in_array(strtolower($order->get_currency()), WC_Stripe_Helper::no_decimal_currencies())) { |
|
| 683 | + $amount = wc_price($response->amount); |
|
| 685 | 684 | } |
| 686 | 685 | |
| 687 | - if ( isset( $response->balance_transaction ) ) { |
|
| 688 | - $this->update_fees( $order, $response->balance_transaction ); |
|
| 686 | + if (isset($response->balance_transaction)) { |
|
| 687 | + $this->update_fees($order, $response->balance_transaction); |
|
| 689 | 688 | } |
| 690 | 689 | |
| 691 | 690 | /* translators: 1) dollar amount 2) transaction id 3) refund message */ |
| 692 | - $refund_message = sprintf( __( 'Refunded %1$s - Refund ID: %2$s - Reason: %3$s', 'woocommerce-gateway-stripe' ), $amount, $response->id, $reason ); |
|
| 693 | - $order->add_order_note( $refund_message ); |
|
| 694 | - WC_Stripe_Logger::log( 'Success: ' . html_entity_decode( strip_tags( $refund_message ) ) ); |
|
| 691 | + $refund_message = sprintf(__('Refunded %1$s - Refund ID: %2$s - Reason: %3$s', 'woocommerce-gateway-stripe'), $amount, $response->id, $reason); |
|
| 692 | + $order->add_order_note($refund_message); |
|
| 693 | + WC_Stripe_Logger::log('Success: ' . html_entity_decode(strip_tags($refund_message))); |
|
| 695 | 694 | |
| 696 | 695 | return true; |
| 697 | 696 | } |
@@ -706,44 +705,44 @@ discard block |
||
| 706 | 705 | */ |
| 707 | 706 | public function add_payment_method() { |
| 708 | 707 | $error = false; |
| 709 | - $error_msg = __( 'There was a problem adding the card.', 'woocommerce-gateway-stripe' ); |
|
| 708 | + $error_msg = __('There was a problem adding the card.', 'woocommerce-gateway-stripe'); |
|
| 710 | 709 | $source_id = ''; |
| 711 | 710 | |
| 712 | - if ( empty( $_POST['stripe_source'] ) && empty( $_POST['stripe_token'] ) || ! is_user_logged_in() ) { |
|
| 711 | + if (empty($_POST['stripe_source']) && empty($_POST['stripe_token']) || ! is_user_logged_in()) { |
|
| 713 | 712 | $error = true; |
| 714 | 713 | } |
| 715 | 714 | |
| 716 | - $stripe_customer = new WC_Stripe_Customer( get_current_user_id() ); |
|
| 715 | + $stripe_customer = new WC_Stripe_Customer(get_current_user_id()); |
|
| 717 | 716 | |
| 718 | - $source = ! empty( $_POST['stripe_source'] ) ? wc_clean( $_POST['stripe_source'] ) : ''; |
|
| 717 | + $source = ! empty($_POST['stripe_source']) ? wc_clean($_POST['stripe_source']) : ''; |
|
| 719 | 718 | |
| 720 | - $source_object = WC_Stripe_API::retrieve( 'sources/' . $source ); |
|
| 719 | + $source_object = WC_Stripe_API::retrieve('sources/' . $source); |
|
| 721 | 720 | |
| 722 | - if ( isset( $source_object ) ) { |
|
| 723 | - if ( ! empty( $source_object->error ) ) { |
|
| 721 | + if (isset($source_object)) { |
|
| 722 | + if ( ! empty($source_object->error)) { |
|
| 724 | 723 | $error = true; |
| 725 | 724 | } |
| 726 | 725 | |
| 727 | 726 | $source_id = $source_object->id; |
| 728 | - } elseif ( isset( $_POST['stripe_token'] ) ) { |
|
| 729 | - $source_id = wc_clean( $_POST['stripe_token'] ); |
|
| 727 | + } elseif (isset($_POST['stripe_token'])) { |
|
| 728 | + $source_id = wc_clean($_POST['stripe_token']); |
|
| 730 | 729 | } |
| 731 | 730 | |
| 732 | - $response = $stripe_customer->add_source( $source_id ); |
|
| 731 | + $response = $stripe_customer->add_source($source_id); |
|
| 733 | 732 | |
| 734 | - if ( ! $response || is_wp_error( $response ) || ! empty( $response->error ) ) { |
|
| 733 | + if ( ! $response || is_wp_error($response) || ! empty($response->error)) { |
|
| 735 | 734 | $error = true; |
| 736 | 735 | } |
| 737 | 736 | |
| 738 | - if ( $error ) { |
|
| 739 | - wc_add_notice( $error_msg, 'error' ); |
|
| 740 | - WC_Stripe_Logger::log( 'Add payment method Error: ' . $error_msg ); |
|
| 737 | + if ($error) { |
|
| 738 | + wc_add_notice($error_msg, 'error'); |
|
| 739 | + WC_Stripe_Logger::log('Add payment method Error: ' . $error_msg); |
|
| 741 | 740 | return; |
| 742 | 741 | } |
| 743 | 742 | |
| 744 | 743 | return array( |
| 745 | 744 | 'result' => 'success', |
| 746 | - 'redirect' => wc_get_endpoint_url( 'payment-methods' ), |
|
| 745 | + 'redirect' => wc_get_endpoint_url('payment-methods'), |
|
| 747 | 746 | ); |
| 748 | 747 | } |
| 749 | 748 | } |