curl - Paypal API (NVP) TransactionSearch 给出错误 10004 - 开始日期无效

标签 curl paypal paypal-rest-sdk paypal-nvp nvp

好的,所以下面的代码给我上面的错误“开始日期无效”。即使我已经从各种来源验证它是一个有效且格式正确的开始日期。代码的 res 没有问题,因为它成功返回了正确的余额。我到处搜索,但找不到解决方案。请帮忙。谢谢!

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")));

最佳答案

我认为您对 $this->encodeFields() 的调用(在 call() 内部)导致您的参数值是 double 的-urlencode( )$this->encodeFields() 正在对 $fields 数组中的每个元素调用 urlencode();但是,http_build_query() 会自动为您完成这项工作。因此,您可能在调用 PayPal 时以 STARTDATE=2016-02-24T00%253A00%253A00Z 结束,而实际上它应该是 STARTDATE=2016-02-24T00%3A00 %3A00Z.

关于curl - Paypal API (NVP) TransactionSearch 给出错误 10004 - 开始日期无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37449639/

相关文章:

asp.net-mvc-4 - 如何开始使用新的 PayPal 开发网站

c# - Paypal c# REST API 请求未记录的配置部分

php - cURL无法完全模拟HTTP Post

json - 由于 ipv6 (Ubuntu) 在 Curl POST 之前延迟 10 秒

php - Paypal :从沙箱上线

Paypal 直接付款

api - PayPal REST API 付款可以计算运费和税费吗?

Paypal rest API 计费协议(protocol) webhooks

http - 测试 Web 应用程序 URL

batch-file - 将 Curl 输出写入批处理文件变量