三行代码  ›  专栏  ›  技术社区  ›  Sins97

条带API有效负载格式问题

  •  0
  • Sins97  · 技术社区  · 3 周前

    我试图在我的python应用程序中集成stripe API,因此不使用python stripe库,而仅使用stripe API端点。我面临的问题是有效负载,尤其是在发送数组或嵌套字典时,如下所示:

    def create_payment_intent(api_key, authorized_amt):
    headers = {
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/x-www-form-urlencoded",
    }
    payload = {
        "amount": authorized_amt,
        "currency": "usd",
        "capture_method": "manual",
        "payment_method_types": ["card"],
    }
    res = requests.request(
        "POST", url=PAYMENT_INTENT_URL, data=payload, headers=headers
    )
    payment_intent = res.json()
    return payment_intent
    

    当我运行此程序时,会出现以下错误:

    {'error': {'message': 'Invalid array', 'param': 'payment_method_types', 'request_log_url': 'https://dashboard.stripe.com/test/logs/req_r9TIQ1wnYF8Gi4?t=1699501606', 'type': 'invalid_request_error'}}

    但当我像下面这样更改它的格式时,它就起作用了:

    "payment_method_types[]": "card"

    这对对象也是一样的,我必须像下面这样发送才能使其工作:

      payload= {
       "card[number]": 4242424242424242,
        "card[exp_month]": 12,
        "card[exp_year]": 2034,
        "card[cvc]": 999
       }
    

    为什么是这样?

    1 回复  |  直到 3 周前
        1
  •  1
  •   qichuan    3 周前

    这是因为Stripe API端点所期望的数据类型是 application/x-www-form-urlencoded