Paypal



Constructeur


namespace Model;

use Model\Paiement;
use Silex\Application;

use PayPal\Api\Amount;
use PayPal\Api\CreditCard;
use PayPal\Api\Details;
use PayPal\Api\FundingInstrument;
use PayPal\Api\Item;
use PayPal\Api\ItemList;
use PayPal\Api\Payer;
use PayPal\Api\Payment;
use PayPal\Api\Transaction;
use PayPal\Api\RedirectUrls;
use PayPal\Api\ExecutePayment;
use PayPal\Api\PaymentExecution;


class Paiement
{
	private $clientid;
	private $clientsecret;

	private $apiContext;
	private $path;

	public function __construct(Application $app)
	{
		$this->clientid     = $app['config']['paypal']['clientid'];
		$this->clientsecret = $app['config']['paypal']['clientsecret'];

		// After Step 1
		$this->apiContext = new \PayPal\Rest\ApiContext(
			new \PayPal\Auth\OAuthTokenCredential(
				$this->clientid,
				$this->clientsecret
			)
		);

		$this->path = 'https://www.proxidata.fr';
	}

	...
}
Étape 1: PayPal Payments - similar to Express Checkout in Classic APIs


public function PaymentUsingPayPalAsPaymentMethod($key, $price)
{
	$payer = new Payer();
	$payer->setPaymentMethod("paypal");

	$item1 = new Item();
	$item1
		->setName($name)
		->setCurrency('EUR')
		->setQuantity(1)
		->setSku($key)
		->setPrice($price);

	$itemList = new ItemList();
	$itemList->setItems(array($item1));

	$amount = new Amount();
	$amount
		->setCurrency("EUR")
		->setTotal($price);

	$transaction = new Transaction();
	$transaction
		->setAmount($amount)
		->setItemList($itemList)
		->setDescription("Payment description")
		->setInvoiceNumber(uniqid());

	$redirectUrls = new RedirectUrls();
	$redirectUrls
		->setReturnUrl($this->path."/paypal/$key?success=true")
		->setCancelUrl($this->path."/paypal/$key?success=false");

	$payment = new Payment();
	$payment
		->setIntent("sale")
		->setPayer($payer)
		->setRedirectUrls($redirectUrls)
		->setTransactions(array($transaction));

	$request = clone $payment;

	try {
		$payment->create($this->apiContext);
	} catch (\PayPal\Exception\PayPalConnectionException $ex) {
		echo $ex->getCode(); // Prints the Error Code
		echo $ex->getData(); // Prints the detailed error message
		die($ex);
	} catch (Exception $ex) {
		echo $e->getMessage();
		die($ex);
	}

	$approvalUrl = $payment->getApprovalLink();

	return [
		"id"      => $payment->getId(),
		"state"   => $payment->getState(),
		"request" => $request,
		"payment" => $payment,
		"link"    => $approvalUrl
	];
}
Étape 2: Execute after Success (required step after user approval)


public function executePaymentSample($price, $paymentId, $payerID)
{
	$payment = Payment::get($paymentId, $this->apiContext);

	$execution = new PaymentExecution();
	$execution->setPayerId($payerID);

	$transaction = new Transaction();
	$amount = new Amount();

	$amount->setCurrency('EUR');
	$amount->setTotal($price);
	$transaction->setAmount($amount);

	$execution->addTransaction($transaction);

	try {
		$result = $payment->execute($execution, $this->apiContext);

		try {
			$payment = Payment::get($paymentId, $this->apiContext);
		} catch (PayPal\Exception\PayPalConnectionException $ex) {
			echo $ex->getCode(); // Prints the Error Code
			echo $ex->getData(); // Prints the detailed error message
			die($ex);
		} catch (Exception $ex) {
			die($ex);
		}
	} catch (PayPal\Exception\PayPalConnectionException $ex) {
		echo $ex->getCode(); // Prints the Error Code
		echo $ex->getData(); // Prints the detailed error message
		die($ex);
	} catch (Exception $ex) {
		die($ex);
	}

	return [
		"id"      => $payment->getId(),
		"state"   => $payment->getState(),
		"payment" => $payment
	];

	return $payment;
}
Constructeur


namespace Model;

use Model\Paiement;
use Silex\Application;

use PayPal\Api\Amount;
use PayPal\Api\CreditCard;
use PayPal\Api\Details;
use PayPal\Api\FundingInstrument;
use PayPal\Api\Item;
use PayPal\Api\ItemList;
use PayPal\Api\Payer;
use PayPal\Api\Payment;
use PayPal\Api\Transaction;
use PayPal\Api\RedirectUrls;
use PayPal\Api\ExecutePayment;
use PayPal\Api\PaymentExecution;


class Paiement
{
	private $clientid;
	private $clientsecret;

	private $apiContext;
	private $path;

	public function __construct(Application $app)
	{
		$this->clientid     = $app['config']['paypal']['clientid'];
		$this->clientsecret = $app['config']['paypal']['clientsecret'];

		// After Step 1
		$this->apiContext = new \PayPal\Rest\ApiContext(
			new \PayPal\Auth\OAuthTokenCredential(
				$this->clientid,
				$this->clientsecret
			)
		);

		$this->path = 'https://www.proxidata.fr';
	}

	...
}
Payer à partir d'une carte bancaire


public function paymentsUsingCreditCardInformation($price, $firstname, $lastname, $type, $number, $month, $year, $cryptogramme)
{
	$card = new CreditCard();
	$card
		->setType(strtolower($type))
		->setNumber($number)
		->setExpireMonth($month)
		->setExpireYear($year)
		->setCvv2($cryptogramme)
		->setFirstName(ucfirst(strtolower($firstname)))
		->setLastName(ucfirst(strtolower($lastname)));

	$fi = new FundingInstrument();
	$fi->setCreditCard($card);

	$payer = new Payer();
	$payer
		->setPaymentMethod("credit_card")
		->setFundingInstruments(array($fi));

	$item1 = new Item();
	$item1
		->setName($name)
		->setDescription('description')
		->setCurrency('EUR')
		->setQuantity(1)
		->setPrice($price);

	$itemList = new ItemList();
	$itemList->setItems(array($item1));

	$amount = new Amount();
	$amount
		->setCurrency("EUR")
		->setTotal($price);

	$transaction = new Transaction();
	$transaction
		->setAmount($amount)
		->setItemList($itemList)
		->setDescription("Payment description")
		->setInvoiceNumber(uniqid());

	$payment = new Payment();
	$payment
		->setIntent("sale")
		->setPayer($payer)
		->setTransactions(array($transaction));

	$request = clone $payment;

	try {
		$payment->create($this->apiContext);
	} catch (\PayPal\Exception\PayPalConnectionException $ex) {
		$ex->getCode(); // Prints the Error Code
		$ex->getData(); // Prints the detailed error message

		return [
			"ok"      => false,
			"getCode" => $ex->getCode(),
			"getData" => $ex->getData()
		];

		die($ex);
	} catch (Exception $ex) {
		$e->getMessage();

		return [
			"ok"         => false,
			"getMessage" => $ex->getMessage()
		];

		die($ex);
	}

	return [
		"ok"      => true,
		"id"      => $payment->getId(),
		"state"   => $payment->getState(),
		"request" => $request,
		"payment" => $payment
	];
}
Créer une application et optenir des accès

Basculer du compte de test vers le compte réel


$this->apiContext = new \PayPal\Rest\ApiContext(
    new \PayPal\Auth\OAuthTokenCredential(
        $this->clientid,
        $this->clientsecret
    )
);

// Paramètres supplémentaires
$this->apiContext->setConfig([
	'log.LogEnabled' => true,
	'log.FileName'   => 'PayPal.log',
	'log.LogLevel'   => 'FINE',
	'mode'           => 'live', // sandbox or live
]);