MVC PayPal整合

时间:2016-04-25 13:09:00

标签: c# asp.net-mvc entity-framework paypal code-first

我的网站正常运行,因此用户可以通过卡付款但是现在我还需要使用PayPal,我似乎无法将产品从购物车发送到paypal控制器,每个产品都需要在订单中发送。

这是我的PayPal控制器;

namespace T_shirt_Company_v3.Controllers
{
    public class PayPalController : Controller
    {
        public ActionResult RedirectFromPaypal()
        {
            return View();
        }
        public ActionResult CancelFromPaypal()
        {
            return View();
        }
        public ActionResult NotifyFromPaypal()
        {
            return View();
        }



        public ActionResult ValidateCommand(string RecordId, string CartTotal)
        {



            bool useSandbox = Convert.ToBoolean(ConfigurationManager.AppSettings["IsSandbox"]);
            var paypal = new PayPal(useSandbox);


            paypal.item_name = RecordId;
            paypal.amount = CartTotal;
            return View(paypal);
        }
    }
}

我的结帐视图,我需要详细信息;

@model T_shirt_Company_v3.ViewModels.ShoppingCartViewModel

@{
    ViewBag.Title = "Shopping Cart";
}
<script src="/Scripts/jquery-1.4.4.min.js"
        type="text/javascript"></script>
<script type="text/javascript">
    $(function () {
        // Document.ready -> link up remove event handler
        $(".RemoveLink").click(function () {
            // Get the id from the link
            var recordToDelete = $(this).attr("data-id");
            if (recordToDelete != '') {
                // Perform the ajax post
                $.post("/ShoppingCart/RemoveFromCart", {"id": recordToDelete },
                    function (data) {
                        // Successful requests get here
                        // Update the page elements
                        if (data.ItemCount == 0) {
                            $('#row-' + data.DeleteId).fadeOut('slow');
                        } else {
                            $('#item-count-' + data.DeleteId).text(data.ItemCount);
                        }
                        $('#cart-total').text(data.CartTotal);
                        $('#update-message').text(data.Message);
                        $('#cart-status').text('Cart (' + data.CartCount + ')');
                    });
            }
        });
    });
</script>
<center>
    <h3>
        Review your cart:
    </h3>
    <p class="button">
        @using (Html.BeginForm("ValidateCommand", "PayPal"))
            {
                <input type="submit" name="btnConfirm" value="Check Out with Paypal" />
            }


        @Html.ActionLink((string)ViewBag.CartStatus, (string)ViewBag.Link, (string)ViewBag.Link2)

        @Html.ActionLink("Continue Shopping ", "Index", "Store")



</p>
<div id="update-message">
</div>
<table>
    <tr>
        <th>
            Product Name
        </th>
        <th>
            Price (each)
        </th>
        <th>
            Quantity
        </th>
        <th></th>
    </tr>
    @foreach (var item in
                Model.CartItems)
    {
        <tr id="row-@item.RecordId">
            <td>
                @Html.ActionLink(item.Product.Title,
"Details", "Store", new { id = item.ProductId }, null)
            </td>
            <td>
                @item.Product.Price
            </td>
            <td id="item-count-@item.RecordId">
                @item.Count
            </td>
            <td>
                <a href="#" class="RemoveLink"
                   data-id="@item.RecordId">
                    Remove
                    from cart
                </a>
            </td>
        </tr>
    }
    <tr>
        <td>
            Total
        </td>
        <td></td>
        <td></td>
        <td id="cart-total">
            @Model.CartTotal
        </td>
    </tr>
</table>
</center>

和控制器;

namespace T_shirt_Company_v3.Controllers
{
    public class ShoppingCartController : Controller
    {
        TshirtStoreDB storeDB = new TshirtStoreDB();
        //
        // GET: /ShoppingCart/
        public ActionResult Index()
        {

            var cart = ShoppingCart.GetCart(this.HttpContext);

                // Set up the ViewModel
                ShoppingCartViewModel viewModel = new ShoppingCartViewModel
                {
                    CartItems = cart.GetCartItems(),
                    CartTotal = cart.GetTotal()
                };


            if (viewModel.CartItems.Any())
            {
                ViewBag.CartStatus = "Proceed to checkout or ";
                ViewBag.Link = "AddressAndPayment";
                ViewBag.Link2 = "Checkout";

            }
            else
            {
                ViewBag.CartStatus = "Cart is empty please ";
                ViewBag.Link = "Index";
                ViewBag.Link2 = "Store";
            }

            // Return the view
            return View(viewModel);
        }


        //
        // GET: /Store/AddToCart/5(ID)
        public ActionResult AddToCart(int id)
        {
            // Retrieve the Product from the database
            var addedProduct = storeDB.Products
                .Single(product => product.ProductId == id);

            // Add it to the shopping cart
            var cart = ShoppingCart.GetCart(this.HttpContext);

            cart.AddToCart(addedProduct);

            // Go back to the main store page for more shopping
            return RedirectToAction("Index");
        }
        //
        // AJAX: /ShoppingCart/RemoveFromCart/5(ID)
        [HttpPost]
        public ActionResult RemoveFromCart(int id)
        {
            // Remove the item from the cart
            var cart = ShoppingCart.GetCart(this.HttpContext);

            // Get the name of the product to display confirmation
            string productName = storeDB.Carts
                .Single(item => item.RecordId == id).Product.Title;

            // Removes item from cart
            int itemCount = cart.RemoveFromCart(id);

            // Display the confirmation message saying removed from cart
            var results = new ShoppingCartRemoveViewModel
            {
                Message = Server.HtmlEncode(productName) +
                    " has been removed from your shopping cart.",
                CartTotal = cart.GetTotal(),
                CartCount = cart.GetCount(),
                ItemCount = itemCount,
                DeleteId = id
            };
            return Json(results);
        }
        //
        // GET: /ShoppingCart/CartSummary
        [ChildActionOnly]
        public ActionResult CartSummary()
        {
            var cart = ShoppingCart.GetCart(this.HttpContext);

            ViewData["CartCount"] = cart.GetCount();
            return PartialView("CartSummary");
        }


        //test close connection when done
        protected override void Dispose(bool disposing)
        {
            storeDB.Dispose();
        }
    }
}

2 个答案:

答案 0 :(得分:1)

假设我正确阅读了你的代码,你只是渲染数据,而你根本就没有发送它。

  • 您的form只有button(这是唯一发送的“数据”)
  • 您需要将您呈现的数据包含为form字段(<input />

H个。

答案 1 :(得分:0)

由于PayPal Api依赖于在使用之前必须理解的一组操作,开发人员应该专注于理解操作摘要,在使用Api之前,在正式使用Api之前还可以使用沙箱进行测试。

您应该知道如何使用PayPal的RESTful端点结构。

您可以查看PayPal Api文档,详细了解如何使用REST API Reference.

将您的网络应用程序与PayPal集成