Laravel Payment Gateway intregration error

380 views Asked by At

I am getting this error while trying to integrate Phonepe with Laravel.

Undefined property: stdClass::$data

Please provide a solution for the problem or suggest the best Laravel package for integrating the PhonePe gateway.

I have used the below code,

data = array (
    'merchantId' => 'MERCHANTUAT',
    'merchantTransactionId' => uniqid(),
    'merchantUserId' => 'M1V4WG0RLQS6',
    'amount' => 1,
    'redirectUrl' => route('response'),
    'redirectMode' => 'POST',
    'callbackUrl' => route('response'),
    'mobileNumber' => '7708325543',
    'paymentInstrument' =>
        array (
            'type' => 'PAY_PAGE',
        ),
);
$encode = base64_encode(json_encode($data));
$saltKey = '9bbeca7d-96b9-4b59-b5ee-2ffa5d0763e9';
$saltIndex = 1;
$string = $encode.'/pg/v1/pay'.$saltKey;
$sha256 = hash('sha256',$string);
$finalXHeader = $sha256.'###'.$saltIndex;
$url = "https://api-preprod.phonepe.com/apis/pg-sandbox/pg/v1/pay"; 
$response = Curl::to($url)
    ->withHeader('Content-Type:application/json')
    ->withHeader('X-VERIFY:'.$finalXHeader)
    ->withData(json_encode(['request' => $encode]))
    ->post();

$rData = json_decode($response);

return redirect()->to($rData->data->instrumentResponse->redirectInfo->url);data = array (
    'merchantId' => 'MERCHANTUAT',
    'merchantTransactionId' => uniqid(),
    'merchantUserId' => '5555555',
    'amount' => 1,
    'redirectUrl' => route('response'),
    'redirectMode' => 'POST',
    'callbackUrl' => route('response'),
    'mobileNumber' => '11111111',
    'paymentInstrument' =>
        array (
            'type' => 'PAY_PAGE',
        ),
);
$encode = base64_encode(json_encode($data));
$saltKey = '9bbeca7d-96b9-4b59-b5ee-2ffa5d079';
$saltIndex = 1;
$string = $encode.'/pg/v1/pay'.$saltKey;
$sha256 = hash('sha256',$string);
$finalXHeader = $sha256.'###'.$saltIndex;
$url = "https://api-preprod.phonepe.com/apis/pg-sandbox/pg/v1/pay";    
$response = Curl::to($url)
    ->withHeader('Content-Type:application/json')
    ->withHeader('X-VERIFY:'.$finalXHeader)
    ->withData(json_encode(['request' => $encode]))
    ->post();    
$rData = json_decode($response);    
return redirect()->to($rData->data->instrumentResponse->redirectInfo->URL);

Above is code for the PhonePe integration in Laravel.

2

There are 2 answers

0
Mit Kathrotia On

You are getting the error Undefined property: stdClass::$data because you have defined the variable as data instead of $data.

define your variable like below,

$data = array (
    'merchantId' => 'MERCHANTUAT',
    'merchantTransactionId' => uniqid(),
    'merchantUserId' => 'M1V4WG0RLQS6',
    'amount' => 1,
    'redirectUrl' => route('response'),
    'redirectMode' => 'POST',
    'callbackUrl' => route('response'),
    'mobileNumber' => '7708325543',
    'paymentInstrument' =>
        array (
            'type' => 'PAY_PAGE',
        ),
);
0
Shahrukh Hussain On

You should first check the response of the $rData variable to ensure you are receiving the correct data.