获得验证以在Laravel中工作

时间:2019-05-20 05:08:46

标签: laravel laravel-5 laravel-5.8

我正在创建一个页面,在用户提交之前,他们需要添加其地址。

例如:用户转到订单确认单,而他们忘记添加地址,而是在用户单击“付款/收款单选按钮和付款选项单选按钮”时填写了页面的其余部分。确认按钮,页面需要返回并出现错误,提示您未输入地址。

页面的地址部分只有一个“添加地址”按钮,可将用户带到表单中输入其地址。

有没有办法做到这一点。我以为验证可能会奏效,但在这种情况下我认为我做的不正确。

我有一个隐藏的表单,它可以获取一些信息,并且其中包含一个地址字段,以便将其传递到控制器中,以便我可以保存它,但这就像它没有捡起

我的order-confirmation.blade.php

@extends('layouts.public')
@section('content')
    <div class="content_wrapper">
        <h1>Order Confirmation</h1>
        <?php
            $delivery = getDeliveryFee();
        ?>

        {{ $invoice_number }}
        <div class="row">
            <div class="col-lg-12">
                <div class="row">
                    <div class="col-lg-12 mt-15">
                        @if($message = Session::get('success'))
                            <div class="alert alert-success" role="alert">
                                {{ $message }}

                                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                    <span aria-hidden="true">&times;</span>
                                </button>
                            </div>
                        @endif

                        @if($message = Session::get('error'))
                            <div class="alert alert-danger" role="alert">
                                {{ $message }}

                                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                    <span aria-hidden="true">&times;</span>
                                </button>
                            </div>
                        @endif
                    </div>
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-lg-12">
                <div class="accordion" id="accordionExample">
                    <div class="card">
                        <div class="card-header" id="headingOne">
                            <h2 class="mb-0">
                                <button class="btn btn-link" type="button" data-toggle="collapse" data-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
                                    Billing Information 
                                </button>
                            </h2>
                        </div>

                        <div id="collapseOne" class="collapse {{ !$errors->any() ? 'show' : '' }}" aria-labelledby="headingOne" data-parent="#accordionExample">
                            <div class="card-body">
                                <div class="row">
                                    <div class="col-lg-6">
                                        <div class="address">
                                            @if ($errors->confirmation_errors->has('delivery_address'))
                                                <div class="help-block text-danger">
                                                    <strong>Please add an address NOW</strong>
                                                </div>
                                            @endif

                                            @foreach($addresses as $address)
                                                @if(!empty($address->complex))
                                                    {{ $address->complex }},
                                                @endif
                                                <div>{{ $address->address }},</div>
                                                <div>{{ $address->suburb }},</div>
                                                <div>{{ $address->city }},</div>
                                                <div>{{ $address->province }},</div>
                                                <div>{{ $address->postal_code }}</div>

                                                <div class="row edit-delete">
                                                    <div class="col-lg-2">
                                                        <a class="btn btn-primary edit-button" href="{{ route('account.edit.delivery.address', [$address->id]) }}">Edit</a> 
                                                        <span>/</span>
                                                    </div>

                                                    <div class="col-lg-2">
                                                        <form action="{{ route('account.delete.delivery.address', [$address->id]) }}" method="post">
                                                            @csrf
                                                            {{ method_field('DELETE') }}
                                                            <button class="btn btn-danger delete-button"><i class="fa fa-pencil"></i> Delete</button>
                                                        </form>
                                                    </div>
                                                </div>
                                            @endforeach
                                        </div>
                                    </div>

                                    <div class="col-lg-6">
                                        <a href="{{ route('account.add.address') }}" class="btn btn-dark float-right mt-15">Add Address</a>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="card">
                        <div class="card-header" id="headingTwo">
                            <h2 class="mb-0">
                                <button class="btn btn-link collapsed" id="delivery-collection" type="button" data-toggle="collapse" data-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
                                    Delivery/Collection
                                </button>
                            </h2>
                        </div>

                        <div id="collapseTwo" class="collapse {{ $errors->confirmation_errors->any() ? 'show' : '' }}" aria-labelledby="headingTwo" data-parent="#accordionExample">
                            <div class="card-body">
                                <p>
                                    Please select your delivery option
                                </p>

                                @if ($errors->confirmation_errors->has('delivery_collection'))
                                    <div class="help-block text-danger">
                                        <strong>Please select your delivery option</strong>
                                    </div>
                                @endif

                                <div class="delivery-option">
                                    <input type="radio" class="form-check-input {{ $errors->confirmation_errors->has('delivery_collection') ? 'is-invalid' : '' }}" name="delivery-option" id="delivery" value="delivery">
                                    <label for="delivery" class="form-check-label">
                                        Delivery
                                    </label>

                                    <input type="radio" class="form-check-input {{ $errors->confirmation_errors->has('delivery_collection') ? 'is-invalid' : '' }}" name="delivery-option" id="collection" value="collection">
                                    <label for="collection" class="form-check-label">
                                        Collection
                                    </label>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="card">
                        <div class="card-header" id="headingThree">
                            <h2 class="mb-0">
                                <button class="btn btn-link collapsed" type="button" data-toggle="collapse" data-target="#collapseThree" aria-expanded="false" aria-controls="collapseThree">
                                    Payment Options
                                </button>
                            </h2>
                        </div>

                        <div id="collapseThree" class="collapse {{ $errors->any() ? 'show' : '' }}" aria-labelledby="headingThree" data-parent="#accordionExample">
                            <div class="card-body">
                                @if ($errors->has('payment_option'))
                                    <div class="help-block text-danger">
                                        <strong>Please select a payment option</strong>
                                    </div>
                                @endif

                                <div class="row">
                                    <div class="col-lg-12">
                                        <div class="payment-option">
                                            <input type="radio" class="form-check-input {{ $errors->has('payment_method') ? 'is-invalid' : '' }}" name="payment_method" id="payfast-eft" value="payfast-eft">
                                            <label for="payfast-eft" class="form-check-label">
                                                EFT with PayFast
                                            </label>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="card">
                        <div class="card-header" id="headingThree">
                            <h2 class="mb-0">
                                <button class="btn btn-link collapsed" type="button" data-toggle="collapse" data-target="#collapseFour" aria-expanded="false" aria-controls="collapseThree">
                                    Review Your Order
                                </button>
                            </h2>
                        </div>

                        <div id="collapseFour" class="collapse" aria-labelledby="collapseFour" data-parent="#accordionExample">
                            <div class="card-body">
                                <table class="table table-bordered">
                                    <thead>
                                        <tr>
                                            <th scope="col">Product</th>
                                            <th scope="col">Code</th>
                                            <th scope="col">Quantity</th>
                                            <th scope="col">Unit Price</th>
                                            <th scope="col">Total</th>
                                        </tr>
                                    </thead>

                                    <tbody>
                                        @foreach($products as $product)
                                            <?php
                                                $image = getImagesArray($product['item']['image']);
                                            ?>
                                            <tr>
                                                <th>
                                                    @if(!empty($image))
                                                        <img src={!! asset("product_images/thumbs/$image[0]") !!}>
                                                    @endif

                                                    {{ $product['item']['title'] }}
                                                </th>
                                                <td>{{ $product['item']['supplier_code'] }}</td>
                                                <td>{{ $product['qty'] }}</td>
                                                <td>R {{ $product['item']['price'] }}</td>
                                                <td>R {{ $product['price'] }}</td>
                                            </tr>
                                        @endforeach

                                        <tr>
                                            <th colspan="4">
                                                <div class="float-right">
                                                    Sub Total       
                                                </div>
                                            </th>

                                            <td id="totalPrice" data-price="{{ $totalPrice }}">
                                                R {{ $totalPrice }}
                                            </td>
                                        </tr>

                                        <tr class="delivery-fees">
                                            <th colspan="4">
                                                <div class="float-right">
                                                    Delivery Fee
                                                </div>
                                            </th>

                                            <td id="delivery-price" data-price="{{ $delivery }}">
                                                R {{ $delivery }}
                                            </td>
                                        </tr>

                                        <?php
                                            $total = $totalPrice + $delivery;
                                        ?>

                                        <tr class="total-price">
                                            <th colspan="4">
                                                <div class="float-right">
                                                    Total:      
                                                </div>
                                            </th>

                                            <td>
                                                R <span id="completePrice"></span>
                                            </td>
                                        </tr>
                                    </tbody>
                                </table>

                                <div class="confirm-order-btn pb-15">
                                    @foreach($products as $product)
                                        <!-- BEGIN PAYFAST EFT -->
                                        <div class="payfast-eft" style="display: none">
                                            <form action="{{ route('payment.gateway') }}" method="POST">
                                                @csrf
                                                <input type="hidden" name="merchant_id" value="merchant_id">
                                                <input type="hidden" name="merchant_key" value="merchant_key">
                                                <input type="hidden" name="return_url" value="{{ route('payfast.success') }}">
                                                <input type="hidden" name="cancel_url" value="{{ route('payfast.cancel') }}"> 
                                                <input type="hidden" name="m_payment_id" value="{{ $invoice_number }}">
                                                <input type="hidden" name="amount" class="completePrice" value="">
                                                <input type="hidden" name="item_name" value="{{ $product['item']['title'] }}">
                                                <input type="hidden" name="item_description" value="{{ $product['item']['description'] }}">
                                                <input type="hidden" name="email_confirmation" value="1">
                                                <input type="hidden" name="confirmation_address" value="">
                                                <input type="hidden" name="payment_method" value="payfast_eft">
                                                <input type="hidden" name="delivery_collection" class="delivery_collection" value="">
                                                <input type="hidden" name="delivery_fee" class="delivery_fee" value="{{ $delivery }}">
                                                <!-- THIS IS WHERE THE ADDRESS IS ADDED TO THE HIDDEN FORM -->
                                                <input type="hidden" name="delivery_address" class="delivery_address" value="{{ $address }}">

                                                <?php
                                                    $success = url('payfast-success');
                                                    $cancel = url('payfast-cancel');
                                                    $notify = url('payfast-notify');

                                                    $original_str = getAscii('merchant_id=merchant_id&merchant_key=merchant_key&return_url='.$success.'&cancel_url='.$cancel.'&notify_url='.$notify.'&m_payment_id=01AB&amount='.$totalPrice.'&item_name=Test Item&item_description=A test product&email_confirmation=1&confirmation_address=email@domain.com&payment_method=eft');
                                                    $hash_str = hash('MD5', $original_str);
                                                    $hash = strtolower($hash_str);
                                                ?>

                                                <input type="hidden" name="signature" value="{{ $hash }}">

                                                <button type="submit" class="btn btn-success float-right confirm-payfast-order">
                                                    Confirm Order
                                                </button>
                                            </form>
                                        </div>
                                        <!-- END PAYFAST EFT -->
                                    @endforeach
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script>
        $(document).ready(function(){
            var price = $("#totalPrice").data('price'); //get data-price by this syntax

            $('#completePrice').html(price);
            $('.completePrice').val(price);

            $('input[type="radio"]').click(function(){
                if($(this).attr("value")=="collection"){
                    $(".delivery-fees").hide('slow');

                    var price = $("#totalPrice").data('price'); //get data-price by this syntax
                    var deliveryprice = 0;  //get data-price by this syntax
                    var totalPrice = parseFloat(price) + parseFloat(deliveryprice);

                    $('#completePrice').html(totalPrice);
                    $('.completePrice').val(totalPrice);
                    $('.delivery_collection').val('collection');
                    $('.confirm-order').removeAttr('disabled');
                    $('.confirm-payfast-order').removeAttr('disabled');
                }

                if($(this).attr("value")=="delivery"){
                    $(".delivery-fees").show('slow');

                    var price = $("#totalPrice").data('price'); //get data-price by this syntax
                    var deliveryprice = $("#delivery-price").data('price');  //get data-price by this syntax
                    var totalPrice = parseFloat(price) + parseFloat(deliveryprice);

                    $('#completePrice').html(totalPrice);
                    $('.completePrice').val(totalPrice);
                    $('.delivery_collection').val('delivery');
                }
            });

            /* BEGIN EFT PAYFAST */
            $('input[type="radio"]').click(function(){
                if($(this).attr("value")=="payfast-eft"){
                    $(".payfast-eft").show('slow');
                    $(".payfast-card").hide();
                    $(".i-pay").hide();
                    $(".confirm-order").hide();
                    $(".payfast-debit-card").hide();
                }       
            });
            /* END EFT PAYFAST */
        });
    </script>   
@stop

我的控制器功能

public function paymentGateway(Request $request)
{

    if($request->payment_method == 'payfast_eft')
    {
        $process = 'Order Paid';
        $paid = '1';
    }

    if($request->delivery_collection == 'collection')
    {
        $delivery_fee = null;
    }else{
        $delivery_fee = $request->delivery_fee;
    }

    $orders = Order::all();

    $oldCart = Session::get('cart');
    $cart = new Cart($oldCart);

    $validation = Validator::make($request->all(), $this->getRules());

    if($validation->fails())
    {
        return redirect()->route('cart.deliveryConfirmation')
            ->withErrors($validation, 'confirmation_errors')
            ->with('error', 'There were validation errors');
    }

    foreach($orders as $order)
    {
        $order = Order::find($order->id)->where('invoice_number', $request->m_payment_id)->first();

        $order->cart = serialize($cart);
        $order->address = $request->delivery_address;
        $order->delivered_date = null;
        $order->delivery_fee = $delivery_fee;
        $order->delivery_option = $request->delivery_collection;
        $order->process = $process;
        $order->order_date = Carbon::now('+2:00');
        $order->payment_method = $request->payment_method;
        $order->paid = $paid;
        $order->order_price = $request->amount;

        $order->save();
    }

    $merchant_id = $request->merchant_id;
    $merchant_key = $request->merchant_key;
    $return_url = $request->return_url;
    $cancel_url = $request->cancel_url;
    $m_payment_id = $request->m_payment_id;
    $amount = $request->amount;
    $item_name = $request->item_name;
    $item_description = $request->item_description;
    $email_confirmation = '1';
    $confirmation_address = 'email@domain.com';
    $payment_method = $request->payment_method;
    $signature = $request->signature;

    if($request->payment_method == 'payfast_eft')
    {
        $url = 'https://sandbox.payfast.co.za/eng/process?merchant_id='.$merchant_id.'&merchant_key='.$merchant_key.'&return_url='.$return_url.'&cancel_url='.$cancel_url.'&m_payment_id='.$m_payment_id.'&amount='.$amount.'&item_name='.$item_name.'&item_description='.$item_description.'&email_confirmation='.$email_confirmation.'&confirmation_address='.$confirmation_address.'&payment_method='.$payment_method;
    }

    return redirect()->to($url);
}

protected function getRules()
{
    return [
        'delivery_collection' => 'required',
        'payment_method' => 'required',
        'delivery_address' => 'required'
    ];
}

1 个答案:

答案 0 :(得分:0)

基本上在paymentGateway控制器上,如果不重定向到选择地址并将其保存在db中的上一页,则需要检查是否提供了地址