Question

I'm trying to use PayPal's Classic API to do a TransactionSearch. I keep getting an Error 81002 Unspecified Method: Method specified is not supported. Of course PayPal's documentation is "so" helpful. Anyone have any ideas on what I might be doing wrong?

Here is the class I'm using... API credentials are loaded from $this->config...

<?php
class PayPal {
private $base = array();
private $param = array();

public function __construct($registry) {
    $this->config = $registry->get('config');
    $this->request = $registry->get('request');

    // Operate in sandbox or live mode?
    $sandbox = $this->config->get('paypal_sandbox');

    $base = $this->base($sandbox);

    // Set request parameters
    $param['request'] = array(
        'startdate'     => '2014-03-18T00:00:00-07:00Z', // TODO Get search parameter from $this->request
        'enddate'       => '2014-03-19T14:22:23-07:00Z'
        );      

    $transactions = $this->getTransactions($base,$param);
    print_r($transactions);

}

// Set Security // 

public function base($sandbox) {

    if($sandbox=='1') { 
        $base = array(
            'url'       => 'https://api-3t.sandbox.paypal.com/nvp',
            'username'  => $this->config->get('paypal_username'),
            'password'  => $this->config->get('paypal_password'),
            'signature' => $this->config->get('paypal_signature')
            );
    }

    if($sandbox=='0') { 
        $base = array(
            'url'       => 'https://api-3t.paypal.com/nvp',
            'username'  => $this->config->get('paypal_username'),
            'password'  => $this->config->get('paypal_password'),
            'signature' => $this->config->get('paypal_signature')
            );
    }       

    return $base; 
}

public function call($base,$post) {
    $post .= '&PWD='.$base['password'];
    $post .= '&USER='.$base['username'];
    $post .= '&SIGNATURE='.$base['signature'];

    $ch = curl_init($base['url']);
    curl_setopt($ch, CURLOPT_PORT, 443);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
    curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);    


    $response = curl_exec($ch);
    if(empty($response)) { return 'No response received.'; }
    else {

        return $response;
    }

    curl_close($ch);
}

// Transactions (Classic API) //

public function getTransactions($base,$param) {
    $post  = 'METHOD=TransactionSearch';
    $post .= 'VERSION=58.0';

    foreach ($param['request'] as $key => $value) {
            $post .= '&' . strtoupper($key) . '=' . $value;
    }

    $transactions = $this->call($base,$post);
    return $transactions;
}

} ?>

Était-ce utile?

La solution

Thanks @Andrew. Just figured out I was missing an & in front of VERSION so it was compiling METHOD=TransactionSearchVERSION.... instead of METHOD=TransactionSearch&VERSION...

Gonna leave this up in case someone else finds it useful for working with PayPal NVP. What a pain in the brain!

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top