craftcms / stripe

Sync and extend Stripe products and subscriptions.
MIT License
5 stars 0 forks source link

Synchronisation of subscription payments not happening automatically with webhooks #7

Closed iparr closed 4 months ago

iparr commented 4 months ago

Description

I have set up some test products and output them to my Twig templates. I have Stripe CLI setup. I have the following code:

{% for price in product.prices %}
    <li>
        {{ price.data|unitAmount }}
        {{ tag('a', {
            text: "Buy now",
            href: price.getCheckoutUrl(),
        }) }}
    </li>
{% endfor %}

I can hit that button and buy a product. This is listed in the Stripe Dashboard (I'm in test mode throughout) successfully.

If I use the "Sync All Data from Stripe" feature in the CMS Dashboard (or the equivalent CLI command) then it will provide a record of this in /stripe/subscription as well as the FE template I have built.

But how do I get this to happen automatically without manually syncing? I can see my Stripe CLI chatting away with checkout.session.completed but Craft doesn't seem to register this without a manual full update.

My expectations are that with the setup I have this would happen automatically. That's what the webhooks are for, right?

I don't know where to start trying to make this happen. I am building a subscription system and this seems like a sensible way to go.

Additional info

i-just commented 4 months ago

Hi, thanks for getting in touch!

So you have your Stripe CLI listening to webhooks via stripe listen --forward-to https://<my-craft-project.ddev.site>/stripe/webhooks/handle, you can see this local listener in your https://dashboard.stripe.com/test/webhooks, and you have added the webhook signing secret from the Stripe CLI to your .env file or to control panel > Stripe > Settings > Webhook Signing Secret?

When you say it’s chatting away with checkout.session.completed, do you see status 200 returned for the events that follow?

iparr commented 4 months ago

I see the local listener in the Stripe dashboard. 200 returned and the expected events (delete subscription, et al).

I've added and re-checked that signing secret. It is set in my .env file and then also present in the control panel dashboard.

For customer.subscription.deleted and checkout.session.completed I get status 400 in my CLI (but 200 in the Stripe Dashboard)

If I dive into my Stripe Dashboard 'Event log' under "Webhook CLI responses" I get 400 errors.

I don't know why this is, because I've set up all the keys as prescribed by the documentation.

i-just commented 4 months ago

Thanks for all the details! Do you, by any chance, have a “standard” webhook set up for the environment too? By that, I mean a webhook that shows in your Control Panel > Stripe > Webhooks and points to your local site’s URL? If you do, I would remove it and check if this resolves the issue.

If you look in the storage/logs/ web log file, are there any errors that could indicate what’s causing the 400 error?

iparr commented 4 months ago

I did, but I removed it and the same issue occurred. Still 400s.

I can't see anything I can understand in the logs, but I have found a: 2024-05-17 14:53:12 [web.ERROR] [application] Stripe webhook handler failed with message:No signatures found matching the expected signature for payload {"memory":2374912}

In the Stripe logs, under "Response body" it returns HTML for my website but 12 lines of an error page.

i-just commented 4 months ago

Thanks for confirming! This error is thrown by Stripe. Double-check that the value you have saved for the Webhook Signing Secret matches exactly the one shown when you start listening via the Stripe CLI (make sure it’s not overwritten via config/stripe.php etc), make sure you’re logged in to the correct account via the Stripe CLI and that the API version shown when you start listening is reported as 2024-04-10.

iparr commented 4 months ago

Top line: 200's everywhere and my FE templates are showing things synced as I'd hoped.

I hope the shift to live is smoother!

Thank you so much for your patience and help @i-just - I can't overstate how important this is to my projects. I was worried that such synchronising wasn't 'baked in' to this new plugin. Closing this, but hopefully findable by someone who has migrated from Stripe Commerce and is in a similar bind.