php – Paypal项目描述与其余api sdk

前端之家收集整理的这篇文章主要介绍了php – Paypal项目描述与其余api sdk前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用 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相关文章