Last active
August 29, 2015 14:22
-
-
Save MichelleGlauser/fcdb53b6ccdad0c0bc48 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
yourviews.py
yoururls.py
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.