From e74ba6e7c968f534af6090df53b1412a973ea9fd Mon Sep 17 00:00:00 2001 From: Hynek Vilimek Date: Mon, 21 Mar 2016 11:22:20 +0100 Subject: [PATCH] ga: add option to add parameter that helps track payments --- src/MetisFW/PayPal/DI/PayPalExtension.php | 4 ++- src/MetisFW/PayPal/Helpers/GaTracking.php | 27 +++++++++++++++++++ src/MetisFW/PayPal/PayPalContext.php | 17 ++++++++++++ .../PayPal/Payment/BasePaymentOperation.php | 5 ++++ tests/PayPal/DI/PayPalExtensionTest.phpt | 4 +++ .../PayPal/Payment/PaymentOperationTest.phpt | 21 +++++++++++++++ 6 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 src/MetisFW/PayPal/Helpers/GaTracking.php diff --git a/src/MetisFW/PayPal/DI/PayPalExtension.php b/src/MetisFW/PayPal/DI/PayPalExtension.php index 3f54d98..69430aa 100644 --- a/src/MetisFW/PayPal/DI/PayPalExtension.php +++ b/src/MetisFW/PayPal/DI/PayPalExtension.php @@ -14,6 +14,7 @@ class PayPalExtension extends CompilerExtension { */ public $defaults = array( 'currency' => 'CZK', + 'gaTrackingEnabled' => true ); public function loadConfiguration() { @@ -39,7 +40,8 @@ public function loadConfiguration() { $builder->addDefinition($this->prefix('PayPal')) ->setClass('MetisFW\PayPal\PayPalContext', array($this->prefix('@apiContext'))) ->addSetup('setConfig', array($config['sdkConfig'])) - ->addSetup('setCurrency', array($config['currency'])); + ->addSetup('setCurrency', array($config['currency'])) + ->addSetup('setGaTrackingEnabled', array($config['gaTrackingEnabled'])); } /** diff --git a/src/MetisFW/PayPal/Helpers/GaTracking.php b/src/MetisFW/PayPal/Helpers/GaTracking.php new file mode 100644 index 0000000..16fe7d4 --- /dev/null +++ b/src/MetisFW/PayPal/Helpers/GaTracking.php @@ -0,0 +1,27 @@ +getRedirectUrls(); + + $url = new Url($redirectUrls->getReturnUrl()); + $url->setQueryParameter('utm_nooverride', 1); + + $redirectUrls->setReturnUrl($url->getAbsoluteUrl()); + $payment->setRedirectUrls($redirectUrls); + + return $payment; + } + +} diff --git a/src/MetisFW/PayPal/PayPalContext.php b/src/MetisFW/PayPal/PayPalContext.php index bd6f7be..ac4b5eb 100644 --- a/src/MetisFW/PayPal/PayPalContext.php +++ b/src/MetisFW/PayPal/PayPalContext.php @@ -13,6 +13,9 @@ class PayPalContext extends Object { /** @var string */ private $currency; + /** @var bool */ + private $gaTrackingEnabled; + /** * @param string $clientId * @param string $secret @@ -49,4 +52,18 @@ public function getApiContext() { return $this->apiContext; } + /** + * @param bool $value + */ + public function setGaTrackingEnabled($value) { + $this->gaTrackingEnabled = $value; + } + + /** + * @return bool + */ + public function isGaTrackingEnabled() { + return $this->gaTrackingEnabled; + } + } diff --git a/src/MetisFW/PayPal/Payment/BasePaymentOperation.php b/src/MetisFW/PayPal/Payment/BasePaymentOperation.php index 8eee206..c17f370 100644 --- a/src/MetisFW/PayPal/Payment/BasePaymentOperation.php +++ b/src/MetisFW/PayPal/Payment/BasePaymentOperation.php @@ -2,6 +2,7 @@ namespace MetisFW\PayPal\Payment; +use MetisFW\PayPal\Helpers\GaTracking; use MetisFW\PayPal\PayPalContext; use MetisFW\PayPal\PayPalException; use Nette\InvalidArgumentException; @@ -83,6 +84,10 @@ public function getPayment() { * @return Payment */ public function createPayment(Payment $payment) { + if($this->context->isGaTrackingEnabled()) { + $payment = GaTracking::addTrackingParameters($payment); + } + try { return $payment->create($this->context->getApiContext()); } diff --git a/tests/PayPal/DI/PayPalExtensionTest.phpt b/tests/PayPal/DI/PayPalExtensionTest.phpt index 81df64d..00244f7 100644 --- a/tests/PayPal/DI/PayPalExtensionTest.phpt +++ b/tests/PayPal/DI/PayPalExtensionTest.phpt @@ -5,6 +5,7 @@ namespace MetisFWTests\PayPal\DI; use MetisFW\PayPal\DI\PayPalExtension; use MetisFW\PayPal\Payment\PlainPaymentOperation; use MetisFW\PayPal\Payment\SimplePaymentOperation; +use MetisFW\PayPal\PayPalContext; use Nette\Configurator; use Tester\Assert; use Tester\TestCase; @@ -21,6 +22,7 @@ class PayPalExtensionTest extends TestCase { $config->addConfig(__DIR__.'/../../paypal.config.neon'); $container = $config->createContainer(); + /** @var PayPalContext $paypal */ $paypal = $container->getByType('MetisFW\PayPal\PayPalContext'); Assert::notEqual(null, $paypal); @@ -32,6 +34,8 @@ class PayPalExtensionTest extends TestCase { $plainOperationFactory = $container->getByType('MetisFW\PayPal\Payment\PlainPaymentOperationFactory'); $operation = $plainOperationFactory->create(array()); Assert::true($operation instanceof PlainPaymentOperation); + + Assert::true($paypal->isGaTrackingEnabled()); } } diff --git a/tests/PayPal/Payment/PaymentOperationTest.phpt b/tests/PayPal/Payment/PaymentOperationTest.phpt index 1cde999..9f9fa42 100644 --- a/tests/PayPal/Payment/PaymentOperationTest.phpt +++ b/tests/PayPal/Payment/PaymentOperationTest.phpt @@ -62,6 +62,27 @@ class PaymentOperationTest extends TestCase { $result = $operation->createPayment($payment); Assert::equal('created', $result->getState()); + Assert::equal('http://localhost/return', $payment->getRedirectUrls()->getReturnUrl()); + Assert::notEqual(null, $result->getApprovalLink()); + } + + public function testCreatePaymentGaTracking() { + $credentials = new OAuthTokenCredential($this->config['clientId'], $this->config['secretId']); + $apiContext = \Mockery::mock('\PayPal\Rest\ApiContext', array($credentials))->makePartial(); + $context = new PayPalContext($apiContext); + $context->setGaTrackingEnabled(true); + + $operation = new DummyPaymentOperation($context); + $payment = $operation->getPayment(); + + $redirectUrls = new RedirectUrls(); + $redirectUrls->setReturnUrl('http://localhost/return'); + $redirectUrls->setCancelUrl('http://localhost/cancel'); + $payment->setRedirectUrls($redirectUrls); + + $result = $operation->createPayment($payment); + Assert::equal('created', $result->getState()); + Assert::equal('http://localhost/return?utm_nooverride=1', $payment->getRedirectUrls()->getReturnUrl()); Assert::notEqual(null, $result->getApprovalLink()); }