python - 如何将 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 - 这应该有效,对吗?我错过了任何步骤吗?
解决方案
调用时customer.add_payment_method
您应该传入 PaymentMethod ID,看起来您正试图将整个 JSON 对象转储到那里。
你可能想要
customer.add_payment_method(payment_method_json.id)
反而。
推荐阅读
- google-app-engine - Stackdriver 日志分组忽略受监控的资源定义
- user-interface - 使用批处理文件通过 GUI 将参数发送到 MFC 应用程序
- javascript - 期望 toHaveBeenCalledWith 不能使用最新的 CreateReactApp 设置
- express - 有没有办法使用普罗米修斯检查 nodejs 应用程序是否活着?
- jwt - 使用 JWT 对 3rd 方进行身份验证的声明
- microservices - 自治微服务
- html - 是否可以仅删除汉字的多余空间但在html中保留必要的代码符号?
- c# - 2列OLEDB ACCESS的总和值
- swiftui - SwiftUI 步进器布局问题
- elasticsearch - Elasticsearch,根据兄弟字段但从不同的索引对aggs进行排序