将PaymentDetailsItemType数组传递给PayPalAPI

时间:2012-09-30 17:16:54

标签: c# paypal

我在ASP.Net C#4.0中使用PayPal沙箱。我添加了以下Web引用:

 https://www.paypalobjects.com/wsdl/PayPalSvc.wsdl 

如果我有1件商品(请给我两件T恤),我可以将它传递给PayPal对象并完成我的交易,没有任何问题:

       var item = new PayPalAPIHelper.PayPalWS.PaymentDetailsItemType
        {
            Quantity = 2, //etc...etc
        };
         var paymentDetails = new  PayPalAPIHelper.PayPalWS.PaymentDetailsType();
        paymentDetails.PaymentDetailsItem = new PaymentDetailsItemType[]{item};

现在我的困境是当顾客想要不同的物品(1件T恤,1条裤子)。现在我需要多个PaymentDetailsItemType对象传递给paymentDetails对象。我试过这个:

        //now i have an array of different items 
        var items = new PayPalAPIHelper.PayPalWS.PaymentDetailsItemType[3];
        items[0].Description = "T-shirt";
        items[1].Description = "Jeans";
        paymentDetails.PaymentDetailsItem = new PaymentDetailsItemType[]{items};

我收到此错误:

       Cannot implicitly convert type 'PayPalAPIHelper.PayPalWS.PaymentDetailsItemType[]' 
       to 'PayPalAPIHelper.PayPalWS.PaymentDetailsItemType'    

我可以这样做并编译:

   paymentDetails.PaymentDetailsItem = new PaymentDetailsItemType[]{items[0],items[1]};

但是当我运行它时,当我调用PayPal服务时,我在元素'Quantity'中得到数据类型''不匹配:

            PayPalAPIHelper.PayPalWS.PaymentDetailsType paymentDetails = new PayPalAPIHelper.PayPalWS.PaymentDetailsType();

        paymentDetails.PaymentDetailsItem = new PayPalAPIHelper.PayPalWS.PaymentDetailsItemType[] { items[0],items[1] };

        reqDetails.PaymentDetails = new PayPalAPIHelper.PayPalWS.PaymentDetailsType[] { paymentDetails };

        PayPalAPIHelper.PayPalWS.SetExpressCheckoutReq req = new PayPalAPIHelper.PayPalWS.SetExpressCheckoutReq()
        {
            SetExpressCheckoutRequest = new PayPalAPIHelper.PayPalWS.SetExpressCheckoutRequestType()
            {
                Version = Version,
                SetExpressCheckoutRequestDetails = reqDetails
            }
        };

        // query PayPal and get token
         //error occurs here
        PayPalAPIHelper.PayPalWS.SetExpressCheckoutResponseType resp = BuildPayPalWebservice().SetExpressCheckout(req); //this is where the error occurs

如何将多种类型传递给PayPal API Web服务,以便用户可以订购不同的商品?提前致谢

1 个答案:

答案 0 :(得分:2)

首先,你已经有了一个数组,所以仅仅为了分配到paymentDetails.PaymentDetailsItem而创建一个新数组是没有意义的:

var items = new PayPalAPIHelper.PayPalWS.PaymentDetailsItemType[3];
…
paymentDetails.PaymentDetailsItem = items;  // this is wrong: new PaymentDetailsItemType[]{items};

通过编写new PaymentDetailsItemType[]{items},您可以创建一个新数组并尝试使用另一个数组PaymentDetailsItemType(类型为items的内容)初始化其第一个项目(PaymentDetailsItemType[]类型) - 因此不相容)。

其次,确保填充数组中所有项目的所有必需属性。最有可能的是,你应该做一件T恤和一件牛仔裤的例子:

items[0].Description = "T-shirt";
items[0].Quantity = 1;
items[1].Description = "Jeans";
items[1].Quantity = 1;