【问题标题】:Submit POST data from controller从控制器提交 POST 数据
【发布时间】:2021-10-22 12:17:34
【问题描述】:

我正在尝试实施 Payhere 支付网关(位于斯里兰卡的本地网关)。根据他们的文档,我们可以提交如下数据。

<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>

这将重定向到网址“https://sandbox.payhere.lk/pay/checkout”并打开一个寡妇提交卡详细信息。

但我想做的是,从没有视图的控制器发送相关的 POST 数据(与上面的功能相同,但在数据库插入完成后通过控制器)

任何建议如何做到这一点。我曾尝试使用 HTTP 客户端,但由于上述网关重定向到外部 url 并弹出一个收集数据的窗口。


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

【问题讨论】:

    标签: laravel payment-gateway


    【解决方案1】:

    第一点,您不能在此处提出 Guzzle 请求,因为您需要在此处打开一个新窗口。 (你可以使用 HTTP client 代替 Guzzle)

    您可以执行以下操作来满足您的要求

    1. 从控制器内部的 db 中获取所有相关数据,然后返回包含该数据的视图,其中该视图具有 payhere 表单,您可以使用 css 隐藏表单。 payhereCheckout ()

    2. 现在通过 JQuery 或 JS 提交表单。您只需通过 JS 或 JQuery 单击表单提交按钮。 只需放置一个小型加载程序或其他可能向用户显示服务器正在处理请求的东西。

    我将提供一些您需要如何实现此目的的代码。我没有使用这个流程来 PayHere,但是 SL 中的所有支付网关都使用表单提交。所以该流程应该相同。这是示例代码。

    控制器

      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>
    

    我没有将它用于Payhere,但我已经将它用于WEBXPAY。两个网关使用相同的进程。

    希望这能满足您的要求。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-22
      • 1970-01-01
      • 2013-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多