我使用PaypalApi进行在线商店。
要通过API发送付款,您的小计金额+税金+运费必须等于订单的总额,Paypal仅限于2位小数。我的问题是,有时一些值变为3位小数,当我尝试将其格式化为两位小数时,我的计算最终会出错。无论我如何扭曲并使用一种不等于TotalAmount的方法结束。
以下是paymentMethod的代码。
private Payment ExecutePayment(APIContext apiContext, string payerId, string paymentId)
{
var paymentExecution = new PaymentExecution() { payer_id = payerId };
this.payment = new Payment() { id = paymentId };
return this.payment.Execute(apiContext, paymentExecution);
}
private Payment CreatePayment(APIContext apiContext, string redirectUrl)
{
//similar to credit card create itemlist and add item objects to it
var countryParams = LanguageService.GetUiCultureByCountryPrefix(Country);
var productsInPaypalCart = (List<Dictionary<string, object>>)Session["ProductsInPaypalCart"];
double subtotalAmount = 0;
double unitPrice = 0;
double taxedUnitPrice = 0;
var itemList = new ItemList() { items = new List<PayPal.Api.Item>() };
double shipping = 0;
double totalAmount = 0;
double taxedAmount = 0;
double taxInTwoDecimails = 0;
double subtotalAmountInTwoDecimals = 0;
foreach (var product in productsInPaypalCart)
{
var dbProduct = Service.ProductService.GetProduct(int.Parse(product["reference"].ToString()), Country);
shipping = dbProduct != null && dbProduct.ShippingFee.HasValue && shipping < dbProduct.ShippingFee ? dbProduct.ShippingFee.Value : shipping;
unitPrice = (int)product["unit_price"] / 100;
taxedUnitPrice = (unitPrice * 0.875);
itemList.items.Add(new PayPal.Api.Item()
{
name = (string)product["name"],
currency = countryParams.Currency,
price = (taxedUnitPrice).ToString("F").Replace(",", "."),
quantity = Convert.ToString(product["quantity"]),
}
);
subtotalAmount += (taxedUnitPrice * (int)product["quantity"]);
totalAmount += (unitPrice * (int)product["quantity"]);
taxedAmount += ((totalAmount * 0.125) + shipping * 0.25);
}
var b = Math.Round((decimal)subtotalAmount, 2);
subtotalAmountInTwoDecimals += Convert.ToDouble(b);
var a = Math.Round((decimal)taxedAmount, 2);
taxInTwoDecimails += Convert.ToDouble(a);
var payer = new Payer() { payment_method = "paypal" };
// Configure Redirect Urls here with RedirectUrls object
var redirUrls = new RedirectUrls()
{
cancel_url = redirectUrl,
return_url = redirectUrl
};
// similar as we did for credit card, do here and create details object
var details = new Details()
{
tax = ((totalAmount * 0.125) + shipping * 0.25).ToString("F").Replace(",", "."),
shipping = (shipping * 0.75).ToString("F").Replace(",", "."),
subtotal = (subtotalAmount).ToString("F").Replace(",", ".")
};
// similar as we did for credit card, do here and create amount object
var amount = new Amount()
{
currency = countryParams.Currency,
total = ((subtotalAmount + taxInTwoDecimails) + (shipping * 0.75)).ToString("F").Replace(",", "."), // Total must be equal to sum of shipping, tax and subtotal.
details = details
};
var transactionList = new List<Transaction>();
transactionList.Add(new Transaction()
{
description = "Transaction description.",
invoice_number = Guid.NewGuid().ToString(),
amount = amount,
item_list = itemList
});
this.payment = new Payment()
{
intent = "order",
payer = payer,
transactions = transactionList,
redirect_urls = redirUrls
};
// Create a payment using a APIContext
return this.payment.Create(apiContext);
}
提前致谢。
答案 0 :(得分:0)
听起来你正在分别计算“总金额”和“小计+税金+运费”的总和。为什么不首先确保小计,税金和运费只计算到2位小数,然后将它们一起添加并发送 作为总金额? IE用2个小数点完成所有操作