Example #1
0
        //
        public static string FormatPaypalException(this PayPalException ex)
        {
            var exception = ex.InnerException as ConnectionException;

            if (exception == null)
            {
                return(Utils.FormatError(ex));
            }

            try
            {
                var repsonse = exception.Response;

                var result = _jsSerializer.Deserialize <PaypalExceptionToken>(repsonse);

                if (result.details == null || result.details.Count.Equals(0))
                {
                    return(result.message);
                }

                return(result.details.Aggregate(String.Empty, (current, detail) => current + ((String.IsNullOrEmpty(current) ? "" : "::") + (detail.field + "-" + detail.issue))));

                //foreach (var detail in result.details)
                //{
                //    message += (string.IsNullOrEmpty(message) ? "" : "::") + (detail.field + "-" + detail.issue);
                //}
            }
            catch (Exception)
            {
                return(Utils.FormatError(ex));
            }
        }
        /// <summary>
        /// Throws PartnerDomainException by parsing PayPal exception.
        /// </summary>
        /// <param name="ex">Exceptions from PayPal SDK.</param>
        private void ParsePayPalException(PayPalException ex)
        {
            if (ex is PaymentsException)
            {
                PaymentsException pe = ex as PaymentsException;

                // Get the details of this exception with ex.Details and format the error message in the form of "We are unable to process your payment –  {Errormessage} :: [err1, err2, .., errN]".
                StringBuilder errorString = new StringBuilder();
                errorString.Append(Resources.PaymentGatewayErrorPrefix);

                // build error string for errors returned from financial institutions.
                if (pe.Details != null)
                {
                    string errorName = pe.Details.name.ToUpper();

                    if (errorName == null || errorName.Length < 1)
                    {
                        errorString.Append(pe.Details.message);
                        throw new PartnerDomainException(ErrorCode.PaymentGatewayFailure).AddDetail("ErrorMessage", errorString.ToString());
                    }
                    else if (errorName.Contains("UNKNOWN_ERROR"))
                    {
                        throw new PartnerDomainException(ErrorCode.PaymentGatewayPaymentError);
                    }
                    else if (errorName.Contains("VALIDATION") && pe.Details.details != null)
                    {
                        // Check if there are sub collection details and build error string.
                        errorString.Append("[");
                        foreach (ErrorDetails errorDetails in pe.Details.details)
                        {
                            // removing extrataneous information.
                            string errorField = errorDetails.field;
                            if (errorField.Contains("payer.funding_instruments[0]."))
                            {
                                errorField = errorField.Replace("payer.funding_instruments[0].", string.Empty).ToString();
                            }

                            errorString.AppendFormat("{0} - {1},", errorField, errorDetails.issue);
                        }

                        errorString.Replace(',', ']', errorString.Length - 2, 2); // remove the last comma and replace it with ].
                    }
                    else
                    {
                        errorString.Append(Resources.PayPalUnableToProcessPayment);
                    }
                }

                throw new PartnerDomainException(ErrorCode.PaymentGatewayFailure).AddDetail("ErrorMessage", errorString.ToString());
            }

            if (ex is IdentityException)
            {
                // ideally this shouldn't be raised from customer experience calls.
                // can occur when admin has generated a new secret for an existing app id in PayPal but didnt update portal payment configuration.
                throw new PartnerDomainException(ErrorCode.PaymentGatewayIdentityFailureDuringPayment).AddDetail("ErrorMessage", Resources.PaymentGatewayIdentityFailureDuringPayment);
            }

            // few PayPalException types contain meaningfull exception information only in InnerException.
            if (ex is PayPalException && ex.InnerException != null)
            {
                throw new PartnerDomainException(ErrorCode.PaymentGatewayFailure).AddDetail("ErrorMessage", ex.InnerException.Message);
            }
            else
            {
                throw new PartnerDomainException(ErrorCode.PaymentGatewayFailure).AddDetail("ErrorMessage", ex.Message);
            }
        }
 /// <summary>
 /// Copy constructor provided by convenience for derived classes.
 /// </summary>
 /// <param name="ex">The original exception to copy information from.</param>
 protected PayPalException(PayPalException ex) : base(ex.Message, ex.InnerException)
 {
 }