• Resolved fccoc

    (@fccoc)


    We’re using the Stripe plugin with Woocommerce subscriptions. To set up an automatic payment the subscription requires the Stripe customer ID (which we have) and the Payment Method Token (currently empty). Is there any way to enter something in this field to tell Stripe to use the custiomer’s default payment nethod stored in Stripe? If not we will have to add these manually from the Stripe dashboard which will take some time.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Clayton R

    (@mrclayton)

    Hi @fccoc

    Are you migrating from another system or plugin? I would recommend using the action woocommerce_scheduled_subscription_payment provided by WooCommerce Subscriptions. You can use that action to update the subscription’s payment method right before the renewal payment is processed.

    Here is an Example (this is not tested code, it’s provided as a guide):

    add_action('woocommerce_scheduled_subscription_payment', function($subscription_id){
    	if ( ! is_object( $subscription_id ) ) {
    		$subscription = wcs_get_subscription( $subscription_id );
    	} else {
    		$subscription = $subscription_id;
    	}
    	if($subscription && !$subscription->is_manual()){
    		if(in_array($subscription->get_payment_method(), ['stripe_cc', 'stripe_applepay'])){
    			$default_payment_method = $subscription->get_meta('_payment_method_token');
    			
    			if(!$default_payment_method){
    				$customer = WC_Stripe_Gateway::load()->customers->retrieve($subscription->get_meta('_wc_stripe_customer'));
    				if(!is_wp_error($customer)){
    					$default_payment_method = $customer->invoice_settings->default_payment_method;
    					if($default_payment_method){
    						$subscription->update_meta('_payment_method_token');
    						$subscription->save();
    					}
    				}	
    			}
    		}
    	}
    }, 5);

    This code checks to see if the subscription has a saved payment method or not. If the payment method token is empty, the plugin fetches the Stripe customer object and assigns their default payment method to the subscription.

    Kind Regards

    Thread Starter fccoc

    (@fccoc)

    Thanks for that – it should do what I need.. I’m migrating from a third party system outside WordPress.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Subscription payment method’ is closed to new replies.