asp.net-mvc - 使用第 3 方购物车 MVC .NET 核心的 Pay pal 集成
问题描述
我正在尝试将 pay pal 集成到我的自定义购物车中。我已使用 Pay Pal 官方网站上的信息进行整合。我正在使用我的沙盒帐户来测试付款。它似乎不起作用,我不知道是什么导致了问题。调用 pay Pal 视图时,它只返回 pay pal 错误。这是 PayPalPartialView:
@{
int count = 1;
}
<form class="paypalform" action="https://www.paypal.com/us/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="upload" value="1">
<input type="hidden" name="business" value="sb-2mrjh7443549@personal.example.com">
@foreach (var item in Model)
{
<input type="hidden" name="item_name_@count" value="@item.ProductName" />
<input type="hidden" name="amount_@count" value="@item.Price" />
<input type="hidden" name="quantity_@count" value="@item.Quantity" />
count++;
}
<input type="hidden" name="currency_code" value="EUR">
<input type="image" src="http://www.paypal.com/en_US/i/btn/x-click-but01.gif" name="submit" alt="Make payments with PayPal - it's fast, free and secure!">
</form>
还有购物车索引视图:
@model CartViewModel
@{
ViewData["Title"] = "Cart Overview";
}
@if (Model.CartItems.Count > 0)
{
<h1>Cart Overview</h1>
<div class="cartWrapper">
<div class="cartbg d-none">
<h3 class="text-center">Redirecting you to paypal...</h3>
<img src="~/Images/ajax_loader.gif" />
</div>
<table class="table">
<tr>
<th>Product</th>
<th>Quantity</th>
<th></th>
<th>Price</th>
<th>Total</th>
</tr>
@foreach (var item in Model.CartItems)
{
<tr>
<td>@item.ProductName</td>
<td>@item.Quantity</td>
<td>
<a asp-action="Add" asp-route-id="@item.ProductId" class="btn btn-sm btn-primary">+</a>
<a asp-action="Decrease" asp-route-id="@item.ProductId" class="btn btn-sm btn-success">-</a>
<a asp-action="Remove" asp-route-id="@item.ProductId" class="btn btn-sm btn-danger">Remove</a>
</td>
<td>@item.Price.ToString("C2")</td>
<td>@Model.CartItems.Where(x => x.ProductId == item.ProductId).Sum(x => x.Quantity * x.Price).ToString("C2")</td>
</tr>
}
<tr>
<td class="text-right" colspan="4">Grand total: @Model.GrandTotal.ToString("C2")</td>
</tr>
<tr>
<td class="text-right" colspan="4">
<a asp-action="Clear" class="btn btn-danger">Clear cart</a>
<a href="#" class="btn btn-primary checkout">Checkout</a>
</td>
</tr>
</table>
</div>
}
else
{
<h3 class="display-4 text-center">Cart is empty</h3>
}
<partial name="~/Views/Cart/_PayPalPartial.cshtml" for="CartItems" />
@section Scripts{
<script>
$(function () {
$("a.checkout").click(function (e) {
e.preventDefault();
$("div.cartbg").removeClass("d-none");
$.get("/cart/clear", {}, function () {
$("form.paypalform").submit();
});
});
});
</script>
}
购物车控制器:
using CmsShoppingCart.Infrastructure;
using CmsShoppingCart.Models;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace CmsShoppingCart.Controllers
{
public class CartController : Controller
{
private readonly CmsSchoppingCartContext context;
public CartController(CmsSchoppingCartContext context)
{
this.context = context;
}
//GET /cart
public IActionResult Index()
{
List<CartItem> cart = HttpContext.Session.GetJson<List<CartItem>>("Cart") ?? new List<CartItem>();
CartViewModel cartVM = new CartViewModel
{
CartItems = cart,
GrandTotal = cart.Sum(x => x.Price * x.Quantity)
};
return View(cartVM);
}
//GET /cart/add/id
public async Task<IActionResult> Add(int id)
{
Product product = await context.Products.FindAsync(id);
List<CartItem> cart = HttpContext.Session.GetJson<List<CartItem>>("Cart") ?? new List<CartItem>();
CartItem cartItem = cart.Where(x => x.ProductId == id).FirstOrDefault();
if (cartItem == null)
{
cart.Add(new CartItem(product));
}
else
{
cartItem.Quantity += 1;
product.Quantity--;
context.SaveChanges();
}
HttpContext.Session.SetJson("Cart", cart);
if(HttpContext.Request.Headers["X-Requested-With"] != "XMLHttpRequest")
return RedirectToAction("Index");
return ViewComponent("SmallCart");
}
//GET /cart/decrease/id
public async Task<IActionResult> Decrease(int id)
{
Product product = await context.Products.FindAsync(id);
List<CartItem> cart = HttpContext.Session.GetJson<List<CartItem>>("Cart");
CartItem cartItem = cart.Where(x => x.ProductId == id).FirstOrDefault();
if (cartItem.Quantity > 1)
{
--cartItem.Quantity;
product.Quantity++;
context.SaveChanges();
}
else
{
cart.RemoveAll(x => x.ProductId == id);
}
if (cart.Count == 0)
{
HttpContext.Session.Remove("Cart");
}
else
{
HttpContext.Session.SetJson("Cart", cart);
}
return RedirectToAction("Index");
}
//GET /cart/remove/id
public IActionResult Remove(int id)
{
List<CartItem> cart = HttpContext.Session.GetJson<List<CartItem>>("Cart");
cart.RemoveAll(x => x.ProductId == id);
if (cart.Count == 0)
{
HttpContext.Session.Remove("Cart");
}
else
{
HttpContext.Session.SetJson("Cart", cart);
}
return RedirectToAction("Index");
}
//GET /cart/clear
public IActionResult Clear()
{
HttpContext.Session.Remove("Cart");
// return RedirectToAction("Page", "Pages");
//return Redirect("/");
if (HttpContext.Request.Headers["X-Requested-With"] != "XMLHttpRequest")
return Redirect(Request.Headers["Referer"].ToString());
return Ok();
}
}
}
推车型号:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace CmsShoppingCart.Models
{
public class CartItem
{
public int ProductId { get; set; }
public string ProductName { get; set; }
public int Quantity { get; set; }
public decimal Price { get; set; }
public decimal Total { get { return Quantity * Price; } }
public string Image { get; set; }
public CartItem()
{
}
public CartItem(Product product)
{
ProductId = product.Id;
ProductName = product.Name;
Price = product.Price;
Quantity = 1;
Image = product.Image;
}
}
}
和购物车查看模型:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace CmsShoppingCart.Models
{
public class CartViewModel
{
public List<CartItem> CartItems { get; set; }
public decimal GrandTotal { get; set; }
}
}
解决方案
推荐阅读
- web-scraping - 使用 Scrapy 发送 post 请求
- html - 如何在Angular中调整垫芯片的大小
- javascript - 在创建动态表单字段时防止 InvalidArgumentException
- angular - 如何在 Angular 中加载和自定义共享组件
- typescript - 是否可以有 typescript 构建类型并且只输入到特定目录?
- laravel - 使用 Vue Js 和 Bootstrap 展开折叠图标
- java - Spring JPA 不检测已删除的表
- javascript - 自举模态故障
- wordpress - 文件上传进度未显示在 WordPress 媒体库(带 CPanel 的 VPS)上
- ios - 自动调整多行标签的自定义文本字段的大小