samedi 20 juillet 2019

How to send post data using CURL POST to third party payment gateway in laravel 5?

I'm trying to integrate a payment gateway for my eCommerce website.How do I send form data to their API ? This is what they are requesting from us.

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

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

`
}

?>

I've tried this on my laravel application using a Payment Controller. Get data from vuejs form and send data to laravel controller.

This is my code inside the 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);


}

I expected to integrate the given payment gateway, but the output is 'undefined'.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire