php - 带有其余 api sdk 的 Paypal 项目描述

标签 php paypal

我正在使用 https://github.com/paypal/rest-api-sdk-php

我想显示一个项目描述评论,这里是代码:

  $amountDetails = new Details();
    $amountDetails->setSubtotal('7.41');
    $amountDetails->setTax('0.03');
    $amountDetails->setShipping('0.03');

    $amount = new Amount();
    $amount->setCurrency('USD');
    $amount->setTotal('7.47');
    $amount->setDetails($amountDetails);

    $transaction = new Transaction();
    $transaction->setAmount($amount);
    $transaction->setDescription('This is the payment transaction description.');




    $RedirectUrls = new RedirectUrls();
    $RedirectUrls ->setReturnUrl('http://localhost/mrSurvey/public/#/pricing');
    $RedirectUrls ->setCancelUrl('http://localhost/mrSurvey/public/#/pricing');

    $payment = new Payment();
    $payment->setIntent('sale');
    $payment->setPayer($payer);
    $payment->setTransactions(array($transaction));
    $payment->setRedirectUrls($RedirectUrls);

我只能看到描述,但我想看到项目编号和小计,我缺少什么?

更新: 所以我读到我需要添加一些东西:所以我做了这样的事情:

 $item = new Item();
 $item->setQuantity('1');
 $item->setName('benny');
 $item->setPrice('7.41');
 $item->setCurrency('USD');
 $item->setSku('blah');


 $items = new ItemList();
 $items->addItem(array($item));

...

$transaction->setItemList($items);

...

$payment = new Payment();
$payment->setIntent('sale');
$payment->setPayer($payer);
$payment->setTransactions(array($transaction));
$payment->setRedirectUrls($RedirectUrls);

$response = $payment->create($apiContext)->toarray();

return Response::json($response);

现在上面的代码给了我 400 错误...因为添加了项目的东西,有什么线索吗?

最佳答案

从您获得的更新来看,您似乎走在了正确的轨道上。但看起来你也有一些问题,然后只是陈述的问题

尝试将您的代码添加到 catch 中并回显来自 paypal 的消息

try{
  //your code here
}
catch(PayPal\Exception\PayPalConnectionException $e) {
  //This will show error from paypal
  $e->getData()
}

我猜我遇到类似错误的原因是您没有正确添加您的项目(税收、运费、小计等...)等等。试试这个示例 @ https://github.com/paypal/PayPal-PHP-SDK/blob/master/sample/payments/ExecutePayment.php并首先让它工作,然后修改示例中的代码,以满足您的需求。

下面是我修改的一些代码,它适用于我。

请注意,您有一个项目描述和一个交易描述

 //run x through loop with ++
 $x = 0;

 //for item 
 $items[$x] = new Item();
 $items->setName($item_name)
 ->setDescription($item_description)
 ->setCurrency($currency)
 ->setQuantity($item_quantity)
 ->setTax($item_tax)
 ->setPrice($item_price)
 ->setSku($item_sku);

  $itemList = new ItemList();
  $itemList->setItems($items);

  //for transaction
  $transaction = new Transaction();
  $transaction
  ->setAmount($amount)
  ->setItemList($itemList)
  ->setDescription($payment_description)
  ->setInvoiceNumber($invoice_number);

function getTotals($quantity, $tax, $price){
  $tax = $tax * $quantity;
  $subtotal = $price * $quantity;
  $total = $tax + $subtotal;
} 
total = getTotal($item_quantity, $item_tax, $item_price);

关于php - 带有其余 api sdk 的 Paypal 项目描述,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25948253/

相关文章:

paypal - 在 PayPal IPN 模拟器中测试 HTTPS 响应服务器时出错

php - 包含多个项目的 Paypal 购物车无法使用

python - PayPal:Python 传入 JSON 请求未映射到 API 请求

php - 如何在Linux中设置权限/var/www以允许PHP-Apache自动覆盖

php - MySQL ID 未传递到下一页

php模返回错误的结果

php - 如何使用 php api 检查电子邮件或手机 paypal 帐户状态?

php - 打开像页面一样的 href 到特定位置

php - 我可以在准备好的语句中参数化表名吗?

api - Paypal API 是否能够提供付款?