Stripe payment do something when payment is successfull Django

Question:

I have an app about posting an advertises and by default i made an expiration date for

every advertise (30 days) now i wanna use stripe to extend the expiration date.

what i have so far is the checkout but i want when the payment i success i update the
database.

my checkout view :


class StripeCheckoutView(APIView):
    def post(self, request, *args, **kwargs):
        adv_id = self.kwargs["pk"]
        adv = Advertise.objects.get(id = adv_id)
        try:
            adv = Advertise.objects.get(id = adv_id)
            checkout_session = stripe.checkout.Session.create(
                line_items=[
                    {
                        'price_data': {
                            'currency':'usd',
                             'unit_amount': 50 * 100,
                             'product_data':{
                                 'name':adv.description,

                             }
                        },
                        'quantity': 1,
                    },
                ],
                metadata={
                    "product_id":adv.id
                },
                mode='payment',
                success_url=settings.SITE_URL + '?success=true',
                cancel_url=settings.SITE_URL + '?canceled=true',
            )
            return redirect(checkout_session.url)
        except Exception as e:
            return Response({'msg':'something went wrong while creating stripe session','error':str(e)}, status=500)

Models:


# Create your models here.
class Advertise(models.Model):
    owner = models.ForeignKey(User, on_delete=models.CASCADE, related_name="advertise")
    category = models.CharField(max_length= 200, choices = CATEGORY)
    location = models.CharField(max_length= 200, choices = LOCATIONS)
    description = models.TextField(max_length=600)
    price = models.FloatField(max_length=100)
    expiration_date  = models.DateField(default = Expire_date, blank=True, null=True)
    created_at = models.DateTimeField(auto_now_add=True, blank=True, null=True)
    updated_at = models.DateTimeField(auto_now=True, blank=True, null=True)
    #is_active

    class Meta:
        ordering = ['created_at']


    def __str__(self):
        return self.category

So what i want to do is check if the payment is successful nad if so i extend the expiration_date of this adv.

Thanks in advance.

Answers:

In order to fulfill orders with Stripe Checkout, you need to set up webhooks[1] and listen for the event checkout.session.completed[2], which occurs when a Checkout Session has been successfully completed. More details can be found in this guide[3].

Meanwhile, according to your question it looks like you are building a recurring payment scenario for your customers. I may invite you to take a look at Stripe Billing and Subscriptions APIs[4]. You can keep tracking of active Subscriptions using webhooks also following this guide[5].

[1] https://stripe.com/docs/webhooks

[2] https://stripe.com/docs/api/events/types#event_types-checkout.session.completed

[3] https://stripe.com/docs/payments/checkout/fulfill-orders

[4] https://stripe.com/docs/billing/subscriptions/build-subscriptions

[5] https://stripe.com/docs/billing/subscriptions/webhooks#active-subscriptions

Answered By: os4m37