【问题标题】:Laravel stripe checkout.js not loadedLaravel 条带 checkout.js 未加载
【发布时间】:2017-06-17 11:59:24
【问题描述】:

我正在开发一个允许一次性付款的网络应用程序。为此,我正在使用 Laravel 和 Cashier。

我正在展示一些带有典型 Stripe 支付按钮的产品。单击它应该会显示 checkout.js 表单,但这没有发生。

产品视图基本上是一个显示条纹按钮的循环:

<div class="row">
 @foreach ($products as $product)
    <form action="{{ route('frontend.user.pay', $product->id) }}" method="POST">
    {{ csrf_field() }}
     <div class="col-sm-5 col-md-5">
       <div class="thumbnail">
         <div class="caption">
           <h3>{{ $product->name }}</h3>
           <p>{{ $product->small_description }}</p>
           <p>Buy for ${{ substr_replace($product->price, '.', 2, 0) }}</p>
           <p>
           <script src="https://checkout.stripe.com/checkout.js" class="stripe-button"                       
            data-key="{{ env('STRIPE_PUBLIC') }}"
            data-amount="{{ $product->price }}"
            data-name="Stripe.com"
            data-description="Widget"
            data-locale="auto"
            data-currency="usd">
           </script>
          </p>
        </div>
       </div>
      </div>
     </form>
   @endforeach
   </div><!--row-->

当我点击按钮时,Stripe pay 覆盖没有加载。

相关路线是

Route::get('product', 'ProductController@index')->name('product');
Route::post('pay/{product}', 'OrderController@payWithStripe')->name('pay');

Controller文件如下:

public function payWithStripe(Request $request, Product $product) {
  $token = $request->input('_token'); 
  return $this->chargeCustomer($product->id, $product->price, $product->name, $token);
}

public function chargeCustomer($product_id, $product_price, $product_name, $token) {
    \Stripe\Stripe::setApiKey(env('STRIPE_SECRET'));

    if (!$this->isStripeCustomer()) {
       $customer = $this->createStripeCustomer($token);
    }
    else {
       $customer = \Stripe\Customer::retrieve(access()->user()->stripe_id);
    }
 return $this->createStripeCharge($product_id, $product_price, $product_name, $customer);
 }

 public function createStripeCharge($product_id, $product_price, $product_name, $customer) {
   try {
       $charge = \Stripe\Charge::create(array(
           "amount" => $product_price,
           "currency" => "usd",
           "customer" => $customer->id,
           "description" => $product_name
        ));
    } catch(\Stripe\Error\Card $e) {
       return redirect()
           ->route('frontend.user.product')
           ->with('error', 'Your credit card was been declined. Please try again or contact us.');
    }
        return $this->postStoreOrder($product_id);
  }


 public function createStripeCustomer($token) {
    \Stripe\Stripe::setApiKey(env('STRIPE_SECRET'));

    $customer = \Stripe\Customer::create(array(
        "description" => access()->user()->email,
        "source" => $token
    ));

    access()->user()->stripe_id = $customer->id;
    access()->user()->save();

    return $customer;
 }

 public function isStripeCustomer() {
    return access()->user() && \App\Models\Access\User\User::where('id', access()->user()->id)->whereNotNull('stripe_id')->first();
 }

问题是:

  • 1) 叠加支付(应输入 VISA 卡详细信息的地方)是 不显示
  • 2) 在chargeCustomer 函数中,客户不是 在 Stripe 中生成。我收到一个错误“没有这样的令牌:xxxxxxxx”。 打印出来绝对是隐藏的令牌(从综合浏览量中检查)。问题可能与 _token 有关,我看到应该使用 stripeToken。但是 stripeToken 总是返回 null。

【问题讨论】:

    标签: laravel-5 laravel-cashier


    【解决方案1】:

    这可能是因为 app.js 被调用并且它们都发生了冲突。尝试删除 app.js 链接,看看它的反应。

    【讨论】:

    • 问题出在 app.js 上。删除它并尝试对我有用。
    【解决方案2】:

    这是旧的,但我遇到了同样的问题。

    从 #app 更改我的根元素的 id 让它工作......

    【讨论】:

      猜你喜欢
      • 2017-05-12
      • 2021-12-11
      • 2017-11-06
      • 2023-04-05
      • 2019-02-19
      • 2016-01-28
      • 2017-01-04
      • 1970-01-01
      • 2020-04-09
      相关资源
      最近更新 更多