Paypal项目描述与其余api sdk

时间:2014-09-20 11:26:19

标签: 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个错误...因为添加了项目的东西,任何线索?

3 个答案:

答案 0 :(得分:2)

您可能会在更新后的正确轨道上看到它。但是看起来你也有一些问题,那就是陈述的问题

尝试将代码添加到来自paypal的catch和echo out消息中

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);

答案 1 :(得分:1)

我找到了答案,我们需要更多有关创建交易的详细信息,您必须将正确的数字设置为subTotal(),total()等......

Each price item * Quantity = Subtotal

Subtotal +- Tax, Shipping etc... = total

这就是我所拥有的:

        $sdkConfig = array(
            "mode" => "sandbox"
        );

        $cred = new OAuthTokenCredential($client_id, $secret, $sdkConfig);
        $apiContext = new ApiContext($cred, 'Request' . time());
        $apiContext->setConfig($sdkConfig);

        $payer = new Payer();
        $payer->setPaymentMethod("paypal");

        foreach ($cartHasItems as $key => $order) {
            $item[$key] = new PayPalItem();
            $item[$key]->setName($order->getItem()->getName())
                ->setCurrency('EUR')
                ->setQuantity($order->getQuantity())
                ->setPrice($order->getItem()->getPrice());
        }

        if($cart->getPromoCode()){
            $item[1] = new PayPalItem();
            $item[1]->setName('Promo code '.$cart->getPromoCode()->getCode())
                ->setCurrency('EUR')
                ->setQuantity(1)
                ->setPrice(-$this->getDoctrine()->getRepository('AppBundle:CartHasItems')->countTotalForCart($cart, 'Promo'));
        }

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

        $details = new Details();
        $details->setTax($this->getDoctrine()->getRepository('AppBundle:CartHasItems')->countTotalForCart($cart, 'HT')*20/100)
                ->setSubtotal($this->getDoctrine()->getRepository('AppBundle:CartHasItems')->countTotalForCart($cart, 'HT'));

        $amount = new Amount();
        $amount->setCurrency("EUR")
            ->setTotal($this->getDoctrine()->getRepository('AppBundle:CartHasItems')->countTotalForCart($cart, 'All'))
            ->setDetails($details);

        $transaction = new Transaction();
        $transaction->setAmount($amount)
            ->setItemList($itemList)
            ->setDescription("Payment description")
            ->setInvoiceNumber(uniqid());

        $redirectUrls = new RedirectUrls();
        $redirectUrls->setReturnUrl($this->generateUrl('paypal_success', array(), UrlGeneratorInterface::ABSOLUTE_URL));
        $redirectUrls->setCancelUrl($this->generateUrl('order_validation', array('type' => 'paypal'), UrlGeneratorInterface::ABSOLUTE_URL));

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

        $payment->create($apiContext);

答案 2 :(得分:0)

添加此 ...

$transaction->setDescription("Payment description")

...