/// <summary> /// Constructs exception composing details from response, method specific error description and actual inner exception /// </summary> /// <param name="response">Response of failed request</param> /// <param name="stripeErrorMessage">Method specific error description</param> /// <param name="inner">Actual inner exception</param> /// <returns>Composed exception</returns> public static PaymentStripeException Compose(HttpWebResponse response, string stripeErrorMessage, Exception inner) { int statusCode = (int)response.StatusCode; string responseErrMsg = string.Empty; try { using(var reponseStream = response.GetResponseStream()) { using (var responseReader = new System.IO.StreamReader(reponseStream)) { string responseStr = responseReader.ReadToEnd(); dynamic responseObj = responseStr.JSONToDynamic(); responseErrMsg = responseObj.error.message; } } } catch (Exception) { // dlatushkin 2014/04/07: // there is no way to test some cases (50X errors for example) // so try/catch is used to swallow exception } string specificError = System.Environment.NewLine; if (responseErrMsg.IsNotNullOrWhiteSpace()) specificError += StringConsts.PAYMENT_STRIPE_ERR_MSG_ERROR.Args( responseErrMsg) + System.Environment.NewLine; specificError += stripeErrorMessage; PaymentStripeException ex = null; if (statusCode == 400) ex = new PaymentStripeException(StringConsts.PAYMENT_STRIPE_400_ERROR + specificError, inner); if (statusCode == 401) ex = new PaymentStripeException(StringConsts.PAYMENT_STRIPE_401_ERROR + specificError, inner); if (statusCode == 402) ex = new PaymentStripeException(StringConsts.PAYMENT_STRIPE_402_ERROR + specificError, inner); if (statusCode == 404) ex = new PaymentStripeException(StringConsts.PAYMENT_STRIPE_404_ERROR + specificError, inner); if (statusCode == 500 || statusCode == 502 || statusCode == 503 || statusCode == 504) ex = new PaymentStripeException(StringConsts.PAYMENT_STRIPE_50X_ERROR.Args(statusCode) + specificError, inner); return ex; }
/// <summary> /// Overload of Refund method with Stripe-typed session parameter /// Developers, don't call this method directly. Call Transaction.Refund instead. /// </summary> public Transaction Refund(StripeSession session, ITransactionContext context, ref Transaction charge, Amount?amount = null, string description = null, object extraData = null) { var fromActualData = PaySystemHost.AccountToActualData(context, charge.From); var refundAmount = amount ?? charge.Amount; try { var bodyPrms = new Dictionary <string, string>() { { PRM_AMOUNT, ((int)((refundAmount.Value * 100))).ToString() } }; if (description.IsNotNullOrWhiteSpace()) { bodyPrms.Add(PRM_REASON, description); } var prms = new WebClient.RequestParams() { Uri = new Uri(REFUND_URI.Args(charge.ProcessorToken)), Caller = this, UName = session.SecretKey, Method = HTTPRequestMethod.POST, BodyParameters = bodyPrms }; dynamic obj = WebClient.GetJson(prms); dynamic lastRefund = ((NFX.Serialization.JSON.JSONDataArray)obj.refunds.Data).First(); var created = ((long)obj.created).FromSecondsSinceUnixEpochStart(); var taId = PaySystemHost.GenerateTransactionID(session, context, TransactionType.Refund); var refundTA = new Transaction(taId, TransactionType.Refund, Account.EmptyInstance, charge.From, this.Name, lastRefund["id"], refundAmount, created, description, isCaptured: true, canRefund: false); StatRefund(charge, amount); return(refundTA); } catch (Exception ex) { var wex = ex as System.Net.WebException; if (wex != null) { var response = wex.Response as System.Net.HttpWebResponse; if (response != null) { string errorMessage = this.GetType().Name + ".Refund(money: {0}, card: {1}, description: '{2}')" .Args(charge.Amount, fromActualData.AccountNumber, description); PaymentStripeException stripeEx = PaymentStripeException.Compose(response, errorMessage, wex); if (stripeEx != null) { throw stripeEx; } } } StatRefundError(); throw new PaymentStripeException(StringConsts.PAYMENT_CANNOT_CAPTURE_CAPTURED_PAYMENT_ERROR + this.GetType() + " .Refund(session='{0}', charge='{1}')".Args(session, charge), ex); } }
private Transaction doCharge(StripeSession session, Account from, Account to, Amount amount, bool capture = true, string description = null, object extraData = null) { var fromActualData = session.FetchAccountData(from); try { var bodyPrms = new Dictionary <string, string>() { { PRM_AMOUNT, ((int)((amount.Value * 100))).ToString() }, { PRM_CURRENCY, amount.CurrencyISO.ToString().ToLower() }, { PRM_DESCRIPTION, description }, { PRM_CAPTURE, capture.ToString() } }; fillBodyParametersFromAccount(bodyPrms, fromActualData); var prms = new WebClient.RequestParams(this) { UserName = session.SecretKey, Method = HTTPRequestMethod.POST, BodyParameters = bodyPrms }; dynamic obj = WebClient.GetJsonAsDynamic(new Uri(CHARGE_URI), prms); var created = ((long)obj.created).FromSecondsSinceUnixEpochStart(); var taId = session.GenerateTransactionID(TransactionType.Charge); var ta = new Transaction(taId, TransactionType.Charge, TransactionStatus.Success, from, to, this.Name, obj.AsGDID, created, amount, description: description, extraData: extraData); if (capture) { ta.__Apply(Transaction.Operation.Capture(TransactionStatus.Success, created, token: obj.AsGDID, description: description, amount: amount.Value, extraData: extraData)); } StatCharge(amount); return(ta); } catch (Exception ex) { StatChargeError(); var wex = ex as System.Net.WebException; if (wex != null) { var response = wex.Response as System.Net.HttpWebResponse; if (response != null) { string errorMessage = this.GetType().Name + ".Charge(money: {0}, card: {1}, amount: '{2}')".Args(amount.Value, fromActualData.AccountID, amount); var stripeEx = PaymentStripeException.Compose(response, errorMessage, wex); throw stripeEx; } } throw new PaymentStripeException(StringConsts.PAYMENT_CANNOT_CHARGE_PAYMENT_ERROR + this.GetType() + " .Capture(session='{0}', card='{1}', amount='{2}')".Args(session, from, amount), ex); } }
/// <summary> /// Constructs exception composing details from response, method specific error description and actual inner exception /// </summary> /// <param name="response">Response of failed request</param> /// <param name="stripeErrorMessage">Method specific error description</param> /// <param name="inner">Actual inner exception</param> /// <returns>Composed exception</returns> public static PaymentStripeException Compose(HttpWebResponse response, string stripeErrorMessage, Exception inner) { int statusCode = (int)response.StatusCode; string responseErrMsg = string.Empty; try { using (var reponseStream = response.GetResponseStream()) { using (var responseReader = new System.IO.StreamReader(reponseStream)) { string responseStr = responseReader.ReadToEnd(); dynamic responseObj = responseStr.JSONToDynamic(); responseErrMsg = responseObj.error.message; } } } catch (Exception) { // dlatushkin 2014/04/07: // there is no way to test some cases (50X errors for example) // so try/catch is used to swallow exception } string specificError = System.Environment.NewLine; if (responseErrMsg.IsNotNullOrWhiteSpace()) { specificError += StringConsts.PAYMENT_STRIPE_ERR_MSG_ERROR.Args(responseErrMsg) + System.Environment.NewLine; } specificError += stripeErrorMessage; PaymentStripeException ex = null; if (statusCode == 400) { ex = new PaymentStripeException(StringConsts.PAYMENT_STRIPE_400_ERROR + specificError, inner); } if (statusCode == 401) { ex = new PaymentStripeException(StringConsts.PAYMENT_STRIPE_401_ERROR + specificError, inner); } if (statusCode == 402) { ex = new PaymentStripeException(StringConsts.PAYMENT_STRIPE_402_ERROR + specificError, inner); } if (statusCode == 404) { ex = new PaymentStripeException(StringConsts.PAYMENT_STRIPE_404_ERROR + specificError, inner); } if (statusCode == 500 || statusCode == 502 || statusCode == 503 || statusCode == 504) { ex = new PaymentStripeException(StringConsts.PAYMENT_STRIPE_50X_ERROR.Args(statusCode) + specificError, inner); } return(ex); }