Improve add payment method logic

This commit is contained in:
Mike Jolley 2017-08-07 12:39:16 +01:00
parent dc1c8843a1
commit d46988b6a6
2 changed files with 26 additions and 14 deletions

View File

@ -374,27 +374,41 @@ class WC_Form_Handler {
*/
public static function add_payment_method_action() {
if ( isset( $_POST['woocommerce_add_payment_method'], $_POST['payment_method'], $_POST['_wpnonce'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'woocommerce-add-payment-method' ) ) {
ob_start();
$payment_method = wc_clean( $_POST['payment_method'] );
$payment_method_id = wc_clean( wp_unslash( $_POST['payment_method'] ) );
$available_gateways = WC()->payment_gateways->get_available_payment_gateways();
// Validate
$available_gateways[ $payment_method ]->validate_fields();
// Process
if ( wc_notice_count( 'wc_errors' ) == 0 ) {
$result = $available_gateways[ $payment_method ]->add_payment_method();
// Redirect to success/confirmation/payment page
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 added.', 'woocommerce' ) );
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'] );
exit();
}
}
}
}
/**

View File

@ -154,9 +154,7 @@ class WC_Payment_Gateways {
if ( $gateway->is_available() ) {
if ( ! is_add_payment_method_page() ) {
$_available_gateways[ $gateway->id ] = $gateway;
} elseif ( $gateway->supports( 'add_payment_method' ) ) {
$_available_gateways[ $gateway->id ] = $gateway;
} elseif ( $gateway->supports( 'tokenization' ) ) {
} elseif ( $gateway->supports( 'add_payment_method' ) || $gateway->supports( 'tokenization' ) ) {
$_available_gateways[ $gateway->id ] = $gateway;
}
}