Summer Sale! All accounts are 50% off this week.

joesty84's avatar

Laravel Use View

Hello, I have a controller page ( with some functions )

this page [With functions], checkout.blade view get method using calling. {{route('show')}};

but , some functions (checkout.blade) I get an Undefined variable ($secureamount) error.

When I run controller functions in checkout.blade. No Problem . Was Run.

My question is;

How controller page include chekout.blade ? For Example

"use checkout.blade'"

public function show()

. . . . . . . . . etc.

Thank You.

Best Regards.

0 likes
13 replies
tykus's avatar

Your question is incoherent, please organise it so we can better understand!

1 like
Snapey's avatar

controller page is not a term I recognise?

1 like
joesty84's avatar

@Tray2 Thank you for your suggestion. Situation in Question is a little different.

Tray2's avatar

@joesty84 No it's not, you are doing a lot of strange things in your controller's show method. And the strangest of them all is that you make a post request inside the show method, and show is supposed to fetch a records from the database, and pass it to the view.

If you watch the free series you will learn that you need to pass the data to the view, and you don't do that at all.

 if($response->json('status') === 'success') {
        return view('front.checkout', [
            'iframeSrc' => sprintf('wwwcom/pay/%s', $response->json('token')),
        ]);   
        return view('front.checkout', compact('iframeSrc'));
    }

Any variable you want to use in your view, you need to pass to it, and you don't pass any $secureamount in your method.

In short, your code is a bloody mess, and you do things you should never do in a get.

1 like
joesty84's avatar

Controller:

namespace App\Http\Controllers;

use Illuminate\Http\Response as LaravelResponse; use Illuminate\Http\Request; use App\Http\Controllers\Controller; ............

class PayController extends Controller {

public function show()
{


                    
						$merchantId = '------';
                        $merchantKey = '------';
                        $merchantSalt = '------';
                        $email = 'Auth::user()->email';
                        $paymentAmount = Crypt::decrypt($secureamount)*"100";
                        $merchantOid = '$random_id';
                        $userName = $request->user()->fname.' '.$request->user()->lname;
                        $userAddress = $request->user()->address;
                        $userPhone = $request->user()->mobile;
                        $merchantOkUrl = route("success");
                        $merchantFailUrl = route("fail");
                        $userBasket = '';
                        $timeoutLimit = '30';
                        $debugOn = 1;
                        $testMode = 0;
                        $noInstallment = 0;
                        $maxInstallment = 0;
                        $currency = 'GBP';
                        $userIp = $request->ip();
						
						$hashStr = $merchantId . $userIp . $merchantOid . $email . $paymentAmount . $userBasket 
                . $noInstallment . $maxInstallment . $currency . $testMode;
    $stToken = base64_encode(hash_hmac('sha256', $hashStr . $merchantSalt, $merchantKey, true));
    
    $response = Http::timeout(20)
        ->withOptions([
            'verify' => App::environment('production'),
            'allow_redirects' => true,
            'curl' => [
                CURLOPT_FRESH_CONNECT => true,
            ],
        ])->post('wwwcom/api/get-token', [
            'merchant_id' => $merchantId,
            'user_ip' => $userIp,
            'merchant_oid' => $merchantOid,
            'email' => $email,
            'payment_amount' => $paymentAmount,
            'st_token' => $stToken,
            'user_basket' => $userBasket,
            'debug_on' => $debugOn,
            'no_installment' => $noInstallment,
            'max_installment' => $maxInstallment,
            'user_name' => $userName,
            'user_address' => $userAddress,
            'user_phone' => $userPhone,
            'merchant_ok_url' => $merchantOkUrl,
            'merchant_fail_url' => $merchantFailUrl,
            'timeout_limit' => $timeoutLimit,
            'currency' => $currency,
            'test_mode' => $testMode,
        ])->throw(function (Response $response, RequestException $e) {
         
            return back()->withErrors($e->getMessage());
        });
        $iframeSrc=$this->iframeSrc;
        

    if($response->json('status') === 'success') {
        return view('front.checkout', [
            'iframeSrc' => sprintf('wwwcom/pay/%s', $response->json('token')),
        ]);   
        return view('front.checkout', compact('iframeSrc'));
    }

   
    return back()->withErrors($response->json('reason'));
						
}

}

Route;

Route::get('gotocheckout', [PayController::class, 'show'])->name('show');

checkout.blade.php;

					    <meta charset="UTF-8">
					    <div class="card-header"><h1>Credit / Debit Card</h1></div>
					    <div class="card-body">
					    <div style="width: 100%;margin: 0 auto;display: table;">
					     {{route('show')}};
				        
						<script src="wwwcom/js/iframeResizer.min.js"></script>
						<iframe src="{{ $iframeSrc }}" id="payiframe" frameborder="0"scrolling="no" style="width: 100%;"></iframe>
						<script>iFrameResize({},'#payiframe');</script>
						</div>
					    </div>
						</div>																																																																																								

1 First question is, when I run the get part in the route here, one.

Undefined variable ($secureamount) error.

When I write the information in the controller directly in blade, ($secureamount) works.

How can I achieve this in controller? (How can I include the checkout.blade template in the controller?

2 Second question is; {{ $iframeSrc }} This part is required in the controller. Likewise, when I create it as above; Undefined variable ($iframeSrc) error. How can I pull this function from the Controller to blade?

arthvrian's avatar

You MUST set $secureamount value

$paymentAmount = Crypt::decrypt($secureamount)*"100";

BEFORE calling it, maybe through $request->input('secureamount') or $request->post('secureamount') or whatever

You set $iframeSrc=$this->iframeSrc; but never passed to the view and return view('front.checkout', compact('iframeSrc')); is never reached

1 like
joesty84's avatar

@arthvrian

Hello, I think it's done, the last step is left. When you use the return section under the show function in the controller, it prints token on the screen. but normally checkout.blade continues. Since this 'token' is important, I need to place it in the $token section under the route(show).

How can I place (add) 'token' created in the controller into the blade? .

The route, blade, and class are shown below.

Best Regards

Checkout.blade.php;

					    <meta charset="UTF-8">
					    <div class="card-header"><h1>Credit / Debit Card</h1></div>
					    <div class="card-body">
					    <div style="width: 100%;margin: 0 auto;display: table;">
					     {{route('show')}};
				        
						<script src="wwwcom/js/iframeResizer.min.js"></script>
						<iframe src="wwwcom/pay/{{$token}}" id="payiframe" frameborder="0"scrolling="no" style="width: 100%;"></iframe>
						<script>iFrameResize({},'#payiframe');</script>
						</div>
					    </div>
						</div>		

Route;

Route::get('gotocheckout', [PayController::class, 'show'])->name('show');

Controller;

$result=json_decode($result,1);
						
			    		if($result['status']=='success')
						$token=$result['token'];
			    		else
						die("IFRAME failed. reason:".$result['reason']);
						#########################################################################
				
		
        return view("front.checkout",compact("token"));
jaseofspades88's avatar

Imagine, writing code like that, being 'happy' with it and then expecting people to invest time in trying to wade through it to try and make heads or tails of it... format your code properly and explain what problem you're actually trying to fix...

joesty84's avatar

@jaseofspades88 I know now bad code, but I'm writing about this situation in case someone can help me. Really saddens me that you wrote like this.

Please or to participate in this conversation.