我有一个用户在会议中注册的多步骤表单,所有步骤都在相同的registration.blade.php
页面中,在步骤1和步骤2完成了一个ajax请求,以验证表单字段。
具体步骤如下:
我的怀疑是在第二步和第三步之间。
使用引用处理支付的代码:
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
}
如果选择的付款是信用卡,则需要执行以下代码对信用卡进行收费,然后在payments表中插入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
}
我的疑问是流程应该是怎样的,处理引用支付的代码和处理信用卡支付的代码应该放在哪里。所以有可能实现这个场景:
现在,我只需要让步骤1和步骤2正常工作。为了处理step1和step2,我使用了RegistrationController
。在step1中,使用对RegistrationController
的storeUserInfo()
方法的ajax post请求来验证用户引入的信息。如果返回200
,则用户转到step2。
在step2中,用户选择支付方法,然后单击"go to step 3“也会向RegistrationController
的storePaymentMethods()
发出ajax请求,以验证用户是否选择了至少一种支付方法。我的疑问是,在此方法返回代码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);
}
我现在使用的多步表单的注册流程的完整摘要:
因此,对于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>
step1图片更好地解释:
当用户单击"Go to step 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
}
});
});
然后在ConferencesController中有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);
}
因此,如果返回代码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>
step2图片更好地解释:
当用户点击"Go to step 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
}
});
});
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);
}
然后是step3 div。在step3目录中,根据上一步中选择的付款方式(信用卡或参考资料),将显示#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>
为了更好地解释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>
//继续我现在拥有的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);
}
}
step1
的路由
Route::post('/conference/{id}/{slug?}/registration/storeRegistrationInfo', [
'uses' => 'RegistrationController@storeRegistrationInfo',
'as' =>'conferences.storeRegistrationInfo'
]);
step2
的路由
Route::post('/conference/{id}/{slug?}/registration/storePaymentMethods', [
'uses' => 'RegistrationController@storePaymentMethods',
'as' =>'conferences.storePaymentMethods'
]);
发布于 2018-05-27 08:25:57
正如你所怀疑的那样,为了更好地组织你的代码和分离逻辑(DRY概念),你应该做一些事情:
1. for processing your references
2. for processing credit cards
3. for storing payment methods (store payment method in session, until user completes step3)
你完全可以自由地决定如何命名这些函数,但我建议使用processViaReferences
、processViaCreditCard
和storePaymentMethod
之类的函数。
如果你把它们分开,它将更容易阅读和验证。
在cour代码中为它们创建相应的post路由并进行适当的引用。
然后,通过@include
包含这些零件视图,以便在视图级别上将其分开。对于您在javascript/jQuery中的引用,请记住保留它们的正确ids。你可以提取javascript到其他视图部件视图或者单独的js文件中,如果你还没有这样做的话。
您的视图/表单代码应转到这些部件视图...
发布于 2018-05-27 01:53:05
没有明确的说明,你需要自己决定如何组织你的代码。
如果您不能准确地选择在何处放置负责付款的PHP代码,那么我将把两种付款的处理放在PaymentsController.php中。
因此,很明显,在选择付款方法后,您需要在数据库中形成类似于订单的内容。
https://stackoverflow.com/questions/50491553
复制相似问题