首页 > 解决方案 > 创建付款时的 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;
    }
}

我尝试搜索不同的解决方案或相关问题,但我没有找到任何东西。谢谢您的帮助。

标签: c#asp.net-mvcpaypalpaypal-rest-sdk

解决方案


推荐阅读