0

I have looked at a bunch of questions/answers on here and none of them seem to solve my problem. I am integrating a payment system that returns a JSON string via a JS library that I then need to parse on the server to compare the hash values to ensure that it is valid. So I get their response and attempt to pass it via jQuery AJAX to a PHP file on the server. The response I get from the payment system is valid JSON. If I pass it directly the result I get on the server seems to be URL encoded. If I JSON.stringify() it, it adds a bunch of extra quotes which results in invalid JSON.

function isValidJSON($str) {
    json_decode($str);
    return json_last_error() == JSON_ERROR_NONE;
}

$json_params = file_get_contents("php://input");
error_log($json_params);

//error_log($_POST['jsresp']);
//$respObj = json_decode(stripslashes($_POST['jsresp']));

//error_log($json_params);

if (strlen($json_params) > 0 && isValidJSON($json_params)) {
    $respObj = json_decode($json_params);
} else {
    error_log('bad json '.$json_params);
}

$result = 0;

$resp = json_encode($respObj->Response);
$hash = $respObj->Hash;
error_log($hash);
$calcHash = base64_encode(hash_hmac('sha512', $resp, $app->getSageClientSecret(), true));
error_log($calcHash);   

if($hash === $calcHash) {
    $result = 1;    
}

$app->updateCartResponse($_COOKIE['qid'], $result);

And here is the jQuery AJAX call to send the data:

$(document).on('click', 'button#sps-close.sps.btn.btn-primary', function(){
    var resp = $("#resp_holder").val();
    $.ajax({
        url: "<?=$env?>sources/processors/process_hash.php",
        data: { jsresp : resp },        
        type: "post",
        //dataType: "json",                                                                  
        success: function( data ) {    
            // nothing to do here.                                                    
        }
    });
    var url = $("#redirect_url").val();
    if(url != "") {
        location.href = $("#redirect_url").val();
    }
}); 

When I do it this way, the JSON that gets to the server looks like this:

jsresp%5Bresponse%5D=%7B%22requestId%22%3A%22443594%22%2C%22gatewayResponse%22%3A%7B%22status%22%3A%22Approved%22%2C%22reference%22%3A%22EBGHNHChw0%22%2C%22message%22%3A%22APPROVED+658658%22%2C%22code%22%3A%22658658%22%2C%22cvvResult%22%3A%22P%22%2C%22avsResult%22%3A%22+%22%2C%22riskCode%22%3A%2200%22%2C%22networkId%22%3A%2210%22%2C%22isPurchaseCard%22%3Afalse%2C%22orderNumber%22%3A%22443594%22%2C%22transactionId%22%3A%22NDViMWYzNmEwNWNiOGQxZjIwOTAwNzU4MmVjYzJhMWQ%22%2C%22timestamp%22%3A%222017-11-16T23%3A17%3A12.6584893-05%3A00%22%7D%7D&jsresp%5Bhash%5D=bgd1e0Cxhj5s1FQaUFFYk7BMnSIl4Ez1jPMopZFp%2B4MyN9chFZZoo%2F3IuZPX7bbQ%2BRyaReKN1CNJXxRmjnLMRQ%3D%3D

I don't understand what I'm doing wrong or how to get it there properly.

Shafiqul Islam
  • 5,570
  • 2
  • 34
  • 43
  • Why do you have `dataType: "json",` commented out? – Jay Blanchard Nov 17 '17 at 14:01
  • you should use `data: JSON.stringify({ jsresp : resp }),`. It's a default jQuery behaviour to urlEncode when you pass an object, not if it's already a string – Kaddath Nov 17 '17 at 14:01
  • 1
    you also might need to add `contentType: 'application/json',` if it still doesn't work – Kaddath Nov 17 '17 at 14:02
  • So, is `$("#resp_holder").val()` a string in JSON format? If so, you should do this: `var resp = JSON.parse($("#resp_holder").val());`, then jQuery will get it as an object and pass the whole thing as a JSON – Piyin Nov 17 '17 at 14:10

2 Answers2

0

Based on your sample, this is what you get on the server

jsresp[response]={"requestId":"443594","gatewayResponse":{"status":"Approved","reference":"EBGHNHChw0","message":"APPROVED 658658","code":"658658","cvvResult":"P","avsResult":" ","riskCode":"00","networkId":"10","isPurchaseCard":false,"orderNumber":"443594","transactionId":"NDViMWYzNmEwNWNiOGQxZjIwOTAwNzU4MmVjYzJhMWQ","timestamp":"2017-11-16T23:17:12.6584893-05:00"}}&jsresp[hash]=bgd1e0Cxhj5s1FQaUFFYk7BMnSIl4Ez1jPMopZFp+4MyN9chFZZoo/3IuZPX7bbQ+RyaReKN1CNJXxRmjnLMRQ==

So your $_REQUEST['jsresp'] is an array you need to json_decode the 'response' index of the array :

$respObj = json_decode($_REQUEST['jsresp']['response']);
$hash = $_REQUEST['jsresp']['hash'];

And then you can go on...

Paparsifal
  • 156
  • 1
  • 4
0

First i will advice you to look at the value in your #resp_holder, if the value there is correct as you want it, then you can post it using anyhow you want it, json is a string, and as long as that string conforms to json standard, and they are no special characters in the it like &, then you can decode it in your php.

You can use html or json dataType to post it.

Sunday Okoi
  • 303
  • 2
  • 7