首页 > 解决方案 > 如何将 PaymentMethod 附加到 DjStripe 中的条带客户?

问题描述

我有以下 Django 类:

class PurchaseSubscriptionView(APIView):


    def post(self, request, user_id):
        price_name = request.data.get("price_name")
        payment_method = request.data.get("payment_method")
        user = User.objects.get(id=user_id)

        customer, created = djstripe.models.Customer.get_or_create(subscriber=user)
        payment_method_json = json.dumps(payment_method)
        customer.add_payment_method(payment_method_json)
        price = djstripe.models.Price.objects.get(nickname=price_name)
        customer.subscribe(price=price)

一切正常,直到 customer.add_payment_method(payment_method_json)

此时我得到:

stripe.error.InvalidRequestError: Request req_8KcCUrPg7z8Aok: No such PaymentMethod: '{\"id\": \"pm_1IaFMyJs57u3g5HDGoe83cGx\", \"object\": \"payment_method\", \"billing_details\": {\"address\": {\"city\": null, \"country\": null, \"line1\": null, \"line2\": null, \"postal_code\": null, \"state\": null}, \"email\": null, \"name\": null, \"phone\": null}, \"card\": {\"brand\": \"visa\", \"checks\": {\"address_line1_check\": null, \"address_postal_code_check\": null, \"cvc_check\": null}, \"country\": \"US\", \"exp_month\": 2, \"exp_year\": 2022, \"funding\": \"credit\", \"generated_from\": null, \"last4\": \"4242\", \"networks\": {\"available\": [\"visa\"], \"preferred\": null}, \"three_d_secure_usage\": {\"supported\": true}, \"wallet\": null}, \"created\": 1617002320, \"customer\": null, \"livemode\": false, \"type\": \"card\"}'

到底发生了什么?我在生成它后从我的客户那里传递了 PaymentMethod - 这应该有效,对吗?我错过了任何步骤吗?

标签: pythondjangostripe-payments

解决方案


调用时customer.add_payment_method您应该传入 PaymentMethod ID,看起来您正试图将整个 JSON 对象转储到那里。

你可能想要

customer.add_payment_method(payment_method_json.id)

反而。


推荐阅读