2017-07-25 46 views
0

我試圖用PayPal創建付款,並且出現錯誤。PayPal傳入的JSON請求未映射到API

錯誤:

400 { 「名稱」: 「MALFORMED_REQUEST」, 「消息」: 「傳入JSON請求不映射到API請求」, 「information_link」: 「https://developer.paypal.com/webapps/developer/docs/api/#MALFORMED_REQUEST」, 「debug_id」: 「f79dc9a739991」}異常'PayPal \ Exception \ PayPalConnectionException',並在消息'訪問https://api.sandbox.paypal.com/v1/payments/payment時得到Http響應代碼400'。在/ home/nork/domain/nork.lt/public_html/cms/xxx/system/payay/trace:#0 /home/nork/domains/nork.lt/public_html/cms/xxx/system/payments/PayPal-PHP-SDK/paypal/rest-api-sdk-php/lib/PayPal/Transport/PayPalRestCall.php (73):PayPal \ Core \ PayPalHttpConnection-> execute('{「intent」:「sale ...')#1 /home/nork/domains/nork.lt/public_html/cms/xxx/system/payments/PayPal PayPal/Transport/PayPalRestCall->執行(Array,'/ v1/payments/pa ...') ,'POST','{「intent」:「sale ...',NULL)#2 /home/nork/domains/nork.lt/public_html/cms/xxx/system/payments/PayPal-PHP-SDK/paypal /rest-api-sdk-php/lib/PayPal/Api/Payment.php(577):PayPal \ Common \ PayPalResourceModel :: executeCall('/ v1/payments/pa ...','POST','{「 intent「:」sale ...',NULL,Object(PayPal \ Rest \ ApiContext),NULL)#3 /home/nork/domains/nork.lt/public_html/cms/xxx/checkout.php(73):PayPal \阿比\ Paym ent->創建(對象(貝\休息\ APICONTEXT))#4 {主}

代碼:

<?php 

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; 

if(isset($_GET['type']) && isset($_GET['amount']) && isset($_GET['gold'])) 
{ 
    if($_GET['type'] == 'paypal') // If payment is PayPal 
    { 
     require 'system/payments/paypal.php'; 

     $gold   = $_GET['gold']; 
     $amount   = $_GET['amount']; 
     $pricePerVnt = 0.00; 
     $productName = ''; 
     $shipping = 0.00; 

     switch($gold) 
     { 
      case 'first': 
      { 
       $productName = 'first name'; 
       $pricePerVnt = 1.19; 
       break; 
      } 
      case 'second': 
      { 
       $productName = 'second name'; 
       $pricePerVnt = 0.21; 
       break; 
      } 
     } 
     if(!$pricePerVnt) die(); 

     $totalAmount = $amount * $pricePerVnt; 

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

     $item = new Item(); 
     $item->setName($productName)->setCurrency('USD')->setQuantity('1')->setPrice($pricePerVnt); 

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

     $amount = new Amount(); 
     $amount->setCurrency('USD')->setTotal($totalAmount); 

     $transaction = new Transaction(); 
     $transaction->setAmount($pricePerVnt)->setDescription($productName)->setInvoiceNumber(uniqid()); 

     $redirectUrls = new RedirectUrls(); 
     $redirectUrls->setReturnUrl('http://google.lt')->setCancelUrl('http://cancel.lt'); 

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

     try { 
      $payment->create($paypal); 
     } 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); 
     } 
     //print_r($payment); 

     //echo $approvalUrl = $payment->getApprovalLink(); 
     //header('Location: {$approvalUrl}'); 
    } 
} 

?> 

回答

0

答案似乎是,您沒有足夠的發送請求的值:

{"intent":"sale...', NULL)

請注意null。爲了看到更詳細的錯誤轉儲,你可以做調整的複雜的var_dump:

ini_set('xdebug.var_display_max_depth', 10); 
ini_set('xdebug.var_display_max_children', 256); 
ini_set('xdebug.var_display_max_data', 1024); 

然後你會看到完整的JSON,可以找出缺失或畸形。

0

這裏:

object(PayPal\Exception\PayPalConnectionException)#18 (9) {["url":"PayPal\Exception\PayPalConnectionException":private]=> 
    string(50) "https://api.sandbox.paypal.com/v1/payments/payment" 
    ["data":"PayPal\Exception\PayPalConnectionException":private]=> 
    string(213) "{"name":"MALFORMED_REQUEST","message":"Incoming JSON request does not map to API request","information_link":"https://developer.paypal.com/webapps/developer/docs/api/#MALFORMED_REQUEST","debug_id":"a963484dd5bd0"}" 
    ["message":protected]=> 
    string(93) "Got Http response code 400 when accessing https://api.sandbox.paypal.com/v1/payments/payment." 
    ["string":"Exception":private]=> 
    string(0) "" 
    ["code":protected]=> 
    int(400) 
    ["file":protected]=> 
    string(146) "/home/nork/domains/nork.lt/public_html/cms/xxx/system/payments/PayPal-PHP-SDK/paypal/rest-api-sdk-php/lib/PayPal/Core/PayPalHttpConnection.php" 
    ["line":protected]=> 
    int(202) 
    ["trace":"Exception":private]=> 
    array(4) { 
    [0]=> 
    array(6) { 
     ["file"]=> 
     string(145) "/home/nork/domains/nork.lt/public_html/cms/xxx/system/payments/PayPal-PHP-SDK/paypal/rest-api-sdk-php/lib/PayPal/Transport/PayPalRestCall.php" 
     ["line"]=> 
     int(73) 
     ["function"]=> 
     string(7) "execute" 
     ["class"]=> 
     string(32) "PayPal\Core\PayPalHttpConnection" 
     ["type"]=> 
     string(2) "->" 
     ["args"]=> 
     array(1) { 
     [0]=> 
     string(235) "{"intent":"sale","payer":{"payment_method":"paypal"},"redirect_urls":{"return_url":"http://google.lt","cancel_url":"http://cancel.lt"},"transactions":{"amount":1.19,"description":"RuneScape 2007 Gold","invoice_number":"5977898964140"}}" 
     } 
    } 
    [1]=> 
    array(6) { 
     ["file"]=> 
     string(147) "/home/nork/domains/nork.lt/public_html/cms/xxx/system/payments/PayPal-PHP-SDK/paypal/rest-api-sdk-php/lib/PayPal/Common/PayPalResourceModel.php" 
     ["line"]=> 
     int(104) 
     ["function"]=> 
     string(7) "execute" 
     ["class"]=> 
     string(31) "PayPal\Transport\PayPalRestCall" 
     ["type"]=> 
     string(2) "->" 
     ["args"]=> 
     array(5) { 
     [0]=> 
     array(1) { 
      [0]=> 
      string(26) "PayPal\Handler\RestHandler" 
     } 
     [1]=> 
     string(20) "/v1/payments/payment" 
     [2]=> 
     string(4) "POST" 
     [3]=> 
     string(235) "{"intent":"sale","payer":{"payment_method":"paypal"},"redirect_urls":{"return_url":"http://google.lt","cancel_url":"http://cancel.lt"},"transactions":{"amount":1.19,"description":"RuneScape 2007 Gold","invoice_number":"5977898964140"}}" 
     [4]=> 
     NULL 
     } 
    } 
    [2]=> 
    array(6) { 
     ["file"]=> 
     string(132) "/home/nork/domains/nork.lt/public_html/cms/xxx/system/payments/PayPal-PHP-SDK/paypal/rest-api-sdk-php/lib/PayPal/Api/Payment.php" 
     ["line"]=> 
     int(577) 
     ["function"]=> 
     string(11) "executeCall" 
     ["class"]=> 
     string(33) "PayPal\Common\PayPalResourceModel" 
     ["type"]=> 
     string(2) "::" 
     ["args"]=> 
     array(6) { 
     [0]=> 
     string(20) "/v1/payments/payment" 
     [1]=> 
     string(4) "POST" 
     [2]=> 
     string(235) "{"intent":"sale","payer":{"payment_method":"paypal"},"redirect_urls":{"return_url":"http://google.lt","cancel_url":"http://cancel.lt"},"transactions":{"amount":1.19,"description":"RuneScape 2007 Gold","invoice_number":"5977898964140"}}" 
     [3]=> 
     NULL 
     [4]=> 
     object(PayPal\Rest\ApiContext)#2 (2) { 
      ["requestId":"PayPal\Rest\ApiContext":private]=> 
      NULL 
      ["credential":"PayPal\Rest\ApiContext":private]=> 
      object(PayPal\Auth\OAuthTokenCredential)#3 (7) { 
      ["clientId":"PayPal\Auth\OAuthTokenCredential":private]=> 
      string(80) "AcAvvJw6aK8kEs_lTfv8TjuPiPzHX8Yk_Q9mvDO6AR3grRa09kOBrNVAZyvs-EpKrB9HP0A6UPZBgCrh" 
      ["clientSecret":"PayPal\Auth\OAuthTokenCredential":private]=> 
      string(80) "EIXpeAtUZVOf1syl-Jlt0I90lhx8oRMHPpmmRRRxuZ2hUF9LFQJK2fUFa6yz_rRuRaFMLGwJmXBk2cut" 
      ["accessToken":"PayPal\Auth\OAuthTokenCredential":private]=> 
      string(97) "A21AAGgcZ9Ls3UfzYYWU1q4ScO4WcX45JNbtYNJIosxP2kEq7i60yA0ZZrhHPI-anHRtaVATMjTDZKiNE-D0pSYSCkMk1VKHw" 
      ["tokenExpiresIn":"PayPal\Auth\OAuthTokenCredential":private]=> 
      int(32382) 
      ["tokenCreateTime":"PayPal\Auth\OAuthTokenCredential":private]=> 
      int(1501006218) 
      ["cipher":"PayPal\Auth\OAuthTokenCredential":private]=> 
      object(PayPal\Security\Cipher)#4 (1) { 
       ["secretKey":"PayPal\Security\Cipher":private]=> 
       string(80) "EIXpeAtUZVOf1syl-Jlt0I90lhx8oRMHPpmmRRRxuZ2hUF9LFQJK2fUFa6yz_rRuRaFMLGwJmXBk2cut" 
      } 
      ["_propMap":"PayPal\Common\PayPalModel":private]=> 
      array(0) { 
      } 
      } 
     } 
     [5]=> 
     NULL 
     } 
    } 
    [3]=> 
    array(6) { 
     ["file"]=> 
     string(63) "/home/nork/domains/nork.lt/public_html/cms/xxx/checkout.php" 
     ["line"]=> 
     int(65) 
     ["function"]=> 
     string(6) "create" 
     ["class"]=> 
     string(18) "PayPal\Api\Payment" 
     ["type"]=> 
     string(2) "->" 
     ["args"]=> 
     array(1) { 
     [0]=> 
     object(PayPal\Rest\ApiContext)#2 (2) { 
      ["requestId":"PayPal\Rest\ApiContext":private]=> 
      NULL 
      ["credential":"PayPal\Rest\ApiContext":private]=> 
      object(PayPal\Auth\OAuthTokenCredential)#3 (7) { 
      ["clientId":"PayPal\Auth\OAuthTokenCredential":private]=> 
      string(80) "AcAvvJw6aK8kEs_lTfv8TjuPiPzHX8Yk_Q9mvDO6AR3grRa09kOBrNVAZyvs-EpKrB9HP0A6UPZBgCrh" 
      ["clientSecret":"PayPal\Auth\OAuthTokenCredential":private]=> 
      string(80) "EIXpeAtUZVOf1syl-Jlt0I90lhx8oRMHPpmmRRRxuZ2hUF9LFQJK2fUFa6yz_rRuRaFMLGwJmXBk2cut" 
      ["accessToken":"PayPal\Auth\OAuthTokenCredential":private]=> 
      string(97) "A21AAGgcZ9Ls3UfzYYWU1q4ScO4WcX45JNbtYNJIosxP2kEq7i60yA0ZZrhHPI-anHRtaVATMjTDZKiNE-D0pSYSCkMk1VKHw" 
      ["tokenExpiresIn":"PayPal\Auth\OAuthTokenCredential":private]=> 
      int(32382) 
      ["tokenCreateTime":"PayPal\Auth\OAuthTokenCredential":private]=> 
      int(1501006218) 
      ["cipher":"PayPal\Auth\OAuthTokenCredential":private]=> 
      object(PayPal\Security\Cipher)#4 (1) { 
       ["secretKey":"PayPal\Security\Cipher":private]=> 
       string(80) "EIXpeAtUZVOf1syl-Jlt0I90lhx8oRMHPpmmRRRxuZ2hUF9LFQJK2fUFa6yz_rRuRaFMLGwJmXBk2cut" 
      } 
      ["_propMap":"PayPal\Common\PayPalModel":private]=> 
      array(0) { 
      } 
      } 
     } 
     } 
    } 
    } 
    ["previous":"Exception":private]=> 
    NULL 
} 
相關問題