【发布时间】:2017-04-26 18:48:26
【问题描述】:
我在网上商店使用 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);
}
提前致谢。
【问题讨论】:
标签: c# .net model-view-controller paypal paypal-rest-sdk