craftcms / commerce

Fully integrated ecommerce for Craft CMS.
https://craftcms.com/commerce
Other
218 stars 170 forks source link

subscriptionFormHtml unknown method #523

Closed bymayo closed 5 years ago

bymayo commented 5 years ago

Description

Using the default template code for subscriptions (https://github.com/craftcms/commerce-docs/blob/v2/en/subscription-templates.md#subscribing) I get an error that says:

Calling unknown method: craft\commerce\stripe\gateways\Gateway::getSubscriptionFormHtml()

As well when we try to use {{ cart.gateway.getPaymentFormHtml({})|raw }} it gives us an error that parameters are missing. (I'm guessing between {} there needs to be something set, but the docs don't state anything)

Unsure if this is a documentation error, or there is a problem somewhere.

EDIT: I just realised it's looking for Stripe, should this be posted in the Stripe repo?!

Steps to reproduce

  1. Use template code from https://github.com/craftcms/commerce-docs/blob/v2/en/subscription-templates.md#subscribing

Additional info

andris-sevcenko commented 5 years ago

This is a documentation error. This method has been removed as of Commerce 2.0.0-beta.11.

The reason for that is that there's no reason to use that form - you would never allow the user to choose any subscription parameters.

You should be modifying the parameters of a subscription programmatically via module or a plugin by listening to the craft\commerce\services\Subscriptions:: EVENT_BEFORE_CREATE_SUBSCRIPTION event. (https://github.com/craftcms/commerce/blob/develop/src/services/Subscriptions.php#L59-L74).

I'll leave this open as a reminder to update the documentation.

bymayo commented 5 years ago

I'm a little confused with this.

Is there no payment form that can be used to subscribe to a subscription plan?

andris-sevcenko commented 5 years ago

The form still exists, but the helper method that generates the HTML does not.

You only need the planId parameter to subscribe the user to a plan. You can still use the HTML that the method would generate, it's just that in 99% cases you would not ever want to let the user subscribing to specify his own parameters such as trial days and whatever else the gateway has support for.

Subscription parameters usually depend on business logic and business logic usually is hidden in custom plugins and modules. Hence, if you need to modify trialDays (currently the only subscription parameter), you should hook in the event that is fired before creating a subscription which will give you access to the parameter form (https://github.com/craftcms/commerce/blob/develop/src/events/CreateSubscriptionEvent.php).

If, however, you're talking about a payment form, then, that's a different story. The only requirement is that the user has at least one stored payment source. If he/she does not, then just add the gateway's payment form data to the subscription form and that should be taken care of.

andris-sevcenko commented 5 years ago

Anyway, updating this documentation is on my list. It's just a matter of getting down that list to that item.

bymayo commented 5 years ago

Gotcha! Luckily we're not needing to modify trial days etc.

But we do only need just a payment form. So i'll try using:

{{ cart.gateway.getPaymentFormHtml({})|raw }}

Then modify the parameters inside that method.

I look forward to updated docs as they are a little confusing at the moment - Mainly on the subscription side of things.

andris-sevcenko commented 5 years ago

Yep, working on it as we speak. Feels like that will save us some time in the future :)

You can add the payment form if the user does not have a payment source stored (https://github.com/pixelandtonic/DotAllCommerceBerlin2018/blob/master/after/shop/services/index.html#L61-L66)

Stripe will just use the default payment source stored for the customer.

bymayo commented 5 years ago

Perfect - Think it's working okay now with just that payment form 💯

andris-sevcenko commented 5 years ago

Documentation updated.