<?php
class PaypalSubscription{
private $username;
private $password;
private $signature;
private $offers;
private $endpoint;
private $sandbox;
public function __construct($username, $password, $signature, $offers, $sandbox = true){
$this->username = $username;
$this->password = $password;
$this->signature = $signature;
$this->offers = $offers;
$this->sandbox = $sandbox;
$this->endpoint = "
https://api-3t.".($this->sandbox ? "sandbox." : "")."paypal.com/mep/dashboard";
}
public function nvp($options = []) {
$curl = curl_init();
$data = [
'USER' => $this->username,
'PWD' => $this->password,
'SIGNATURE' => $this->signature,
'METHOD' => 'SetExpressCheckout',
'VERSION' => 124.0,
];
$data = array_merge($data, $options);
curl_setopt_array($curl, [
CURLOPT_URL => $this->endpoint,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => http_build_query($data),
]);
$response = curl_exec($curl);
$responseArray =[];
parse_str($response, $responseArray);
return $responseArray;
}
public function subscribe($offer_id)
{
if (!isset($this->offers[$offer_id])){
throw new Exception('cette offre n\'existe pas');
}
$offer = $this->offers[$offer_id];
$data = [
'METHOD' => 'SetExpressCheckout',
'VERSION' => 124.0,
'PAYMENTREQUEST_0_ITEMAMT' => $offer['price'],
'PAYMENTREQUEST_0_AMT' => $offer['price'] * 1.2,
'L_PAYMENTREQUEST_0_AMT0' => $offer['price'] * 1.2,
'PAYMENTREQUEST_0_TAXAMT' => $offer['price'] * 0.2,
'L_PAYMENTREQUEST_0_TAXAMT0' => $offer['price'] * 0.2,
'PAYMENTREQUEST_0_CURRENCYCODE' => 'EUR',
'PAYMENTREQUEST_0_CUSTOM' => $offer_id,
'L_BILLINGTYPE0' => 'RecurringPayments',
'L_BILLINGAGREEMENTDESCRIPTION0' => $offer['name'].' = '. $offer['price_text'],
'CANCELURL' => '
http://localhost/projet/CancelPaypal.php',
'RETURNURL' => '
http://localhost/projet/process.php'
];
$response = $this->nvp($data);
if (!isset($response['TOKEN'])){
throw new Exception($response['L_LONGMESSAGE0']);
}
$token = $response['TOKEN'];
$url = "
https://www.". ($this->sandbox ? "sandbox." : "") ."paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=$token";
header('location : '.$url);
}
public function getCheckoutDetail ($token){
$data = [
'METHOD' => 'GetExpressCheckoutDetail',
'TOKEN' => $token
];
$response = $this->nvp($data);
var_dump($response);
}
}
j'ai pas le retour de l'api de paypal (le token necessaire pour la suite de mon tuto). Et mon tuto c'est "Grafikart: paiement recurent"