【问题标题】:Json data how to get the contents PHPJson数据如何获取PHP的内容
【发布时间】:2015-11-26 13:33:17
【问题描述】:

我第一次尝试使用 paypal developer 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}");

【问题讨论】:

  • 你传入了$paypal,但这似乎不是一个使用的变量?另外,从外观上看,setItems 调用应该传递一个数组。
  • 我确实使用 [] 将 Items 设置为一个数组,但是我收到一个错误,提示 found '[' expected )
  • 那么听起来您使用的是 PHP 5.3 或更低版本?假设是这样,将项目包装在一个数组中,例如array($item) 代替。
  • 我是 5.3.8 谢谢我试试看。
  • setTransactions($transaction) 应该是 setTransactions(array($transaction))

标签: php json api paypal


【解决方案1】:

添加日志记录肯定会帮助您调查消息的原始细节。这是添加配置以启用日志记录的链接:

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

此外,还有很多 sample codes 可以帮助您设置代码。这是显示创建"Payment with PayPal" 的那个。您实际上可以通过关注 instructions here 在本地运行它们。

【讨论】:

    猜你喜欢
    • 2021-08-04
    • 1970-01-01
    • 2016-08-30
    • 1970-01-01
    • 1970-01-01
    • 2020-09-18
    • 2011-04-05
    • 2015-01-05
    • 1970-01-01
    相关资源
    最近更新 更多