PHP Paypal API : set shipping costs and optional discounts to the order

标签 php paypal

我在使用 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/

相关文章:

php:如何在字符串中查找数字?

paypal - 是否可以在 Paypal 中创建虚拟帐户

Paypal:处理纠纷,哪个API?

php - 我如何将(?) url 分离到主域+其余部分?

java - 使用 token 对客户端和服务器端进行身份验证的正确方法?

php - 发送tcp数据到ip

php - Fancybox - 无法在 iframe 中打开类似 facebook 的框

java - 为什么我得到 : Unable to execute dex: Cannot merge new index 67124 into a non-jumbo instruction

php - 由于 OpenSSL 错误,无法完成 Paypal 即时付款通知

paypal - payflow 透明重定向总是采取错误 url