DUPLICATE_REQUEST_ID

InvokeTech
Contributor
Contributor

Using the .NET PayPal SDK, I keep getting this 400 response every time I try to process a payment using a credit card in the sandbox environment. Exact same code works in the production. I've tried all different kinds of generated credit card number, and real ones. I've tried calling apiContext.ResetRequestId(). Still same error response. Here's my code:

 

            var config = ConfigManager.Instance.GetProperties();
            var accessToken = new OAuthTokenCredential(config).GetAccessToken();
            var apiContext = new APIContext(accessToken);
            apiContext.Config = ConfigManager.Instance.GetProperties();

            var transaction = new Transaction();
            transaction.amount = new Amount()
            {
                currency = "USD",
                total = string.Format("{0:N}", amount),
                details = new Details()
                {
                    subtotal = string.Format("{0:N}", amount)
                }
            };
            transaction.invoice_number = invoiceNumber;

            var payer = new Payer();
            payer.payment_method = "credit_card";
            payer.funding_instruments = new List<FundingInstrument>()
            {
                new FundingInstrument()
                {
                    credit_card = new CreditCard()
                    {
                        cvv2 = cvv2,
                        expire_month = expireMonth,
                        expire_year = expireYear + 2000,
                        first_name = firstName,
                        last_name = lastName,
                        number = cardNumber,
                        type = GetCreditCardType(cardNumber)
                    }
                }
            };

            var payment = new Payment();
            payment.intent = "sale";
            payment.payer = payer;
            payment.transactions = new List<Transaction>() { transaction };

            try
            {
                apiContext.ResetRequestId();
                var createdPayment = payment.Create(apiContext);

                if (createdPayment.state == "failed")
                    throw new Exception("Transaction not approved");

                if (createdPayment.transactions.Count > 0 && createdPayment.transactions[0].related_resources.Count > 0)
                {
                    string d = createdPayment.transactions[0].related_resources[0].sale.create_time;
                    // https://stackoverflow.com/questions/3556144/how-to-create-a-net-datetime-from-iso-8601-format
                    DateTime create_time = DateTime.Parse(d, null, System.Globalization.DateTimeStyles.RoundtripKind);
                    d = createdPayment.transactions[0].related_resources[0].sale.update_time;
                    DateTime update_time = DateTime.Parse(d, null, System.Globalization.DateTimeStyles.RoundtripKind);
                    return new Domain.Models.Payment()
                    {
                        PaymentID = createdPayment.id,
                        CreateTime = create_time,
                        UpdateTime = update_time,
                        TransactionID = createdPayment.transactions[0].related_resources[0].sale.id
                    };
                }
                else
                {
                    //return createdPayment.id;
                    throw new Exception("No sale for payment");
                }
            }
            catch (PayPal.PaymentsException ex)
            {
                // TODO figure out what the problem was so we can add it to the log and error message.
                //ex.
                if (ex.Details != null)
                {
                    string msg = ex.Details.message;
                    if (ex.Details.details != null)
                    {
                        foreach (var d in ex.Details.details)
                        {
                            msg += string.Format(" (field = {0}, issue = {1}, code = {2}, purchase_unit_reference_id = {3}) (invoice: {4}) ",
                                d.field, d.issue, d.code, d.purchase_unit_reference_id, invoiceNumber);
                        }
                    }

                    throw new Exception(msg, ex);
                }
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
Login to Me Too
1 REPLY 1

Oregon11
Advisor
Advisor

I'm going to post the link for the PayPal Merchant Technical Support Community.

 

https://www.paypal-community.com/mts

 

Login to Me Too

Haven't Found your Answer?

It happens. Hit the "Login to Ask the community" button to create a question for the PayPal community.