0

I have a subscription model:

class Subscription(models.Model):

STATUS_CURRENT = ["trialing", "active"]

plan_amount = models.CharField(max_length=120, null=True, blank=True)
plan_id = models.CharField(max_length=120, null=True, blank=True)
subscription_id = models.CharField(max_length=120)
subscription_owner = models.OneToOneField(Provider, related_name='subscription')
cancel_at_period_end = models.BooleanField(default=False)
canceled_at = models.DateTimeField(blank=True, null=True)
current_period_end = models.DateTimeField(blank=True, null=True)
current_period_start = models.DateTimeField(blank=True, null=True)
ended_at = models.DateTimeField(blank=True, null=True)
status = models.CharField(max_length=25)# trialing, active, past_due, canceled, or unpaid

def status_display(self):
    return self.status.replace("_", " ").title()

def __str__(self):
    return self.subscription_id

I also have the following stripe webhook set up and have no problems with that:

@require_POST
@csrf_exempt
def subscription_cancelled_callback(request):
# Process webhook data in `request.body and parse it as JSON`
stripe_payload = json.loads(request.body)

print('cancelled')

return HttpResponse(status=200)

What I am trying to achieve is update my subscription object for example update the status of the subscription in my backend with the json returned through the webhook, i.e "active" or "cancelled"

New to using webhooks but not sure how to update the object from the webhook json. Any pointers would be a great help.

This is an example of the webhook json on a test webhook: { "created": 1326853478, "livemode": false, "id": "evt_00000000000000", "type": "customer.subscription.deleted", "object": "event", "request": null, "pending_webhooks": 1, "api_version": "2017-02-14", "data": { "object": { "id": "sub_00000000000000", "object": "subscription", "application_fee_percent": null, "billing": "charge_automatically", "cancel_at_period_end": false, "canceled_at": null, "created": 1510580811, "current_period_end": 1513172811, "current_period_start": 1510580811, "customer": "cus_00000000000000", "discount": null, "ended_at": 1510598517, "items": { "object": "list", "data": [ { "id": "si_BlEkx6XqXqAgh1", "object": "subscription_item", "created": 1510580811, "metadata": { }, "plan": { "id": "pro-monthly", "object": "plan", "amount": 9999, "created": 1509374183, "currency": "usd", "interval": "month", "interval_count": 1, "livemode": false, "metadata": { }, "name": "Pro Plan", "statement_descriptor": null, "trial_period_days": null }, "quantity": 1 } ], "has_more": false, "total_count": 1, "url": "/v1/subscription_items?subscription=sub_BlEk5E9CBuFdTJ" }, "livemode": false, "metadata": { }, "plan": { "id": "pro-monthly_00000000000000", "object": "plan", "amount": 9999, "created": 1509374183, "currency": "usd", "interval": "month", "interval_count": 1, "livemode": false, "metadata": { }, "name": "Pro Plan", "statement_descriptor": null, "trial_period_days": null }, "quantity": 1, "start": 1510580811, "status": "canceled", "tax_percent": null, "trial_end": null, "trial_start": null } } }

1
  • What's in the JSON? It should contain some sort of identifier, probably the subscription id, and you should be able to look for your subscription object with that identifier. Commented Nov 13, 2017 at 18:42

1 Answer 1

1

I assume stripe_json has the "id" fields which will be mapped to your model let say it is mapped to subscription_id and it also has status that you want to update on subscription model

stripe_payload = {"id": "some-id", "status": "cancelled"}

then all you need to do is:

status = stripe_payload['status']
sub_id = stripr_payload['id']
Subscription.objects.filter(id=sub_id).update(**{"status": status})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.