问题描述
我一步一步地遵循了本教程: https://appdividend.com/2018/12/05/laravel-stripe-payment-gateway-integration-tutorial-with-example/
I followed this tutorial step by step:https://appdividend.com/2018/12/05/laravel-stripe-payment-gateway-integration-tutorial-with-example/
但是,当我进行测试时,出现以下错误:
However, when I go to test it out, I get the following error:
条带\错误\ InvalidRequest没有这样的付款方式:
Stripe \ Error \ InvalidRequestNo such payment_method:
一些注意事项:
-
我确保Stripe处于测试模式,并且我的Stripe API密钥设置正确,并使用了推荐的测试卡:4242 4242 4242 4242 | 04/22 | 222 | 12345
I made sure that Stripe is in test mode, that my stripe API keys are set properly, and used the recommended testing card: 4242 4242 4242 4242 | 04/22 | 222 | 12345
我仔细阅读了这篇文章的评论,发现其他人也有一个相似"的问题-但不是关于付款方式的错误.
I perused through the comments of the article, and see that other people have a "similar" issue - but not specifically an error regarding the payment method.
自从Laravel 5.8发布以来,Cashier 10被发布了-我看到的是有关"paymentIntents"的点点滴滴-所以我不确定这是否是导致问题的原因.
Since Laravel 5.8 was released, and Cashier 10 was released - I am seeing bits and pieces about "paymentIntents" - so I'm not sure if that is what is causing the problem.
有人对我可以如何解决此错误有任何想法吗?
Does anybody have any ideas on what I can do to fix this error?
谢谢!
以下是我使用过的各种代码:
Here is the various bits of code that I used:
路由(web.php)
Routes (web.php)
Route::group(['middleware' => 'auth'], function() {
Route::get('/home', 'HomeController@index')->name('home');
Route::get('/plans', 'PlanController@index')->name('plans.index');
Route::get('/plan/{plan}', 'PlanController@show')->name('plans.show');
Route::post('/subscription', 'SubscriptionController@create')-
>name('subscription.create');
});
计划模型(plan.php)
Plan Model (plan.php)
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Plan extends Model {
protected $fillable = [
'name',
'slug',
'stripe_plan',
'cost',
'description'
];
public function getRouteKeyName() {
return 'slug';
}
}
计划控制器(PlanController.php)
Plan Controller (PlanController.php)
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Plan;
class PlanController extends Controller {
public function index() {
$plans = Plan::all();
return view('plans.index', compact('plans'));
}
public function show(Plan $plan, Request $request) {
return view('plans.show', compact('plan'));
}
}
订阅控制器(SubscriptionController.php)
Subscription Controller (SubscriptionController.php)
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Plan;
class SubscriptionController extends Controller {
public function create(Request $request, Plan $plan) {
$plan = Plan::findOrFail($request->get('plan'));
$request->user()
->newSubscription('main', $plan->stripe_plan)
->create($request->stripeToken);
return redirect()->route('home')->with('success', 'Your plan subscribed successfully');
}
}
显示视图(show.blade.php)
Show View (show.blade.php)
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-12">
<div class="">
<p>You will be charged ${{ number_format($plan->cost, 2) }} for {{ $plan->name }} Plan</p>
</div>
<div class="card">
<form action="{{ route('subscription.create') }}" method="post" id="payment-form">
@csrf
<div class="form-group">
<div class="card-header">
<label for="card-element">
Enter your credit card information
</label>
</div>
<div class="card-body">
<label for="card-element">Credit or debit card</label>
<div id="card-element">
<!-- A Stripe Element will be inserted here. -->
</div>
<!-- Used to display form errors. -->
<div id="card-errors" role="alert"></div>
<input type="hidden" name="plan" value="{{ $plan->id }}" />
</div>
</div>
<div class="card-footer">
<button class="btn btn-dark" type="submit">Submit Payment</button>
</div>
</form>
</div>
</div>
</div>
</div>
@endsection
@section('scripts')
<script src="https://js.stripe.com/v3/"></script>
<script>
// Create a Stripe client.
var stripe = Stripe('{{ env("STRIPE_KEY") }}');
// Create an instance of Elements.
var 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.)
var style = {
base: {
color: '#32325d',
lineHeight: '18px',
fontFamily: '"Helvetica Neue", Helvetica, sans-serif',
fontSmoothing: 'antialiased',
fontSize: '16px',
'::placeholder': {
color: '#aab7c4'
}
},
invalid: {
color: '#fa755a',
iconColor: '#fa755a'
}
};
// Create an instance of the card Element.
var card = elements.create('card', {style: style});
// Add an instance of the card Element into the `card-element` <div>.
card.mount('#card-element');
// Handle real-time validation errors from the card Element.
card.addEventListener('change', function(event) {
var displayError = document.getElementById('card-errors');
if (event.error) {
displayError.textContent = event.error.message;
} else {
displayError.textContent = '';
}
});
// Handle form submission.
var form = document.getElementById('payment-form');
form.addEventListener('submit', function(event) {
event.preventDefault();
stripe.createToken(card).then(function(result) {
if (result.error) {
// Inform the user if there was an error.
var errorElement = document.getElementById('card-errors');
errorElement.textContent = result.error.message;
} else {
// Send the token to your server.
stripeTokenHandler(result.token);
}
});
});
// Submit the form with the token ID.
function stripeTokenHandler(token) {
// Insert token ID into the form so it gets submitted to the server
var form = document.getElementById('payment-form');
var hiddenInput = document.createElement('input');
hiddenInput.setAttribute('type', 'hidden');
hiddenInput.setAttribute('name', 'stripeToken');
hiddenInput.setAttribute('value', token.id);
form.appendChild(hiddenInput);
// Submit the form
form.submit();
}
</script>
@endsection
推荐答案
使用该教程,您需要在版本10停止使用条纹令牌之前使用Laravel Cashier版本.
Using that tutorial you need to use Laravel Cashier version prior to version 10 that stopped using Stripe Tokens.
对于新项目,我建议您使用Laravel Cashier 10和Stripe Elements,否则当旧API贬值时,您将不得不在不久的将来进行一些认真的重构.
For new projects I suggest that you use Laravel Cashier 10 and Stripe Elements as you would otherwise end up having to do some serious refactoring in the near future when the old API gets depreciated.
由于Laravel Cashier 10刚刚发布,因此除原始文档外,没有太多其他信息.我刚刚启动并使用了一个项目,如果您决定走这条路,我很乐意回答任何问题.
As Laravel Cashier 10 has just been released there are not much info other than the original docs. I just got a project up and running using it and am happy to answer any questions if you decide to go that route.
新过程基本上是:
- 创建setupIntent
- 使用Stripe Elements收集付款信息和CC
- 将其与setupIntent一起发送至Stripe,并使用stripe.handleCardSetup()接收付款方式.
- 设置新的订阅时,请使用payment_method而不是已折旧的令牌.
- 让您的Stripe Webhook处理付款/订阅更新.
这篇关于无法通过Laravel 5.8/Cashier/Stripe设置订阅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!