Submit POST data from controller

Viewed 118

I am trying to implement Payhere payment gateway (Local gateway based in Sri Lanka). As per their documentation we could submit data as below.

<html>
<body>
<form method="post" action="https://sandbox.payhere.lk/pay/checkout">   
    <input type="hidden" name="merchant_id" value="121XXXX">    <!-- Replace your Merchant ID -->
    <input type="hidden" name="return_url" value="http://sample.com/return">
    <input type="hidden" name="cancel_url" value="http://sample.com/cancel">
    <input type="hidden" name="notify_url" value="http://sample.com/notify">  
    <br><br>Item Details<br>
    <input type="text" name="order_id" value="ItemNo12345">
    <input type="text" name="items" value="Door bell wireless"><br>
    <input type="text" name="currency" value="LKR">
    <input type="text" name="amount" value="1000">  
    <br><br>Customer Details<br>
    <input type="text" name="first_name" value="Saman">
    <input type="text" name="last_name" value="Perera"><br>
    <input type="text" name="email" value="samanp@gmail.com">
    <input type="text" name="phone" value="0771234567"><br>
    <input type="text" name="address" value="No.1, Galle Road">
    <input type="text" name="city" value="Colombo">
    <input type="hidden" name="country" value="Sri Lanka"><br><br> 
    <input type="submit" value="Buy Now">   
</form> 
</body>
</html>

This will redirect to url "https://sandbox.payhere.lk/pay/checkout" and opens a widow to submit card details.

enter image description here

enter image description here

But what I want to do is, send related POST data from a controller without a view (Same function as above, but through controller when after an database insertion done)

Any suggestion how to do this. I have tried using HTTP client, but since above gateway redirect to external url and popsup a window that's collect data.


    $client = new GuzzleHttp\Client();
    $response = $client->request('POST', 'https://sandbox.payhere.lk/pay/checkout', [
        'form_params' => [
            "merchant_id" => '1218XXX',
            "return_url" => route('admin.dashboard'),
            "cancel_url" => 'key-app.test',
            "notify_url" => route('notify'),
            "order_id" => '1',
            "items" => 'Test',
            "currency" => 'LKR',
            "amount" => '50.00',
            "first_name" => 'Arafath',
            "last_name" => 'a',
            "email" => 'mhmaarafath@gmail.com',
            "phone" => '0765245237',
            "address" => 'Mount Lavinia',
            "city" => 'Colombo',
            "country" => 'Sri Lanka',

        ]
    ]);
    return redirect('https://sandbox.payhere.lk/pay/checkout');

1 Answers

First point you can't make a Guzzle request here since you need to open a new window here. (instead of Guzzle you can use HTTP client )

Yo can do something like below to accomplish your requirement

  1. Get all related data from db inside controller and then return a view with that data where that view has the payhere form and you can hide the form with css. payhereCheckout ()

  2. Now submit the form via JQuery or JS . What you have to do is just click form submit button via JS or JQuery. Just put a small loader or something which may show the user to that the server is processing the request.

I'll provide some codes how you need to achieve this. I have not used this process to PayHere but all the payment gateways in SL use a form submission .So that process should same.Here is the example code.

Controller

  class PayHereChekoutControler extends Controller
  {
   public function payhereCheckout () {

      $userData = UserDataModel::find(Auth::id());

      $email = $userData->email;
      $firstName = $userData->first_name;
      //Other required paramas here or just pass the collection to view and get the required feilds there.

      return view('payhere_hidden_checkout', compact(
        'first_name',
        'email',
      ));
   }

 }

web.php

Route::get('payhere-payment,'[PayHereChekoutControler::class, 'payhereCheckout']);

payhere_hidden_checkout.blade.php

<form id="payhere" style="display:none" method="post" action="https://sandbox.payhere.lk/pay/checkout">   
   
    <input type="text" name="first_name" value="{{$first_name}}">
    <input type="text" name="email" value="{{$email}}">
    <input type="submit" value="Buy Now">  

</form> 

<script>
    $(document).ready(function () { // Now the DOM is ready
        //Start loader or something here. You don't need to hide the loader because once form submitted it will redirect to payhere
        setTimeout(() => {
            $('form#payhere').submit();
        }, 500)
    });
</script>

I have not used this for Payhere but I have implemented this to WEBXPAY. Both the gateways use same process.

Hope this will make a scene to your requirement.

Related