Force saving of payment method in Stripe
-
Is there an action hook or filter that I can use to force plugin to save customer/payment method in Stripe whether they are a logged in user or whether they select checkbox to do so?
-
You can use the filter
wc_stripe_force_save_payment_method
https://docs.paymentplugins.com/wc-stripe/api/source-class-WC_Stripe_Payment_Intent.html#
Kind regards
Hello @mrclayton,
I’ve seen that filter, but it doesn’t actually appear to save the payment info/customer without user’s registering on the website.
I see
$user_id
mentioned in many places that actually create a Stripe customer.Normally in Stripe, you can create a customer without sending any info to Stripe. I’m trying to force customer creation/payment method saving for future usage if needed independent of users creating accounts or checking out as a guest.
Let me know if that is still supposed to work.
Hi @el-terrible-bmw,
Ok I see what your requirement is. By default, the plugin will only save the payment method if the user is logged in. You still want to use that filter because you have to tell Stripe that the payment method can be attached in the future. But there is an additional step since you want to create a Stripe customer.
If the user is not logged in, you will need to create the customer in Stripe then attach the payment method. You need to use a WooCommerce action that is triggered after the payment is processed. Here is some example code:
add_action('woocommerce_pre_payment_complete, function($order_id){ $order = wc_get_order($order_id); $payment_method = WC()->payment_gateways()->payment_gateways()[$order->get_paymen_method()]; // if the order has a customer ID, then a Stripe customer already exists to return // if the payment method is not part of the Stripe plugin, return. if($order->get_customer_id || !$payment_method instanceof WC_Payment_Gateway_Stripe){return;} $result = \WC_Stripe_Customer_Manager::instance()->create_customer( WC()->customer ); if ( ! is_wp_error( $result ) ) { $order->update_meta_data( \WC_Stripe_Constants::CUSTOMER_ID, $result->id ); $order->save(); // save the payment method. $result = $payment_method->create_payment_method( $order->get_meta( \WC_Stripe_Constants::PAYMENT_METHOD_TOKEN ), $result->id ); } });
Thanks @mrclayton.
One more thing, can you tell me what context this is supposed to be in?
$this->payment_method->create_payment_method()
Is it
WC_Payment_Gateway_Stripe_CC
? I’m trying to see how to access it but most of the methods aren’t static and it doesn’t appear to have a static way to access it.I added an update to my reply. There is no use of
$this
. It’s just:$payment_method->create_payment_method( $order->get_meta( \WC_Stripe_Constants::PAYMENT_METHOD_TOKEN ), $result->id );
Thank you so much @mrclayton
This is the final code I have that does what I need. Works well and tested.
add_filter( 'wc_stripe_force_save_payment_method', '__return_true', 10, 3 ); add_action('woocommerce_pre_payment_complete', 'create_customer_in_stripe', 10, 1 ); function create_customer_in_stripe ($order_id) { $order = wc_get_order($order_id); $payment_method = WC()->payment_gateways()->payment_gateways()[$order->get_payment_method()]; // if the order has a customer ID, then a Stripe customer already exists to return if ($order->get_customer_id) { return; } // If order is not using Stripe CC payment method, do not create a customer if ($order->get_payment_method() != 'stripe_cc') { return; } $result = \WC_Stripe_Customer_Manager::instance()->create_customer( WC()->customer ); if ( ! is_wp_error( $result ) ) { $order->update_meta_data( \WC_Stripe_Constants::CUSTOMER_ID, $result->id ); $order->save(); // Save the payment method. $result = $payment_method->create_payment_method( $order->get_meta( \WC_Stripe_Constants::PAYMENT_METHOD_TOKEN ), $result->id ); } }
- The topic ‘Force saving of payment method in Stripe’ is closed to new replies.