Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save custom fields to new subscriptions and get active subscriptions? #25

Open
mandrasch opened this issue Sep 17, 2024 · 3 comments
Open
Labels
bug Something isn't working

Comments

@mandrasch
Copy link

mandrasch commented Sep 17, 2024

Description

Hi, this is more of a question rather than a bug (apologies, but discussions are not open here).

The native subscriptions in Craft Commerce have the ability to set custom fields in POST subscriptions/subscribe

  • fields[...] Subscription custom field values, indexed by their handles.

How can I achieve this with this new plugin?

I saw that custom field can be attached to subscriptions as well.

Because of the redirect to Hosted Checkout of Stripe, I assume I have to keep this in a session or submit the values I want to store in the thankYou-URL as GET parameter?

What would be the most robust place to do this - after a new subscription was sucessfully paid? 🤔

Use case: I want to store some additional fields to a subscription, e.g. discountType for a reduced membership like senior, student, etc. (same product) + some uploaded files for verification. The user enters this information beforehand and pays -and I wanted to store the data and uploaded files within the subscription.

Also I'm wondering how I get all current active subscriptions for a user? In craft commerce it was like this:

craft.subscriptions.user(currentUser).status('active').all()

I'll dig into source code of this plugin, guess it's just not documented in README.md yet?

Thanks very much in advance!

Additional info

@i-just
Copy link
Contributor

i-just commented Sep 18, 2024

Hi, thanks for getting in touch!

Yes, you could store that info in the session and retrieve it when displaying the success page or pass it in the success URL (in the query string). You could also use custom_fields which are available when creating a Stripe checkout session (when you use the checkout links or form, Stripe plugin creates a checkout session).

If your success (thank you) URL is configured to pass the session ID back to the page (that’s the ?session={CHECKOUT_SESSION_ID} part), you could then retrieve the session by its id. It’ll have the data submitted to the custom fields (if you choose to use those) and the subscription ID (if it was a subscription that was created). You could then retrieve that session by id, grab the data you need (from custom fields, session or the URL) and process it as you wish.

You could also intercept the checkout.session.completed event (which will have the submitted data and the subscription id if the purchased product was a recurring one) and process the data the way you want. Please note that until Stripe plugin 1.2 is released, you’d have to listen for that event on your own (more info here: #20).

There are some caveats when using custom fields. You can specify a maximum of 3 custom fields, and they can only be text, number, or dropdown type.

Also I'm wondering how I get all current active subscriptions for a user?

You can do that via the element query, like so:
craft.stripeSubscriptions.user(currentUser).status('live').all()

Or, via behaviour attached to the user, like so:
currentUser.getStripeSubscriptions().where('stripeStatus', 'active').all()

I hope this clarifies things a bit.

I’ll close this now, but if you have any further questions or if anything above doesn’t make sense, feel free to reach out again!

@i-just i-just closed this as completed Sep 18, 2024
@mandrasch
Copy link
Author

mandrasch commented Sep 18, 2024

Hi @i-just,

thanks very much for the detailed and quick response 👍 (For the current project, we decided to stay with the current way / not use this new plugin - but your answer helped very much to evaluate both options. Much appreciated!)

I just wondered one thing about the new way:

I can add custom fields to subscriptions in the plugin settings, but these are just meant to be filled later via twig or PHP?

image

The challenge with that - as far as I can see - is that I need the stripe webhook to ping craftcms first in order to fill them? Before that the subscription does not exist in craft as far as I saw in my tests. Just as optional feedback.

(I guess that's what confused me, because in the old way (craft commerce subscriptions) these can be inserted via POST subscriptions/subscribe directly while subscribing and by seeing the custom field settings screen I assumed this will be the same here in the new plugin)

@i-just
Copy link
Contributor

i-just commented Sep 19, 2024

Hi @mandrasch,

Not a problem at all! I’m glad you found it helpful. We had a chat about the points you made, and we should make adding custom data to subscriptions easier.

We’ll have a look at a few options and see how we can improve this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants