Paypal Xamain: How to add multiple item dynamically using code behind?

Hello,

I am currently using PayPal.Forms NuGet Package for Paypal.

I want to add multiple item using code behind. But when I used the following code it only add last line of PayPal Items to PayPal list:

//Code Starts
int CartCount=_CarttList.Length;

PayPalItem[] PayPalItems = new PayPalItem[] { };

for(int i=0;i<CartCount;i++)
{
PayPalItems = new PayPalItem[] { new PayPalItem(_CarttList[i].ProductName, _CarttList[i].Qty, _CarttList[i].Price, _CarttList[i].Currency, _CarttList[i].SKU };
}

var result = await CrossPayPalManager.Current.Buy(new PayPalItem[] { PayPalItemTest },Shipping, Tax);
//Code End

Any help would be appreciated.

Thank you.

Best Answer

Answers

  • Sorry posted a wrong code. Instead of this:

    var result = await CrossPayPalManager.Current.Buy(new PayPalItem[] { PayPalItemTest },Shipping, Tax);

    My code Line is this:

    var result = await CrossPayPalManager.Current.Buy(PayPalItems ,Shipping, Tax);

  • JuniorJiangJuniorJiang Member, Xamarin Team Xamurai
    edited December 2018

    Hi @masroorejaz1985 , the problem is here:

    PayPalItem[] PayPalItems = new PayPalItem[] { };
    for(int i=0;i<CartCount;i++)
    {
    PayPalItems = new PayPalItem[] { new PayPalItem(_CarttList[i].ProductName, _CarttList[i].Qty, _CarttList[i].Price, _CarttList[i].Currency, _CarttList[i].SKU };
    }
    var result = await CrossPayPalManager.Current.Buy(PayPalItems ,Shipping, Tax);
    

    In the for loop , do not add PayPalItem to PayPalItems , from these code ,just every create new PayPalItem[] to PayPalItems . Then every time just can get the last line of PayPal Items.

    Workround:

    for(int i=0;i<CartCount;i++)
        {
     //   PayPalItems just add new PayPalItem like this .Beacuse not used PayPal , maybe this `add `method not completely written correctly.You also can refer to.
           PayPalItems.add(new PayPalItem(xxx));
        }
    
  • Hello @JuniorJiang

    You mean something like this:

    for(int i=0;i<CartCount;i++)
    {
    var PayPalItems = new PayPalItem[] { new PayPalItem(_CarttList[i].ProductName, _CarttList[i].Qty, _CarttList[i].Price, _CarttList[i].Currency, _CarttList[i].SKU };
    }

    I have tried it but the problem still remains. It's only adding the last line.

    The problem with adding multiple item is you have to add it in a single line, example:

    PayPalItems = new PayPalItem[] {
    new PayPalItem("sample item #3 with a longer name", 1, new Decimal (37.99), "USD", "sku-33333"),
    new PayPalItem("sample item #1", 1, new Decimal (87.50), "USD","sku-12345678")
    };

    Which is impossible to do when using loop.

    Kindly provide any other workaround.

    Thank you

  • JuniorJiangJuniorJiang Member, Xamarin Team Xamurai

    @masroorejaz1985 not this , not understand the workround.Just like this.

    for(int i=0;i<CartCount;i++)
        {
     //   PayPalItems just add new PayPalItem like this .Beacuse not used PayPal , maybe this `add `method not completely written correctly.You also can refer to.
           PayPalItems.add(new PayPalItem(_CarttList[i].ProductName, _CarttList[i].Qty, _CarttList[i].Price, _CarttList[i].Currency, _CarttList[i].SKU));
        }
    
  • @JuniorJiang

    PayPalItems.add Does not exist.

    Can you please provide a complete/detail example. Maybe I am not getting it.

    Thank you

  • @JuniorJiang

    Thank you so much. It's working fine now.

Sign In or Register to comment.