Hello,
I have a small problem when I run ./manage.py djstripe_init_plans, the result is
Unknown command: 'djstipe_init_plans'
Type 'manage.py help' for usage.
I'm using cookiecutter for my project. Rest of the /.manage.py are running fine, only the last one is not working, in the end when I go to /payment/subscribe/ the template is empty. Can you help me solve this, thanks.
Hi @Copser - Apologies, this is an inconsistency in the documentation with how it used to be done. There isn't currently support for initialising plans from a local definition. You need to define them within Stripe, and then let the webhooks create the plans. I realise this isn't the most convenient solution, especially if you're programmatically creating your development, testing, staging or production environments. So I'll need to discuss this again with @kavdev and @jleclanche.
A potential workaround for now (and one that I personally follow with my integration) is to write your own code against the Stripe API to create the plans. Just bear in mind that plans are mostly immutable, so if you make changes to a plan after creating it, you'll have to use a different plan ID (or delete the upstream plan, which has it's own issues).
For example, here's some pseudo-code (based on what I do) - You can replace meta with your own means for obtaining the Stripe attributes required for creating a plan:
from djstripe import models as djstripe_models
def create_stripe_plan(meta):
djstripe_plan = {
'stripe_id': meta.get_plan_id(),
'amount': meta.get_amount(), # float, gets convert to int*100 by create()
'currency': meta.get_currency(),
'interval_count': meta.get_interval_count(),
'interval': meta.get_interval(),
'metadata': meta.get_metadata(),
'name': meta.get_name(),
'statement_descriptor': meta.get_statement_descriptor(),
'trial_period_days': meta.get_trial_period_days()
}
return djstripe_models.Plan.create(**djstripe_plan)
It's fairly straight-forward, but you can refer to the source-code for Plan.create().
I have a management command which syncs the current plans from stripe, do we want something like that for the case where you don't have webhooks setup or the plans already exist? It is pretty basic:
from django.core.management.base import BaseCommand
from djstripe.models import Plan
class Command(BaseCommand):
help = "Sync plans from stripe to the local database"
def handle(self, *args, **options):
for plan in Plan.api_list():
Plan.sync_from_stripe_data(plan)
Probably needs to deal with live / test mode a bit better though.
Sorry for not participating in the discussion, I was on the vacation. @kbrownlees thanks for your management command but as @lskillen said I've
_You need to define them within Stripe, and then let the webhooks create the plans_
Nevertheless it would be awesome that they initially fix this issues, once again thank you all.
The command may be correct, however the pasted output contains the following:
“Unknown command: 'djstipe_init_plans'”
Missing an “r” perhaps?
^ Looks like we've had the management command misspelled for years lol
There's a sync_plans_from_stripe command now, use that instead.