Braintree测试Request.Params总是空的

时间:2016-05-27 14:25:15

标签: c# asp.net-mvc braintree

我正在实施webhook测试代码,我遇到了一个问题。

在我向我的webhook监听器发布示例通知后,Params不在请求中:

Request.Params["bt_signature"]

Request.Params["bt_payload"]

所以听众失败了。

下面是我的Post Webhook代码和Listener代码;我不确定我是否正确使用gateway.WebhookTesting.SampleNotification

POST测试Webhook

private async Task PostTestNotification()
{
  try
  {
    using (var client = new HttpClient())
    {
        client.BaseAddress = new Uri("http://localhost:50709/");

        var gateway = config.GetGateway();

        // Create sample notification
        Dictionary<String, String> sampleNotification = gateway.WebhookTesting.SampleNotification(
            WebhookKind.SUBSCRIPTION_CHARGED_SUCCESSFULLY, "sub_id_1234"
        );

        // Convert sample notification to JSON
        string payloadJson = JsonConvert.SerializeObject(sampleNotification);

        // Create StringContent of json sample notificaiton
        var data = new StringContent(payloadJson);

        // data looks like this when debugging { "bt_payload":"PG5vdGlmaWNhdGlvbj48dGltZXN0YW1wIHR5cGU9ImRhdGV0aW1lIj4yMDE2LTA1LTI3IDEzOjM2OjEwWjwvdGltZXN0YW1wPjxraW5kPnN1YnNjcmlwdGlvbl9jaGFyZ2VkX3N1Y2Nlc3NmdWxseTwva2luZD48c3ViamVjdD48c3Vic2NyaXB0aW9uPjxpZD5zdWJfaWRfMTIzNDwvaWQ+PHRyYW5zYWN0aW9ucz48dHJhbnNhY3Rpb24+PGlkPnN1Yl9pZF8xMjM0PC9pZD48YW1vdW50PjQ5Ljk5PC9hbW91bnQ+PHN0YXR1cz5zdWJtaXR0ZWRfZm9yX3NldHRsZW1lbnQ8L3N0YXR1cz48ZGlzYnVyc2VtZW50LWRldGFpbHM+PGRpc2J1cnNlbWVudC1kYXRlIHR5cGU9ImRhdGUiPjIwMTMtMDctMDk8L2Rpc2J1cnNlbWVudC1kYXRlPjwvZGlzYnVyc2VtZW50LWRldGFpbHM+PGJpbGxpbmc+PC9iaWxsaW5nPjxjcmVkaXQtY2FyZD48L2NyZWRpdC1jYXJkPjxjdXN0b21lcj48L2N1c3RvbWVyPjxkZXNjcmlwdG9yPjwvZGVzY3JpcHRvcj48c2hpcHBpbmc+PC9zaGlwcGluZz48c3Vic2NyaXB0aW9uPjwvc3Vic2NyaXB0aW9uPjwvdHJhbnNhY3Rpb24+PC90cmFuc2FjdGlvbnM+PGFkZF9vbnMgdHlwZT0iYXJyYXkiPjwvYWRkX29ucz48ZGlzY291bnRzIHR5cGU9ImFycmF5Ij48L2Rpc2NvdW50cz48L3N1YnNjcmlwdGlvbj48L3N1YmplY3Q+PC9ub3RpZmljYXRpb24+\n","bt_signature":"69r68j6hnzjpnq4j|508a7b4b3bbbe15c241c742331acfc5bacf37c54"}

        // POST
        HttpResponseMessage response = await client.PostAsync("webhooks/accept", data);

        // RESPONSE
        if (response.IsSuccessStatusCode == true)
        {
            // SUCCESS CONTENT
            string resultJSON = await response.Content.ReadAsStringAsync();
        }
        else
        {
            // FAIL CONTENT
            dynamic problem = await response.Content.ReadAsStringAsync();
        }
    }
  }
  catch (Exception ex)
  {
    //
    Console.WriteLine("Exception: " + ex.Message);
  }
}

Webhook LISTENER

// webhooks / accept endpoint

public async Task<ActionResult> accept()
{
  try
  {
    var gateway = config.GetGateway();

    if (Request.HttpMethod == "POST")
    {
       var bt_signature = Request.Params["bt_signature"]; <<<<<<< ALWAYS EMPTY >>>>>>>>>
       var bt_payload = Request.Params["bt_payload"]; <<<<<<< ALWAYS EMPTY >>>>>>>>>

        WebhookNotification webhookNotification = gateway.WebhookNotification.Parse(
            Request.Params["bt_signature"],
            Request.Params["bt_payload"]
            ); <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< EXCEPTION WHEN HIT - Value cannot be null, Parameter name: Input >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


        // ACTION Webhook 
        if (webhookNotification.Kind == WebhookKind.SUBSCRIPTION_CANCELED)
        {
            IsActive = false;
            await Logger.LogInsight("", "WEBHOOK: SUBSCRIPTION_CANCELED " + webhookNotification.Subscription.Id );
        }
        else if (webhookNotification.Kind == WebhookKind.SUBSCRIPTION_CHARGED_SUCCESSFULLY)
        {
            IsActive = true;
            await Logger.LogInsight("", "WEBHOOK: SUBSCRIPTION_CHARGED_SUCCESSFULLY " + webhookNotification.Subscription.Id);
        }

        // code ommitted for brevity, similar to above checking all 'kind' values 

     }     
   }
 }

为什么Braintree请求参数为空?

1 个答案:

答案 0 :(得分:0)

问题出在Webhook端点,Braintree sample code不正确,它意味着使用MVC,但开发人员需要使用Web Api,并且示例代码不起作用。

为了实现这一点,我将Sample Notification POST保持不变,并创建了一个新的Webhook监听器:

首先创建一个类来接收来自POST的两个braintree字符串:

public class bt
{
    public string bt_payload { get; set; }
    public string bt_signature { get; set; }
}

现在创建一个空的Web Api 2控制器:

    [HttpPost]
    [Route("api/webhooks/accept")]
    public async Task<IHttpActionResult> accept(bt bt_lot)
    {
        var gateway = config.GetGateway();

        WebhookNotification webhookNotification = gateway.WebhookNotification.Parse(
            bt_lot.bt_signature,
            bt_lot.bt_payload
            );

        if (webhookNotification.Kind == WebhookKind.SUBSCRIPTION_CANCELED)
        {
            // take your action here...
        }

我在这里发布了关于Braintree的所有经验,因为这里没有太多的帮助,我希望它能帮助其他人。

我不得不说Braintree帮助工作人员非常出色并且总是以非常详细的答案回答问题,95%的时间解决了我遇到的任何问题,但这个问题让我摸不着头脑,因为他们的例子没有用,而且帮助工作人员像我一样认为代码应该有效。