Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

woocommerce - How to setup charges to be recurring?

I am building a new feature on top of what is already provided with the WooCommerce Stripe plugin, to enable customers to change their existing ongoing subscriptions which basically cancels their current subscription and sets up a new one to start when their previous one ends. I do this via the Stripe API and then update it accordingly in WooCommerce which works great.

I seem to be missing one part, ensuring the charge is recurring. Does anyone know how WooCommerce Subscriptions sets up the charges to be recurring for future payments with Stripe?

enter image description here

I initiate the charge in the following way:

    $charge = $stripe->charges->create([
        'amount' => intval( $newOrder->get_total() * 100 ),
        'currency' => 'gbp',
        'source' => $card,
        'description' => 'Subscription Update ' . $user->user_email,
        'metadata' => [
            'Order ID' => $newOrder->get_id(),
            'payment_type' => 'recurring',
        ...

I noticed that they set a metadata field of 'payment_type' to recurring, so I added that.

I did find this article for setting up a card for future payments but I am not sure if this is what I need or if I would have to do it a different way?

https://stripe.com/docs/payments/save-and-reuse

Should I be using the PaymentIntent instead of Charge?

https://stripe.com/docs/api/payment_intents/create

Thanks

question from:https://stackoverflow.com/questions/65933748/how-to-setup-charges-to-be-recurring

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The process of setting up a Subscription (i.e. recurring charges) can be found here [1]. The provided code creates a one-off Charge only. Metadata is rather a way to pass ad-hoc data on objects via the API [2], so isn't relevant in this case. PaymentIntents are the way to go for a Billing integration as they're SCA ready [3].

[1] https://stripe.com/docs/billing/subscriptions/fixed-price

[2] https://stripe.com/docs/api/metadata

[3] https://stripe.com/docs/strong-customer-authentication


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...