Skip to content

Instantly share code, notes, and snippets.

@MichelleGlauser
Last active August 29, 2015 14:22
Show Gist options
  • Save MichelleGlauser/fcdb53b6ccdad0c0bc48 to your computer and use it in GitHub Desktop.
Save MichelleGlauser/fcdb53b6ccdad0c0bc48 to your computer and use it in GitHub Desktop.
# models.py
def subscribe(self, plan, quantity=1, trial_days=None,
charge_immediately=True, prorate=djstripe_settings.PRORATION_POLICY, discount_code=None):
cu = self.stripe_customer
"""
Trial_days corresponds to the value specified by the selected plan
for the key trial_period_days.
"""
if ("trial_period_days" in djstripe_settings.PAYMENTS_PLANS[plan]):
trial_days = djstripe_settings.PAYMENTS_PLANS[plan]["trial_period_days"]
if trial_days:
resp = cu.update_subscription(
plan=djstripe_settings.PAYMENTS_PLANS[plan]["stripe_plan_id"],
trial_end=timezone.now() + datetime.timedelta(days=trial_days),
prorate=prorate,
quantity=quantity,
coupon=coupon
)
else:
resp = cu.update_subscription(
plan=djstripe_settings.PAYMENTS_PLANS[plan]["stripe_plan_id"],
prorate=prorate,
quantity=quantity,
coupon=coupon
)
self.sync_current_subscription()
if charge_immediately:
self.send_invoice()
subscription_made.send(sender=self, plan=plan, stripe_response=resp)
# forms.py
class PlanForm(forms.Form):
plan = forms.ChoiceField(choices=PLAN_CHOICES)
coupon = forms.CharField(max_length=30, required=False)
# views.py
class SubscribeFormView(LoginRequiredMixin, FormValidMessageMixin, SubscriptionMixin, FormView):
# TODO - needs tests
form_class = PlanForm
template_name = "djstripe/subscribe_form.html"
success_url = reverse_lazy("djstripe:account")
form_valid_message = "Congratulations! You've successfully subscribed to Zana Edge. We'll be in touch shortly via email."
def post(self, request, *args, **kwargs):
"""
Handles POST requests, instantiating a form instance with the passed
POST variables and then checked for validity.
"""
form_class = self.get_form_class()
form = self.get_form(form_class)
if form.is_valid():
try:
customer, created = Customer.get_or_create(
subscriber=subscriber_request_callback(self.request))
customer.update_card(self.request.POST.get("stripe_token"))
customer.subscribe(form.cleaned_data["plan"], coupon=coupon)
except stripe.StripeError as e:
# add form error here
self.error = e.args[0]
return self.form_invalid(form)
# redirect to confirmation page
return self.form_valid(form)
else:
return self.form_invalid(form)
@MichelleGlauser
Copy link
Author

I changed "discount_code" to "coupon", I just didn't show that part of the code in my latest comment. I moved the docstring. I'm not sure what you mean in your fourth point. How do I extend? Do I just make a view in my own project that has the same title like I've done with the subscribe_form template?

@kavdev
Copy link

kavdev commented Jun 11, 2015

yourviews.py

from djstripe.views import SubscribeFormView

class MySubscribeFormView(SubscribeFormView):
    form_valid_message = "Congratulations! You've successfully subscribed to Zana Edge. We'll be in touch shortly via email."

yoururls.py

from .views import MySubscribeFormView

url("^subscribe/$", MySubscribeFormView.as_view(), name="mysubscribe")

I'm not sure if setting the name to djstripe:subscribe and adding it above the djstripe url import will work. Give it a try though - if it doesn't work, you just need to change the action on the subscribe form here to {% url 'mysubscribe' %} either by overriding the entire template or setting it via javascript. If you've already customized that template (djstripe/subscribe_form.html) and haven't overridden it outside the package, I can help with that too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment