Stripe Credit Card + Subscription Management (Frontend)

Hi Everyone,

Using the changing-stripe-billing-information function works great to save stripe_customer_id and use it to update(actually just add) credit card information. The only problem is that the new credit card is not being set as the default payment method (see below in Stripe Dashboard: the test Visa card is the original payment method, which is the default. However, after adding the Amex, the Visa(old) it remains the default).

I have tried to use the code below to set the new credit card (the test Amex in this case) as the default payment method for all future subscription payments, but it didn’t work for me.

I am hoping someone in this great GF community who has the skills, can help to complete this changing-stripe-billing-information script in the GF Docs; otherwise, this is useless. Adding a credit card is excellent, but logically, the reason you are changing is to be used for future payments.

Please help out if you can. Thank you,
Laz

[https://docs.gravityforms.com/changing-stripe-billing-information/]


add_action( 'gform_stripe_post_create_subscription', function( $subscription, $feed, $entry, $form ) {
    $feed_name = rgars( $feed, 'meta/feedName' );

    // Check if the feed name matches your target feed name
    if ( $feed_name == 'Update Billing' ) {
        // Replace 'stripe_customer_id' with the actual user meta key where the Stripe customer ID is stored
        $stripe_customer_id = get_user_meta( get_current_user_id(), 'stripe_customer_id', true );

        if ( ! empty( $stripe_customer_id ) ) {
            // Retrieve the payment method ID from the subscription
            $payment_method_id = $subscription->payment_method_id;

            // Retrieve the customer's current default payment method
            $customer = \Stripe\Customer::retrieve( $stripe_customer_id );
            $current_default_payment_method = $customer->invoice_settings->default_payment_method;

            // If the new payment method is different from the current default, update it
            if ( $payment_method_id !== $current_default_payment_method ) {
                \Stripe\Customer::update(
                    $stripe_customer_id,
                    [
                        'invoice_settings' => [
                            'default_payment_method' => $payment_method_id,
                        ],
                    ]
                );
            }
        }
    }
}, 10, 4 );
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.