Effectivement, vous avez raison. Le PayPal Checkout SDK en PHP a également été abandonné. Cependant, vous pouvez toujours intégrer PayPal en utilisant l’API REST de PayPal directement sans SDK. Vous utiliserez des requêtes HTTP standard en PHP (via cURL
ou des bibliothèques comme Guzzle) pour interagir avec l’API PayPal.
Créer un compte PayPal Developer
Obtenir un jeton d’accès OAuth 2.0
La première étape consiste à obtenir un jeton d’accès pour authentifier vos requêtes API.
Voici un exemple de code en PHP pour obtenir un jeton d’accès :
$clientId = 'YOUR_CLIENT_ID';
$clientSecret = 'YOUR_CLIENT_SECRET';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api-m.sandbox.paypal.com/v1/oauth2/token");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, "$clientId:$clientSecret");
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");
$headers = [];
$headers[] = "Accept: application/json";
$headers[] = "Accept-Language: en_US";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
$json = json_decode($response);
$accessToken = $json->access_token;
Exemple de création d’une commande de paiement avec cURL en PHP :
$accessToken = 'YOUR_ACCESS_TOKEN';
$orderData = [
"intent" => "CAPTURE",
"purchase_units" => [[
"amount" => [
"currency_code" => "USD",
"value" => "100.00"
]
]],
"application_context" => [
"cancel_url" => "https://example.com/cancel",
"return_url" => "https://example.com/return"
]
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api-m.sandbox.paypal.com/v2/checkout/orders");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($orderData));
$headers = [];
$headers[] = "Content-Type: application/json";
$headers[] = "Authorization: Bearer $accessToken";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
$order = json_decode($response);
echo "Approve the payment at: " . $order->links[1]->href;
Exemple pour capturer un paiement :
$orderId = 'ORDER_ID';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api-m.sandbox.paypal.com/v2/checkout/orders/$orderId/capture");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$headers = [];
$headers[] = "Content-Type: application/json";
$headers[] = "Authorization: Bearer $accessToken";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
$capture = json_decode($response);
print_r($capture);
Tester avec l’environnement Sandbox
Utilisez toujours l’URL sandbox pour tester vos paiements :
https://api-m.sandbox.paypal.com/
https://api-m.paypal.com/
Passer en production
Une fois vos tests terminés, passez en mode production en remplaçant les URL de l’API et les identifiants de sandbox par ceux de production.
Vous pouvez consulter la documentation de l’API PayPal pour plus de détails sur les autres fonctionnalités disponibles.
En utilisant directement les appels d’API, vous évitez de dépendre d’un SDK obsolète et avez plus de contrôle sur l’intégration des paiements dans votre site.