Nouvelle API paypal basée sur REST

Par Luc Sanchez, il y a 11 ans


Bonjour,

Paypal propose une nouvelle API basée sur REST, il n'exites pas de tuto en français pour le moment (ou alors je n'en ai pas trouvé).

Pouvez-vous faire un tuto pour php ?

7 réponses

Lartak, il y a 11 ans

Un tutoriel existe déja sur le site, pas spécifiquement pour PayPal (un tutoriel ne sera pas fait pour chaque API ou autre), mais il est sur REST concernant les API.

Luc Sanchez, il y a 11 ans

Merci mais paypal met à disposition une classe php : PayPal-PHP-SDK
Et elle ne colle pas aux spécifications du tuto que tu cite.

Grafikart, il y a 11 ans

Effectivement ça m'a été demandé. Perso je n'ai pas encore quitté Paypal express checkout pour le moment. Un jour ou j'aurais du temps j'essaierais de voir comment ça marche (ce n'est jamais facil avec paypal)

Bousty, il y a 11 ans

Je travaille actuellement dessus pour un client. Si j'ai la motivation je ferai un tuto dans les jours qui viennent.

Luc Sanchez, il y a 11 ans

Super merci

Grafikart, il y a 11 ans

Au pire tu peux envoyer des explications rapides, jpeux faire la partie vidéo :)

Luc Sanchez, il y a 11 ans

Bonjour, voici un premier jet pour créer une vente sur paypal
en premier lieu il faut ajouter les classes qui vont bien avec composer.
"paypal/rest-api-sdk-php": "1.1.*"

pour ajouter les identifiant de test et les stocker dans un dossier en dehors de la racine du site

j'ai fait une petit classe pour t'aider, elle est très succinte mais elle fonctionne.
ha au faite 2 choses.

  • l’adaptation pour un tuto est facile
  • pour les taxes l'attendu n'est pas un pourcentage mais un montant.
    et voila...
<?php use PayPal\Api\Amount; use PayPal\Api\Details; use PayPal\Api\Item; use PayPal\Api\ItemList; use PayPal\Api\Payer; use PayPal\Api\Payment; use PayPal\Api\RedirectUrls; use PayPal\Api\Transaction; use PayPal\Exception; class paypalConfig { private $paypalValuesKeys; private $paymentDescription; private $item; private $urlOK; private $urlFail; private $amount; private $shipping = 0; private $tax; private $subtotal; private $total; private $currency = 'EUR'; private $paymentMethod = 'paypal'; public function getPaymentMethod() { return $this->paymentMethod; } public function setPaymentMethod($paymentMethod) { $this->paymentMethod = $paymentMethod; return $this; } public function getShipping() { return $this->shipping; } public function getPaypalValuesKeys() { return $this->paypalValuesKeys; } public function getPaymentDescription() { return $this->paymentDescription; } public function getItem() { return $this->item; } public function getUrlOK() { return $this->urlOK; } public function getUrlFail() { return $this->urlFail; } public function getAmount() { return $this->amount; } public function getTax() { return $this->tax; } public function getSubtotal() { return $this->subtotal; } public function getCurrency() { return $this->currency; } public function getTotal() { return $this->total; } public function setTotal($total) { $this->total = $total; return $this; } public function setTax($tax) { $this->tax = $tax; return $this; } public function setPaypalValuesKeys($paypalValues) { if (is_array($paypalValues)) { $this->paypalValuesKeys = $paypalValues; return $this; } else { throw new \Exception('$paypalValues is not an array() !'); } } public function setPaymentDescription($paymentDescription) { $this->paymentDescription = $paymentDescription; return $this; } public function setUrlOK($urlOK) { $this->urlOK = $urlOK; return $this; } public function setAmount($amount) { $this->amount = $amount; return $this; } public function setUrlFail($urlFail) { $this->urlFail = $urlFail; return $this; } public function setItem(array $itemValues) { $shoppingCart = []; foreach ($itemValues as $itemValue) { try { $item = new Item(); $item->setName($itemValue['name']) ->setCurrency($this->getCurrency()) ->setQuantity($itemValue['quantity']) ->setPrice($itemValue['price']); array_push($shoppingCart, $item); } catch (PayPal\Exception $exc) { echo $exc->getMessage(); } } $this->item = $shoppingCart; return $this; } public function setShipping($shipping) { $this->shipping = $shipping; return $this; } public function setSubtotal($subtotal) { $this->subtotal = $subtotal; return $this; } public function setCurrency($currency) { $this->currency = $currency; return $this; } public function configure() { try { $apiContext = new \PayPal\Rest\ApiContext( new \PayPal\Auth\OAuthTokenCredential( $this->getPaypalValuesKeys()['settings.paypal.api.id'], // ClientID $this->getPaypalValuesKeys()['settings.paypal.api.secret'] // ClientSecret )); try { $payer = new Payer(); $payer->setPaymentMethod($this->getPaymentMethod()); } catch (PayPal\Exception $exc) { echo $exc->getMessage(); } try { $itemList = new ItemList(); $itemList->setItems($this->getItem()); } catch (PayPal\Exception$exc) { echo $exc->getMessage(); } try { $details = new Details(); $details->setShipping($this->getShipping()) ->setTax($this->getTax()) ->setSubtotal($this->getSubtotal()); } catch (PayPal\Exception $exc) { echo $exc->getMessage(); } try { $amount = new Amount(); $amount->setCurrency($this->getCurrency()) ->setTotal($this->getTotal()) ->setDetails($details); } catch (PayPal\Exception $exc) { echo $exc->getMessage(); } try { $transaction = new Transaction(); $transaction->setAmount($amount) ->setItemList($itemList) ->setDescription($this->getPaymentDescription()) ->setInvoiceNumber(uniqid() . '-npacenter'); } catch (PayPal\Exception $exc) { echo $exc->getMessage(); } try { $redirectUrls = new RedirectUrls(); $redirectUrls->setReturnUrl($this->getUrlOK()) ->setCancelUrl($this->getUrlFail()); } catch (PayPal\Exception $exc) { echo $exc->getMessage(); } try { $payment = new Payment(); $payment->setIntent('sale') ->setPayer($payer) ->setRedirectUrls($redirectUrls) ->setTransactions([$transaction]); } catch (PayPal\Exception $exc) { echo $exc->getMessage(); } $request = clone $payment; $payment->create($apiContext); $approvalUrl = $payment->getApprovalLink(); // var_dump($approvalUrl, $request, $payment); return $approvalUrl; } catch (\PayPal\Exception\PPConnectionException $exc) { echo $exc->getMessage(); } } }