nattreid/paypal

Paypal for Nette Framework

1.0.2 2021-02-15 10:35 UTC

This package is auto-updated.

Last update: 2024-12-15 19:15:32 UTC


README

Nastavení v config.neon

extensions:
    payPal: NAttreid\PayPal\DI\PayPalExtension

payPal:
	clientId: xxxXXXxXXXXxxxx
	secret: xxxXXXxXXXXxxxx
	experienceProfileId: xxxXXXxXXXXxxxx
	sdkConfig:
		mode: sandbox
		log.Enabled: true
		log.FileName: '%logDir%/PayPal.log'
		log.LogLevel: DEBUG
		validation.level: log
		cache.enabled: true
		cache.FileName: '%tempDir%/paypal/auth.cache'
		http.CURLOPT_CONNECTTIMEOUT: 30

sdkConfig je nastavení paypal/PayPal-PHP-SDK, sdk-config-sample

Použití

/** @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'
    );
}