2014-02-12 65 views
0

我試圖讓PHP使用PayPal帳戶進行工作,我一直在收到響應「傳入的JSON請求沒有映射到API請求」。 我檢查了我用jsonlint發送的JSON,它是有效的JSON。它也似乎與樣本中爲此付款類型發送的內容相匹配。PayPal REST API格式錯誤的JSON錯誤

require __DIR__ . '/../bootstrap.php'; 
use PayPal\Api\Amount; 
use PayPal\Api\Details; 
use PayPal\Api\Item; 
use PayPal\Api\ItemList; 
use PayPal\Api\Payer; 
use PayPal\Api\Payment; 
use PayPal\Api\RedirectUrls; 
use PayPal\Api\Transaction; 
session_start(); 

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

// ### Itemized information 
$item1 = new Item(); 
$item1->setName($item1Name) 
    ->setCurrency($currency) 
    ->setQuantity($item1Quantity) 
    ->setPrice($item1Price); 
$item2 = new Item(); 
$item2->setName($item2Name) 
    ->setCurrency($currency) 
    ->setQuantity($item2Quantity) 
    ->setPrice($item2Price); 

$itemList = new ItemList(); 
$itemList->setItems(array($item1, $item2)); 

// ### Additional payment details 
$details = new Details(); 
$details->setShipping($shipping) 
    ->setTax($tax) 
    ->setSubtotal($subtotal); 

// ### Amount 
$amount = new Amount(); 
$amount->setCurrency($currency) 
    ->setTotal(number_format(($subtotal + ($shipping + $tax)), 2)) 
    ->setDetails($subtotal); 

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

// ### Redirect urls 
$baseUrl = getBaseUrl(); 
$redirectUrls = new RedirectUrls(); 
$redirectUrls->setReturnUrl("$baseUrl/ExecutePayment.php?success=true") 
    ->setCancelUrl("$baseUrl/ExecutePayment.php?success=false"); 

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

// ### Create Payment 
$payment->create($apiContext); 
} catch (PayPal\Exception\PPConnectionException $ex) { 
    echo "Exception: " . $ex->getMessage() . PHP_EOL; 
    var_dump($ex->getData()); 
    exit(1); 
} 

// ### Get redirect url 
foreach($payment->getLinks() as $link) { 
    if($link->getRel() == 'approval_url') { 
     $redirectUrl = $link->getHref(); 
     break; 
    } 
} 

// ### Redirect buyer to PayPal website 
$_SESSION['paymentId'] = $payment->getId(); 
if(isset($redirectUrl)) { 
    header("Location: $redirectUrl"); 
    exit; 
} 

的PayPal.log包含以下內容:

PayPal\Core\PPHttpConnection: Connecting to https://api.sandbox.paypal.com/v1/oauth2/token 
PayPal\Core\PPHttpConnection: Payload grant_type=client_credentials 
PayPal\Core\PPHttpConnection: Adding header User-Agent: PayPalSDK/rest-sdk-php 0.6.0 (lang=PHP;v=5.3.3;bit=64;os=********************;machine=x86_64;openssl=**********;curl=7.15.5) 
PayPal\Core\PPHttpConnection: Adding header Authorization: Basic *********************************** 
PayPal\Core\PPHttpConnection: Adding header Accept: */* 
PayPal\Core\PPHttpConnection: Connecting to https://api.sandbox.paypal.com/v1/payments/payment 
PayPal\Core\PPHttpConnection: Payload {"intent":"sale","payer":{"payment_method":"paypal"},"redirect_urls":{"return_url":"http:\/\/www.mysite.com\/paypal-test\/sample\/payments\/ExecutePayment.php?success=true","cancel_url":"http:\/\/www.mysite.com\/paypal-test\/sample\/payments\/ExecutePayment.php?success=false"},"transactions":[{"amount":{"currency":"EUR","total":"554.00","details":"550.00"},"item_list":{"items":[{"name":"Item 1 Name","currency":"EUR","quantity":50,"price":"7.00"},{"name":"Item 2 Name","currency":"EUR","quantity":20,"price":"10.00"}]},"description":"Payment description"}]} 
PayPal\Core\PPHttpConnection: Adding header Content-Type: application/json 
PayPal\Core\PPHttpConnection: Adding header User-Agent: PayPalSDK/rest-sdk-php 0.6.0 (lang=PHP;v=5.3.3;bit=64;os=*******************;machine=x86_64;openssl=************;curl=7.15.5) 
PayPal\Core\PPHttpConnection: Adding header Authorization: Bearer gSJ0P0foNQcWg3V76VjvSietNLejlF4-kfSFNkTcyCk 
PayPal\Core\PPHttpConnection: Adding header PayPal-Request-Id: 773226502471885139220716063968 

任何幫助是非常讚賞。

在此先感謝。

+0

我已經想通了。我將一個數字傳入了Amount-> SetDetails,而不是所需的對象。 // ###金額 $ amount = new Amount(); $ amount-> setCurrency($ currency) - > setTotal(number_format(($ subtotal +($ shipping + $ tax)),2)) - > setDetails($ subtotal); 有一件事幫我弄清楚了,比較PayPal示例(/sample/PayPal.log)的日誌和我自己嘗試的日誌。 災難避免! –

回答

1

我已經想通了。我將一個數字傳入了Amount-> SetDetails,而不是所需的對象。

// ### Amount 
$amount = new Amount(); 
$amount->setCurrency($currency) 
->setTotal(number_format(($subtotal + ($shipping + $tax)), 2)) 
->setDetails($subtotal); 

有一件事,讓我弄明白是PayPal的樣品(/sample/PayPal.log)的日誌與日誌我自己嘗試進行比較。

災難避免了!