3
votes

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]': '[email protected]',
    '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.

2

2 Answers

3
votes

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]': '[email protected]',
    '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)
0
votes

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