如何使用CURL POST将发布数据发送到laravel 5中的第三方支付网关?

问题描述 投票:0回答:1

我正在尝试为我的电子商务网站集成付款网关。如何将表单数据发送到他们的API?这就是他们向我们提出的要求。

<html>
<body>
    <form method="post" action="https://sandbox.payhere.lk/pay/checkout">   
        <input type="hidden" name="merchant_id" value="123456">    <!-- 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="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="[email protected]">
        <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>

<?php

    $merchant_id         = $_POST['merchant_id'];
    $order_id             = $_POST['order_id'];
    $payhere_amount     = $_POST['payhere_amount'];
    $payhere_currency    = $_POST['payhere_currency'];
    $status_code         = $_POST['status_code'];
    $md5sig                = $_POST['md5sig'];

    $merchant_secret = '123456'; // Replace with your Merchant Secret (Can be found on your PayHere account's Settings page)

    $local_md5sig = strtoupper (md5 ( $merchant_id . $order_id . $payhere_amount . $payhere_currency . $status_code . strtoupper(md5($merchant_secret)) ) );

    if (($local_md5sig === $md5sig) AND ($status_code == 2) ){
            //TODO: Update your database as payment success
    }

?>

我已经使用付款控制器在laravel应用程序上尝试过此操作。从vuejs表单获取数据并将数据发送到laravel控制器。

这是我在paymentController中的代码。

public function store(Request $request)
{
    $merchant_id = $request->input('merchant_id');
    $order_id = $request->input('order_id');
    $payhere_amount = $request->input('amount');
    $payhere_currency = $request->input('currency');
    $merchant_secret =$request->input('merchant_secret');
    $status_code ='2';
    $md5sig = strtoupper (md5 ( $merchant_id . $order_id . $payhere_amount . $payhere_currency . $status_code . strtoupper(md5($merchant_secret)) ) );

    $url="https://sandbox.payhere.lk/pay/checkout";

    $method = 'POST';

    $data = array([
            "merchant_id"=> $merchant_id,
            "order_id"=> $order_id,
            "payhere_amount" => $payhere_amount,
            "payhere_currency" => $payhere_currency,
            "merchant_secret" => $merchant_secret,
            "status_code" => $status_code,
            "md5sig" => $md5sig
    ]);

    $data_string = json_encode($data);

    $ch = curl_init($url); 
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST,$method);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
    $result = curl_exec($ch); 
    curl_close($ch);
    //print_r($result);
}

我希望集成给定的支付网关,但是输出是'undefined'。

php forms curl laravel-5 payment-gateway
1个回答
0
投票

CURL对于您要执行的操作不是一个很好的解决方案-而是创建一个额外的步骤并从表单中发布。这对我来说适合一个商人:

    $ch = curl_init();

    // Set cURL options - Use curl_setopt for greater PHP compatibility
    // Base settings
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
    curl_setopt( $ch, CURLOPT_HEADER, false );      
    curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 2 );
    curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );

    // Standard settings
    curl_setopt( $ch, CURLOPT_URL, $url );
    curl_setopt( $ch, CURLOPT_POST, count($data_string));
    curl_setopt( $ch, CURLOPT_POSTFIELDS, $data_string );

    // Execute CURL
    $response = curl_exec( $ch );

    curl_close( $ch );
© www.soinside.com 2019 - 2024. All rights reserved.