So I have an API from a Payment gateway which return all the transaction in a day which is in Json format something like this.
{
"status":"1",
"msg":"3 transactions settled on 2016-09-29",
"Txn_details":[
{
"payuid":"58800xxxxxx",
"txnid":"xxxxxx-27410",
"txndate":"2016-09-27 11:06:58",
"mode":"DC",
"amount":"15174.51",
"requestid":"xxxxxxxx",
"requestdate":"2016-09-29 18:17:18",
"requestaction":"capture",
"requestamount":"15174.51",
"mer_utr":"xxxxxxx",
"mer_service_fee":"151.75000",
"mer_service_tax":"22.76000",
"mer_net_amount":"15000.00000",
"bank_name":"VISA",
"issuing_bank":"xxxxxx",
"merchant_subvention_amount":"0.00"
},
{
"payuid":"58800xxxxxx",
"txnid":"xxxxxx-27410",
"txndate":"2016-09-27 11:06:58",
"mode":"DC",
"amount":"15174.51",
"requestid":"xxxxxxxx",
"requestdate":"2016-09-29 18:17:18",
"requestaction":"capture",
"requestamount":"15174.51",
"mer_utr":"xxxxxxx",
"mer_service_fee":"151.75000",
"mer_service_tax":"22.76000",
"mer_net_amount":"15000.00000",
"bank_name":"VISA",
"issuing_bank":"xxxxxx",
"merchant_subvention_amount":"0.00"
}
]
}
So In this Json I have to look for a value called requestid
so I have to look for that id by calling the API from the day of payment to next 7 days
and match every record of each day weather it has the requestid
. My code is like this
function recursiveUTNRcheck($date_of_payment,$payment_reference,$rec_counter=7){
$date = substr($date_of_payment, 0, 10);
$utrNos ='';$brtoggle=false;
for($i=1;$i<=$rec_counter;$i++){
$sett_date= date('Y-m-d', strtotime($date. ' + '.$i.' days'));
$responsePayu = $this->callPayU($sett_date);
$utrnos=array('status'=>0);
if(!is_null($responsePayu)){
$utrNos = json_decode($responsePayu,true);
foreach ($utrNos['Txn_details'] as $value) {
# code...
if($value['requestid']==$payment_reference){
$utrnos['status']=1;
$utrnos['data'] = $value;
$brtoggle=true;
break;
}
}
if($brtoggle)
break;
}
}
return $utrnos;
}
My problem is if suppose there is like 2000 payment in a day , then the for loop will have to fun for 2000 times and it has to do keep doing it for everyday until it gets it id ,
Worst case could be 7*2000 = 14000
times .
So I was thinking can there be a better way to match the string requestid
directly into the json string ? so I think the performance might be bit better than decoding and checking the array.
Thanks