?
Solved

Pass shopping basket contents to PayPal

Posted on 2012-09-03
3
Medium Priority
?
575 Views
Last Modified: 2016-03-24
I'm working in ASP.NET 4 here and I've got some problems with the shopping cart I've set up. The cart itself functions fine, I just can't get the basket items to pass to PayPal.

The Total is passed okay, but if I have say 2 or 3 items in the basket, it just all gets passed as System.Collections.Generic.List`1.

Here is the code I have to work with if anyone can help me figure this out:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;

namespace ShoppingCartExample
{
    [Serializable]
    public class CartItem
    {
        private int _Id;
        private string _Genre;
        private string _Title;
        private string _Description;
        private int _Quantity;
        private double _Price;
        private double _subTotal;

        public CartItem()
        {
        }
        public CartItem(int Id, string Genre, string Title, string Description, int Quantity, double Price)
        {
            _Id = Id;
            _Genre = Genre;
            _Title = Title;
            _Description = Description;
            _Quantity = Quantity;
            _Price = Price;
            _subTotal = Quantity * Price;
        }
        public int Id
        {
            get { return _Id; }
            set { _Id = value; }
        }
        public string Genre
        {
            get { return _Genre; }
            set { _Genre = value; }
        }
        public string Title
        {
            get { return _Title; }
            set { _Title = value; }
        }
        public string Description
        {
            get { return _Description; }
            set { _Description = value; }
        }
        public int Quantity
        {
            get { return _Quantity; }
            set { _Quantity = value; }
        }
        public double Price
        {
            get { return _Price; }
            set { _Price = value; }
        }
        public double subTotal
        {
            get { return _Quantity * _Price; }
        }
    }

    [Serializable]
    public class Cart
    {
        private DateTime _dateCreated;
        private DateTime _lastUpdate;
        private List<CartItem> _items;

        public Cart()
        {
            if (this._items == null)
            {
                this._items = new List<CartItem>();
                this._dateCreated = DateTime.Now;
            }
        }

        public List<CartItem> Items
        {
            get { return _items; }
            set { _items = value; }
            
        }
 

        public void Insert(int Id, double Price, int Quantity, string Title)
        {
            int ItemIndex = ItemIndexofID(Id);
            if (ItemIndex == -1)
            {
                CartItem NewItem = new CartItem();
                NewItem.Id = Id;
                NewItem.Quantity = Quantity;
                NewItem.Price = Price;
                NewItem.Title = Title;
                _items.Add(NewItem);
            }
            else
            {
                _items[ItemIndex].Quantity += 1;
            }
            _lastUpdate = DateTime.Now;
        }

        public void Update(int RowID, int Id, int Quantity, double Price)
        {
            CartItem Item = _items[RowID];
            Item.Id = Id;
            Item.Quantity = Quantity;
            Item.Price = Price;
            _lastUpdate = DateTime.Now;
        }

        public void DeleteItem(int rowID)
        {
            _items.RemoveAt(rowID);
            _lastUpdate = DateTime.Now;
        }

        private int ItemIndexofID(int Id)
        {
            int index = 0;
            foreach (CartItem item in _items)
            {
                if (item.Id == Id)
                {
                    return index;
                }
                index += 1;
            }
            return -1;
        }

        public double Total
        {
            get
            {
                double t = 0;
                if (_items == null)
                {
                    return 0;
                }
                foreach (CartItem Item in _items)
                {
                    t += Item.subTotal;
                }
                return t;
            }
        }
    }
}

Open in new window


And here is how I am using PayPal:

<a href="https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_xclick&business=myemailhere&item_name=<%=Profile.SCart.Items %>&amount=<%=Profile.SCart.Total %>" style="border-style: none"><img src="https://www.paypal.com/en_US/i/btn/btn_xpressCheckout.gif" align="left" style="margin-right:7px;"></a>

Open in new window

0
Comment
Question by:afv003
  • 2
3 Comments
 
LVL 17

Assisted Solution

by:OmniUnlimited
OmniUnlimited earned 1500 total points
ID: 38362071
This is the disadvantage of working with PayPal.  PayPal can only process items one at a time (it has its own shopping cart feature, but every time you click the "Add to Shopping Cart" button, it will take you to PayPal to process that item before allowing you to return to your site and continue shopping.)

You will need to set up your PayPal Merchant account for use with PayPal shopping cart buttons and allow PayPal to process your shopping cart one item at a time if you want to use PayPal to process your orders.
0
 

Author Comment

by:afv003
ID: 38362224
I'm not sure if this will work for the way I have the cart configured. maybe you can help me understand a little better. I have all of the products in a database. The products.aspx page shows all of these products, or you can filter down by type, etc.

Is there a way to code the shopping cart button programatically to get the info about the item for the button? I have a rotating inventory and it wouldn't be possible to code each button manually from within paypal.
0
 
LVL 17

Accepted Solution

by:
OmniUnlimited earned 1500 total points
ID: 38362248
You run into a problem because each product must have its own hosted_button_id which you would need to set up individually for each item you would want to sell.  This hosted_button_id is generated automatically by PayPal when the button for that product is created.  You could store these hosted_button_id's in your database with the other product information, but this is an indispensible piece of information that needs to be sent with the other information (e.g. qty, cmd, etc.) for your product to be processed correctly.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question