Question

Hi i know this is a noob question but i cant find any documentation for this, I want to pay 1 or more items in the same transaction, but i get this error:

Exception in HttpConnection Execute: Invalid HTTP response The remote server returned an error: (400) Bad Request.

im hard-coding the item-list part, but i dont understan the differences beetween Amount.Total and the price of all my items price * quantity

public ActionResult CreatePayment(string description, decimal price, decimal tax = 0, decimal shipping = 0)
    {
        var viewData = new PayPalViewData();
        var guid = Guid.NewGuid().ToString();

        var paymentInit = new Payment
        {
            intent = "authorize",
            payer = new Payer
            {
                payment_method = "paypal"
            },
            transactions = new List<Transaction>
            {
                new Transaction 
                {
                    item_list = new ItemList{
                        items = new List<Item>{
                                new Item{
                                name = "item 1",
                                currency = "USD",
                                price = "20",
                                quantity = "2"
                                },
                                new Item{
                                name = "item 2",
                                currency = "USD",
                                price = "40",
                                quantity = "1"
                                },
                                new Item{
                                name = "item 3",
                                currency = "USD",
                                price = "40",
                                quantity = "1"
                                }
                        }
                    },
                    amount = new Amount
                    {
                        currency = "EUR",
                        total = (price + tax + shipping).ToString(),
                        details = new Details
                        {
                            subtotal = price.ToString(),
                            tax = tax.ToString(),
                            shipping = shipping.ToString()
                        }
                    },
                    description = description
                },
            },
            redirect_urls = new RedirectUrls
            {
                return_url = Utilities.ToAbsoluteUrl(HttpContext, String.Format("~/paypal/confirmed?id={0}", guid)),
                cancel_url = Utilities.ToAbsoluteUrl(HttpContext, String.Format("~/paypal/canceled?id={0}", guid)),
            },
        };

        viewData.JsonRequest = JObject.Parse(paymentInit.ConvertToJson()).ToString(Formatting.Indented);

        try
        {
            var accessToken = new OAuthTokenCredential(ConfigManager.Instance.GetProperties()["ClientID"], ConfigManager.Instance.GetProperties()["ClientSecret"]).GetAccessToken();
            var apiContext = new APIContext(accessToken);
            var createdPayment = paymentInit.Create(apiContext);

            var approvalUrl = createdPayment.links.ToArray().FirstOrDefault(f => f.rel.Contains("approval_url"));

            if (approvalUrl != null)
            {
                Session.Add(guid, createdPayment.id);

                return Redirect(approvalUrl.href);
            }

            viewData.JsonResponse = JObject.Parse(createdPayment.ConvertToJson()).ToString(Formatting.Indented);

            return View("Error", viewData);
        }
        catch (PayPalException ex)
        {
            viewData.ErrorMessage = ex.Message;

            return View("Error", viewData);
        }
    }

if i delete the item-list, it works but just 1 item described in the amount what can i do? do you have a guide for this? Paypal guids and demos are for one item only

Thanks

Was it helpful?

Solution

My guess, which is without seeing the full response is that your total does not add up with the details/item list. The validation adds up all the item values, which has to equal subtotal, the subtotal + tax etc (details) has to equal total.

sum (item.price * item.count) == sub total

sum of details == total

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top