I am not able to make an api call in the backend python. Till “log-2” it is working. After that it gives internal server error in response. Can anyone explain the logic I’m missing? I am working on it for a few days now. I have imported three libraries for the below code
import http.client
import urllib.parse
import urllib3
def create_payment_intent(customer_id, price, artist_bank_account_number):
customer_id = customer_id
price = price
artist_bank_account_number = artist_bank_account_number
create_payment_intent_api = "https://api.stripe.com/v1/payment_intents"
try:
# Construct the request parameters
# application_fees = int(0.1 * price)
parameters = {
"amount": price,
"currency": "usd",
"customer": customer_id,
"application_fee_amount": application_fees,
"capture_method": CaptureMethod.MANUAL
}
# Encode parameters into URL-encoded form data
request_body = urllib.parse.urlencode(parameters)
request_body_data = request_body.encode('utf-8')
# Construct headers
headers = {
"Content-Type": "application/x-www-form-urlencoded",
"Authorization": "Bearer sk_test",
"Stripe-Account": artist_bank_account_number
}
print(create_payment_intent_api)
print(request_body_data)
print(headers)
# response = requests.post(create_payment_intent_api, json=parameters, headers=headers)
# print(response)
# response_data = response.json()
# print(response_data)
# Establish connection to the Stripe API server
conn = http.client.HTTPSConnection("api.stripe.com")
print("log-1")
# Send a POST request
conn.request("POST", create_payment_intent_api, request_body_data, headers)
print("log-2")
# Get the response
response = conn.getresponse()
print(response)
response_data = response.read().decode('utf-8')
print(response_data)
if response.status == 200:
print("Payment intent created successfully:", response_data)
return response_data, response.status
else:
print("Error creating payment intent:", response_data)
return response_data, response.status
except http.client.HTTPException as http_err:
print("HTTP error occurred:", http_err)
return str(http_err), 500
except Exception as e:
print("Error:", str(e))
return str(e), 500
New contributor
Shruti Joshi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.