woocommerce /
woocommerce-gateway-stripe
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | if ( ! defined( 'ABSPATH' ) ) { |
||
| 3 | exit; |
||
| 4 | } |
||
| 5 | |||
| 6 | /** |
||
| 7 | * Class that handles SEPA payment method. |
||
| 8 | * |
||
| 9 | * @extends WC_Gateway_Stripe |
||
| 10 | * |
||
| 11 | * @since 4.0.0 |
||
| 12 | */ |
||
| 13 | class WC_Gateway_Stripe_Sepa extends WC_Stripe_Payment_Gateway { |
||
| 14 | /** |
||
| 15 | * Notices (array) |
||
| 16 | * @var array |
||
| 17 | */ |
||
| 18 | public $notices = array(); |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Is test mode active? |
||
| 22 | * |
||
| 23 | * @var bool |
||
| 24 | */ |
||
| 25 | public $testmode; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Alternate credit card statement name |
||
| 29 | * |
||
| 30 | * @var bool |
||
| 31 | */ |
||
| 32 | public $statement_descriptor; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * API access secret key |
||
| 36 | * |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | public $secret_key; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Api access publishable key |
||
| 43 | * |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | public $publishable_key; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Should we store the users credit cards? |
||
| 50 | * |
||
| 51 | * @var bool |
||
| 52 | */ |
||
| 53 | public $saved_cards; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Constructor |
||
| 57 | */ |
||
| 58 | public function __construct() { |
||
| 59 | $this->id = 'stripe_sepa'; |
||
| 60 | $this->method_title = __( 'Stripe SEPA Direct Debit', 'woocommerce-gateway-stripe' ); |
||
| 61 | /* translators: link */ |
||
| 62 | $this->method_description = sprintf( __( 'All other general Stripe settings can be adjusted <a href="%s">here</a>.', 'woocommerce-gateway-stripe' ), admin_url( 'admin.php?page=wc-settings&tab=checkout§ion=stripe' ) ); |
||
| 63 | $this->supports = array( |
||
| 64 | 'products', |
||
| 65 | 'refunds', |
||
| 66 | 'tokenization', |
||
| 67 | 'add_payment_method', |
||
| 68 | 'subscriptions', |
||
| 69 | 'subscription_cancellation', |
||
| 70 | 'subscription_suspension', |
||
| 71 | 'subscription_reactivation', |
||
| 72 | 'subscription_amount_changes', |
||
| 73 | 'subscription_date_changes', |
||
| 74 | 'subscription_payment_method_change', |
||
| 75 | 'subscription_payment_method_change_customer', |
||
| 76 | 'subscription_payment_method_change_admin', |
||
| 77 | 'multiple_subscriptions', |
||
| 78 | 'pre-orders', |
||
| 79 | ); |
||
| 80 | |||
| 81 | // Load the form fields. |
||
| 82 | $this->init_form_fields(); |
||
| 83 | |||
| 84 | // Load the settings. |
||
| 85 | $this->init_settings(); |
||
| 86 | |||
| 87 | $main_settings = get_option( 'woocommerce_stripe_settings' ); |
||
| 88 | $this->title = $this->get_option( 'title' ); |
||
| 89 | $this->description = $this->get_option( 'description' ); |
||
| 90 | $this->enabled = $this->get_option( 'enabled' ); |
||
| 91 | $this->testmode = ( ! empty( $main_settings['testmode'] ) && 'yes' === $main_settings['testmode'] ) ? true : false; |
||
| 92 | $this->saved_cards = ( ! empty( $main_settings['saved_cards'] ) && 'yes' === $main_settings['saved_cards'] ) ? true : false; |
||
| 93 | $this->publishable_key = ! empty( $main_settings['publishable_key'] ) ? $main_settings['publishable_key'] : ''; |
||
| 94 | $this->secret_key = ! empty( $main_settings['secret_key'] ) ? $main_settings['secret_key'] : ''; |
||
| 95 | $this->statement_descriptor = ! empty( $main_settings['statement_descriptor'] ) ? $main_settings['statement_descriptor'] : ''; |
||
| 96 | |||
| 97 | if ( $this->testmode ) { |
||
| 98 | $this->publishable_key = ! empty( $main_settings['test_publishable_key'] ) ? $main_settings['test_publishable_key'] : ''; |
||
| 99 | $this->secret_key = ! empty( $main_settings['test_secret_key'] ) ? $main_settings['test_secret_key'] : ''; |
||
| 100 | } |
||
| 101 | |||
| 102 | add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) ); |
||
| 103 | add_action( 'admin_notices', array( $this, 'check_environment' ) ); |
||
| 104 | add_action( 'admin_head', array( $this, 'remove_admin_notice' ) ); |
||
| 105 | add_action( 'wp_enqueue_scripts', array( $this, 'payment_scripts' ) ); |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Checks to make sure environment is setup correctly to use this payment method. |
||
| 110 | * |
||
| 111 | * @since 4.0.0 |
||
| 112 | * @version 4.0.0 |
||
| 113 | */ |
||
| 114 | public function check_environment() { |
||
| 115 | if ( ! current_user_can( 'manage_woocommerce' ) ) { |
||
| 116 | return; |
||
| 117 | } |
||
| 118 | |||
| 119 | $environment_warning = $this->get_environment_warning(); |
||
| 120 | |||
| 121 | if ( $environment_warning ) { |
||
| 122 | $this->add_admin_notice( 'bad_environment', 'error', $environment_warning ); |
||
| 123 | } |
||
| 124 | |||
| 125 | foreach ( (array) $this->notices as $notice_key => $notice ) { |
||
| 126 | echo "<div class='" . esc_attr( $notice['class'] ) . "'><p>"; |
||
| 127 | echo wp_kses( $notice['message'], array( 'a' => array( 'href' => array() ) ) ); |
||
| 128 | echo '</p></div>'; |
||
| 129 | } |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Checks the environment for compatibility problems. Returns a string with the first incompatibility |
||
| 134 | * found or false if the environment has no problems. |
||
| 135 | * |
||
| 136 | * @since 4.0.0 |
||
| 137 | * @version 4.0.0 |
||
| 138 | */ |
||
| 139 | public function get_environment_warning() { |
||
| 140 | if ( 'yes' === $this->enabled && ! in_array( get_woocommerce_currency(), $this->get_supported_currency() ) ) { |
||
| 141 | $message = __( 'SEPA is enabled - it requires store currency to be set to Euros.', 'woocommerce-gateway-stripe' ); |
||
| 142 | |||
| 143 | return $message; |
||
| 144 | } |
||
| 145 | |||
| 146 | return false; |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Returns all supported currencies for this payment method. |
||
| 151 | * |
||
| 152 | * @since 4.0.0 |
||
| 153 | * @version 4.0.0 |
||
| 154 | * @return array |
||
| 155 | */ |
||
| 156 | public function get_supported_currency() { |
||
| 157 | return apply_filters( 'wc_stripe_sepa_supported_currencies', array( |
||
| 158 | 'EUR', |
||
| 159 | ) ); |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Checks to see if all criteria is met before showing payment method. |
||
| 164 | * |
||
| 165 | * @since 4.0.0 |
||
| 166 | * @version 4.0.0 |
||
| 167 | * @return bool |
||
| 168 | */ |
||
| 169 | public function is_available() { |
||
| 170 | if ( ! in_array( get_woocommerce_currency(), $this->get_supported_currency() ) ) { |
||
| 171 | return false; |
||
| 172 | } |
||
| 173 | |||
| 174 | return parent::is_available(); |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * All payment icons that work with Stripe. |
||
| 179 | * |
||
| 180 | * @since 4.0.0 |
||
| 181 | * @version 4.0.0 |
||
| 182 | * @return array |
||
| 183 | */ |
||
| 184 | public function payment_icons() { |
||
| 185 | return apply_filters( 'wc_stripe_payment_icons', array( |
||
| 186 | 'sepa' => '<i class="stripe-pf stripe-pf-sepa stripe-pf-right" alt="SEPA" aria-hidden="true"></i>', |
||
| 187 | ) ); |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Get_icon function. |
||
| 192 | * |
||
| 193 | * @since 1.0.0 |
||
| 194 | * @version 4.0.0 |
||
| 195 | * @return string |
||
| 196 | */ |
||
| 197 | public function get_icon() { |
||
| 198 | $icons = $this->payment_icons(); |
||
| 199 | |||
| 200 | $icons_str = ''; |
||
| 201 | |||
| 202 | $icons_str .= $icons['sepa']; |
||
| 203 | |||
| 204 | return apply_filters( 'woocommerce_gateway_icon', $icons_str, $this->id ); |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * payment_scripts function. |
||
| 209 | * |
||
| 210 | * Outputs scripts used for stripe payment |
||
| 211 | * |
||
| 212 | * @access public |
||
| 213 | */ |
||
| 214 | public function payment_scripts() { |
||
| 215 | if ( ! is_cart() && ! is_checkout() && ! isset( $_GET['pay_for_order'] ) && ! is_add_payment_method_page() ) { |
||
| 216 | return; |
||
| 217 | } |
||
| 218 | |||
| 219 | wp_enqueue_style( 'stripe_paymentfonts' ); |
||
| 220 | wp_enqueue_script( 'woocommerce_stripe' ); |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Initialize Gateway Settings Form Fields. |
||
| 225 | */ |
||
| 226 | public function init_form_fields() { |
||
| 227 | $this->form_fields = require( WC_STRIPE_PLUGIN_PATH . '/includes/admin/stripe-sepa-settings.php' ); |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Displays the mandate acceptance notice to customer. |
||
| 232 | * |
||
| 233 | * @since 4.0.0 |
||
| 234 | * @version 4.0.0 |
||
| 235 | * @return string |
||
| 236 | */ |
||
| 237 | public function mandate_display() { |
||
| 238 | /* translators: statement descriptor */ |
||
| 239 | printf( __( 'By providing your IBAN and confirming this payment, you are authorizing %s and Stripe, our payment service provider, to send instructions to your bank to debit your account and your bank to debit your account in accordance with those instructions. You are entitled to a refund from your bank under the terms and conditions of your agreement with your bank. A refund must be claimed within 8 weeks starting from the date on which your account was debited.', 'woocommerce-gateway-stripe' ), WC_Stripe_Helper::clean_statement_descriptor( $this->statement_descriptor ) ); |
||
|
0 ignored issues
–
show
|
|||
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Renders the Stripe elements form. |
||
| 244 | * |
||
| 245 | * @since 4.0.0 |
||
| 246 | * @version 4.0.0 |
||
| 247 | */ |
||
| 248 | public function form() { |
||
| 249 | ?> |
||
| 250 | <fieldset id="wc-<?php echo esc_attr( $this->id ); ?>-form" class="wc-payment-form"> |
||
| 251 | <?php do_action( 'woocommerce_credit_card_form_start', $this->id ); ?> |
||
| 252 | <p class="wc-stripe-sepa-mandate"><?php $this->mandate_display(); ?></p> |
||
| 253 | <p class="form-row form-row-wide validate-required"> |
||
| 254 | <label for="stripe-sepa-owner"> |
||
| 255 | <?php esc_html_e( 'IBAN Account Name.', 'woocommerce-gateway-stripe' ); ?> |
||
| 256 | </label> |
||
| 257 | <input id="stripe-sepa-owner" name="stripe_sepa_owner" value="" /> |
||
| 258 | </p> |
||
| 259 | <p class="form-row form-row-wide validate-required"> |
||
| 260 | <label for="stripe-sepa-iban"> |
||
| 261 | <?php esc_html_e( 'IBAN Account Number.', 'woocommerce-gateway-stripe' ); ?> |
||
| 262 | </label> |
||
| 263 | <input id="stripe-sepa-iban" name="stripe_sepa_iban" value="" /> |
||
| 264 | </p> |
||
| 265 | <!-- Used to display form errors --> |
||
| 266 | <div class="stripe-source-errors" role="alert"></div> |
||
| 267 | <?php do_action( 'woocommerce_credit_card_form_end', $this->id ); ?> |
||
| 268 | <div class="clear"></div> |
||
| 269 | </fieldset> |
||
| 270 | <?php |
||
| 271 | } |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Payment form on checkout page |
||
| 275 | */ |
||
| 276 | public function payment_fields() { |
||
| 277 | $user = wp_get_current_user(); |
||
|
0 ignored issues
–
show
$user is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the Loading history...
|
|||
| 278 | $total = WC()->cart->total; |
||
| 279 | $display_tokenization = $this->supports( 'tokenization' ) && is_checkout() && $this->saved_cards; |
||
| 280 | |||
| 281 | // If paying from order, we need to get total from order not cart. |
||
| 282 | if ( isset( $_GET['pay_for_order'] ) && ! empty( $_GET['key'] ) ) { |
||
| 283 | $order = wc_get_order( wc_get_order_id_by_order_key( wc_clean( $_GET['key'] ) ) ); |
||
| 284 | $total = $order->get_total(); |
||
| 285 | } |
||
| 286 | |||
| 287 | if ( is_add_payment_method_page() ) { |
||
| 288 | $pay_button_text = __( 'Add Payment', 'woocommerce-gateway-stripe' ); |
||
|
0 ignored issues
–
show
$pay_button_text is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the Loading history...
|
|||
| 289 | $total = ''; |
||
| 290 | } else { |
||
| 291 | $pay_button_text = ''; |
||
|
0 ignored issues
–
show
$pay_button_text is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the Loading history...
|
|||
| 292 | } |
||
| 293 | |||
| 294 | echo '<div |
||
| 295 | id="stripe-sepa_debit-payment-data" |
||
| 296 | data-amount="' . esc_attr( WC_Stripe_Helper::get_stripe_amount( $total ) ) . '" |
||
| 297 | data-currency="' . esc_attr( strtolower( get_woocommerce_currency() ) ) . '">'; |
||
| 298 | |||
| 299 | View Code Duplication | if ( $this->description ) { |
|
|
0 ignored issues
–
show
This code seems to be duplicated across your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. Loading history...
|
|||
| 300 | if ( $this->testmode ) { |
||
| 301 | $this->description .= ' ' . __( 'TEST MODE ENABLED. In test mode, you can use IBAN number DE89370400440532013000.', 'woocommerce-gateway-stripe' ); |
||
| 302 | $this->description = trim( $this->description ); |
||
| 303 | } |
||
| 304 | echo apply_filters( 'wc_stripe_description', wpautop( wp_kses_post( $this->description ) ) ); |
||
| 305 | } |
||
| 306 | |||
| 307 | if ( $display_tokenization ) { |
||
| 308 | $this->tokenization_script(); |
||
| 309 | $this->saved_payment_methods(); |
||
| 310 | } |
||
| 311 | |||
| 312 | $this->form(); |
||
| 313 | |||
| 314 | View Code Duplication | if ( apply_filters( 'wc_stripe_display_save_payment_method_checkbox', $display_tokenization ) && ! is_add_payment_method_page() && ! isset( $_GET['change_payment_method'] ) ) { |
|
|
0 ignored issues
–
show
This code seems to be duplicated across your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. Loading history...
|
|||
| 315 | $this->save_payment_method_checkbox(); |
||
| 316 | } |
||
| 317 | |||
| 318 | echo '</div>'; |
||
| 319 | } |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Process the payment |
||
| 323 | * |
||
| 324 | * @param int $order_id Reference. |
||
| 325 | * @param bool $retry Should we retry on fail. |
||
| 326 | * @param bool $force_save_source Force save the payment source. |
||
| 327 | * |
||
| 328 | * @throws Exception If payment will not be accepted. |
||
| 329 | * |
||
| 330 | * @return array|void |
||
| 331 | */ |
||
| 332 | public function process_payment( $order_id, $retry = true, $force_save_source = false ) { |
||
| 333 | try { |
||
| 334 | $order = wc_get_order( $order_id ); |
||
| 335 | |||
| 336 | // This comes from the create account checkbox in the checkout page. |
||
| 337 | $create_account = ! empty( $_POST['createaccount'] ) ? true : false; |
||
| 338 | |||
| 339 | if ( $create_account ) { |
||
| 340 | $new_customer_id = WC_Stripe_Helper::is_pre_30() ? $order->customer_user : $order->get_customer_id(); |
||
| 341 | $new_stripe_customer = new WC_Stripe_Customer( $new_customer_id ); |
||
| 342 | $new_stripe_customer->create_customer(); |
||
| 343 | } |
||
| 344 | |||
| 345 | $prepared_source = $this->prepare_source( get_current_user_id(), $force_save_source ); |
||
| 346 | |||
| 347 | // Store source to order meta. |
||
| 348 | $this->save_source( $order, $prepared_source ); |
||
| 349 | |||
| 350 | // Result from Stripe API request. |
||
| 351 | $response = null; |
||
| 352 | |||
| 353 | if ( $order->get_total() > 0 ) { |
||
| 354 | // This will throw exception if not valid. |
||
| 355 | $this->validate_minimum_order_amount( $order ); |
||
| 356 | |||
| 357 | WC_Stripe_Logger::log( "Info: Begin processing payment for order $order_id for the amount of {$order->get_total()}" ); |
||
| 358 | |||
| 359 | // Make the request. |
||
| 360 | $response = WC_Stripe_API::request( $this->generate_payment_request( $order, $prepared_source ) ); |
||
| 361 | |||
| 362 | if ( ! empty( $response->error ) ) { |
||
| 363 | // If it is an API error such connection or server, let's retry. |
||
| 364 | if ( 'api_connection_error' === $response->error->type || 'api_error' === $response->error->type ) { |
||
| 365 | if ( $retry ) { |
||
| 366 | sleep( 5 ); |
||
| 367 | return $this->process_payment( $order_id, false, $force_save_source ); |
||
| 368 | } else { |
||
| 369 | $message = 'API connection error and retries exhausted.'; |
||
| 370 | $order->add_order_note( $message ); |
||
| 371 | throw new Exception( $message ); |
||
| 372 | } |
||
| 373 | } |
||
| 374 | |||
| 375 | // Customer param wrong? The user may have been deleted on stripe's end. Remove customer_id. Can be retried without. |
||
| 376 | if ( preg_match( '/No such customer/i', $response->error->message ) && $retry ) { |
||
| 377 | delete_user_meta( WC_Stripe_Helper::is_pre_30() ? $order->customer_user : $order->get_customer_id(), '_stripe_customer_id' ); |
||
| 378 | |||
| 379 | return $this->process_payment( $order_id, false, $force_save_source ); |
||
| 380 | } elseif ( preg_match( '/No such token/i', $response->error->message ) && $prepared_source->token_id ) { |
||
| 381 | // Source param wrong? The CARD may have been deleted on stripe's end. Remove token and show message. |
||
| 382 | $wc_token = WC_Payment_Tokens::get( $prepared_source->token_id ); |
||
| 383 | $wc_token->delete(); |
||
| 384 | $message = __( 'This card is no longer available and has been removed.', 'woocommerce-gateway-stripe' ); |
||
| 385 | $order->add_order_note( $message ); |
||
| 386 | throw new Exception( $message ); |
||
| 387 | } |
||
| 388 | |||
| 389 | $localized_messages = WC_Stripe_Helper::get_localized_messages(); |
||
| 390 | |||
| 391 | $message = isset( $localized_messages[ $response->error->type ] ) ? $localized_messages[ $response->error->type ] : $response->error->message; |
||
| 392 | |||
| 393 | $order->add_order_note( $message ); |
||
| 394 | |||
| 395 | throw new Exception( $message ); |
||
| 396 | } |
||
| 397 | |||
| 398 | do_action( 'wc_gateway_stripe_process_payment', $response, $order ); |
||
| 399 | |||
| 400 | // Process valid response. |
||
| 401 | $this->process_response( $response, $order ); |
||
| 402 | } else { |
||
| 403 | $order->payment_complete(); |
||
| 404 | } |
||
| 405 | |||
| 406 | // Remove cart. |
||
| 407 | WC()->cart->empty_cart(); |
||
| 408 | |||
| 409 | // Return thank you page redirect. |
||
| 410 | return array( |
||
| 411 | 'result' => 'success', |
||
| 412 | 'redirect' => $this->get_return_url( $order ), |
||
| 413 | ); |
||
| 414 | |||
| 415 | } catch ( Exception $e ) { |
||
| 416 | wc_add_notice( $e->getMessage(), 'error' ); |
||
| 417 | WC_Stripe_Logger::log( 'Error: ' . $e->getMessage() ); |
||
| 418 | |||
| 419 | do_action( 'wc_gateway_stripe_process_payment_error', $e, $order ); |
||
| 420 | |||
| 421 | if ( $order->has_status( array( 'pending', 'failed' ) ) ) { |
||
| 422 | $this->send_failed_order_email( $order_id ); |
||
| 423 | } |
||
| 424 | |||
| 425 | return array( |
||
| 426 | 'result' => 'fail', |
||
| 427 | 'redirect' => '', |
||
| 428 | ); |
||
| 429 | } |
||
| 430 | } |
||
| 431 | } |
||
| 432 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: