如何将购物车物品发送到贝宝

时间:2012-07-30 14:19:01

标签: c# asp.net-mvc paypal

我正在使用mvc而我正试图将我的整个购物车发送到PayPal。我可以让它添加一个项目而不是整个购物车。

这是我的控制器:

public ActionResult PostToPayPal(Cart cart, ShippingDetails shippingInfo)
{
    PayPal paypal = new PayPal();
    paypal.cmd = "_xclick";
    paypal.business = ConfigurationManager.AppSettings["BusinessAccountKey"];

    bool useSandbox = Convert.ToBoolean(ConfigurationManager.AppSettings["UseSandBox"]);
    if (useSandbox)
        ViewBag.actionURL = "https://www.sandbox.paypal.com/cgi-bin/webscr";
    else
    {
        ViewBag.actionURL = "https://www.paypal.com/cgi-bin/webscr";
    }

    paypal.cancel_return = System.Configuration.ConfigurationManager.AppSettings["CancelURL"];
    paypal.@return = ConfigurationManager.AppSettings["ReturnURL"];
    paypal.notify_url = ConfigurationManager.AppSettings["NotifyURL"];

    paypal.currency_code = ConfigurationManager.AppSettings["CurrencyCode"];

    foreach (var lines in cart.Lines )
    {
        paypal.item_name = lines.Product.Name;
        paypal.amount = lines.Product.Price.ToString("c");
    }
    //paypal.item_name = item;
    //paypal.amount = amount;
    return View(paypal);

}

以下是我的观点:

@model SportsStore.Domain.Entities.PayPal

@{
    ViewBag.Title = "PostToPayPal";
}

<h2>PostToPayPal</h2>

        <form id="frm" action="@ViewBag.actionURL">

                @Html.HiddenFor(model => model.cmd)
                @Html.HiddenFor(model => model.business)
                @Html.HiddenFor(model => model.no_shipping)
                @Html.HiddenFor(model => model.@return)
                @Html.HiddenFor(model => model.cancel_return)
                @Html.HiddenFor(model => model.notify_url)
                @Html.HiddenFor(model => model.currency_code)
                @Html.HiddenFor(model => model.item_name)
                @Html.HiddenFor(model => model.amount)

            <p>
                <h4>redirecting to paypal...</h4>
            </p>



        </form>

<script type="text/javascript" language="javascript">
    $(this.document).ready(function () {
        var frm = $("form");
        frm.submit();
    })
</script>

这是我的购物车:

@model SportsStore.WebUI.Models.CartIndexViewModel

@ {     ViewBag.Title =“小弓窥视:你的购物车”; }

您的购物车

<table width="90%" align="center">

    <thead>
        <tr>
            <th align="center">Quantity</th>
            <th align="left">Item</th>
            <th align="right">Price</th>
            <th align="right">Subtotal</th>
        </tr>
    </thead>

    <tbody>
        @foreach(var line in Model.Cart.Lines)
        {
            <tr>
                <td align="center">@line.Quantity</td>
                <td align="left">@line.Product.Name</td>
                <td align="right">@line.Product.Price.ToString("c")</td>
                <td align="right">@((line.Quantity * line.Product.Price).ToString("c"))</td>
                <td>
                    @using(Html.BeginForm("RemoveFromCart", "Cart"))
                    {
                        @Html.Hidden("ProductId", line.Product.ProductID)
                        @Html.HiddenFor(x => x.ReturnUrl)
                        <input class="actionButtons" type="submit" value="Remove"/>
                    }
                </td>
            </tr>
        }
    </tbody>

    <tfoot>
        <tr>
            <td colspan="3" align="right">Total:</td>
            <td align="right">
                @Model.Cart.ComputeTotalValue().ToString("c")
            </td>
        </tr>
    </tfoot>

</table>

<p align="center" class="actionButtons">
    <a href="@Model.ReturnUrl">Continue Shopping</a>
    @Html.ActionLink("Checkout now", "Checkout")
</p>

购物车资格代码:

private readonly List<CartLine> lineCollection = new List<CartLine>();

public IEnumerable<CartLine> Lines
{
    get { return lineCollection; }
}

public void AddItem(Product product, int quantity)
{
    CartLine line = lineCollection
        .Where(p => p.Product.ProductID == product.ProductID)
        .FirstOrDefault();

    if (line == null)
    {
        lineCollection.Add(new CartLine {Product = product, Quantity = quantity});
    }
    else
    {
        line.Quantity += quantity;
    }
}

public void RemoveLine(Product product)
{
    lineCollection.RemoveAll(l => l.Product.ProductID == product.ProductID);
}

public decimal ComputeTotalValue()
{
    return lineCollection.Sum(e => e.Product.Price*e.Quantity);
}

public void Clear()
{
    lineCollection.Clear();
}

}

公共类CartLine {     公共产品{get;组; }     public int Quantity {get;组; } }

任何帮助???

...谢谢

0 个答案:

没有答案