In the request (I didn't write it) this little script:
<script type="text/javascript">
function oncallback(e) {
$('#message').html(e.data);
}
</script>
Writes to the browser window a JSON string:
{"event":"success","method":"sale","request_id":"275936ad-83c0-4afd-88ff-6943c4ee2781","response_code":"A01","response_description":"TEST APPROVAL","trace_number":"8da65a44-3c07-4590-8b62-302f533aaa18","authorization_code":"123456","customer_token":"0InRBKpTSy2VlUL0wbLZlQ","paymethod_token":"vPSGgKycRXWWqkxiiXm_IA","total_amount":"8.88","order_number":"COR-199287","version_number":"1.0","last_4":"1111","method_used":"visa","hash_method":"md5","save_token":"true","expire_month":"02","expire_year":"2020","signature":"731c764db3d3d5c25e371fbffd331e5c","utc_time":"636584905150041669"}
I would like to have the string display in the browser window all nice and pretty like this:
{
"event": "success",
"method": "sale",
"request_id": "275936ad-83c0-4afd-88ff-6943c4ee2781",
"response_code": "A01",
"response_description": "TEST APPROVAL",
"trace_number": "8da65a44-3c07-4590-8b62-302f533aaa18",
"authorization_code": "123456",
"customer_token": "0InRBKpTSy2VlUL0wbLZlQ",
"paymethod_token": "vPSGgKycRXWWqkxiiXm_IA",
"total_amount": "8.88",
"order_number": "COR-199287",
"version_number": "1.0",
"last_4": "1111",
"method_used": "visa",
"hash_method": "md5",
"save_token": "true",
"expire_month": "02",
"expire_year": "2020",
"signature": "731c764db3d3d5c25e371fbffd331e5c",
"utc_time": "636584905150041669"
}
I have tried variations of JSON.stringify() but not getting anywhere...
such as:
JSON.stringify($('#message').html(e.data)),undefined, 2);
Is this possible?
Full script here: https://www.calligraphydallas.com/stuff/forteco.new.php
If you want to see the final JSON response string, click the Pay Now button and use:
card number: 4111111111111111
expiry: anything in the future
CVV: any 3-digit number
edit: Still no joy :(
<html>
<?php
$APILoginID = 'LI9KrwLnR';
$SecureTransKey = '5ULeuwN7N3h4';
$ordernumber = 'COR-199287';
$totalamount = 8.88;
$method = 'sale';
$version = '1.0';
$millitime = microtime(true) * 1000;
$utc = number_format(($millitime * 10000) + 621355968000000000 , 0, '.', '');
$data = "$APILoginID|$method|$version|$totalamount|$utc|$ordernumber||";
$hash = hash_hmac('md5',$data,$SecureTransKey);
$expire = number_format(($millitime * 10000) + 621355968001000000 , 0, '.', '');
?>
<head>
<style>
#message {
white-space: pre;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
function oncallback(e) {
var jsonString = $('#message').html(e.data);
var str = JSON.stringify(jsonString, null, 2);
$('#message').html(str);
}
</script>
<script type="text/javascript" src="https://sandbox.forte.net/checkout/v1/js"></script> <!-- sandbox -->
<script src="https://code.jquery.com/jquery-1.11.0.min.js"></script>
</head>
<body>
<pre id="message"></pre>
<button api_login_id=<?php echo $APILoginID;?>
version_number=<?php echo $version;?>
method=<?php echo $method;?>
callback="oncallback"
total_amount=<?php echo $totalamount;?>
utc_time=<?php echo $utc;?>
signature=<?php echo $hash;?>
billing_email_address_attr="required"
order_number=<?php echo $ordernumber;?>
consumer_id="123ABC"
>Pay Now</button>
</body>
</html>
Edit:
If this is the string:
{"event":"success","method":"sale","request_id":"e26ff0a0-1762-40f7-a747-f1d1e3da298d","response_code":"A01","response_description":"TEST APPROVAL","trace_number":"3f3ace6b-8e6c-4d53-8252-0e4df4af01bf","authorization_code":"123456","customer_token":"pNEFB2w4S5G5uhw4RAOnDA","paymethod_token":"tOxI8ULPQ8yJAk2zkIDFGQ","total_amount":"8.88","order_number":"COR-199287","version_number":"1.0","last_4":"1111","method_used":"visa","hash_method":"md5","save_token":"true","expire_month":"02","expire_year":"2020","signature":"31a7e07128e5a5d792dc0403b610a2fb","utc_time":"636585555449053529"}
produced by this little script:
<script>
function oncallback(e) {
$('#message').html(e.data);
}
</script>
<html>
<body>
<pre id="message"></pre>
<button callback="oncallback"></button>
</body>
</html>
It seems like this should work:
<script>
function oncallback(e) {
JSON.stringify($('#message').html(e.data),null,2);
}
</script>
<html>
<body>
<pre id="message"></pre>
<button callback="oncallback"></button>
</body>
</html>
But it does not. It produces the same string.
Thoughts anyone?
JSON.stringify output to div in pretty print way
Change your <div>
to a <pre>
and it should work.
var stuff = {
"event": "success",
"method": "sale",
"request_id": "275936ad-83c0-4afd-88ff-6943c4ee2781",
"response_code": "A01",
"response_description": "TEST APPROVAL",
"trace_number": "8da65a44-3c07-4590-8b62-302f533aaa18",
"authorization_code": "123456",
"customer_token": "0InRBKpTSy2VlUL0wbLZlQ",
"paymethod_token": "vPSGgKycRXWWqkxiiXm_IA",
"total_amount": "8.88",
"order_number": "COR-199287",
"version_number": "1.0",
"last_4": "1111",
"method_used": "visa",
"hash_method": "md5",
"save_token": "true",
"expire_month": "02",
"expire_year": "2020",
"signature": "731c764db3d3d5c25e371fbffd331e5c",
"utc_time": "636584905150041669"
};
var str = JSON.stringify(stuff, null, 2); // spacing level = 2
$('#message').html(str)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>
<body>
<pre id='message'>
</pre>
</body>
</html>
To fit your example:
<script>
function oncallback(e) {
var formatted_json = JSON.stringify(e.data, null, 2);
$('#message').html(formatted_json);
}
</script>
<html>
<body>
<pre id="message"></pre>
<button callback="oncallback"></button>
</body>
</html>