无法使用python成功进行条带支付意图

问题描述 投票:0回答:1

我无法在后端 python 中进行 api 调用。直到“log-2”它才工作。之后它会给出内部服务器错误响应。谁能解释我缺少的逻辑?我现在正在研究它几天。我为以下代码导入了三个库

导入http.client

导入 urllib.parse

导入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
python stripe-payments
1个回答
0
投票

您似乎尝试使用请求模块,但没有让它工作。我能够让它与以下内容一起工作:

import requests

sk = 'sk_test_xyz'

url = 'https://api.stripe.com/v1/payment_intents'

headers = {
    'Content-Type':'application/x-www-form-urlencoded',
    'Authorization':'Bearer '+sk,
    'Stripe-Account':'acct_xyz'
}

body = {
    "amount": 5000,
    "currency": "usd",
    "application_fee_amount": 123,
    "capture_method": 'manual'
}

api_call = requests.post(url,params=body,headers=headers)
print(api_call.text)

但仍然不确定为什么不使用 Stripe 的官方 Python 库。这看起来工作量很大。

© www.soinside.com 2019 - 2024. All rights reserved.