craftcms / commerce-stripe

Stripe payment gateway for Craft Commerce
https://plugins.craftcms.com/commerce-stripe
MIT License
31 stars 48 forks source link

Partial Payments and updating Stripe's Payment Intent amount #279

Closed icreatestuff closed 10 months ago

icreatestuff commented 11 months ago

Description

In our payment form that submits to /commerce/payments/pay we have the option for the customer to choose to pay either a deposit (20% of the order total) or the full amount. This is built as described in the Commerce docs using a field with paymentAmount name. Within the same form we render the relevant gateway payment form HTML like so:

{% namespace cart.gateway.handle|commercePaymentFormNamespace %}
    {{ cart.gateway.getPaymentFormHtml({
        submitButtonClasses: 'cta',
        submitButtonText: 'Pay',
        order: cart
    })|raw }}
{% endnamespace %}

This worked fine in Commerce v3 as I believe the Stripe charge amount was set and then confirmed on the server side once the form had been submitted.

However now in v4, the deposit amount (1st in the list of options), is the amount that is always charged regardless of if 'Full Amount' is selected, I believe because the Payment Intent amount is created discretely on page load using the initial form values but isn't then updated based on changes within the form.

Is this the expected behaviour? How would we go about updating the Payment Intent amount when the paymentAmount field value changes?

Also, and this may be unrelated in which case I can open a different issue, but every page refresh of the payment page creates a new payment intent instead of re-using an existing one. We see these all listed within an orders 'Transactions' table as well as in the Stripe Developer Events/Webhooks area and as incomplete payments. Is this expected? Webhooks are all setup and working as far as I can tell.

Additional info

nfourtythree commented 10 months ago

Hi @icreatestuff

Thank you for raising this issue. We have just pushed out version 4.1.1 of the plugin which includes updated JavaScript relating to partial payments.

The JavaScript provided with the plugin now listens to the change event on the form input with the name paymentAmount and reinitialises the Stripe Elements form. This means if you are using something like a dropdown or a radio form input to select the partial payment amount this should all be working for you now.

It is worth noting that if the correct value is present in a paymentAmount form input on page load then that should be the correct amount to be paid if no further changes are made.

Hope this helps, thanks!

icreatestuff commented 10 months ago

Brilliant, thank you.