JustPaste.it
public function verify_order()
{
if (strcasecmp($this->request->getMethod(), 'POST') === 0 ) {


$key = $this->request->getPost('key');
$salt = env('PAYU_MERCHANT_SALT');
$txnid = $this->request->getPost('txnid');
$amount = $this->request->getPost('amount');
$productInfo = $this->request->getPost('productinfo');
$status = $this->request->getPost('status');
$resphash = $this->request->getPost('hash');
//Calculate response hash to verify
$keyString = $key . '|' . $txnid . '|' . $amount . '|' . $productInfo . '||||||||||||';
$keyArray = explode("|", $keyString);
$reverseKeyArray = array_reverse($keyArray);
$reverseKeyString = implode("|",$reverseKeyArray);
$CalcHashString = strtolower(hash('sha512', $salt . '|' . $status . '|' . $reverseKeyString)); //hash without additionalcharges

//check for presence of additionalcharges parameter in response.
$additionalCharges = "";

if (isset($postdata["additionalCharges"])) {
$additionalCharges = $postdata["additionalCharges"];
//hash with additionalcharges
$CalcHashString = strtolower(hash('sha512', $additionalCharges . '|' . $salt . '|' . $status . '|' . $reverseKeyString));
}

//Do success order processing here...
if ($status == 'success' && $resphash == $CalcHashString) {
//Additional step - Use verify payment api to double check payment.
if ($this->verify_Payment($key, $salt, $txnid, $status)) {

$purchaseModel = new PurchaseModel();
$get_ins_media_id = session()->get('media_ins_id');
$payu_payment_id = $this->request->getPost('mihpayid');
$moviesModel = new MoviesModel();
$get_movie_info = $moviesModel->where('id', session()->get('media_purchase_id'))->first();

$rent_duration = $get_movie_info['rent_duration'];
if ($get_movie_info['stream_start'] >= date("Y-m-d")) {
$stream_start_date = $get_movie_info['stream_start'];
} else {
$stream_start_date = date("Y-m-d");
}
$data = array(
'stream_start_at' => $stream_start_date,
'stream_end_at' => date("Y-m-d", strtotime($stream_start_date . "+ " . $rent_duration . " days")),
'order_status' => 1,
'payu_txn_id' => $payu_payment_id
);
if ($purchaseModel->set($data)->where('id', $get_ins_media_id)->update()) {
if ($get_movie_info['stream_start'] > date("Y-m-d")) {
session()->set('prebook', true);
}
session()->remove('media_purchase_id');
session()->remove('media_ins_id');

return redirect()->to(base_u rl());

} else {
echo "update error";
die;
}

} else {
echo "Payment failed for Hash not verified...";
}

}
}else{
return redirect()->to(base_u rl());
}

}
private function verify_Payment($key, $salt, $txnid, $status)
{
$command = "verify_payment"; //mandatory parameter

$hash_str = $key . '|' . $command . '|' . $txnid . '|' . $salt;
$hash = strtolower(hash('sha512', $hash_str)); //generate hash for verify payment request

$r = array('key' => $key, 'hash' => $hash, 'var1' => $txnid, 'command' => $command);

$qs = http_build_query($r);
//for production
$wsUrl = "https://info.payu.in/merchant/postservice.php?form=2";

//for test
//$wsUrl = "https://test.payu.in/merchant/postservice.php?form=2";

try {
$c = curl_init();
curl_setopt($c, CURLOPT_URL, $wsUrl);
curl_setopt($c, CURLOPT_POST, 1);
curl_setopt($c, CURLOPT_POSTFIELDS, $qs);
curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_SSLVERSION, 6); //TLS 1.2 mandatory
curl_setopt($c, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, 0);
$o = curl_exec($c);
if (curl_errno($c)) {
$sad = curl_error($c);
throw new Exception($sad);
}

$response = json_decode($o, true);

if (isset($response['status'])) {
// response is in Json format. Use the transaction_details part for status
$response = $response['transaction_details'];
$response = $response[$txnid];

if ($response['status'] == $status) //payment response status and verify status matched
return true;
else
return false;
} else {
return false;
}
} catch (Exception $e) {
return false;
}
}