joh*_*hnW 17 php jquery laravel
我有一个多步骤表单供用户在会议中注册,所有步骤都在同一registration.blade.php页面,在步骤1和步骤2完成ajax请求以验证表单字段.
步骤是:
我怀疑是在第2步和第3步之间.
如果执行下面的代码需要所选的付款方式参考,那么会生成一些付款参考,然后在步骤3中向用户提供该参考.当用户付款时,系统会收到通知并应插入付款表中price,payment_method_id,registration_id和status(付费).
使用参考资料处理付款的代码:
public function ReferencesCharge(Request $request)
{
$payment_info = [
'name' => "user name",
'email' => 'user email',
'value' => 'registration total price',
'id' => 'registration id',
];
$newPayment = new Payment($payment_info);
$reference = $newPayment->gererateReferences();
//$reference returns an array with necessary codes to present to the user so he can pay
// after generate references is necessary:
// show in step 3 the generated references
// insert an entry in the payments table when the system receives a notification from 3rd party service informing that the user did the payment
}
Run Code Online (Sandbox Code Playgroud)
如果选择的支付是信用卡需要执行下面的代码的信用卡中扣款,并插在付款表后price,payment_method_id,registration_id和status(付费).然后应将用户重定向到步骤4以显示成功消息:
处理信用卡付款的代码:
public function creditCardCharge(Request $request)
{
Stripe::setApiKey(config('services.stripe.secret'));
$source = $request->stripeToken;
try{
Charge::create([
'currency' => 'eur',
'amount' => 2500,
'source' => $source,
]);
}
catch(\Exception $e){
return response()->json(['status' => $e->getMessage()], 422);
}
// after charging the card with success:
// insert an entry in the payments table
// redirect to success confirmation step to inform the user that payment was done with success
}
Run Code Online (Sandbox Code Playgroud)
我的疑问是流程应该如何,应该放置使用引用处理付款的代码和使用信用卡处理付款的代码.所以有可能实现这种情况:
现在,我只是让步骤1和步骤2正常工作.为了处理step1和step2,我有了RegistrationController.在步骤1中,用户引入的信息使用ajax post请求验证if 的storeUserInfo()方法RegistrationController返回200用户转到step2.
在步骤2中,用户选择在"去到步骤3"的支付方法和点击次数也做一个AJAX请求到storePaymentMethods()的RegistrationController,如果用户选择的至少1付款方法来验证.我怀疑是在这个方法之后返回代码200的过程应该如何.
根据付款方式,必须运行上面的相应代码(生成付款参考的代码或代码以向信用卡收费).
因此,我怀疑如何根据控制器和方法组织此代码,在哪里放置应根据所选付款方式执行的代码.你知道如何实现这一目标吗?
也许一种方法可能类似于下面的方法,storePaymentMethods()但在这种方法中做一切似乎都不正确:
public function storePaymentMethods(Request $request){
$request->validate([
'payment_method' => 'required',
]);
if($request->payment_method == "references"){
// generate codes and present to the user that codes
}
else if($request->payment_method == "credit_card"){
// show credit card inputs to the user
// and process the credit card payment with stripe
}
return response()->json([
'success' => true,
'message' => 'success',
'payment_method' => $request->payment_method,
], 200);
}
Run Code Online (Sandbox Code Playgroud)
我现在拥有的多步骤表单注册流程的完整摘要:
所以对于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>
Run Code Online (Sandbox Code Playgroud)
step1图片更好地解释:
当用户点击"转到步骤2"按钮时,会发出ajax请求来验证数据,如果没有错误,则返回代码200,用户转到步骤2:
$('#goToStep2').on('click', function (event) {
event.preventDefault();
var custom_form = $("#" + page_form_id_step1);
$.ajax({
method: "POST",
url: '{{ route('conferences.storeRegistrationInfo', compact('id','slug') ) }}',
data: custom_form.serialize(),
datatype: 'json',
success: function (data, textStatus, jqXHR) {
var $active = $('.nav-pills li a.active');
nextTab($active);
},
error: function (data) {
// show errors
}
});
});
Run Code Online (Sandbox Code Playgroud)
然后在ConferencesController中有teh storeRegistrationInfo()来处理上面的ajax请求:
public function storeRegistrationInfo(Request $request, $id, $slug = null, Validator $validator){
$rules = [];
$messages = [];
$rules["name_invoice"] = 'required|max:255|string';
$rules["TIN_invoice"] = ['required', 'string', new ValidTIN()];
$validator = Validator::make($request->all(), $rules, $messages);
$errors = $validator->errors();
$errors = json_decode($errors);
if($validator->fails()) {
return response()->json([
'success' => false,
'errors' => $errors
], 422);
}
return response()->json([
'success' => true,
'message' => 'success'
], 200);
}
Run Code Online (Sandbox Code Playgroud)
因此,如果返回代码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>
Run Code Online (Sandbox Code Playgroud)
step2图片更好地解释:
当用户点击"转到步骤3"按钮时,会发出一个ajax请求来验证数据,如果没有错误,则返回代码200,用户转到步骤3,ajax请求:
$("#credit_card_section").hide();
$("#references_section").hide();
var page_form_id_step2 = "step2form";
$('#goToStep3').on('click', function (event) {
event.preventDefault();
var custom_form = $("#" + page_form_id_step2);
$.ajax({
method: "POST",
url: '{{ route('conferences.storePaymentMethods', compact('id','slug') ) }}',
data: custom_form.serialize(),
datatype: 'json',
success: function (data, textStatus, jqXHR) {
var result = data;
if(result['payment_method'] == 'credit_card'){
$("#credit_card_section").show();
$("#references_section").hide();
}else{
$("#references_section").show();
$("#credit_card_section").hide();
}
var $active = $('.nav-pills li a.active');
nextTab($active);
},
error: function (data) {
// show errors
}
});
});
Run Code Online (Sandbox Code Playgroud)
ConferenceController具有storePayment()来处理上面的ajax请求,它验证用户是否选择了支付方法,如果是,则返回代码200:
public function storePaymentMethods(Request $request){
$request->validate([
'payment_method' => 'required',
]);
return response()->json([
'success' => true,
'message' => 'success',
'payment_method' => $request->payment_method,
], 200);
}
Run Code Online (Sandbox Code Playgroud)
然后是step3 div.在step3 div中,根据上一步骤中选择的付款方式(信用卡或参考),div显示div #credit_card_section可见或#references_section可见:
<div>
<form method="post" id="step3form" action="">
{{csrf_field()}}
<div id="credit_card_section">
<!-- present necessary fields to
payments with credit card-->
</div>
<div id="references_section">
<!-- present generated reference to the user so he can pay-->
</div>
</form>
</div>
Run Code Online (Sandbox Code Playgroud)
step3图像更好地解释,根据付款方式,步骤3中显示的信息是不同的.如果付款方式是信用卡step4,则在使用信用卡成功收费后,会出现显示成功消息的div:
然后在使用信用卡完成付款后,应该显示成功消息的第4步div:
<div id="step4">
<p>
<i class="fa fa-plus" aria-hidden="true"></i>
Payment and registration completed with success.
</p>
</div>
Run Code Online (Sandbox Code Playgroud)
//我现在拥有的RegistrationController方法的简历,RegistrationController是我处理多步形式的控制器
class RegistrationController extends Controller
{
public :function storeQuantities(Request $request, $id, $slug = null){
// method that stores in session the ticket types
// selected by the user in the conference details page
Session::put('selectedRtypes', $selectedRtypes);
Session::put('allParticipants' , $allParticipants);
Session::put('customQuestions' , $selectedRtypes[$rtype->name]['questions']);
// and then redirects the user to the registartion page registration.blade.php
// using the route 'conferences.registration
// this route is associated with the displayRegistrationPage() method
return redirect(route('conferences.registration',['id' => $id, 'slug' => $slug]));
}
// method of the route 'conferences.registration' that displays
// the registration.blade.php that is the page with the multi step form
public function displayRegistrationPage(Request $request, $id, $slug=null){
// get the session values
$selectedRtypes = Session::get('selectedRtypes');
$allParticipants = Session::get('allParticipants');
$customQuestions = Session::get('customQuestions');
// redirect the user to the registration.blade.php
if(isset($selectedRtypes)) {
return view('conferences.registration',
['selectedRtypes' => $selectedRtypes, 'customQuestions' => $customQuestions, 'id' => $id, 'slug' => $slug]);
}
else{
// return user to the conference details page
return redirect(route('conferences.show',['id' => $id, 'slug' => $slug]));
}
}
/* the following methods are to handle the registration
multi step form of the registration.blade.php view */
// method to handle the step1 of the multi step form
public function storeRegistrationInfo(Request $request, $id, $slug = null, Validator $validator){
// get and validate the fields of the step1
// and returns code 200 if al is ok
return response()->json([
'success' => true,
'message' => 'success'
], 200);
}
// method to handle the step2 of the multi step form
public function storePaymentMethods(Request $request){
// validate if the payment_method field was filled
// if was filled return code 200
// and returns the payment_method
//so in the step 3 div is possible to show a section for
// when the payment_method is credit card (#credit_card_section)
or transfers ("transfers_section")
return response()->json([
'success' => true,
'message' => 'success',
'payment_method' => $request->payment_method,
], 200);
}
}
Run Code Online (Sandbox Code Playgroud)
路线step1:
Route::post('/conference/{id}/{slug?}/registration/storeRegistrationInfo', [
'uses' => 'RegistrationController@storeRegistrationInfo',
'as' =>'conferences.storeRegistrationInfo'
]);
Run Code Online (Sandbox Code Playgroud)
路线step2:
Route::post('/conference/{id}/{slug?}/registration/storePaymentMethods', [
'uses' => 'RegistrationController@storePaymentMethods',
'as' =>'conferences.storePaymentMethods'
]);
Run Code Online (Sandbox Code Playgroud)
正如您所怀疑的那样,为了更好地构建代码和分离逻辑(DRY 概念),您应该做几件事:
使用 3 个函数创建PaymentsController(首先,您肯定会在此处添加更多函数)
对于如何命名这些函数,您完全自由,但我建议使用诸如processViaReferences,processViaCreditCard和 之类的名称storePaymentMethod。
如果将它们分开,那么阅读和验证会更容易。
为它们创建相应的帖子路由并在代码中适当引用。
创建仅包含您的表单的零件视图。
然后包括这些零件视图,@include以使其在视图级别上分离。对于 javascript/jQuery 中的引用,请记住保留它们的正确 ID。如果您还没有这样做,您也可以将 javascript 提取到其他视图、部件视图或单独的 js 文件。
您的视图/表单代码应该转到这些零件视图...