2

I am building a ecommerce c# mvc Asp.net app where i want that payer can pay on different accounts from which he/she has bought item.

I want to send payment from payer to custom payee i have used paypal.api sdk i added payee in it but it gives error i.e " exception detail :{"name":"MALFORMED_REQUEST","message":"Incoming JSON request does not map to API"

//// Pyapal.api sdk use
    public PaypalHelper CreatePayment(CreatePaymentHelper helper)
        {
            var apiContext = GetApiContext();

            string ProfileId = GetWebProfile(apiContext, helper.ExperienceProfileName).id;
            //ItemList itemList = new ItemList();
            //foreach (var i in helper.Items)
            //{
            //    Item item = new Item()
            //    {
            //        description = i.Description,
            //        currency = i.Currency,
            //        quantity = i.Quantity.ToString(),
            //        price = i.Price.ToString(), /// decimal strng
            //    };
            //    itemList.items.Add(item);
            //}
            var payment = new Payment()
            {
                experience_profile_id = ProfileId,
                intent = "order",
                payer = new Payer()
                {
                    payment_method = "paypal"
                },
                transactions = new List<Transaction>()
                {
                    new Transaction()
                    {
                        description = helper.TransactionDescription,
                        amount = new Amount()
                        {
                            currency = helper.CurrencyName,
                            total = helper.TransactionAmount.ToString()/// decimal and must be string
                        },

                       // item_list = itemList,
                        item_list = new ItemList()
                        {
                            items = helper.Items.Select(x=> new Item()
                            {
                                description = x.Description,
                                currency = x.Currency,
                                quantity = x.Quantity.ToString(),
                                price = x.Price.ToString()
                            }).ToList()
                        },

                    },

                },
                redirect_urls = new RedirectUrls()
                {
                    return_url = helper.ReturnUrl.ToString(),
                    cancel_url = helper.CancelUrl.ToString()
                    //Url.Action("action", "controller", "routeVaslues", Request.Url.Scheme);
                },
                payee = new Payee { email = "RecieverEmail"}
            };

            /// send payment to paypal
            var createdPayment = payment.Create(apiContext);


            ///get approval url where we need to send our user
            var ApprovalUrl = createdPayment.links.FirstOrDefault(x => x.rel.Equals("approval_url", StringComparison.OrdinalIgnoreCase));

            return new PaypalHelper
            {
                CreatedPaymentId = createdPayment.id,
                RedirectUrl = ApprovalUrl.href
            };
        }

Then i tried Chained method but couldn't get it! the request succeded somehow but couldnt get payment nor it authorize the payer!

   public void AdaptiveChainedMethod()
        {

            ReceiverList receivers = new ReceiverList();
            Receiver receiver = new Receiver();
            receiver.email = "ReCIEVEREMAIL";
            receiver.amount = Convert.ToDecimal(22.00);
            receivers.receiver.Add(receiver);

            ReceiverList receiverList = new ReceiverList(receivers.receiver);

            PayRequest payRequest = new PayRequest();
            payRequest.actionType = "CREATE";
            payRequest.receiverList = receiverList;
            payRequest.currencyCode = "USD";
            payRequest.cancelUrl = "http://localhost:44382/cont/Act";
            payRequest.returnUrl= "http://localhost:44382/cont/Act";
            payRequest.requestEnvelope = new RequestEnvelope("en_US");

            APIContext apiContext = GetApiContext();

            Dictionary<string, string> config = new Dictionary<string, string>();
            config.Add("mode", "sandbox");
            config.Add("clientId", "id");
            config.Add("clientSecret", "seceret");
            config.Add("account1.apiUsername", "username");
            config.Add("account1.apiPassword", "pass");
            config.Add("account1.apiSignature", "signature");
            config.Add("account1.applicationId", "APP-80W284485P519543T"); // static account id

            AdaptivePaymentsService service = new AdaptivePaymentsService(config);
            PayResponse response = service.Pay(payRequest);

        }

then I tried PayPalCheckoutSdk.Core PayPalCheckoutSdk.Orders sdk but its API request got stuck every time i hit it and never answers!

         public async static Task<string> test()
        {
            OrderRequest orderRequest = new OrderRequest()
            {

                CheckoutPaymentIntent = "CAPTURE",

                ApplicationContext = new ApplicationContext
                {
                    ReturnUrl = "http://localhost:44382/cont/act",
                    CancelUrl = "http://localhost:44382/cont/act"
                },
                PurchaseUnits = new List<PurchaseUnitRequest>
                 {
                 new PurchaseUnitRequest {

        AmountWithBreakdown = new AmountWithBreakdown
        {
          CurrencyCode = "USD",
          Value = "220.00"
        },
        Payee = new Payee
        {
          Email = "RecieverEmail"
        }
      }
    }
            };
            var request = new OrdersCreateRequest();
          //  request.Prefer("return=representation");
            request.RequestBody(orderRequest);
           HttpResponse response = await client().Execute(request);
            var statusCode = response.StatusCode;
            Order result = response.Result<Order>();
            return "string";
        }



        public static PayPalHttpClient client()
        {
            string clientId = "clientid";
            string clientSecret = "secret";

            // Creating a sandbox environment
            PayPalEnvironment environment = new SandboxEnvironment(clientId, clientSecret);

            // Creating a client for the environment
            PayPalHttpClient client = new PayPalHttpClient(environment);
            return client;
        }
  • `:{"name":"MALFORMED_REQUEST","message":"Incoming JSON request does not map to API"` Please log and provide full detail. What is the full data of the request and/or response? We cannot help you resolve a malformed JSON issue if you do not show us what you are actually sending. – Preston PHX Apr 23 '20 at 21:08
  • The other things you are attempting, especially Adaptive Chained, will not work and cannot be used. – Preston PHX Apr 23 '20 at 21:09
  • i have provided the request snippet (1st code) in my question! i have tried 3 ways the first one response's is malformed request! – Muhammad Afnan Apr 23 '20 at 21:50
  • Response from request is {"name":"MALFORMED_REQUEST","message":"Incoming JSON request does not map to API request","information_link":"https://developer.paypal.com/webapps/developer/docs/api/#MALFORMED_REQUEST","debug_id":"f84f21424dc22"} – Muhammad Afnan Apr 23 '20 at 21:57
  • Well that's not enough to go on, can you log the request's posted data (the runtime value that is sent over the wire, not your code that generates it) or at least the HTTP headers of the response as well? – Preston PHX Apr 24 '20 at 01:57

0 Answers0