我正在使用Cashier 10.1创建一个新应用。过去,在向订阅注册用户时,我会从Stripe向订阅功能发送令牌。现在,它说它需要一种付款方式(id)。我正在使用Stripe注册一种付款方式,并将其传递给我的控制器,但它返回错误“ 该客户没有附加的付款来源”。我阅读了文档,但仅提供了一个示例,该示例说明了如何通过传递到视图$user->createSetupIntent()
向当前用户添加订阅。以下是用户注册时的代码:
组件
pay() {
this.isLoading = true;
if (this.form.payment_method == "cc") {
let that = this;
this.stripe
.createPaymentMethod({
type: "card",
card: that.card
})
.then(result => {
this.form.stripePayment = result.paymentMethod.id;
this.register();
})
.catch(e => {
console.log(e);
});
} else {
this.register();
}
},
register() {
this.form
.post("/register")
.then(data => {
this.isLoading = false;
if (data.type == "success") {
this.$swal({
type: "success",
title: "Great...",
text: data.message,
toast: true,
position: "top-end",
showConfirmButton: false,
timer: 2000
});
setTimeout(() => {
// window.location.replace(data.url);
}, 2000);
}
})
.catch(error => {
this.isLoading = false;
});
}
RegisterController
protected function create(request $request)
{
$request->validate([
'name' => 'required',
'email' => 'required|unique:users',
'username' => 'required|alpha_dash|unique:users',
'phone' => 'required',
'city' => 'required',
'state' => 'required',
'password' => 'required|confirmed',
'agree' => 'required',
]);
$user = User::create([
'username' => $request['username'],
'name' => $request['name'],
'email' => $request['email'],
'phone' => $request['phone'],
'city' => $request['city'],
'state' => $request['state'],
'password' => Hash::make($request['password']),
]);
if ($request['subscription_type'] == 'premier' && $request['payment_method'] == 'cc') {
$user->newSubscription('default', env('STRIPE_PLAN_ID'))->create($request->input('stripePayment'), [
'email' => $request['email'],
]);
}
if ($user) {
$user->assignRole('subscriber');
Auth::login($user);
return response()->json([
'type' => 'success',
'message' => 'you are all set.',
'url' => '/dashboard'
]);
}
答案 0 :(得分:1)
我找到了很好的article,最后解释了新设置。基本上,当我显示注册视图时,我现在创建一个新用户并在其中传递意图。我一直认为用户必须已经保存,而不仅仅是创建。因此,如果有人要这样做:
显示视图
注册控制器
public function show()
{
$user = new User;
return view('auth.register', [
'intent' => $user->createSetupIntent()
]);
}
将意图传递给我的Vue组件
<register-form stripe-key="{{ env('STRIPE_KEY') }}" stripe-intent="{{ $intent->client_secret }}">
</register-form>
将条纹元素添加到div:
mounted() {
// Create a Stripe client.
this.stripe = Stripe(this.stripeKey);
// Create an instance of Elements.
var elements = this.stripe.elements();
var style = {
base: {
color: "#32325d",
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.
this.card = elements.create("card", { style: style });
// Add an instance of the card Element into the `card-element` <div>.
this.card.mount("#card-element");
},
处理数据
pay() {
this.isLoading = true;
if (this.form.payment_method == "cc") {
this.setupCard();
} else {
this.register();
}
},
setupCard() {
this.stripe
.handleCardSetup(this.stripeIntent, this.card, {
payment_method_data: {
billing_details: { name: this.form.name }
}
})
.then(data => {
this.form.stripePayment = data.setupIntent.payment_method;
if (this.form.stripePayment) this.register();
})
.catch(error => {
this.isLoading = false;
console.log(error);
});
},
register(setupIntent) {
this.form
.post("/register")
.then(data => {
this.isLoading = false;
if (data.type == "success") {
this.$swal({
type: "success",
title: "Great...",
text: data.message,
toast: true,
position: "top-end",
showConfirmButton: false,
timer: 2000
});
setTimeout(() => {
window.location.replace(data.url);
}, 2000);
}
})
.catch(error => {
this.isLoading = false;
});
}
保存用户
注册控制器
protected function create(request $request)
{
$request->validate([
'name' => 'required',
'email' => 'required|unique:users',
'username' => 'required|alpha_dash|unique:users',
'phone' => 'required',
'city' => 'required',
'state' => 'required',
'password' => 'required|confirmed',
'agree' => 'required',
]);
$user = User::create([
'username' => $request['username'],
'name' => $request['name'],
'email' => $request['email'],
'phone' => $request['phone'],
'city' => $request['city'],
'state' => $request['state'],
'password' => Hash::make($request['password']),
]);
if ($request['subscription_type'] == 'premier' && $request['payment_method'] == 'cc') {
$user->newSubscription('default', env('STRIPE_PLAN_ID'))->create($request->input('stripePayment'), [
'email' => $request['email'],
]);
}
if ($user) {
$user->assignRole('subscriber');
Auth::login($user);
return response()->json([
'type' => 'success',
'message' => 'you are all set.',
'url' => '/dashboard'
]);
}
}
答案 1 :(得分:0)
由于Strong Customer Authentication (SCA),Cashier更新了与Stripe交互的方式。这意味着卡支付需要不同的用户体验,即3D Secure,才能满足SCA要求。在Stripe上阅读Payment Intents API可能是有益的,并且您可以通过首先与Stripe进行直接交互来创建付款意图,然后将其附加到您的Laravel用户上,来解决此问题。
简单的解决方案可能是具有多步注册过程:
$user->createSetupIntent()
并收集付款详细信息并保存到客户。