得到 stripe.error.InvalidRequestError:请求“random_string”:在 python 中创建条带支付会话时对象无效

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

这是应用程序代码

@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),

这是我的JS代码


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
python-3.x stripe-payments
2个回答
1
投票

如果您已经有价格 ID,则应该在

price
中使用
line_items
,而不是需要一些子参数的
price_data

https://stripe.com/docs/api/checkout/sessions/create#create_checkout_session-line_items-price


0
投票

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);
}
© www.soinside.com 2019 - 2024. All rights reserved.