0
私のオンラインショップはPaypalApiを使用しています。支払いのためのPaypalAPI基準の一致に問題がある
APIを使用して支払いを送信するには、小計額+税金+送料が注文の合計額と同じでなければならず、Paypalはわずか2小数に制限されています。私の問題は、時にはいくつかの値が3小数点になり、2小数点にフォーマットしようとすると、計算が間違ってしまうということです。私がどのようにひねっていても、Imは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);
}
ありがとうございます。
私はそれの例を教えてもらえますか、私はそれらをsepraetlyでどのように計算しているのか分かりません。 –