Skip to content

Commit

Permalink
ga: add option to add parameter that helps track payments
Browse files Browse the repository at this point in the history
  • Loading branch information
Hynek Vilimek committed Mar 21, 2016
1 parent 54e689e commit e74ba6e
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/MetisFW/PayPal/DI/PayPalExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class PayPalExtension extends CompilerExtension {
*/
public $defaults = array(
'currency' => 'CZK',
'gaTrackingEnabled' => true
);

public function loadConfiguration() {
Expand All @@ -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']));
}

/**
Expand Down
27 changes: 27 additions & 0 deletions src/MetisFW/PayPal/Helpers/GaTracking.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace MetisFW\PayPal\Helpers;

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

class GaTracking extends Object {

private function __construct() {
// nothing
}

public static function addTrackingParameters(Payment $payment) {
$redirectUrls = $payment->getRedirectUrls();

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

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

return $payment;
}

}
17 changes: 17 additions & 0 deletions src/MetisFW/PayPal/PayPalContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ class PayPalContext extends Object {
/** @var string */
private $currency;

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

/**
* @param string $clientId
* @param string $secret
Expand Down Expand Up @@ -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;
}

}
5 changes: 5 additions & 0 deletions src/MetisFW/PayPal/Payment/BasePaymentOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace MetisFW\PayPal\Payment;

use MetisFW\PayPal\Helpers\GaTracking;
use MetisFW\PayPal\PayPalContext;
use MetisFW\PayPal\PayPalException;
use Nette\InvalidArgumentException;
Expand Down Expand Up @@ -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());
}
Expand Down
4 changes: 4 additions & 0 deletions tests/PayPal/DI/PayPalExtensionTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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());
}

}
Expand Down
21 changes: 21 additions & 0 deletions tests/PayPal/Payment/PaymentOperationTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down

0 comments on commit e74ba6e

Please sign in to comment.