作者:xeyuxing369 | 来源:互联网 | 2023-08-20 10:59
ImstrugglingwithphilosophicalquestionsinmyDRFstructureusingthepayementhandlerStripe.I
I'm struggling with philosophical questions in my DRF structure using the payement handler Stripe. I'm selling a product that has a django model Product
through my DRF REST API. I'm wondering if I should create the Product
and then handle the payment in my create
view as follow:
我正在使用付款处理程序Stripe在我的DRF结构中与哲学问题作斗争。我正在通过我的DRF REST API销售一款具有django型号产品的产品。我想知道我是否应该创建产品,然后在我的创建视图中处理付款,如下所示:
class ProductViewSet(viewsets.ModelViewSet):
...
def create(self, request):
serializer = ProductSerializer(data=request.data)
serializer.is_valid(raise_exception=True)
product = serializer.save()
try:
respOnse= stripe.Charge.create(
amount=product.cost,
currency="usd",
source=request.data["token"], # Done with Stripe.js
description="Product"
)
product.charge_id = response.charge_id
...
or instead, if I should handle the payement in the serializer of Product
:
或者,如果我应该在产品的序列化器中处理付款:
class ProductSerializer(serializers.Serializer):
...
def create(self, validated_data):
product = Product.objects.create(**validated_data)
# Will raise an Excetpion and stop the creation:
respOnse= stripe.Charge.create(
amount=product.cost,
currency="usd",
source=validated_data["token"], # Done with Stripe.js
description="Product"
)
return product
Which one is the better practice? Or, do I completely miss the point and should do it differently?
哪一个更好的做法?或者,我是否完全忽略了这一点,应该采用不同的方式吗?
Secondly, is there a way to embed Stripe.js and the required form in the Browsable API template for the create
route so I can test my REST without the need of any frontend?
其次,有没有办法在创建路径的Browsable API模板中嵌入Stripe.js和所需的表单,这样我就可以在不需要任何前端的情况下测试我的REST了?
Thank you for your help
谢谢您的帮助
1 个解决方案