private PaymentResult FinalizePayment(string nonce, string sPid) { int pid = int.Parse(sPid); var paymentInfo = _posService.GetPaymentInfo(pid); var payResult = _braintreeService.Pay(new PaymentContext(nonce, paymentInfo)); string error = ""; string transactionId = ""; string info = JsonConvert.SerializeObject(payResult); if (payResult.Success == false) { // use dictionary for manage error payment // this values are present in table of Braintree // errors that are not handled will go to the error log so you can view and manage them //error = payResult.ResponseText; var braintreeError = GetBraintreeError(); if (!string.IsNullOrWhiteSpace(payResult.ResponseCode) && braintreeError.ContainsKey(payResult.ResponseCode)) { error = braintreeError.FirstOrDefault(er => er.Key == payResult.ResponseCode).Value; Logger.Information(string.Format(@"Error on payment for order {0}: Error {1}. Details: {2}", paymentInfo.ContentItemId, payResult.ResponseText, info)); } else { // check is number int errCode; if (!int.TryParse(payResult.ResponseCode, out errCode)) { errCode = -1; } // this range of errors has same error // this default value are in table of errors of Braintree if (errCode >= 2101 && errCode <= 2999) { error = T("The customer's bank is unwilling to accept the transaction. The customer will need to contact their bank for more details regarding this generic decline.").Text; Logger.Information(string.Format(@"Error on payment for order {0}: Error {1}. Details: {2}", paymentInfo.ContentItemId, payResult.ResponseText, info)); } else { error = T("Transaction failed. The customer will have to try again to make the payment").Text; Logger.Error(string.Format(@"Error on payment for order {0}: Error {1}. Details: {2}", paymentInfo.ContentItemId, payResult.ResponseText, info)); } } } // even failed transactions may be recorded transactionId = payResult?.TransactionId ?? ""; _posService.EndPayment(pid, payResult.Success, error, info, transactionId); return(new PaymentResult { Pid = pid, Success = payResult.Success, Error = error, TransactionId = transactionId }); }
public HttpResponseMessage Post() { string esito = ""; var locRequest = HttpContext.Current.Request; string nonce = locRequest["payment_method_nonce"]; string sAmount = locRequest["amount"]; decimal amount = decimal.Parse(sAmount, CultureInfo.InvariantCulture); var payResult = _braintreeService.Pay(nonce, amount, null); esito = (payResult.Success) ? "OK" : "KO"; // create response and return var result = new HttpResponseMessage(HttpStatusCode.OK); result.Content = new System.Net.Http.StringContent(esito, Encoding.UTF8, "text/plain"); return(result); }