我有一个具有show()方法的FrontendController,该方法将用户返回到会议详细信息页面,其中包含会议的可用票证:
class FrontEndController extends Controller
public function show($id, $slug){
$conf = Conference::where('id', $id)->first();
$ticketTypes = TicketType::where('conference_id', $conf->id)->get();
...
return view('conferences.show')->with('conference',$conf)->with('ticket_types', $ticketTypes);
}
}
然后在会议详情页面(show.blade.php)中,用户可以选择他想要的门票:
<ul class="list-group list-group-flush">
{{ csrf_field() }}
@foreach($ticket_types as $ttype)
<li class="list-group-item d-flex align-items-center justify-content-between">
<div class="w-100 text-truncate">
<span>{{$ttype->name}}</span>
</div>
<select class="custom-select form-control" name="ttypes[{{ $ttype->name }}]">
<option selected></option>
@for ($i = $ttype->min_participants; $i <= $ttype-> max_participants; $i++)
<option value="{{ $i }}">{{ $i }}</option>
@endfor
</select>
</li>
@endforeach
</ul>
<input type="submit" class="float-right btn btn-primary" value="Next" />
然后当用户点击“下一步”时,请求转到RegistrationController storeQuantities()方法,该方法在会话中存储用户关于所选票证的信息,并将用户重定向到注册页面(registration.blade.php) :
class RegistrationController extends Controller
{
public function storeQuantities(Request $request, $id, $slug = null){
$ttypeQuantities = $request->get('ttypes');
...
$selectedRtypes[$ttype->name]['quantity'] = $quantity;
$selectedRtypes[$ttype->name]['price'] = $price;
$selectedRtypes[$ttype->name]['subtotal'] = $price * $quantity;
$total+= $selectedRtypes[$ttype->name]['subtotal'];
$selectedRtypes[$ttype->name]['total'] = $total;
...
Session::put('selectedRtypes', $selectedRtypes);
...
return redirect(route('conferences.registration',['id' => $id, 'slug' => $slug]));
}
}
在registration.blade.php中,我有一个包含3个步骤的多步骤表单。所有步骤都在同一页面上。 step1是收集有关正在进行注册的用户的信息,第2步是为用户选择付款方式,第3步是付款。
因此,step1的形式如下:
<div>
<form method="post" id="step1form" action="">
{{csrf_field()}}
<!-- fields of the step 1-->
<input type="submit" href="#step2" id="goToStep2" class="btn next-step" value="Go to step 2"/>
</form>
</div>
当点击“转到第2步”时,代码使用ajax请求转到RegistrationController storeUserInfo(),如果storeUserInfo()返回代码200,则用户使用jQuery转到第2步。
因此,如果返回代码200,则用户位于step2form:
<div>
<form method="post" id="step2form" action="">
{{csrf_field()}}
<!-- fields of the step 2-->
<input type="submit" href="#step3" id="goToStep3" class="btn next-step" value="Go to step 3"/>
</form>
</div>
在步骤3中,用户选择付款方式并单击“转到步骤3”,代码通过aJax请求到RegistrationController storePaymentMethods()
以验证用户在步骤2中引入的信息是否返回代码200用户转到步骤3.
step3的下面有一个表单,其中有一个显示条纹模式的按钮“Pay”:
<form action="{{ route('registration.charge') }}" method="post" id="paymentForm">
{{csrf_field()}}
<input type="hidden" name="stripeToken" id="stripeToken"/>
<input type="submit" href="" id="payment" class="btn btn-primary float-right"
value="Pay"/>
</form>
处理条纹费用的收费方法:
public function charge(Request $request)
{
Stripe::setApiKey(config('services.stripe.secret'));
$source = $request->stripeToken;
Charge::create([
'currency' => 'eur',
'description' => 'Example charge',
'amount' => 2500,
'source' => $source,
]);
}
我的疑问是如何设置金额而不是静态值'2500'插入正确的注册总价。此外,当条纹模式出现时,我想在条纹模式按钮中显示注册的总价格。你知道如何实现这个目标吗?
在charge()
内可能正确的认可可以从会话中获取价值,如:
$selectedRtypes = Session::get('selectedRtypes');
$amount = (collect($selectedRtypes)->first()['total']) * 100;
但是你知道如何在Stripe按钮中设置总值吗?在stripe.open
。
条纹代码:
let stripe = StripeCheckout.configure({
key: "{{config('services.stripe.key')}}",
image: "",
locale: "auto",
token: (token) => {
document.querySelector('#stripeToken').value = token.id;
document.querySelector('#paymentForm').submit();
}
});
document.getElementById('payment').addEventListener('click', function(e){
stripe.open({
name: 'test',
description: 'test',
amount: 1000
});
e.preventDefault();
});