2013-03-27 57 views
3

我有一個沙箱帳戶與貝寶。我可以使用PHP上的curl通過API檢索令牌,但是處理測試卡只返回null。任何人都看到代碼有問題?這是PayPal沙盒的已知問題嗎?然而,如前所述,下面片段中的客戶是捏造的,使用我的真實憑證,我可以成功調用以檢索令牌。PayPal Rest API for Payments在沙箱中返回NULL

的PHP生成請求:

<?php 

    class psPayPal { 

    private $tokenUrl = 'https://api.sandbox.paypal.com/v1/oauth2/token'; 
    private $paymentUrl = 'https://api.sandbox.paypal.com/v1/payments/payment'; 
    private $client = 'dlkjasdfkja;skdjfaksdjfkajsdkfjaejkjefkekjakdjfaksjdfadjf;ja'; 
    private $secret = 'klj;akjdfjeieiadfaldkjfelkajsdfkjaejeiejisadif;alkdsfj;kjjie'; 
    private $token; 
    private $tokenHandle; 
    private $paymentHandle; 

    public function __construct() 
    { 
     $this->tokenHandle = curl_init($this->tokenUrl); 
     $this->buildTokenRequest(); 
    } 

    public function buildTokenRequest() 
    { 
     $header = array(
      'Accept: application/json', 
      'Accept-Language: en_US' 
     ); 

     $user = $this->client . ':' . $this->secret; 

     $data = 'grant_type=client_credentials'; 

     curl_setopt($this->tokenHandle, CURLOPT_HTTPHEADER, $header); 
     curl_setopt($this->tokenHandle, CURLOPT_USERPWD, $user); 
     curl_setopt($this->tokenHandle, CURLOPT_POSTFIELDS, $data); 
     curl_setopt($this->tokenHandle, CURLOPT_RETURNTRANSFER, true); 

     $this->commitTokenRequest(); 
    } 

    public function commitTokenRequest() 
    { 
     $response = json_decode(curl_exec($this->tokenHandle)); 

     $this->token = $response->access_token; 

     curl_close($this->tokenHandle); 
    } 

    public function buildPaymentRequest($cost = '', $description = '') 
    { 
     $this->paymentHandle = curl_init($this->paymentUrl); 

     $header = array(
      'Accept: application/json', 
      'Accept-Language: en_US', 
      'Authorization:Bearer ' . $this->token 
     ); 

     $data = array(
      'intent' => 'sale', 
      'payer' => array(
       'payment_method' => 'credit_card', 
       'funding_instruments' => array(
         array(
          'credit_card' => array(
           'number' => '5500005555555559', 
           'type' => 'mastercard', 
           'expire_year'=> '2018', 
           'cvv2'=> '111', 
           'first_name'=> 'Joe', 
           'last_name' => 'Shopper' 
          ) 
         ) 
       ) 
      ), 
      'transactions' => array(
        array(
         'amount' => array(
          'total' => '39.54', 
          'currency' => 'USD' 
         ), 
         'description' => 'This is my descrription for hats' 
        ) 
      ) 
     ); 
     $d = json_encode($data); 

     curl_setopt($this->paymentHandle, CURLOPT_HTTPHEADER, $header); 
     curl_setopt($this->paymentHandle, CURLOPT_POSTFIELDS, $d); 
     curl_setopt($this->paymentHandle, CURLOPT_RETURNTRANSFER, true); 

     $this->commitPaymentRequest(); 
    } 

    public function commitPaymentRequest() 
    { 
     $response = json_decode(curl_exec($this->paymentHandle)); 

     var_dump($response); 

     curl_close($this->paymentHandle); 
    } 

} 

?> 

我有多種選擇試過,但我相信上面的代碼完成。我還驗證了我使用JSON Lint發送的jSON。該JSON如下:

{ 
"intent": "sale", 
"payer": { 
    "payment_method": "credit_card", 
    "funding_instruments": [ 
     { 
      "credit_card": { 
       "number": "5500005555555559", 
       "type": "mastercard", 
       "expire_year": "2018", 
       "cvv2": "111", 
       "first_name": "Joe", 
       "last_name": "Shopper" 
      } 
     } 
    ] 
}, 
"transactions": [ 
    { 
     "amount": { 
      "total": "39.54", 
      "currency": "USD" 
     }, 
     "description": "This is my descrription for hats" 
    } 
] 

}

任何建議,不勝感激!

+0

您可以嘗試PayPal SDK https://github.com/paypal/rest-api-sdk-php和[創建付款示例](https://github.com/paypal/rest-api-sdk-php/斑點/主/樣品/付款/ CreatePayment.php) – siddick 2013-03-27 16:06:44

回答

5

原來的解決方案是添加一個頭,Content-Type:application/json。出於某種原因,PHP猜測令牌請求中的正確內容類型(可能是因爲Accepts頭),但是當傳遞時,json-array將內容類型更改爲application/xml。現在一切都好!