Hello, everyone.
I acutely need a help with my @action functionality.
So, going in details.
- I have particularly 2 models in my project – Borrowing and Payment (payment for borrowing)
- I have Stripe gateway payment system, implemented in my project.
- Now I want to implement logic of refreshing Stripe session url (in case it was expired).
So, I created the following action inside PaymentViewSet:
@action(
methods=["POST"],
detail=True,
url_path="update_session_url",
)
def recreate_expired_borrowing_session_url(self, request, pk=None):
borrowing = self.get_object()
payment = Payment.objects.get(borrowing=borrowing, type="Payment")
if payment.status == "Expired":
new_session_for_borrowing = create_stripe_session(
borrowing=borrowing, request=self.request
)
payment.status = "Pending"
payment.session_id = new_session_for_borrowing.id
payment.session_url = new_session_for_borrowing.url
payment.save()
return Response({"status": "Session url has been updated"})
return Response({"status": "Session url is still active"})