1. Go to this page and download the library: Download nattreid/paypal library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
nattreid / paypal example snippets
/** @var \Nattreid\PayPal\Control\IPayPalControlFactory @inject */
public $payPalControlFactory;
/** @var \Nattreid\PayPal\PayPalClient @inject */
public $payPalClient;
protected function createComponentButton(): \Nattreid\PayPal\Control\PayPalControl
{
$control = $this->payPalControlFactory->create();
$control->setCurrency('CZK');
foreach ($this->order->items as $item) {
$control->addItem(
$item->name,
$item->count,
$item->price
);
}
$discount = $this->order->discount;
if ($discount) {
$control->addItem(
'Discount',
1,
-$discount
);
}
$control->onSuccess[] = function (Payment $paid, Sale $sale, bool $pending) {
$this->order->paypalTransactionId = $sale->getId();
if ($pending) {
$this->order->setState(Pending::class);
} else {
$this->order->setState(Payed::class);
}
$this->redirect($this->link('success'));
};
$control->onError[] = function (PayPalException $exception) {
Debugger::log($exception->getMessage(), 'paypal');
$this->redirect($this->link('error'));
};
$control->onCancel[] = function () {
$this->order->setState(Cancel::class);
$this->redirect($this->link('cancel'));
};
return $control;
}
public function paypalCheckPayments(): void
{
foreach ($this->orders as $order) {
if ($order->paypalTransactionId !== null) {
try {
$status = $this->payPalClient->checkPayment($order->paypalTransactionId);
if ($status === true) {
$order->setState(Payed::class);
} elseif ($status === false) {
$order->setState(Cancel::class);
}
} catch (PayPalException $ex) {
}
}
}
}
public function refund(): void
{
$this->payPalClient->refund(
'paypalPaymentId'
5.55,
'USD'
);
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.