WC_Form_Handler::add_payment_method_action()
Process the add payment method form.
Description Description
Source Source
File: includes/class-wc-form-handler.php
public static function add_payment_method_action() {
if ( isset( $_POST['woocommerce_add_payment_method'], $_POST['payment_method'] ) ) {
wc_nocache_headers();
$nonce_value = wc_get_var( $_REQUEST['woocommerce-add-payment-method-nonce'], wc_get_var( $_REQUEST['_wpnonce'], '' ) ); // @codingStandardsIgnoreLine.
if ( ! wp_verify_nonce( $nonce_value, 'woocommerce-add-payment-method' ) ) {
return;
}
if ( ! apply_filters( 'woocommerce_add_payment_method_form_is_valid', true ) ) {
return;
}
// Test rate limit.
$current_user_id = get_current_user_id();
$rate_limit_id = 'add_payment_method_' . $current_user_id;
$delay = (int) apply_filters( 'woocommerce_payment_gateway_add_payment_method_delay', 20 );
if ( WC_Rate_Limiter::retried_too_soon( $rate_limit_id ) ) {
wc_add_notice(
sprintf(
/* translators: %d number of seconds */
_n(
'You cannot add a new payment method so soon after the previous one. Please wait for %d second.',
'You cannot add a new payment method so soon after the previous one. Please wait for %d seconds.',
$delay,
'woocommerce'
),
$delay
),
'error'
);
return;
}
WC_Rate_Limiter::set_rate_limit( $rate_limit_id, $delay );
ob_start();
$payment_method_id = wc_clean( wp_unslash( $_POST['payment_method'] ) );
$available_gateways = WC()->payment_gateways->get_available_payment_gateways();
if ( isset( $available_gateways[ $payment_method_id ] ) ) {
$gateway = $available_gateways[ $payment_method_id ];
if ( ! $gateway->supports( 'add_payment_method' ) && ! $gateway->supports( 'tokenization' ) ) {
wc_add_notice( __( 'Invalid payment gateway.', 'woocommerce' ), 'error' );
return;
}
$gateway->validate_fields();
if ( wc_notice_count( 'error' ) > 0 ) {
return;
}
$result = $gateway->add_payment_method();
if ( 'success' === $result['result'] ) {
wc_add_notice( __( 'Payment method successfully added.', 'woocommerce' ) );
}
if ( 'failure' === $result['result'] ) {
wc_add_notice( __( 'Unable to add payment method to your account.', 'woocommerce' ), 'error' );
}
if ( ! empty( $result['redirect'] ) ) {
wp_redirect( $result['redirect'] ); //phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect
exit();
}
}
}
}