所以我正在摆弄这个网站的IPN功能,看看我是不是要将它融入我朋友和我正在研究的一些愚蠢的项目中。说实话,我不太了解C#,我对这门语言很新(几个月的编码练习)。这是他们提供的关于如何使用它的PHP示例:https://github.com/Rocketr/rocketrnet-ipn-php/blob/master/example_rocketr_ipn.php
我试图在MVC 5中创建一个类似的接收器。当IPN命中服务器页面来处理请求时我有模型设置功能但是它似乎每次都失败而不是写任何原始数据我试图捕获到日志。
// GET: Purchases/Incoming
public void Incoming()
{
var ipnDebugLog = HttpRuntime.AppDomainAppPath + "/Logs/IPN/debug.txt";
var testIPNKey = "the hash here";
byte[] ipnToByes = Encoding.ASCII.GetBytes(testIPNKey); // IPN string to byteto hash with
var recvdIPN = Request["HTTP_IPN_HASH"];
HMACSHA256 testHash = new HMACSHA256(ipnToByes); // Setting testHash to IPN secret string
string ipnHeader = Request["IPN_HASH"];
using (StreamWriter sw = new StreamWriter(ipnDebugLog))
{
sw.WriteLine(ipnHeader);
foreach (var reqHead in ipnHeader)
{
sw.WriteLine(reqHead.ToString());
sw.WriteLine("JSON String: " + Request["HTTP_IPN_SECRET"]);
sw.WriteLine(recvdIPN);
sw.WriteLine("From: " + GetIPAddress());
}
}
}
所以这只是我试图从Rocketr发送数据。在网站上它说:
为了验证有效负载的完整性,我们将通过名为“IPN_HASH”的HTTP头发送HMAC签名。 HMAC签名将是带有IPN密码的带签名的json编码POST对象。您可以在此存储库的example_rocketr_ipn.php文件中查看如何验证签名。
我只是愚蠢和新的理解C#这样的功能?我觉得我正在阅读原始数据,但我错了吗?
那么总结一下这个问题我是否正在以不正确的方式读取名为IPN_HASH的原始自定义HTTP头?关于PHP示例,他们使用isset来读取标记为HTTP_IPN_HASH的服务器变量标题吧?
所以我必须转换这个
$hmac = hash_hmac("sha512", json_encode($_POST), trim($IPN_SECRET));
试试这个(根据需要/必要进行调整):
using System;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
namespace Foo
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void PhpHashTest()
{
string IPN_SECRET = "I-am-the-secret";
//Mocking some HTTP POSTed data
var someFormUrlEncodedData = "order_id=1234&product_title=Hello%20World&product_id=Sku123";
//Mocking json_encode($_POST)
var data = HttpUtility.ParseQueryString(someFormUrlEncodedData);
var dictionary = data.AllKeys.ToDictionary(key => key, key => data[key]);
//{"order_id":"1234","product_title":"Hello World","product_id":"Sku123"}
var json = JsonConvert.SerializeObject(dictionary);
byte[] bytes;
using (var hmac512 = new HMACSHA512(Encoding.ASCII.GetBytes(IPN_SECRET)))
{
bytes = hmac512.ComputeHash(Encoding.ASCII.GetBytes(json));
}
//will contain lower-case hex like Php hash_hmac
var hash = new StringBuilder();
Array.ForEach(bytes, b => hash.Append(b.ToString("x2")));
//Assert that Php output exactly matches implementation
Assert.IsTrue(string.Equals("340c0049bde54aa0d34ea180f8e015c96edfc4d4a6cbd7f139d80df9669237c3d564f10366f3549a61871779c2a20d2512c364ee56af18a25f70b89bd8b07421", hash.ToString(), StringComparison.Ordinal));
Console.WriteLine(hash);
}
}
}
不是PHP开发 - 这是我的“Php版本”:
<?php
$IPN_SECRET = 'I-am-the-secret';
# 'order_id=1234&product_title=Hello%20World&product_id=Sku123';
$json = array('order_id' => '1234', 'product_title' => 'Hello World', 'product_id' =>'Sku123');
echo json_encode($json);
echo "<br />";
$hmac = hash_hmac("sha512", json_encode($json), trim($IPN_SECRET));
echo $hmac;
?>
心连心....