Skip to content

Commit

Permalink
[FEATURE] PaymentProviderInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
smichaelsen committed Nov 14, 2015
1 parent 85ea4f3 commit 4519077
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 110 deletions.
155 changes: 49 additions & 106 deletions classes/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
namespace AppZap\Payment;

use AppZap\Payment\Model\OrderInterface;
use AppZap\Payment\Provider\Offline;
use AppZap\Payment\Provider\Paypal;
use AppZap\Payment\Provider\Sofortueberweisung;
use AppZap\Payment\Provider\PaymentProviderInterface;
use AppZap\Payment\Session\SessionHandler;
use AppZap\Payment\Session\SessionHandlerInterface;

Expand All @@ -23,12 +21,7 @@ abstract class Payment
/**
* @var string
*/
protected $abortKey;

/**
* @var bool
*/
protected $debugTokenUrls = false;
protected $encryptionKey;

/**
* @var OrderInterface
Expand All @@ -46,61 +39,11 @@ abstract class Payment
protected $sessionHandler;

/**
* @var string
*/
protected $successKey;

/**
* @param $paymentProviderName
* @return \AppZap\Payment\Payment
* @throws \InvalidArgumentException
*/
public static function getInstance($paymentProviderName)
{

$supportedPaymentProviders = array(
Paypal::PROVIDER_NAME => '\AppZap\Payment\Provider\Paypal',
Sofortueberweisung::PROVIDER_NAME => '\AppZap\Payment\Provider\Sofortueberweisung',
Offline::PROVIDER_NAME => '\AppZap\Payment\Provider\Offline',
);

if (array_key_exists($paymentProviderName, $supportedPaymentProviders)) {
return new $supportedPaymentProviders[$paymentProviderName];
} else {
throw new \InvalidArgumentException('Payment provider ' . htmlentities($paymentProviderName) . ' is not supported.');
}
}

/**
* @param string $abortKey
* @param string $encryptionKey
*/
public function setAbortKey($abortKey)
public function setEncryptionKey($encryptionKey)
{
$this->abortKey = $abortKey;
}

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

/**
* @param boolean $debugTokenUrls
*/
public function setDebugTokenUrls($debugTokenUrls)
{
$this->debugTokenUrls = $debugTokenUrls;
}

/**
* @return boolean
*/
public function getDebugTokenUrls()
{
return $this->debugTokenUrls;
$this->encryptionKey = $encryptionKey;
}

/**
Expand All @@ -111,34 +54,26 @@ public function setOrder(OrderInterface $order)
$this->order = $order;
}

/**
* @return OrderInterface
*/
public function getOrder()
{
return $this->order;
}

/**
* @param array $paymentProviderAuthConfig
*/
public function setPaymentProviderAuthConfig($paymentProviderAuthConfig)
public function setPaymentProviderAuthConfig(array $paymentProviderAuthConfig)
{
$this->paymentProviderAuthConfig = $paymentProviderAuthConfig;
}

/**
* @return array
* @param SessionHandlerInterface $sessionHandler
*/
public function getPaymentProviderAuthConfig()
public function setSessionHandler(SessionHandlerInterface $sessionHandler)
{
return $this->paymentProviderAuthConfig;
$this->sessionHandler = $sessionHandler;
}

/**
* @return SessionHandlerInterface
*/
public function getSessionHandler()
protected function getSessionHandler()
{
if (!$this->sessionHandler instanceof SessionHandlerInterface) {
$this->sessionHandler = new SessionHandler();
Expand All @@ -147,66 +82,74 @@ public function getSessionHandler()
}

/**
* @param SessionHandlerInterface $sessionHandler
*/
public function setSessionHandler(SessionHandlerInterface $sessionHandler)
{
$this->sessionHandler = $sessionHandler;
}

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

/**
* @param string $urlFormat
* @return string
*/
public function getSuccessKey()
protected function getAbortUrl($urlFormat)
{
return $this->successKey;
return sprintf(
$urlFormat,
TokenUtility::getUrlToken($this->order->getIdentifier(), $this->order->getRecordToken(), $this->getReturnKey(PaymentProviderInterface::RETURN_TYPE_ABORT))
);
}

/**
* When you have configured the payment properly this will give you a URL that you can redirect your visitor to,
* so that he can pay the desired amount.
*
* @param string $urlFormat
* @return string
*/
abstract public function getPaymentUrl($urlFormat);

/**
* @param string $urlFormat
* @return string
*/
public function getAbortUrl($urlFormat)
protected function getOfflinePaymentUrl($urlFormat)
{
return sprintf(
$urlFormat,
TokenUtility::getUrlToken($this->order->getIdentifier(), $this->order->getRecordToken(), $this->getAbortKey())
TokenUtility::getUrlToken($this->order->getIdentifier(), $this->order->getRecordToken(), $this->getReturnKey(PaymentProviderInterface::RETURN_TYPE_OFFLINE_PAYMENT))
);
}

/**
* @param string $urlFormat
* @return string
*/
public function getSuccessUrl($urlFormat)
protected function getSuccessUrl($urlFormat)
{
return sprintf(
$urlFormat,
TokenUtility::getUrlToken($this->order->getIdentifier(), $this->order->getRecordToken(), $this->getSuccessKey())
TokenUtility::getUrlToken($this->order->getIdentifier(), $this->order->getRecordToken(), $this->getReturnKey(PaymentProviderInterface::RETURN_TYPE_PAID))
);
}

/**
* @return void
*/
public function execute()
{
}

/**
* @param string $type
* @return string
*/
protected function getReturnKey($type)
{
return hash_hmac('sha1', $type, $this->encryptionKey);
}

/**
* @param OrderInterface $order
* @param string $returnToken
* @return int
* @throws \Exception
*/
public function evaluateReturnToken(OrderInterface $order, $returnToken)
{
$returnTypes = [PaymentProviderInterface::RETURN_TYPE_PAID, PaymentProviderInterface::RETURN_TYPE_ABORT, PaymentProviderInterface::RETURN_TYPE_OFFLINE_PAYMENT];
foreach ($returnTypes as $returnType) {
if (TokenUtility::evaluateUrlToken($order->getIdentifier(), $order->getRecordToken(), $returnToken, $this->getReturnKey($returnType))) {
return $returnType;
}
}
return PaymentProviderInterface::RETURN_TYPE_ERROR;
}

}

?>
32 changes: 32 additions & 0 deletions classes/PaymentFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
namespace AppZap\Payment;

use AppZap\Payment\Provider\Offline;
use AppZap\Payment\Provider\PaymentProviderInterface;
use AppZap\Payment\Provider\Paypal;
use AppZap\Payment\Provider\Sofortueberweisung;

class PaymentFactory
{

/**
* @param string $paymentProviderName
* @return PaymentProviderInterface
*/
public function getPaymentProviderObject($paymentProviderName)
{
// todo: move the supported payment providers to separate composer packages and offer an API to register themselves
$supportedPaymentProviders = array(
Paypal::PROVIDER_NAME => Paypal::class,
Sofortueberweisung::PROVIDER_NAME => Sofortueberweisung::class,
Offline::PROVIDER_NAME => Offline::class,
);

if (array_key_exists($paymentProviderName, $supportedPaymentProviders)) {
return new $supportedPaymentProviders[$paymentProviderName];
} else {
throw new \InvalidArgumentException('Payment provider ' . htmlentities($paymentProviderName) . ' is not supported.', 1447533889);
}
}

}
4 changes: 2 additions & 2 deletions classes/Provider/Offline.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use AppZap\Payment\Payment;

class Offline extends Payment
class Offline extends Payment implements PaymentProviderInterface
{

const PROVIDER_NAME = 'OFFLINE';
Expand All @@ -17,6 +17,6 @@ class Offline extends Payment
*/
public function getPaymentUrl($urlFormat)
{
return $this->getSuccessUrl($urlFormat);
return $this->getOfflinePaymentUrl($urlFormat);
}
}
60 changes: 60 additions & 0 deletions classes/Provider/PaymentProviderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
namespace AppZap\Payment\Provider;

use AppZap\Payment\Model\OrderInterface;
use AppZap\Payment\Session\SessionHandlerInterface;

interface PaymentProviderInterface
{

const RETURN_TYPE_ABORT = 0;
const RETURN_TYPE_PAID = 1;
const RETURN_TYPE_OFFLINE_PAYMENT = 2;
const RETURN_TYPE_ERROR = 3;

/**
* @return void
*/
public function execute();

/**
* When you have configured the payment properly this will give you a URL that you can redirect your visitor to,
* so that he can pay the desired amount.
*
* @param $urlFormat
* @return string
*/
public function getPaymentUrl($urlFormat);

/**
* @param string $key
* @return void
*/
public function setEncryptionKey($key);

/**
* @param OrderInterface $order
* @return void
*/
public function setOrder(OrderInterface $order);

/**
* @param array $paymentProviderAuthConfig
* @return void
*/
public function setPaymentProviderAuthConfig(array $paymentProviderAuthConfig);

/**
* @param SessionHandlerInterface $sessionHandler
* @return void
*/
public function setSessionHandler(SessionHandlerInterface $sessionHandler);

/**
* @param OrderInterface $order
* @param string $returnToken
* @return int
*/
public function evaluateReturnToken(OrderInterface $order, $returnToken);

}
2 changes: 1 addition & 1 deletion classes/Provider/Paypal.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use PayPal\Auth\OAuthTokenCredential;
use PayPal\Rest\ApiContext;

class Paypal extends Payment
class Paypal extends Payment implements PaymentProviderInterface
{

const MODE_LIVE = 'live';
Expand Down
2 changes: 1 addition & 1 deletion classes/Provider/Sofortueberweisung.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use AppZap\Payment\Payment;

class Sofortueberweisung extends Payment
class Sofortueberweisung extends Payment implements PaymentProviderInterface
{

const PROVIDER_NAME = 'SOFORTUEBERWEISUNG';
Expand Down

0 comments on commit 4519077

Please sign in to comment.