我在使用 REST API 为 Paypal 订单设置运费和折扣时遇到了一些问题。
在这里https://github.com/paypal/rest-api-sdk-php/blob/master/lib/PayPal/Api/Amount.php我看到最后一个方法是 setDetails()
所以我使用 Details()
类来设置运费,然后我调用了 $amount->setDetails($details )
但每次我收到 400 错误响应
。
这是代码
// $shipping_cost = 10; $subtotal = 100; $total = 110;
$details = new Details();
$details->setShipping($shipping_cost);
$details->setShipping($subtotal);
// Specify the payment amount.
$amount = new Amount();
$amount->setCurrency($currency);
$amount->setTotal($total);
$amount->setDetails($details);
可以肯定的是,如果我删除 $details
部分,它会工作并且我会被重定向到 paypal,但运费不会显示。
此错误是因为总计有误,且仅在运费为 0 时有效。
我也尝试设置 $amount->setTotal($total);
但没有成功。
这与文档站点上的代码完全相同,但它不起作用。
请帮我解决这个问题,我为此奋斗了 2 天,他们的文档对我一点帮助都没有。
谢谢。
最佳答案
您收到 400 响应代码,因为小计、税金和运费加起来不等于总计金额。
尝试声明以下变量以进行测试:
$subtotal = 100; // These are just test values for this example
$ship_tax = 10;
$ship_cost = 10;
$currency = 'USD';
$total = $subtotal + $ship_tax + $ship_cost;
然后:
$details = new Details();
$details->setSubtotal($subtotal)
->setTax($ship_tax)
->setShipping($ship_cost);
$amount = new Amount();
$amount->setCurrency($currency)
->setTotal($total)
->setDetails($details);
关于PHP Paypal API : set shipping costs and optional discounts to the order,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26524959/