【发布时间】:2022-09-23 05:28:05
【问题描述】:
我正在尝试将 Stripe 支付集成到一个颤振的网络应用程序中。为此,我编写了一个我在 heroku 上托管的 python 脚本:
import json
import os
import stripe
from flask import Flask, render_template, jsonify, request
from flask_cors import CORS
# This is your test secret API key.
stripe.api_key = \'sk_test_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\'
app = Flask(__name__, static_folder=\'public\',static_url_path=\'\', template_folder=\'public\')
CORS(app)
app.config[\'CORS_HEADERS\'] = \'Content-Type\'
def calculate_order_amount(items):
# Replace this constant with a calculation of the order\'s amount
# Calculate the order total on the server to prevent
# people from directly manipulating the amount on the client
return 1400
@app.route(\'/create-payment-intent\', methods=[\'POST\'])
def create_payment():
print(\'Processing checkout\')
request_data = request.data
request_data = json.loads(request_data.decode(\'utf-8\'))
print(request_data)
try:
parking = request_data[\'parking\']
items = [
{
\'price_data\': {
\'currency\':\'aud\',
\'product_data\': {
\'name\': parking[\'name\'],
},
\'unit_amount\': parking[\'amount\'],
},
\'quantity\':1,
}
],
print(request.data)
# Create a PaymentIntent with the order amount and currency
intent = stripe.PaymentIntent.create(
amount=40*100,
currency=\'aud\',
payment_method_types=[\"card\"],
)
return jsonify({
\'clientSecret\': intent[\'client_secret\']
})
except Exception as e:
print(\'Error Occured: {}\'.format(e))
return jsonify(error=str(e)), 403
if __name__ == \'__main__\':
app.run(port=4242)
在我的颤振应用程序中,我正在这样做:
var url = Uri.parse(\'https://spaceshuttleparking-checkout.herokuapp.com/create-payment-intent\');
final response = await http.post(
headers:{
\"Accept\": \"application/json\",
\"Access-Control-Allow-Origin\": \"*\"
},
url,
body: json.encode(
{
\'parking\':{
\'name\':parking.name,
\'amount\':parking.amount,
}
}
)).then((value) {
print(value.body);
print(value.statusCode);
print(value.request);
});
在我的颤振应用程序中,我得到以下输出:
200
POST https://spaceshuttleparking-checkout.herokuapp.com/create-payment-intent
{\"clientSecret\":\"pi_3LXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\"}
在heroku日志上,我得到以下信息:
2022-09-12T07:49:08.216994+00:00 app[web.1]: Processing checkout
2022-09-12T07:49:08.238030+00:00 app[web.1]: {\'parking\': {\'name\': \'Undercover Park & Fly\', \'amount\': 38}}
2022-09-12T07:49:08.238031+00:00 app[web.1]: b\'{\"parking\":{\"name\":\"Undercover Park & Fly\",\"amount\":38}}\'
2022-09-12T07:49:08.647809+00:00 heroku[router]: at=info method=POST path=\"/create-payment-intent\" host=spaceshuttleparking-checkout.herokuapp.com request_id=43b4cc48-c3a1-44ec-b240-821901183e5b fwd=\"119.18.0.79\" dyno=web.1 connect=0ms service=431ms status=200 bytes=291 protocol=https
2022-09-12T07:49:08.647485+00:00 app[web.1]: 10.1.32.94 - - [12/Sep/2022:07:49:08 +0000] \"POST /create-payment-intent HTTP/1.1\" 200 80 \"http://localhost:8620/\" \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36 Edg/105.0.1343.33\"
我对这些东西超级陌生,所以我不确定我错过了什么。为什么服务器不打开条带结帐页面?
-
您必须将
confirm=True传递给stripe.PaymentIntent.create或致电stripe.PaymentIntent.confirm。 stripe.com/docs/api/payment_intents/confirm?lang=python
标签: python flutter dart heroku flutter-web