这是应用程序代码
@app.route("/stripe_pay")
def stripe_pay():
session = stripe.checkout.Session.create(
payment_method_types=['card'],
line_items=[{
'price_data': 'my_price_id',
'quantity': 1,
}],
mode='subscription',
success_url=url_for('index', _external=True) + '?session_id={CHECKOUT_SESSION_ID}',
cancel_url=url_for('add_service', _external=True),
)
return {
'checkout_session_id' : session['id'],
'checkout_public_key' : app.config['STRIPE_PUBLIC_KEY']
}
我尝试在我的应用程序中进行金钱交易并观看了此[教程][1]。我已经检查了所有的密钥并导入了应用程序所需的所有内容。 调试器争论
cancel_url=url_for('add_service', _external=True),
。
button.addEventListener('click', event => {
fetch('/stripe_pay')
.then((result) => { return result.json(); })
.then((data) => {
var stripe = Stripe(data.checkout_public_key);
stripe.redirectToCheckout({
sessionId: data.checkout_session_id
}).then(function (result) {
});
})
});```
*Thank you in advance*
[1]: https://www.youtube.com/watch?v=cC9jK3WntR8&t=7s
如果您已经有价格 ID,则应该在
price
中使用 line_items
,而不是需要一些子参数的 price_data
。
https://stripe.com/docs/api/checkout/sessions/create#create_checkout_session-line_items-price
Stripe 会话错误无效对象
public function createCheckoutSession(Request $request)
{
Stripe::setApiKey(config('services.stripe.secret'));
$session = StripeSession::create([
'payment_method_types' => ['card'],
'line_items' => [[
'price' => $request->input('price_id'), // Use selected product's price ID,//'price_1PJ8RdSAb6N3GapBEYMM12qe',
'quantity' => 1,
]],
'mode' => 'subscription',
'customer_email' => $request->input('email'), // Collect customer email
'billing_address_collection' => 'required', // Require billing address
'shipping_address_collection' => 'required',
'success_url' => route('subscription.success'),
'cancel_url' => route('subscription.cancel'),
'metadata' => [
'customer_name' => $request->input('name'),
'customer_address' => $request->input('address'),
],
'currency' => 'usd',
]);
return redirect($session->url);
}