Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compatibility with Nette 3.0 #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,25 @@
"issues": "https://github.com/MetisFW/PayPal/issues"
},
"require": {
"php": ">=5.6.0",
"nette/di": "^2.4",
"nette/application": "^2.4",
"paypal/rest-api-sdk-php": "^1.5"
"php": "^7.1",
"ext-curl": "*",
"ext-json": "*",
"nette/di": "^3.0",
"nette/application": "^3.0",
"paypal/rest-api-sdk-php": "^1.14"
},
"require-dev": {
"nette/bootstrap": "^2.4",
"nette/robot-loader": "^2.4",
"nette/tester": "^2.4",
"mockery/mockery": "^1.4"
"nette/bootstrap": "^3.0",
"tracy/tracy": "^2.6",
"nette/tester": "^2.2",
"mockery/mockery": "^1.2",
"phpstan/phpstan": "^0.11",
"phpstan/phpstan-nette": "^0.11"
},
"autoload": {
"psr-0": {
"MetisFW\\PayPal\\": "src/"
"psr-4": {
"MetisFW\\PayPal\\": "src/MetisFW/PayPal",
"Tests\\PayPal\\": "tests/PayPal"
}
},
"archive": {
Expand Down
12 changes: 12 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
parameters:
level: 7
paths:
- src
- tests/PayPal
ignoreErrors:
- '#Cannot call method getParameter\(\) on Nette\\Application\\UI\\Presenter|null#'
# - '#Cannot call method redirectUrl\(\) on Nette\\Application\\UI\\Presenter|null#'

includes:
- vendor/phpstan/phpstan-nette/extension.neon
- vendor/phpstan/phpstan-nette/rules.neon
115 changes: 63 additions & 52 deletions src/MetisFW/PayPal/DI/PayPalExtension.php
Original file line number Diff line number Diff line change
@@ -1,60 +1,71 @@
<?php
<?php declare(strict_types=1);

namespace MetisFW\PayPal\DI;

use Nette\Configurator;
use Nette\DI\Compiler;
use Nette\DI\CompilerExtension;
use Nette\Utils\Validators;

class PayPalExtension extends CompilerExtension {

/**
* @var array
*/
public $defaults = array(
'currency' => 'CZK',
'gaTrackingEnabled' => true
);

public function loadConfiguration() {
$builder = $this->getContainerBuilder();
$config = $this->getConfig($this->defaults);

Validators::assertField($config, 'clientId');
Validators::assertField($config, 'secret');
Validators::assertField($config, 'sdkConfig', 'array');

$builder->addDefinition($this->prefix('simplePaymentOperationFactory'))
->setImplement('MetisFW\PayPal\Payment\SimplePaymentOperationFactory');

$builder->addDefinition($this->prefix('plainPaymentOperationFactory'))
->setImplement('MetisFW\PayPal\Payment\PlainPaymentOperationFactory');

$builder->addDefinition($this->prefix('credentials'))
->setClass('PayPal\Auth\OAuthTokenCredential', array($config['clientId'], $config['secret']));

$builder->addDefinition($this->prefix('apiContext'))
->setClass('PayPal\Rest\ApiContext', array($this->prefix('@credentials')));

$paypal = $builder->addDefinition($this->prefix('PayPal'))
->setClass('MetisFW\PayPal\PayPalContext', array($this->prefix('@apiContext')))
->addSetup('setConfig', array($config['sdkConfig']))
->addSetup('setCurrency', array($config['currency']))
->addSetup('setGaTrackingEnabled', array($config['gaTrackingEnabled']));

if (isset($config['experienceProfileId'])) {
$paypal->addSetup('setExperienceProfileId', array($config['experienceProfileId']));
}
}

/**
* @param Configurator $configurator
*/
public static function register(Configurator $configurator) {
$configurator->onCompile[] = function ($config, Compiler $compiler) {
$compiler->addExtension('payPal', new PayPalExtension());
};
}
use Nette\Schema\Expect;
use Nette\Schema\Schema;

class PayPalExtension extends CompilerExtension
{

public function loadConfiguration()
{
$builder = $this->getContainerBuilder();
$config = (array) $this->getConfig();

$builder->addFactoryDefinition($this->prefix('simplePaymentOperationFactory'))
->setImplement('MetisFW\PayPal\Payment\SimplePaymentOperationFactory');

$builder->addFactoryDefinition($this->prefix('plainPaymentOperationFactory'))
->setImplement('MetisFW\PayPal\Payment\PlainPaymentOperationFactory');

$builder->addDefinition($this->prefix('credentials'))
->setType('PayPal\Auth\OAuthTokenCredential')
->setArguments([$config['clientId'], $config['secret']]);

$builder->addDefinition($this->prefix('apiContext'))
->setType('PayPal\Rest\ApiContext')
->setArguments([$this->prefix('@credentials')]);

$paypal = $builder->addDefinition($this->prefix('PayPal'))
->setType('MetisFW\PayPal\PayPalContext')
->setArguments([$this->prefix('@apiContext')])
->addSetup('setConfig', [(array) $config['sdkConfig']])
->addSetup('setCurrency', [$config['currency']])
->addSetup('setGaTrackingEnabled', [$config['gaTrackingEnabled']]);

if ($config['experienceProfileId'] !== null) {
$paypal->addSetup('setExperienceProfileId', [$config['experienceProfileId']]);
}
}

public static function register(Configurator $configurator)
{
$configurator->onCompile[] = function ($config, Compiler $compiler) {
$compiler->addExtension('payPal', new PayPalExtension());
};
}

public function getConfigSchema(): Schema
{
return Expect::structure([
'currency' => Expect::string('CZK'),
'gaTrackingEnabled' => Expect::bool(true),
'experienceProfileId' => Expect::string(),
'clientId' => Expect::string()->required(),
'secret' => Expect::string()->required(),
'sdkConfig' => Expect::structure([
'mode' => Expect::anyOf('sandbox', 'live')->required(),
'log.LogEnabled' => Expect::bool(),
'log.FileName' => Expect::string(),
'log.LogLevel' => Expect::anyOf('emergency', 'alert', 'critical', 'error', 'warning', 'notice', 'info', 'debug'),
'cache.enabled' => Expect::bool(true),
'cache.FileName' => Expect::string(),
]),
]);
}

}
32 changes: 16 additions & 16 deletions src/MetisFW/PayPal/Helpers/GaTracking.php
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
<?php
<?php declare(strict_types=1);

namespace MetisFW\PayPal\Helpers;

use Nette\Http\Url;
use Nette\SmartObject;
use PayPal\Api\Payment;

class GaTracking {
class GaTracking
{

use SmartObject;
private function __construct()
{
// nothing
}

private function __construct() {
// nothing
}
public static function addTrackingParameters(Payment $payment): Payment
{
$redirectUrls = $payment->getRedirectUrls();

public static function addTrackingParameters(Payment $payment) {
$redirectUrls = $payment->getRedirectUrls();
$url = new Url($redirectUrls->getReturnUrl());
$url->setQueryParameter('utm_nooverride', 1);

$url = new Url($redirectUrls->getReturnUrl());
$url->setQueryParameter('utm_nooverride', 1);
$redirectUrls->setReturnUrl($url->getAbsoluteUrl());
$payment->setRedirectUrls($redirectUrls);

$redirectUrls->setReturnUrl($url->getAbsoluteUrl());
$payment->setRedirectUrls($redirectUrls);

return $payment;
}
return $payment;
}

}
141 changes: 60 additions & 81 deletions src/MetisFW/PayPal/PayPalContext.php
Original file line number Diff line number Diff line change
@@ -1,88 +1,67 @@
<?php
<?php declare(strict_types=1);

namespace MetisFW\PayPal;

use Nette\SmartObject;
use PayPal\Rest\ApiContext;

class PayPalContext {

use SmartObject;

/** @var ApiContext */
private $apiContext;

/** @var string */
private $currency;

/** @var bool */
private $gaTrackingEnabled;

/** @var string */
private $experienceProfileId;

/**
* @param string $clientId
* @param string $secret
*/
public function __construct(ApiContext $apiContext) {
$this->apiContext = $apiContext;
}

/**
* @param array $config
*/
public function setConfig(array $config) {
$this->apiContext->setConfig($config);
}

/**
* @param string $currency
*/
public function setCurrency($currency) {
$this->currency = $currency;
}

/**
* @return string
*/
public function getCurrency() {
return $this->currency;
}

/**
* @return ApiContext
*/
public function getApiContext() {
return $this->apiContext;
}

/**
* @param bool $value
*/
public function setGaTrackingEnabled($value) {
$this->gaTrackingEnabled = $value;
}

/**
* @return bool
*/
public function isGaTrackingEnabled() {
return $this->gaTrackingEnabled;
}

/**
* @return string
*/
public function getExperienceProfileId() {
return $this->experienceProfileId;
}

/**
* @param string $experienceProfileId
*/
public function setExperienceProfileId($experienceProfileId) {
$this->experienceProfileId = $experienceProfileId;
}
class PayPalContext
{

/** @var ApiContext */
private $apiContext;

/** @var string */
private $currency;

/** @var bool */
private $gaTrackingEnabled = false;

/** @var string|null */
private $experienceProfileId;

public function __construct(ApiContext $apiContext)
{
$this->apiContext = $apiContext;
}

public function setConfig(array $config): void
{
$this->apiContext->setConfig($config);
}

public function setCurrency(string $currency): void
{
$this->currency = $currency;
}

public function getCurrency(): string
{
return $this->currency;
}

public function getApiContext(): ApiContext
{
return $this->apiContext;
}

public function setGaTrackingEnabled(bool $value): void
{
$this->gaTrackingEnabled = $value;
}

public function isGaTrackingEnabled(): bool
{
return $this->gaTrackingEnabled;
}

public function getExperienceProfileId(): ?string
{
return $this->experienceProfileId;
}

public function setExperienceProfileId(string $experienceProfileId): void
{
$this->experienceProfileId = $experienceProfileId;
}

}
7 changes: 4 additions & 3 deletions src/MetisFW/PayPal/PayPalException.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php
<?php declare(strict_types=1);

namespace MetisFW\PayPal;

class PayPalException extends \RuntimeException {
class PayPalException extends \RuntimeException
{

}
}
Loading