<?php
// Original JSON string with malformed "meCode" field
$jsonString = '{"result":{"mihpayid":21572535051,"mode":"UPI","status":"success","key":"gqavfx","txnid":"1731661562814","amount":"200.00","addedon":"2024-11-15 14:36:13","productinfo":"Product","firstname":"kamal","lastname":"","address1":"","address2":"","city":"","state":"","country":"","zipcode":"","email":"8954484701@gmail.com","phone":"8954484701","udf1":"","udf2":"","udf3":"","udf4":"","udf5":"","udf6":"","udf7":"","udf8":"","udf9":"","udf10":"","card_token":"","card_no":"","field0":"","field1":"","field2":"977902","field3":"naveentiwari947@okaxis","field4":"","field5":"vishalkumar-12337077.payu@indus","field6":"PPPL2157253505115112414361367370f05","field7":"APPROVED OR COMPLETED SUCCESSFULLY|00","field8":"","field9":"Success|Completed Using Callback","payment_source":"payuPureS2S","PG_TYPE":"UPI-PG","error":"E000","error_Message":"No Error","net_amount_debit":200,"discount":"0.00","offer_key":null,"offer_availed":null,"unmappedstatus":"captured","hash":"48ec312dcae46bbd03b8601c3b07541671401d12fd8b21779e84e7c45121cbb69e66efa2a4ea7d253779c89bf064bdde1625d40be5d93ff2d38acea5abe6b70e","bank_ref_no":"468669559695","bank_ref_num":"468669559695","bankcode":"INTENT","surl":"https://payuresponse.firebaseapp.com/success","curl":"https://payuresponse.firebaseapp.com/failure","furl":"https://payuresponse.firebaseapp.com/failure","meCode":"{"pgMerchantId":"INDB000008714559","merchantVpa":"VISHALKUMAR-12337077.payu@indus"}","status":"success"}';
// Step 1: Fix the "meCode" field by removing the extra double quotes inside the JSON string
$jsonString = preg_replace('/"{2}/', '"', $jsonString);
// Step 2: Decode the corrected JSON string
$data = json_decode($jsonString, true);
echo $data;
// Step 3: Check if decoding was successful
if ($data === null) {
echo "Error decoding JSON: " . json_last_error_msg(); // Show error message if decoding fails
} else {
// Step 4: Extract and display the txnid value
if (isset($data['result']['txnid'])) {
echo "Transaction ID: " . $data['result']['txnid']; // Output txnid value
} else {
echo "Error: txnid not found.";
}
}
?>