1

私は mvc を使用しており、カート全体をペイパルに送ろうとしています。カート全体ではなく、単一のアイテムを追加できます。

これが私のコントローラーです:

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 = "Little Bow Peep: あなたのカート"; }

あなたのカート

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

}

public class CartLine { public Product Product { get; 設定; } public int 数量 { get; 設定; } }

何か助けて???

ありがとう...

4

0 に答える 0