2014-03-31 69 views
0

我設法使用Strip.net DLL的一個版本創建付款方式,但即時通訊有問題處理錯誤。我得到了這個。Stripe C#錯誤處理

try 
{ 
    StripeCustomer current = GetCustomer(); 
    // int? days = getaTraildays(); 
    //if (days != null) 
    //{ 
    int chargetotal = 300; //Convert.ToInt32((3.33*Convert.ToInt32(days)*100)); 
    var mycharge = new StripeChargeCreateOptions(); 
    mycharge.AmountInCents = chargetotal; 
    mycharge.Currency = "USD"; 
    mycharge.CustomerId = current.Id; 
    string key = "sk_test_XXX"; 
    var chargeservice = new StripeChargeService(key); 
    StripeCharge currentcharge = chargeservice.Create(mycharge); 
    //} 
}   
catch (StripeException) 
{ 
    lblerror.Text = "Please check your card information and try again"; 
} 

會趕上錯誤,讓用戶知道有這個問題,但即時通訊新的理解,爲什麼它仍然顯示錯誤,如果過程的工作。我知道它是如何編寫漁獲物的問題,但我不確定如何處理,以及我曾經如此努力的一切都失敗了。我想要做的就是重定向到另一個頁面。任何想法

++更新

與奧利維爾Jacot-Descombes一些幫助我改變了我的代碼

catch (StripeException ex) 
{ 
lblerror.Text = (ex.Message); 
} 

,並能夠取得更好的效果

+0

您的'嘗試'塊中的代碼是否有可能拋出一個不同的異常?否則(從提供的小代碼),StripeException應該被捕獲和壓制。 – jsirr13

+0

你的問題似乎是關於第三方API拋出的異常。我建議你查看那裏的錯誤。我們不是該API的開發者。 – tnw

+1

在輸入try-block之前,你是否清除了'lblerror.Text'? –

回答

6

說不上來,如果這是完全在上面的評論中回答,但這裏有一點更多關於此:(特別感謝@ tnw爲你絕對無用的評論)

Stripe Errors

有幾種不同類型的錯誤需要進行不同的處理。正如您在上面的鏈接中看到的那樣,存在api錯誤,無效請求錯誤和卡片錯誤。你應該處理所有三個不同,因爲你不可能不希望顯示API或內部錯誤給你的用戶。

一旦進入異常範圍,您需要的信息就是exception.StripeError對象。有exception.HttpStatusCode我不使用和exception.StripeError物體看起來像這樣:

public class StripeError 
{ 
    [JsonProperty("type")] 
    public string ErrorType { get; set; } 

    [JsonProperty("message")] 
    public string Message { get; set; } 

    [JsonProperty("code")] 
    public string Code { get; set; } 

    [JsonProperty("param")] 
    public string Parameter { get; set; } 

    [JsonProperty("error")] 
    public string Error { get; set; } 

    [JsonProperty("error_description")] 
    public string ErrorSubscription { get; set; } 

    [JsonProperty("charge")] 
    public string ChargeId { get; set; } 
} 

所以你想是這樣的:

 catch (StripeException exception) 
     { 
      switch (exception.StripeError.ErrorType) 
      { 
       case "card_error": 
        //do some stuff, set your lblError or something like this 
        ModelState.AddModelError(exception.StripeError.Code, exception.StripeError.Message); 

        // or better yet, handle based on error code: exception.StripeError.Code 

        break; 
       case "api_error": 
        //do some stuff 
        break; 
       case "invalid_request_error": 
        //do some stuff 
        break; 
       default: 
        throw; 
      } 
     } 
     catch(Exception exception) 
     { 
      etc...etc.. 

確保你把你的StripeException首先被捕獲,否則你會得到編譯時錯誤。

在card_error情況下,您可能還希望根據發生的卡片錯誤類型採取措施。有像這些12(看看上面的鏈接) - 諸如「card_declined」或「invalid_cvc」的東西