条带错误 - $config 必须是字符串或数组

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

我在我的应用程序中使用 Laravel 和 Vue.js,并且在尝试创建 Stripe Checkout 会话时遇到问题。具体来说,当用户尝试下订单时,我收到以下错误:

{     "status": "error",     "message": "There was an error creating the checkout session: $config must be a string or an array" }

这是我的请求代码:

const fullName = ref(null);
const gender = ref(null);
const nationalCardID = ref(null);
const purchase = async () => {
    const button = document.getElementById('purchase-btn');
    button.innerHTML = '<span class="loading loading-spinner loading-md"></span>';
    await new Promise(resolve => setTimeout(resolve, 2000));
    try {
        await axios.post('/order-checkout', {
            offerId: selectedOffer.value.id,
            quantity: checked.value ? 2 : 1,
            fullName: fullName.value,
            gender: gender.value,
            nationalCardID: nationalCardID.value,
        }).then((res) => {
            goToStripeCheckout(res.data.checkout_url)
        }).catch(async (err) => {
            toast.error(err.response.data.message);
            console.error('Error placing order:', err);
            showModal.value = false
            await new Promise(resolve => setTimeout(resolve, 500));
            showModal.value = true;
        });
    } catch (error) {
        toast.error(error);
        console.error('Unexpected error:', error);
    }
    button.innerHTML = 'Purchase'
};

这是我的后端功能:

public function orderCheckout(Request $request)
    {
        $validatedData = $request->validate([
            'offerId' => 'required|integer|exists:offers,id',
            'quantity' => 'required|integer',
            'fullName' => 'required|string|max:255',
            'gender' => 'required|string|max:255',
            'nationalCardID' => 'required|string|max:500'
        ]);
    
        $offer = Offer::find($validatedData['offerId']);
    
        if (!$offer) {
            return response()->json([
                'status' => 'error',
                'message' => 'Offer not found.'
            ], 404);
        }
    
        try {
            $stripe = new StripeClient(env('STRIPE_SECRET_KEY'));
    
            $checkout_session = $stripe->checkout->sessions->create([
                'line_items' => [[
                    'price_data' => [
                        'currency' => 'usd',
                        'product_data' => [
                            'name' => $offer->name,
                        ],
                        'unit_amount' => $offer->total_with_tax * 100,
                    ],
                    'quantity' => $validatedData['quantity'],
                ]],
                'mode' => 'payment',
                'success_url' => config('app.frontend_url') . '/checkout/success?session_id={CHECKOUT_SESSION_ID}',
                'cancel_url' => config('app.frontend_url') . '/checkout/cancel',
            ]);
    
            $order_id = uniqid() . mt_rand(100000, 999999);
    
            $order = new Order();
            $order->order_id = $order_id;
            $order->status = 'unpaid';
            $order->total_price = $offer->total_with_tax;
            $order->session_id = $checkout_session->id;
            $order->user_id = Auth::user()->id;
            $order->offer_id = $validatedData['offerId'];
            $order->full_name = $validatedData['fullName'];
            $order->gender = $validatedData['gender'];
            $order->national_card_id = $validatedData['nationalCardID'];
            $order->save();
    
            $countOrdersCacheKey = 'count_orders';
            if (Cache::get($countOrdersCacheKey)) {
                Cache::forget($countOrdersCacheKey);
            }
            
            $admins = User::where('is_admin', 1)->pluck('id');
            DB::transaction(function () use ($admins) {
                $notifications = [];
                foreach ($admins as $adminId) {
                    $notifications[] = [
                        'user_id' => $adminId,
                        'created_at' => now(),
                        'updated_at' => now(),
                    ];
                }
                OrderNotification::insert($notifications);
    
                event(new OrderNotifications('New Order! Check it out'));
    
                foreach ($admins as $adminId) {
                    Cache::forget("order_notifications_{$adminId}");
                }
            });
    
            return response()->json([
                'status' => 'success',
                'message' => 'Checkout session created successfully.',
                'checkout_url' => $checkout_session->url,
                'order_id' => $order_id,
                'order' => $order
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'status' => 'error',
                'message' => 'There was an error creating the checkout session: ' . $e->getMessage()
            ], 500);
        }
    }

我已在 .env 文件中成功设置了 stripe 密钥,并且我正在使用最新版本的 Stripe PHP SDK。

php laravel symfony stripe-payments payment-gateway
1个回答
0
投票

您的配置变量可能有一个意外的值。您可以尝试调试并确保您具有正确的

config('app.frontend_url')
config('app.frontend_url')
值(或者也许您的语法有错误?)

'success_url' => config('app.frontend_url') . '/checkout/success?session_id={CHECKOUT_SESSION_ID}',
'cancel_url' => config('app.frontend_url') . '/checkout/cancel',
        ]);
© www.soinside.com 2019 - 2024. All rights reserved.