Page 1 sur 1

Appels à CURL pour Paypal

Posté : 18 août 2014, 14:02
par amelaye13
Bonjour à tous, voilà je dois créer une librairie de fonctionnalités qui revoient des requêtes à Paypal pour une appli de mobile.

J'ai crée une classe InitPaypal qui ressemble à ça pour le moment :

<?php
class InitPaypal {
    
    private $clientId = "blibli";
    private $secret = "blabla";
    protected $ch;
    protected $result;
    public $accessToken;
    
    public function __construct(){
        $this->ch = curl_init();     
    }
    
    public function connect(){
        curl_setopt($this->ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/oauth2/token");
        curl_setopt($this->ch, CURLOPT_HEADER, false);
        curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($this->ch, CURLOPT_POST, true);
        curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true); 
        curl_setopt($this->ch, CURLOPT_USERPWD, $this->clientId.":".$this->secret);
        curl_setopt($this->ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");
    }
    
    public function test(){
        curl_setopt($this->ch, CURLOPT_URL, "https://api.sandbox.paypal.com/retail/merchant/v1/locations");
        curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($this->ch, CURLOPT_POST, true);
        curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true); 
        curl_setopt($this->ch, CURLOPT_USERPWD, $this->clientId.":".$this->secret);
        curl_setopt($this->ch, CURLOPT_POSTFIELDS, 'access_token= '.$this->accessToken);
    }
    
    public function exec() {
        $this->result = curl_exec($this->ch);
        return(json_decode($this->result));
    }
    
    public function close(){
        curl_close($this->ch);
    }
}
J'appelle ma classe de cette façon :
<?php
include_once 'classes/InitPaypal.php';


$ch = new InitPaypal();
$ch->connect();
$connect = $ch->exec();
 

$ch->accessToken = $connect->access_token;
           
$ch->test();
$result = $ch->exec();
var_dump($result);
$ch->close();

?>
Au var_dump, ben ma fonction veut pas s'exécuter !!!!

Ce malin me dit : object(stdClass)#3 (5) { ["message"]=> string(13) "Invalid Token" ["errorCode"]=> int(600032) ["developerMessage"]=> string(202) "The provided access token is no longer valid. It may have simply expired; if so, you should use your refresh token to request a new access token. If this fails, your access to this API has been revoked." ["errorType"]=> string(26) "oauth/invalid_access_token" ["correlationId"]=> string(13) "6578ebe21e8a2" }

SVP aidez-moi, car j'ai l'impression de pas m'y prendre de la bonne façon :s

Re: Appels à CURL pour Paypal

Posté : 18 août 2014, 15:37
par yann18
bonjour,
 $ch->accessToken = $connect->access_token; //il est impossible d'invoquer $acces_token sur $connect
ton token n'a pas de valeur.donne une valeur au token avant tout appel à test():
 $ch->accessToken = "la valeur du token";

Re: Appels à CURL pour Paypal

Posté : 18 août 2014, 16:16
par amelaye13
Mais si il a une valeur, elle est renvoyée par le exec. Si je fais un var_dump de cette valeur, c'est bon ...

Re: Appels à CURL pour Paypal

Posté : 18 août 2014, 16:36
par yann18
sauf que dans ta méthode test() tu transmets en POST un token vide(ou null) .

si le token est renvoyé par exec il faut alors affecter à $ch->accessToken la valeur retournée par exec()
 include_once 'classes/InitPaypal.php';


$ch = new InitPaypal();
$ch->connect();
$connect = $ch->exec();
 

$ch->accessToken = $connect;
var_dump($ch->accessToken );//???
           
$ch->test();
$result = $ch->exec();
var_dump($result);
$ch->close();


Re: Appels à CURL pour Paypal

Posté : 18 août 2014, 16:44
par amelaye13
Je viens de te dire, que tel que c'est programmé mon Token n'est NI VIDE, NI NULL ! $connect est un tableau désérialisé ! Lis bien mon code !
Et si ça vait été null, l'api de paypal m'aurait remis à ma place en disant qu'il faut un Token.

Re: Appels à CURL pour Paypal

Posté : 18 août 2014, 16:51
par amelaye13
J'ai changé la fonction test() :
public function test(){
        curl_setopt($this->ch, CURLOPT_URL, "https://api.sandbox.paypal.com/retail/merchant/v1/locations");
        curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($this->ch, CURLOPT_POST, false);
        curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true); 
        curl_setopt($this->ch, CURLOPT_USERPWD, $this->clientId.":".$this->secret);
        curl_setopt($this->ch, CURLOPT_HTTPHEADER, array( 'Authorization: Bearer ' . $this->accessToken ) );
      //  curl_setopt($this->ch, CURLOPT_POSTFIELDS, 'access_token= '.$this->accessToken);
    }
Cette fois j'ai :
string(47) "A015wRbkQbHlmSw-ffjlB6WeI1KarVvegNv6x9QBWvyy4V0" object(stdClass)#3 (5) { ["message"]=> string(31) "The scope provided is incorrect" ["errorCode"]=> int(600033) ["developerMessage"]=> string(70) "This access token is bound to a different scope than the one required." ["errorType"]=> string(20) "oauth/scope_mismatch" ["correlationId"]=> string(13) "ff0d599727423" }

Ayayayaya :/

Re: Appels à CURL pour Paypal

Posté : 19 août 2014, 10:45
par amelaye13
Résolu, j'ai contacté Paypal, le scope était bloqué. 8-)