无法识别的请求 URL(GET:/v1/customers/)。如果您尝试列出对象,请删除尾部斜杠

Ada*_*dam 0 url form-submit subscription laravel laravel-cashier

如果有人知道为什么我在提交条纹订阅表单时不断收到此错误,请帮忙,这个错误让我卡住了一段时间,只是尝试从定价页面或订阅计划页面创建订阅,我有计划以名为 Plan 的模型存储在我的数据库中。所以我想要的是让用户选择每月或每年的定价计划,然后他们将进入付款页面,在那里他们可以付款并激活订阅。我正在使用带有条纹的 Laravel Cashier。

完整错误消息 无法识别的请求 URL (GET: /v1/customers/)。如果您尝试列出对象,请删除尾部斜杠。如果您尝试检索对象,请确保在代码中传递了有效(非空)标识符。请参阅https://stripe.com/docs或者我们可以通过https://support.stripe.com/提供帮助。

计划模型 在此输入图像描述

路线

Route::group(['namespace' => 'Subscriptions'], function() {
Route::get('plans', 'SubscriptionPlanController@index')->name('plans');
Route::get('/payments', 'PaymentsController@index')->name('payments');
Route::post('/payments', 'PaymentsController@store')->name('payments.store');
});
Run Code Online (Sandbox Code Playgroud)

计划页面

<div class="container">
<div class="row justify-content-center">
    <div class="col-md-8">
        <div class="card">
            <div class="card-header">{{ __('Subscription Plans') }}</div>

            <div class="card-body">
                @foreach($plans as $plan)
                    <div>
                        <a href="{{ route('payments', ['plan' => $plan->identifier]) }}">{{$plan->title}}</a>
                        {{-- {{dd($plan->stripe_id)}} --}}
                    </div>
                @endforeach
            </div>
        </div>
    </div>
   </div>
 </div>
Run Code Online (Sandbox Code Playgroud)

这是表格

    <form action="{{ route('payments.store')}}" method="POST" id="payment-form">
    @csrf
     <div class="form-content">


    <input type="hidden" name="plan" id="subscription-plan" value="{{ request('plan') }}">

    <div class="field">
      <input type="text" autocorrect="off" spellcheck="false" id="card-holder-name" maxlength="25" />
      <span class="focus-bar"></span>
      <label for="cardholder">Card holder (Name on card)</label>
    </div>
        
        <div  class="field mb-5" id="card-element">
        <!-- Stripe Elements Placeholder -->
        </div>

    <button id="card-button" type="submit" data-secret="{{ $intent->client_secret }}"> 
  <span>Pay</span></button>
    
  </div>
  
</form>
Run Code Online (Sandbox Code Playgroud)

这是支付控制器

    public function index()
{
   

    $user = auth()->user();

    $data = [
        'intent' => $user->createSetupIntent(),

    ];

    return view('subscriptions.payments')->with($data);
}

public function store(Request $request)
{


    $user = auth()->user();
   
    $paymentMethod = $request->payment_method;
  
    $plan = Plans::where('identifier', $request->plan)
        ->orWhere('identifier', 'basic_product')
        ->first();
    
    $request->user()->newSubscription('default', $plan->stripe_id)->create($paymentMethod);


    return response(['status' => 'success']);
}
Run Code Online (Sandbox Code Playgroud)

这是 JavaScript

// Create a Stripe client.
        const stripe = Stripe('pk_test_51H2OqqLzAo4pwMcyT4h405wpFRAn3FWhvByfvmVnW6tabrIsDoU1dBXJ0UaWexUJeacCJ9uKpb5OBmmA2KaCg4sd00ZZ5tj2q8');

        // Create an instance of Elements.
        const elements = stripe.elements();

        // Custom styling can be passed to options when creating an Element.
        // (Note that this demo uses a wider set of styles than the guide below.)
         
         // const cardElement = elements.create('card', {style: style});
         
        

        // Create an instance of the card Element.
        const cardElement = elements.create('card');

        // Add an instance of the card Element into the `card-element` <div>.
        cardElement.mount('#card-element');

            const cardHolderName = document.getElementById('card-holder-name');
            const cardButton = document.getElementById('card-button');
            const clientSecret = cardButton.dataset.secret;

            const plan = document.getElementById('subscription-plan').value;
    

      // Handle form submission.
        var form = document.getElementById('payment-form');
        form.addEventListener('submit', async (e) => {
          e.preventDefault();
           
           cardButton.disabled = true
            const { setupIntent, error } = await stripe.confirmCardSetup(
                cardButton.dataset.secret, {
                    payment_method: {
                        card: cardElement,
                        billing_details: {
                            name: cardHolderName.value
                        }
                    }
                }
             
                );
               
                if (error) {
                    // Display "error.message" to the user...
                } else {
                  
                
                   var paymentMethod  = setupIntent.payment_method;

            

                    var form = document.getElementById('payment-form');
                    var hiddenInput = document.createElement('input');
                    hiddenInput.setAttribute('type', 'hidden');
                    hiddenInput.setAttribute('name', 'payment_method');
                    hiddenInput.setAttribute('value', paymentMethod);
                    form.appendChild(hiddenInput);

                    // Submit the form
                    form.submit();

      
                }

              
                 
           // });
        });
Run Code Online (Sandbox Code Playgroud)

小智 5

我今晚遇到了这个问题。原来是因为数据库中的“stripe_id”存储为“”(空白)vs NULL。

在 stripe ManagesCustomer 函数中,它基于 !is_null($this->stripe_id) 执行逻辑,然后假设 this 有一个 stripe id(如果它是空字符串)。