paypal-woocommerce icon indicating copy to clipboard operation
paypal-woocommerce copied to clipboard

Standardized way to obtain last 4 of credit card

Open peterjohnhunt opened this issue 3 years ago • 0 comments

Hello!

We are currently using a filter in woocommerce with your plugin to display the last 4 digits of a credit card on the subscription page, so people have an easy reference for when they want to change the card on a subscription.

add_filter( 'woocommerce_my_subscriptions_payment_method', public function payment_method($method, $subscription) {
    $token_id = get_post_meta($subscription->get_id(), '_payment_tokens_id', true);

    if ( !$token_id ) return $method;

    $gateway = wc_get_payment_gateway_by_order($subscription);

    if ( !$gateway ) return $method;

    $tokens = $gateway->get_tokens();

    if ( !$tokens ) return $method;

    $tokens = array_filter($tokens, function($token) use ($token_id){ return $token_id == $token->get_token(); });

    if ( !$tokens ) return $method;

    $token = array_pop($tokens);

    return $token->get_display_name();
}, 10, 2 );

However, this is a bit hacky, and it seems that after a subscription renewal happens, the updated token doesn't get added to the wp_woocommerce_payment_tokens table to be looked up against. Can you provide insights into a more standardized way we can obtain the current last 4 digits of the card attached to a subscription in relation to payflow and your plugin?

Thank you so much!

peterjohnhunt avatar May 07 '21 14:05 peterjohnhunt