2015-01-13 29 views
0

我試圖通過PHP SDK(沙箱環境)創建並執行與PayPal REST API的支付,如下所示。付款創建($payment->create)正常,但付款執行($payment->execute)返回"Incoming JSON request does not map to API request"。 JSON請求是由SDK創建的,那麼會出現什麼問題? 在此先感謝。PayPal REST API通過PHP SDK返回「傳入的JSON請求不映射到API請求」

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

$item = new Item(); 
$item->setName('Any name') 
    ->setCurrency('EUR') 
    ->setQuantity(1) 
    ->setPrice(0.99); 

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

$details = new Details(); 
$details->setTax(0) 
     ->setSubtotal(0.99); 

$amount = new Amount(); 
$amount->setCurrency('EUR') 
     ->setTotal(0.99) 
     ->setDetails($details); 

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

$redirectUrls = new RedirectUrls(); 
$redirectUrls->setReturnUrl(BASE_URL.'/payment/?success=true') 
      ->setCancelUrl(BASE_URL.'/payment/?success=false'); 

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

try { 
    $payment->create($apiContext); 

    $execution = new PaymentExecution(); 
    $result = $payment->execute($execution, $apiContext); 

} catch (Exception $ex) { 
    //Function for extract the error message, 
    //the error message can be showing with 
    //a simple var_dump($ex) 
    $exception = self::getException($ex); 
} 
+0

在谷歌上的一個快速搜索告訴我,這個錯誤通常是由你發送的json無效造成的。確保你已經閱讀過文檔,並且你所放的一切都是他們期望的。 –

回答

0

這已經足夠把

$payment->setIntent('authorize') 

,而不是

​​

和消除執行

$execution = new PaymentExecution(); $result = $payment->execute($execution, $apiContext); 

然後,

$payment->create 
01後

我用的 「href」 從

$payment->links 

做重定向。一切都很完美。謝謝你們。

0

我不是一個PHP開發(.NET),因此基於看完上面,檢查到這部分代碼:

$execution = new PaymentExecution(); 
$result = $payment->execute($execution, $apiContext); 

您發送PaymentExecution沒有payer_idPayment.Id您將在後獲得用戶批准您創建的paypal付款申請d。

這就是說,我沒有看到那部分(儘管如上,不是PHP開發),所以我可能是錯的。步驟如下:

  1. 創建付款

  2. 進入審批流程 - >用戶被重定向到貝寶,批准你在創建#1的付款 - >,並重新回到您的網站( returnUrl)

    a。 PayerID將在此過程中的查詢字符串

    b。該paymentId也將在查詢字符串在這個過程中,用戶從貝寶(您returnUrl)重新回到您的網站

  3. 後 - ExecutePayment,設置它的id你獲得一個(#2B ),發送PaymentExecutionPayerID集到你得到你(#2A)

在C#中(你需要將其轉換爲PHP):

var _payment = new Payment { id = the_payment_id_you_obtained }; 
    var _payExec = new PaymentExecution { payer_id = the_payer_id_you_obtained }; 

    var _response = _payment.Execute(context, _payExec); 

Hth ...

+0

是的,肯定是這個問題,它缺乏支付的批准。謝謝! – Roilld