Question:
Fix Stripe API payload Format issue

Problem:

I'm trying to integrate the Stripe API in my Python application; therefore, I'm not using the Python Stripe library but instead using Stripe API endpoints only. The issue I'm facing is with the payload especially when sending array or nested dictionary like below:


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


when I run this I get error below:

{'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'}}


But when I change its format like below it works:

"payment_method_types[]": "card"


This is same for objects also I will have to send like below to make it work:

payload= {

   "card[number]": 4242424242424242,

    "card[exp_month]": 12,

    "card[exp_year]": 2034,

    "card[cvc]": 999

   }

Solution:

That's because the data type that Stripe API endpoint expects is application/x-www-form-urlencoded.


Suggested blogs:

>PHP cURL to upload video to azure blob storage

>PHP Error Solved: htaccess problem with an empty string in URL

>Plugins and Presets for Vuejs project

>Python Error Solved: load_associated_files do not load a txt file

>Python Error Solved: pg_config executable not found

>Set up Node.js & connect to a MongoDB Database Using Node.js

>Setting up a Cloud Composer environment: Step-by-step guide

>How to merge cells with HTML, CSS, and PHP?


Ritu Singh

Ritu Singh

Submit
0 Answers