简体   繁体   English

json_decode保存类型

[英]json_decode Preservation of Type

I'm using the json_decode function to decode (and verify a postback from a payment processor). 我正在使用json_decode函数进行解码(并验证来自付款处理器的回发)。 the json object received looks as follow 收到的json对象如下

{
   "notification":{
      "version":6.0,
      "attemptCount":0,
      "role":"VENDOR",
      .....
      "lineItems":[
         {
            "itemNo":"1",
            "productTitle":"A passed in title",
            "shippable":false,
            "recurring":false,
            "customerProductAmount":1.00,
            "customerTaxAmount":0.00
         }
      ]
   },
   "verification":"9F6E504D"
}

The verification works as follows, one takes the notification node and append a secret key. 验证的工作方式如下:一个作为通知节点并附加一个密钥。 The first eight characters of the SHA1 hash of this string should match the content of the validation node. 此字符串的SHA1哈希的前八个字符应与验证节点的内容匹配。

However, I noticed that whilst using json_decode, the double value 6.0, 0.00 etc are truncated to integers (6, 0 ,etc). 但是,我注意到在使用json_decode时,双精度值6.0、0.00等被截断为整数(6、0等)。 This messes up the string (in terms of it not generating the correct SHA1-hash). 这会弄乱字符串(就其而言,它不会生成正确的SHA1-hash)。 Do note, I cannot use the depth limit to prevent decoding of the notification branch, since I need to support PHP 5.0. 请注意,由于我需要支持PHP 5.0,因此不能使用深度限制来防止对通知分支进行解码。 How can I tackle this issue. 我该如何解决这个问题。 The (defect) validation code I wrote is: 我编写的(缺陷)验证代码是:

  public function IPN_Check(){
    $o = (json_decode($this->test_ipn));
    $validation_string = json_encode($o->notification);
  }

I tried the following: 我尝试了以下方法:

<?php

var_dump(json_decode('
{
   "notification":{
      "version":6.0,
      "attemptCount":0
   }
}
'));

and got this output: 并得到以下输出:

object(stdClass)#1 (1) {
  ["notification"]=>
  object(stdClass)#2 (2) {
    ["version"]=>
    float(6)
    ["attemptCount"]=>
    int(0)
  }
}

PHP does make a difference between float and int, maybe you could do something like gettype($o->notification[$i]) == 'float' to check whether you need to add a zero using a string. PHP确实在float和int之间有所不同,也许您可​​以执行gettype($o->notification[$i]) == 'float'来检查是否需要使用字符串添加零。

UPD. UPD。 PHP does make a difference between float and int, but json_encode() - may not. PHP确实在float和int之间有所作为,但json_encode() -可能没有。 To be sure, that you encode all values as they are - use json_encode() with JSON_PRESERVE_ZERO_FRACTION parameter, and all your float/double types will be saved correctly. 可以肯定的是,您对所有值都进行了编码-将json_encode()JSON_PRESERVE_ZERO_FRACTION参数一起使用,所有的float / double类型都将正确保存。

It looks like ClickBank they always send it in the same format with only the two top level fields "notification" and "verification". 看起来ClickBank似乎总是以相同的格式发送,只有两个顶级字段“ notification”和“ verification”。 So you can just use substr to remove the first 16 characters ( {"notification": ) and the last 27 characters ( ,"verification":"XXXXXXXX"} ) from the raw JSON and then proceed from there: 因此,您可以使用substr从原始JSON中删除前16个字符( {"notification": substr和后27个字符( ,"verification":"XXXXXXXX"} ),然后从那里开始:

$notification = substr($json, 16, -27);
$verification = strtoupper( substr( hash('sha1', $notification . $secret), 0, 8) );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM