Json数据如何获取PHP的内容

时间:2015-09-01 10:37:27

标签: php json api paypal

我第一次尝试使用paypal开发人员SDK for PHP。我已经关注youtube上的教程一切顺利,直到我尝试执行它并且我收到的响应是malformed_request JSON请求没有映射到API请求。如何获取JSON对象的结构和内容以便进一步调查?请看下面的代码在尝试时发生错误。我正在开发netbeans 7.4

<?PHP
//this is copied from this tutorial here on youtube https://www.youtube.com/watch?v=BD1dOWIABe0
use PayPal\Api\Payer;
use PayPal\Api\Item;
use PayPal\Api\ItemList;
use PayPal\Api\Details;
use PayPal\Api\Amount;
use PayPal\Api\Transaction;
use PayPal\Api\RedirectUrls;
use PayPal\Api\Payment;
require '/../app/start.php';
$pTotal = 20.00;
$shipping = 1.00;
$Total = $pTotal + $shipping ;
$payer = new Payer();
$payer->setPaymentMethod('paypal');
$item = new Item();
$item->setName("tieThisToSomething")
->setCurrency('GBP')
->setQuantity(1)
->setPrice($pTotal);
$itemList = new ItemList();
$itemList->setItems($item); //I have removed square brackets from here []
$details = new Details();
$details->setShipping($shipping)
    ->setSubtotal($pTotal);
$amount = new Amount();
$amount->setCurrency('GBP')
    ->setTotal($Total)
    ->setDetails($details);
$transaction = new Transaction();
$transaction->setAmount($amount)
    ->setItemList($itemList)
    ->setDescription('Pay for all your santa fun')
    ->setInvoiceNumber(uniqid());//perhaps tie this to order number?

$redirectUrls = new RedirectUrls();
$redirectUrls->setCancelUrl(SITE_URL . '/pay.php?success=false')
         ->setReturnUrl(SITE_URL . '/pay.php?success=true'); 

$payment = new Payment();
$payment->setPayer($payer)
    ->setTransactions($transaction)
    ->setIntent('sale')
    ->setRedirectUrls($redirectUrls)   ; //should [] be needed here?

//try {
//    $payment->create($paypal);
//} catch (Exception $e) {
//    die($e);
//}

try {
   $payment->create($paypal);  //Error happens here!
} catch (PayPal\Exception\PayPalConnectionException $ex) {
  echo $ex->getCode(); // Prints the Error Code
  echo $ex->getData(); // Prints the detailed error message 
 die($ex);
} catch (Exception $ex) {
 die($ex);
}

$approvalUrl = $payment->getApprovalLink();
header("Location: {$approvalUrl}");

1 个答案:

答案 0 :(得分:0)

添加日志记录肯定会帮助您调查邮件的原始详细信息。以下是添加配置以启用日志记录的链接:

https://github.com/paypal/PayPal-PHP-SDK/wiki/Adding-Configurations

此外,还有很多sample codes可以帮助您设置代码。以下是显示创建"Payment with PayPal"的内容。你可以通过跟随instructions here来实际运行那些。