3

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

user2671011
  • 145
  • 2
  • 11
  • You could use `preg_match` http://php.net/manual/en/function.preg-match.php, however I think you're trying to do micro-optimizations here. We json_decode, json_encode, etc thousands of times a day on our end and it really doesn't hit our system hard at all. Another added bonus is that json_decode creates legible code. – jardis Oct 04 '16 at 04:30
  • @jardis , thanks for the link yes I was thinking of implementing the pregmatch but since not good at regEx so used the other way, So according to you even if there are millions of transaction in a day it might not effect the process ? – user2671011 Oct 04 '16 at 04:38
  • 1
    Key word being might. You have to benchmark it and see how it performs at different scales. Don't pre-optimize, is the moral of the story :). – jardis Oct 04 '16 at 05:00

0 Answers0