在 Braintree 的 localhost 測試 Webhook
我正在研究 Braintree,我想在處理定期計費時向我的客戶發送自定義電子郵件通知,因此每個月都應該將這些自定義通知發送給所有使用者。為此,我必須使用 webhook 來檢索目前發生的事件,然後根據 webhook 的響應發送電子郵件通知。(我認為這只是這種情況下的解決方案,如果有人知道另一種可能的解決方案,請提出建議)。我想首先在我的本地主機上測試 webhook,並且我嘗試創建一個新的 webhook 並將本地主機路徑指定為檢索 webhook 的目標。但這顯示了一個錯誤“目的地未驗證”…………
我的路徑是:“ http://127.0.0.1:81/webhook/Accept ”
這些是在 webhook 開發過程中可以使用的一些工具:
PostCatcher,
請求箱,
3)ngrok,
PageKite 和
本地隧道
http://telerivet.com/help/api/webhook/testing
https://www.twilio.com/blog/2013/10/test-your-webhooks-locally-with-ngrok.html
那麼另一種測試它的方法是創建一個 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" };我希望這有幫助!