【问题标题】:How do I attach a PaymentMethod to a stripe Customer in DjStripe?如何将 PaymentMethod 附加到 DjStripe 中的条带客户?
【发布时间】:2021-06-25 05:47:30
【问题描述】:

我有以下 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 - 这应该有效,对吗?我是否缺少任何步骤?

【问题讨论】:

    标签: python django stripe-payments


    【解决方案1】:

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

    你可能想要

    customer.add_payment_method(payment_method_json.id)
    

    改为。

    【讨论】:

    • 谢谢!昨天我一直在为此苦苦挣扎。我应该只为这个特定库的每个方法传递 id 吗?
    • 根据经验,您在发出请求时永远不需要传入整个 Stripe 对象 JSON blob。在大多数情况下,您只需要对象的 ID 和一些额外的数据。
    猜你喜欢
    • 2020-01-09
    • 2018-01-03
    • 2020-01-24
    • 2021-10-19
    • 2019-12-25
    • 2021-02-06
    • 1970-01-01
    • 1970-01-01
    • 2011-11-25
    相关资源
    最近更新 更多