这个背景的流程应该如何?(根据所选的付款方式,将代码放在哪里以处理付款?
我有一个多步骤表单供用户在会议中注册,所有步骤都在同一页面中,在步骤1和步骤2中完成ajax请求以验证表单字段。registration.blade.php
步骤如下:
- 步骤1是收集有关用户的一些信息(名称和锡号))
- 第2步是收取付款方式(信用卡或参考)
- 第3步是付款
- 步骤 4 显示成功消息(此步骤仅在使用信用卡成功付款后显示,使用参考付款方式时,此步骤不显示,使用引用付款方式时仅显示步骤 1、2 和 3)
我的疑问是在步骤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
}
如果所选付款是信用卡,则需要执行以下代码以从信用卡中扣款,然后将其插入付款表中,然后,和(已付款)。然后,应将用户重定向到步骤 4 以显示成功消息:price
payment_method_id
registration_id
status
处理信用卡付款的代码:
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正常工作。为了处理步骤1和步骤2,我有.在步骤 1 中,用户引入的信息使用 ajax post 请求验证,方法是 if 返回用户转到步骤 2。RegistrationController
storeUserInfo()
RegistrationController
200
在步骤2中,用户选择支付方式并在“转到步骤3”中点击一下也完成了ajax请求,以验证用户是否选择了至少1种支付方式。我怀疑在此方法返回代码200之后,该过程应该如何。storePaymentMethods()
RegistrationController
根据付款方式,有必要运行上面的相应代码(生成付款参考的代码或从信用卡中扣款的代码)。
因此,我怀疑如何根据控制器和方法组织此代码,将应根据所选付款方式执行的代码放在哪里。你知道要实现这一目标,流程应该如何吗?
也许一种方法可以像下面这样,但它似乎不是很正确,在这种方法中做所有事情: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);
}
我现在使用的多步骤表单注册流程的完整摘要:
因此,对于步骤1,有以下形式:
<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图片解释得更好:
当用户单击“转到步骤 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图片解释得更好:
当用户单击“转到步骤 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 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>
step3
图像以更好地解释,根据付款方式,在步骤3中显示的必要信息是不同的。如果付款方式是信用卡,则在使用信用卡成功扣款后,还会显示一个显示成功消息的 div:step4
然后是步骤4 div,在使用信用卡完成付款后,应显示成功消息:
<div id="step4">
<p>
<i class="fa fa-plus" aria-hidden="true"></i>
Payment and registration completed with success.
</p>
</div>
我现在拥有的注册控制器的方法的恢复,注册控制器是我拥有的处理多步表单的控制器
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'
]);