Nikki
7 months ago

Getting the invoice number to go to cancel function

Posted 7 months ago by Nikki

I'm hoping someone can help me and tell me if I'm going in the right direction or help me out. I'm creating an online shop and I'm using payfast as a payment gateway. Before I had a problem with getting my information saved upon success, but someone suggested that I rather post to a controller and then make that controller go to the url. I did that and it's working and saving when I click confirm. The problem now is that if someone clicks on cancel it still gets saved and I would like that if the user ends up cancelling and going to the cancel page then it deletes the order from the database. I can't see how I can delete the data when a user clicks cancel.

This is my code for both the payPayfast method where I save the data and my cancelPayfast where I would like to delete the data

public function payPayfast(Request $request)
{

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

    $order = new Order();
    $order->cart = serialize($cart);
    $order->delivered_date = null;
    $order->process = 'Order paid';
    $order->order_date = Carbon::now('+2:00');
    $order->paid = '1';
    $order->order_price = $request->amount;

    Auth::user()->orders()->save($order);

    $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 = 'Test Item From Controller';
    $item_description = 'This is a test product';
    $email_confirmation = '1';
    $confirmation_address = '[email protected]';
    $payment_method = $request->payment_method;
    $signature = $request->signature;

    $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);
}

public function cancelPayfast()
{
    $menus_child = Menu::where('menu_id', 0)->with('menusP')->get();
    $contacts = Contact::all();

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

    if(Session::has('cart')){
        Order::find()->delete(); //This is where I'm stuck
    }

    Category::find($id)->delete();

    return view('public.payfast.cancel', compact('menus_child', 'contacts'));
}

and this is my confirmation page. Sorry it's long but I thought it would be better to have all of it there

@extends('layouts.public')
@section('content')
    <div class="content_wrapper">
        <h1>Order Confrimation</h1>

        <?php
            $delivery = getDeliveryFee();
        ?>

        <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 show" aria-labelledby="headingOne" data-parent="#accordionExample">
                            <div class="card-body">
                                <div class="row">
                                    <div class="col-lg-6">
                                        <div class="address">
                                            <div>Unit 39 La Monte,</div>
                                            <div>28 Parklands Main Road,</div>
                                            <div>Parklands,</div>
                                            <div>Cape Town,</div>
                                            <div>Western Cape,</div>
                                            <div>7441</div>

                                            <div class="edit-delete">
                                                <a href="#">Edit</a> /
                                                <a href="#">Delete</a>
                                            </div>
                                        </div>
                                    </div>

                                    <div class="col-lg-6">
                                        <a href="#" 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" type="button" data-toggle="collapse" data-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
                                    Delivery/Collection
                                </button>
                            </h2>
                        </div>

                        <div id="collapseTwo" class="collapse" aria-labelledby="headingTwo" data-parent="#accordionExample">
                            <div class="card-body">
                                <p>
                                    Please select your delivery option
                                </p>

                                <div class="delivery-option">
                                    <div class="form-check">
                                        <input type="radio" class="form-check-input" name="delivery-option" id="delivery" value="delivery">
                                        <label for="delivery" class="form-check-label">
                                            Delivery
                                        </label>
                                    </div>

                                    <div class="form-check">
                                        <input type="radio" class="form-check-input" name="delivery-option" id="collection" value="collection">
                                            <label for="collection" class="form-check-label">
                                                Collection
                                            </label>
                                    </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="#collapseThree" aria-expanded="false" aria-controls="collapseThree">
                                    Payment Options
                                </button>
                            </h2>
                        </div>

                        <div id="collapseThree" class="collapse" aria-labelledby="headingThree" data-parent="#accordionExample">
                            <div class="card-body">
                                <div class="payment-option">
                                    <div class="form-check">
                                        <input type="radio" class="form-check-input" name="payment-option" id="payfast-eft" value="payfast-eft">
                                        <label for="payfast-eft" class="form-check-label">
                                            EFT with PayFast
                                        </label>
                                    </div>

                                    <div class="form-check">
                                        <input type="radio" class="form-check-input" name="payment-option" id="cod" value="cod">
                                        <label for="cod" class="form-check-label">
                                            Cash on Delivery
                                        </label>
                                    </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)
                                            <tr>
                                                <th>{{ $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>

                                    <div class="confirm-order-btn pb-15">
                                        <div class="payfast-eft" style="display: none">

                                            <form action="{{ route('payfast.payPayfast') }}" method="POST">
                                                @csrf
                                                <input type="hidden" name="merchant_id" value="12345678">
                                                <input type="hidden" name="merchant_key" value="1234567891234">
                                                <input type="hidden" name="return_url" value="{{ route('payfast.success') }}">
                                                <input type="hidden" name="cancel_url" value="{{ route('payfast.cancel') }}"> <!-- PASS THE INVOICE NUMBER THRU CANCEL ROUTE -->
                                                <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="Test Item">
                                                <input type="hidden" name="item_description" value="A test product">
                                                <input type="hidden" name="email_confirmation" value="1">
                                                <input type="hidden" name="confirmation_address" value="">
                                                <input type="hidden" name="payment_method" value="eft">

                                                <?php
                                                    $success = url('payfast-success');
                                                    $cancel = url('payfast-cancel');
                                                    $notify = url('payfast-notify');
                                                    $original_str = getAscii('merchant_id=12345678&merchant_key=1234567891234&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&[email protected]&payment_method=eft');
                                                    $hash_str = hash('MD5', $original_str);
                                                    $hash = strtolower($hash_str);
                                                ?>

                                                <input type="hidden" name="signature" value="{{ $hash }}">
                                                <div><input type="submit" value="submit" name="submit" /></div>
                                            </form>
                                        </div>

                                        <button class="btn btn-success float-right confirm-order">
                                            Confirm Order
                                        </button>
                                    </div>
                                </div>
                            </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script>
    $(document).ready(function(){

        var price = $("#totalPrice").data('price');

        $('#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'); 
                var deliveryprice = 0; 
                var totalPrice = parseFloat(price) + parseFloat(deliveryprice);

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

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

                var price = $("#totalPrice").data('price'); 
                var deliveryprice = $("#delivery-price").data('price'); 
                var totalPrice = parseFloat(price) + parseFloat(deliveryprice);

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

            }        
        });

        /* 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();
            }        
        });
        /* END EFT PAYFAST */
    });
    </script>   
@stop

I did try replacing

<input type="hidden" name="cancel_url" value="{{ route('payfast.cancel') }}">

with this

<input type="hidden" name="cancel_url" value="{{ route('payfast.cancel', [$invoice_number]) }}">

and having my web route look like this

Route::get('/payfast-cancel/{invoice_number}', '[email protected]')->name('payfast.cancel');

But I got this error

Missing required parameters for [Route: payfast.cancel] [URI: payfast-cancel/{invoice_number}].

Please sign in or create an account to participate in this conversation.