2016-05-26 14 views
0

Okay, so gibt der folgende Code mir den obigen Fehler "Startdatum ist ungültig". Auch wenn ich aus verschiedenen Quellen verifiziert habe, dass es sich um ein gültiges und gut formatiertes Startdatum handelt. Das Res des Codes hat kein Problem, da es das richtige Guthaben erfolgreich zurückgibt. Ich habe überall gesucht, kann aber keine Lösung finden. Bitte helfen Sie. Vielen Dank!Paypal API (NVP) TransactionSearch Fehler geben 10004 - Startdatum ist ungültig

class Paypal 
{ 
/** 
* API Version 
*/ 
const VERSION = 51.0; 

/** 
* List of valid API environments 
* @var array 
*/ 
private $allowedEnvs = array(
    'beta-sandbox', 
    'live', 
    'sandbox' 
); 

/** 
* Config storage from constructor 
* @var array 
*/ 
private $config = array(); 

/** 
* URL storage based on environment 
* @var string 
*/ 
private $url; 

/** 
* Build PayPal API request 
* 
* @param string $username 
* @param string $password 
* @param string $signature 
* @param string $environment 
*/ 
public function __construct($username, $password, $signature, $environment = 'live') 
{ 
    if (!in_array($environment, $this->allowedEnvs)) { 
    throw new Exception('Specified environment is not allowed.'); 
    } 
    $this->config = array(
    'username' => $username, 
    'password' => $password, 
    'signature' => $signature, 
    'environment' => $environment 
    ); 
} 

/** 
* Make a request to the PayPal API 
* 
* @param string $method API method (e.g. GetBalance) 
* @param array $params Additional fields to send in the request (e.g. array('RETURNALLCURRENCIES' => 1)) 
* @return array 
*/ 
public function call($method, array $params = array()) 
{ 
    $fields = $this->encodeFields(array_merge(
    array(
     'METHOD' => $method, 
     'VERSION' => self::VERSION, 
     'USER'  => $this->config['username'], 
     'PWD'  => $this->config['password'], 
     'SIGNATURE' => $this->config['signature'] 
    ), 
    $params 
    )); 

    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $this->getUrl()); 
    curl_setopt($ch, CURLOPT_POST, count($fields)); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields)); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    $response = curl_exec($ch); 
    if (!$response) { 
    throw new Exception('Failed to contact PayPal API: ' . curl_error($ch) . ' (Error No. ' . curl_errno($ch) . ')'); 
    } 
    curl_close($ch); 
    parse_str($response, $result); 
    return $this->decodeFields($result); 
} 

/** 
* Prepare fields for API 
* 
* @param array $fields 
* @return array 
*/ 
private function encodeFields(array $fields) 
{ 
    return array_map('urlencode', $fields); 
} 

/** 
* Make response readable 
* 
* @param array $fields 
* @return array 
*/ 
private function decodeFields(array $fields) 
{ 
    return array_map('urldecode', $fields); 
} 

/** 
* Get API url based on environment 
* 
* @return string 
*/ 
private function getUrl() 
{ 
    if (is_null($this->url)) { 
    switch ($this->config['environment']) { 
     case 'sandbox': 
     case 'beta-sandbox': 
     $this->url = "https://api-3t.".$this->config['environment'].".paypal.com/nvp"; 
     break; 
     default: 
     $this->url = 'https://api-3t.paypal.com/nvp'; 
    } 
    } 
    return $this->url; 
} 
} 
$paypal = new Paypal('xxxxxxxxxxxxxxxxxxxxxx', 'xxxxxxxxxxxxxxxxxx', 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'); 



print_r($paypal->call('TransactionSearch', array("STARTDATE"=>"2016-02-24T00:00:00Z"))); 
+0

Das richtige Beispiel sollte sein: STARTDATE = 2016-05-25T00: 00: 00Z. siehe https://developer.paypal.com/docs/classic/api/merchant/TransactionSearch_API_Operation_NVP/ –

Antwort

1

Ich denke, Ihr Anruf an $this->encodeFields() (innerhalb von call()) Ihre Parameterwerte doppel- urlencode() ‚d sein verursacht. $this->encodeFields() ruft urlencode() auf jedem der Elemente in Ihrem $fields Array; http_build_query() erledigt dies automatisch für Sie. Daher enden Sie wahrscheinlich mit STARTDATE=2016-02-24T00%253A00%253A00Z in Ihrem Anruf bei PayPal, wenn es eigentlich STARTDATE=2016-02-24T00%3A00%3A00Z sein sollte.

+0

das macht Sinn. Werde es versuchen – MindHacker