c# - 创建付款时的 C# ASP.NET MVC 错误请求 (400)
问题描述
我正在尝试在我的 ASP.NET MVC 项目中集成 Paypal sdk,但是当我尝试创建付款时遇到问题,总是会引发以下异常: PayPal.PaymentsException:'远程服务器上的错误:(400)错误请求' .
这是创建 Paypal 付款的方法。我尝试编写不同版本的代码,但总是在 payment.Create(apiContext) 方法中得到该异常。
这是代码,引发异常的方法在底部:
private Payment CreatePayment(APIContext apiContext, string redirectUrl)
{
var listItems = new ItemList() { items = new List<Item>() };
List<CartItem> cart = (List<CartItem>)Session["cart"];
foreach (var cartItem in cart)
{
listItems.items.Add(new Item()
{
name = cartItem.Stock.articulo.Nombre,
currency = "EUR",
price = cartItem.PrecioTotalArticulo.ToString(),
quantity = cartItem.Cantidad.ToString(),
sku = "sku"
});
}
var payer = new Payer() { payment_method = "paypal" };
var redirUrls = new RedirectUrls()
{
cancel_url = redirectUrl,
return_url = redirectUrl
};
var subtotalCompra = cart.Sum(i => i.PrecioTotalArticulo);
var gastosEnvio = configuracionService.CalcularGastosEnvio(subtotalCompra);
var details = new Details()
{
tax = (0.21 * subtotalCompra).ToString(),
shipping = gastosEnvio.ToString(),
subtotal = subtotalCompra.ToString()
};
var amount = new Amount()
{
currency = "EUR",
total = (Convert.ToDouble(details.tax) + subtotalCompra + gastosEnvio).ToString(),
details = details
};
var transactionList = new List<Transaction>();
transactionList.Add(new Transaction()
{
description = "Paypal Testing",
invoice_number = ventaService.GenerarNumFactura(),
amount = amount,
item_list = listItems
});
payment = new Payment()
{
intent = "sale",
payer = payer,
transactions = transactionList,
redirect_urls = redirUrls
};
return payment.Create(apiContext);
}
这里是我定义 APIContext 的配置类:
public class PaypalConfiguration
{
public readonly static string ClientId;
public readonly static string ClientSecret;
static PaypalConfiguration()
{
var config = GetConfig();
ClientId = config["clientId"];
ClientSecret = config["clientSecret"];
}
public static Dictionary<string, string> GetConfig()
{
return PayPal.Api.ConfigManager.Instance.GetProperties();
}
private static string GetAccessToken()
{
string accessToken = new OAuthTokenCredential(ClientId, ClientSecret, GetConfig()).GetAccessToken();
return accessToken;
}
public static APIContext GetAPIContext()
{
var apiContext = new APIContext(GetAccessToken());
apiContext.Config = GetConfig();
return apiContext;
}
}
我尝试搜索不同的解决方案或相关问题,但我没有找到任何东西。谢谢您的帮助。
解决方案
推荐阅读
- html - 子元素裁剪父元素?
- c# - 如何使用动作过滤器更改输入参数
- java - 异步任务中的 doinbackground() 无法正常工作
- r - 文本分析中的词干提取问题(瑞典数据)
- azureservicebus - 与服务总线主题订阅规则/过滤器中的当前日期进行比较
- python - matplotlib 可视化链表和决策树
- sql - 使用 Substring 或任何其他函数准确地选择一个字符串
- javascript - 将 javascript 数组导出到形状文件
- c++ - Boost.Program_options:带有状态的自定义验证器
- python - Python SKIMAGE 曝光 rescale_intensity 颜色条