Stripe payment在支付成功时做一些事情



我有一个关于发布广告的应用程序,默认情况下我为

设置了一个截止日期每个广告(30天)现在我想用条纹来延长有效期。

到目前为止我所拥有的是结帐,但我希望当付款成功时我更新数据库。

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)

模型:


# 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

所以我想做的是检查支付是否成功,如果成功,我延长这个广告的expiration_date。

提前感谢。

为了使用Stripe Checkout完成订单,您需要设置webhooks[1]并侦听Checkout . Session .completed[2]事件,该事件发生在Checkout会话成功完成时。更多细节可以在本指南中找到[3]。

同时,根据你的问题,看起来你正在为你的客户建立一个循环支付方案。我可能会邀请你看看条纹计费和订阅api[4]。你也可以按照这个指南[5]使用webhooks跟踪活动订阅。

[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

相关内容

  • 没有找到相关文章

最新更新