那么另一种测试它的方法是创建一个 WebAPI 并通过 Postman 将数据发布到您的 POST 方法。为此,只需在 Visual Studio 中创建一个 WebAPI。在 API 控制器中,创建一个 POST 方法。
/// <summary>
/// Web API POST method for Braintree Webhook request
/// The data is passed through HTTP POST request.
/// A sample data set is present in POSTMAN HTTP Body
/// /api/webhook
/// </summary>
/// <param name="BTRequest">Data from HTTP request body</param>
/// <returns>Webhook notification object</returns>
public WebhookNotification Post([FromBody]Dictionary<String, String> BTRequest)
{
WebhookNotification webhook = gateway.WebhookNotification.Parse(BTRequest["bt_signature"], BTRequest["bt_payload"]);
return webhook;
}
在 Postman 中,将 Body 中的以下数据作为原始 JSON 发布。
{
"bt_signature":"Generated Data",
"bt_payload":"Very long generated data"
}
上述 Json 字典的数据是通过以下代码生成的:
Dictionary<String, String> sampleNotification = gateway.WebhookTesting.SampleNotification(WebhookKind.DISPUTE_OPENED, "my_Test_id");
// Your Webhook kind and your test ID
只需从示例通知中选择数据并将其放在 JSON 中。运行您的 WebAPI,放置调试器。在 Postman 中添加 localhost URL,选择 POST,然后单击发送。你的 POST 方法应该被击中。
另外,不要忘记添加您的网关详细信息:
private BraintreeGateway gateway = new BraintreeGateway
{
Environment = Braintree.Environment.SANDBOX,
MerchantId = "Your Merchant Key",
PublicKey = "Your Public Key",
PrivateKey = "Your Private Key"
};
我希望这有帮助!