首页 > 解决方案 > 如何使用 c# asp.net 在贝宝中向自定义收款人发送付款?

问题描述

我正在构建一个电子商务 c# mvc Asp.net 应用程序,我希望付款人可以在他/她购买商品的不同帐户上付款。

我想将付款从付款人发送到自定义收款人我使用了 paypal.api sdk 我在其中添加了收款人,但它给出了错误,即“异常详细信息:{“name”:“MALFORMED_REQUEST”,“message”:“传入的 JSON 请求没有映射到 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
            };
        }

然后我尝试了链式方法,但无法得到它!请求以某种方式成功,但无法获得付款,也无法授权付款人!

   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);

        }

然后我尝试了 PayPalCheckoutSdk.Core PayPalCheckoutSdk.Orders sdk,但是每次我点击它时它的 API 请求都会卡住并且永远不会回答!

         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;
        }

标签: asp.net-mvcpaypalpaypal-sandboxpaypal-adaptive-paymentspaypal-rest-sdk

解决方案


推荐阅读