3

I am doing stripe payment integration using python and use the following data:

import requests
import json
pos = requests.post
url = "https://api.stripe.com/v1/sources"
headers = {'AUTHORIZATION': 'Bearer sk_test_NXht3wZpuYWRIWpMDDqT3RG2'}
data = {
    'type': 'alipay',
    'owner[email]': 'abc@xyz.com',
    'redirect[return_url]': 'https://www.google.com',
    'amount': '500',
    'currency': 'USD',
    'metadata': {
        'data': 'data'
    }
}
pos(url, data=data, headers=headers).text
json.loads(pos(url, data=data, headers=headers).text)

When give the metadata it gives error '{\n "error": {\n "message": "Invalid hash",\n "param": "metadata",\n "type": "invalid_request_error"\n }\n}\n' but according to stripe documentation metadata can be used( https://stripe.com/docs/api/curl#create_source-metadata)

Can anyone tell the solution why it gives that error.

Matthias Wiehl
  • 1,799
  • 16
  • 22
user_123
  • 426
  • 7
  • 23

2 Answers2

3

This will solve the problem.

import requests
import json
pos = requests.post
url = "https://api.stripe.com/v1/sources"
headers = {'AUTHORIZATION': 'Bearer sk_test_NXht3wZpuYWRIWpMDDqT3RG2'}
data = {
    'type': 'alipay',
    'owner[email]': 'abc@xyz.com',
    'redirect[return_url]': 'https://www.google.com',
    'amount': '500',
    'currency': 'USD',
    'metadata[data]': 'data'
}
pos(url, data=data, headers=headers).text
json.loads(pos(url, data=data, headers=headers).text)
user_123
  • 426
  • 7
  • 23
0

Stripe does not support JSON payloads for the parameters. Instead, they require application/x-www-form-urlencoded.

At the moment, you are sending metadata as a hash and you are not encoding it properly so Stripe is rejecting it.

The best solution here is to avoid doing this yourself and instead rely on Stripe's official Python library that you can find here: https://github.com/stripe/stripe-python

koopajah
  • 23,792
  • 9
  • 78
  • 104