【发布时间】:2014-07-23 23:57:08
【问题描述】:
我正在接收带有以下 JSON 对象的 HTTP Post:
{"notification":{"version":6.0,"attemptCount":0,"role":"VENDOR","site":"nicholeen","receipt":"********","currency":"USD","transactionType":"TEST","transactionTime":1406070122781,"paymentMethod":"VISA","totalProductAmount":1.00,"totalTaxAmount":0.00,"totalShippingAmount":0.00,"customer":{"shipping":{"firstName":"TEST","lastName":"USER","fullName":"Test User","email":"testuser@somesite.com","address":{}},"billing":{"firstName":"TEST","lastName":"USER","fullName":"Test User","email":"testuser@somesite.com","address":{}}},"lineItems":[{"itemNo":"1","productTitle":"A passed in title","shippable":false,"recurring":false,"customerProductAmount":1.00,"customerTaxAmount":0.00}]},"verification":"2745A502"}
我需要将“通知”JSON 对象转换为分配给 PHP 变量的 JSON 字符串,而不会丢失任何数据,包括小数。
目前我使用$ipn_raw = file_get_contents('php://input'); 接收IPN。
然后我将 JSON json_decode 为 PHP 变量,然后使用 json_encode 重新编码通知部分。代码如下:
$ipn_raw = file_get_contents('php://input');
$ipn = json_decode($ipn_raw);
$notification = $ipn['notification'];
$result = json_encode($notification);
但是,结果去掉了一些值,给了我以下信息:
{"version":6,"attemptCount":0,"role":"VENDOR","site":"nicholeen","receipt":"********","currency":"USD","transactionType":"TEST","transactionTime":1406095846441,"paymentMethod":"VISA","totalProductAmount":1,"totalTaxAmount":0,"totalShippingAmount":0,"customer":{"shipping":{"firstName":"TEST","lastName":"USER","fullName":"Test User","email":"testuser@somesite.com","address":[]},"billing":{"firstName":"TEST","lastName":"USER","fullName":"Test User","email":"testuser@somesite.com","address":[]}},"lineItems":[{"itemNo":"1","productTitle":"A passed in title","shippable":false,"recurring":false,"customerProductAmount":1,"customerTaxAmount":0}]}
您可以看到版本 6.0 现在只是版本 6,totalProductAmount 是 1.00 现在是 1,等等。
如何在结果中不改变任何值的情况下做到这一点?
谢谢!
根据要求,这里有一些额外的背景信息,说明为什么我需要所有内容都与原始内容保持一致。 Clickbank 为我提供了以下信息,以便创建必要的 SHA 1 哈希来验证我从他们那里收到的第 6 版 IPN(请参阅https://support.clickbank.com/entries/22803622-Instant-Notification-Service)
1) 使用 JSON 库/DSL 从 IPN 中提取“通知”JSON 对象。 2) 将“通知”JSON 对象转换为 JSON 字符串。该字符串应以大括号 { 开头并以大括号 } 结尾,因为它是 JSON 对象的表示形式。 3) 将密钥直接附加到通知对象末尾大括号之后的字符串上。没有空格或分隔符。 4) SHA 1 散列在步骤 3 中创建的字符串。 5) 取第 4 步中创建的哈希的前 8 个字符。这就是 v6 IPN 中“验证”字段的值。
如果需要,我可以为其余步骤提供我的所有代码,但在这一点上,我唯一遇到的问题是将通知对象单独保存在一个字符串中。
【问题讨论】:
-
您知道
6.0和6只是同一个数字的两个不同表示。您甚至可以尝试使用 PHP:var_dump(6.0 == 6); // will print true -
是的,knittl。我知道它们是相同的表示;但是,我需要有完全相同的输出才能为我上面没有提到的另一个步骤生成匹配的 SHA 1 哈希。
-
那么你应该在你的问题中提到每一个必要的细节;)你的输出中的空白呢? JSON 字符串
{ "v" : 1 }和{"v":1}也描述了同一个对象。 -
我在问题的底部添加了一些背景信息。我不确定空格是否会影响 SHA1 哈希的生成。
-
在加密算法中,即使是字节也会改变即将到来的哈希