创建付款时发生C#ASP.NET MVC错误请求(400)

时间:2018-09-12 08:57:33

标签: c# asp.net-mvc paypal paypal-rest-sdk

我正在尝试在我的ASP.NET MVC项目中集成Paypal sdk,但是当我尝试创建Payment时遇到问题,总是会出现以下异常: 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);
    }

这是在Configuration类中定义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;
    }
}

我尝试搜索不同的解决方案或相关问题,但是我什么也没找到。 感谢您的帮助。

0 个答案:

没有答案