我有我的網站工作,所以用戶可以通過卡支付,但現在我也需要使用貝寶,我似乎無法將產品從購物車發送到貝寶控制器,每個單獨的產品需求按順序發送。MVC 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="[email protected]">
<td>
@Html.ActionLink(item.Product.Title,
"Details", "Store", new { id = item.ProductId }, null)
</td>
<td>
@item.Product.Price
</td>
<td id="[email protected]">
@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();
}
}
}