【发布时间】: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))