3

I originally posted this question as a comment on a different SO thread (older: PayPal REST API .net SDK - 400 Bad Requests) but since I haven't seen any response, I figured a question is probably best.

Without changing the RestAPISDK myself, is there a way to get it to return the error messages back to my code when a validation error occurs? Right now the only course of action I have is to read the log file to see what happened, but that does me no good when trying to report back to a user why charging failed for something as simple as the card number isn't valid, it's expired, etc. Right now we get back a 400 Bad Request, but since the failure is logged the API obviously sees what happened and just doesn't report it.

Here is the code I am using - the error gets hit when I do payment.Create(apiContext).

var card = new PayPal.Api.Payments.CreditCard
        {
            billing_address = new PayPal.Api.Payments.Address
            {
                city = customer.BillingAddress.City,
                country_code = customer.BillingAddress.Country,
                line1 = customer.BillingAddress.Address1,                    
                postal_code = customer.BillingAddress.PostalCode,
                state = customer.BillingAddress.StateProvince
            },
            cvv2 = customer.CreditAccount.SecurityCode,
            expire_month = customer.CreditAccount.ExpirationMonth,
            expire_year = customer.CreditAccount.ExpirationYear,
            first_name = customer.FirstName,
            last_name = customer.LastName,
            number = customer.CreditAccount.CardNumber
        };

        //only send line 2 if it exists, otherwise it'll error out
        if (!string.IsNullOrEmpty(customer.BillingAddress.Address2))
            card.billing_address.line2 = customer.BillingAddress.Address2;

        switch (customer.CreditAccount.CardType)
        {
            case CardTypes.AmericanExpress:
                card.type = "amex";
                break;

            default:
                card.type = customer.CreditAccount.CardType.ToString().ToLower();
                break;
        }

        var amt = new PayPal.Api.Payments.Amount
        {
            currency = "USD",
            details = new PayPal.Api.Payments.Details
            {
                shipping = Math.Round(customer.CreditAccount.Shipping, 2).ToString(),
                subtotal = Math.Round(customer.CreditAccount.Amount, 2).ToString(),
                tax = Math.Round(customer.CreditAccount.Tax, 2).ToString()
            },
            total = Math.Round(customer.CreditAccount.GrandTotal, 2).ToString()
        };

        var payer = new PayPal.Api.Payments.Payer
        {
            funding_instruments = (new FundingInstrument[] { new FundingInstrument { credit_card = card } }).ToList(),
            payment_method = "credit_card"
        };

        var payment = new Payment
        {
            intent = "sale",
            payer = payer,
            transactions = (new Transaction[] {
                new Transaction { description = customer.CreditAccount.Description, amount = amt }
            }).ToList()
        };

        try
        {
            var accessToken = new PayPal.OAuthTokenCredential(this.Configuration.ClientID, this.Configuration.ClientSecret).GetAccessToken();
            var apiContext = new PayPal.APIContext(accessToken);

            var result = payment.Create(apiContext);

            if (result.state == "approved")
                creditResponse = new CreditResponse { AuthorizationCode = result.id, Status = CreditResponseTypes.Success, TransactionId = result.id };
            else
                creditResponse = new CreditResponse { Status = CreditResponseTypes.Declined };
        }
        catch (Exception ex)
        {
            creditResponse = new CreditResponse
            {
                Message = ex.ToString(),
                Status = CreditResponseTypes.Error
            };
        }

Edited: added sample code

Community
  • 1
  • 1
Scott Salyer
  • 2,165
  • 7
  • 45
  • 82
  • There is something bad with your code. Post the code you have written using PAYPAL SDK. – Murtaza Khursheed Hussain Jul 17 '13 at 08:13
  • Code posted - it's pretty generic. – Scott Salyer Jul 17 '13 at 14:24
  • I wouldn't use the Paypal API do do this, I would do it before you send it to Paypal. Google the LUHN algorithm for your respected language. You will find tons of links. http://en.wikipedia.org/wiki/Luhn_algorithm – user2592547 Jul 17 '13 at 17:45
  • 1
    It's not that I'm trying to utilize their API just for validation, rather whenever a card fails to process (why isn't relevant) an exception is thrown and the real error just shows up in their log file. I need a way to get that real error message back instead of just a hard exception. – Scott Salyer Jul 17 '13 at 21:13

0 Answers0