Skip to content

Commit

Permalink
PaymentRequest + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieuduffeler committed Aug 27, 2013
1 parent 9c2e206 commit 7aaa395
Show file tree
Hide file tree
Showing 14 changed files with 499 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
nbproject/*
vendor/*
build/*
1 change: 1 addition & 0 deletions VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.0.1
10 changes: 10 additions & 0 deletions autoload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

$loader = require 'vendor/autoload.php';

// add non PSR-0 code to include path
set_include_path( __DIR__ . '/lib' . PATH_SEPARATOR .
get_include_path());

// enable include path class loading
$loader->setUseIncludePath(true);
6 changes: 6 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "marlon-be/marlon-sips",
"autoload": {
"psr-0": { "Sips": "lib/" }
}
}
27 changes: 27 additions & 0 deletions lib/Sips/Passphrase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Sips;

final class Passphrase
{
/**
* @var string
*/
private $passphrase;

public function __construct($passphrase)
{
if(!is_string($passphrase)) {
throw new \InvalidArgumentException("String expected");
}
$this->passphrase = $passphrase;
}

/**
* String representation
*/
public function __toString()
{
return (string) $this->passphrase;
}
}
171 changes: 171 additions & 0 deletions lib/Sips/PaymentRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
<?php

namespace Sips;

use Sips\ShaComposer\ShaComposer;
use \BadMethodCallException;
use \InvalidArgumentException;

class PaymentRequest
{
const TEST = "https://payment-webinit.simu.sips-atos.com/paymentInit";

/** @var ShaComposer */
private $shaComposer;

private $sipsUri = self::TEST;

private $parameters = array();

private $sipsFields = array(
'amount', 'currencyCode', 'merchantId', 'normalReturnUrl',
'transactionReference', 'keyVersion', 'templateName', 'customerLanguage'
);

private $requiredFields = array(
'amount', 'currencyCode', 'merchantId', 'normalReturnUrl',
'transactionReference', 'keyVersion'
);

public $allowedcurrencies = array(
'EUR' => '978', 'USD' => '840', 'CHF' => '756', 'GBP' => '826',
'CAD' => '124', 'JPY' => '392', 'MXP' => '484', 'TRL' => '792',
'AUD' => '036', 'NZD' => '554', 'NOK' => '578', 'BRC' => '986',
'ARP' => '032', 'KHR' => '116', 'TWD' => '901', 'SEK' => '752',
'DKK' => '208', 'KRW' => '410', 'SGD' => '702'
);

public $allowedlanguages = array(
'nl', 'fr', 'ge', 'en', 'sp', 'it'
);

public function __construct(ShaComposer $shaComposer)
{
$this->shaComposer = $shaComposer;
}

/** @return string */
public function getShaSign()
{
return $this->shaComposer->compose($this->toArray());
}

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

public function setSipsUri($sipsUri)
{
$this->validateUri($sipsUri);
$this->sipsUri = $sipsUri;
}

public function setNormalReturnUrl($uri)
{
$this->validateUri($uri);
$this->parameters['normalReturnUrl'] = $uri;
}

public function setOrderid($orderid)
{
if(strlen($orderid) > 30) {
throw new \InvalidArgumentException("Orderid cannot be longer than 30 characters");
}
if(preg_match('/[^a-zA-Z0-9_-]/', $orderid)) {
throw new \InvalidArgumentException("Orderid cannot contain special characters");
}
$this->parameters['orderid'] = $orderid;
}

/**
* Set amount in cents, eg EUR 12.34 is written as 1234
*/
public function setAmount($amount)
{
if(!is_int($amount)) {
throw new InvalidArgumentException("Integer expected. Amount is always in cents");
}
if($amount <= 0) {
throw new InvalidArgumentException("Amount must be a positive number");
}
if($amount >= 1.0E+15) {
throw new InvalidArgumentException("Amount is too high");
}
$this->parameters['amount'] = $amount;

}

public function setCurrency($currency)
{
if(!array_key_exists(strtoupper($currency), $this->allowedcurrencies)) {
throw new InvalidArgumentException("Unknown currency");
}
$this->parameters['currencyCode'] = $this->allowedcurrencies[$currency];
}

public function setLanguage($language)
{
if(!in_array($language, $this->allowedlanguages)) {
throw new InvalidArgumentException("Invalid language locale");
}
$this->parameters['customerLanguage'] = $language;
}

public function __call($method, $args)
{
if(substr($method, 0, 3) == 'set') {
$field = lcfirst(substr($method, 3));
if(in_array($field, $this->sipsFields)) {
$this->parameters[$field] = $args[0];
return;
}
}

if(substr($method, 0, 3) == 'get') {
$field = lcfirst(substr($method, 3));
if(array_key_exists($field, $this->parameters)) {
return $this->parameters[$field];
}
}

throw new BadMethodCallException("Unknown method $method");
}

public function toArray()
{
$this->validate();
return $this->parameters;
}

/** @return PaymentRequest */
public static function createFromArray(ShaComposer $shaComposer, array $parameters)
{
$instance = new static($shaComposer);
foreach($parameters as $key => $value)
{
$instance->{"set$key"}($value);
}
return $instance;
}

public function validate()
{
foreach($this->requiredFields as $field) {
if(empty($this->parameters[$field])) {
throw new \RuntimeException($field . " can not be empty");
}
}
}

protected function validateUri($uri)
{
if(!filter_var($uri, FILTER_VALIDATE_URL)) {
throw new InvalidArgumentException("Uri is not valid");
}
if(strlen($uri) > 200) {
throw new InvalidArgumentException("Uri is too long");
}
}
}
31 changes: 31 additions & 0 deletions lib/Sips/ShaComposer/AllParametersShaComposer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Sips\ShaComposer;

use Sips\Passphrase;
use Sips\ShaComposer\ShaComposer;

class AllParametersShaComposer implements ShaComposer
{
/**
* @var string Passphrase
*/
private $passphrase;

public function __construct(Passphrase $passphrase)
{
$this->passphrase = $passphrase;
}

public function compose(array $parameters)
{
// compose SHA string
$shaString = '';
foreach($parameters as $key => $value) {
$shaString .= $key . '=' . $value;
$shaString .= (array_search($key, array_keys($parameters)) != (count($parameters)-1)) ? '|' : $this->passphrase;
}

return hash('sha256', $shaString);
}
}
15 changes: 15 additions & 0 deletions lib/Sips/ShaComposer/ShaComposer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Sips\ShaComposer;

/**
* SHA Composers interface
*/
interface ShaComposer
{
/**
* Compose SHA string based on Sips response parameters
* @param array $parameters
*/
public function compose(array $parameters);
}
38 changes: 38 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
failOnError="false"
syntaxCheck="false"
bootstrap="./tests/bootstrap.php"
>

<logging>
<log type="coverage-html" target="build/coverage" title="Sips"
charset="UTF-8" yui="true" highlight="true" lowUpperBound="35"
highLowerBound="70" />
<log type="coverage-clover" target="build/logs/clover.xml" />
<log type="junit" target="build/logs/junit.xml"
logIncompleteSkipped="false" />
<log type="testdox-html" target="build/logs/testdox/index.html"/>
</logging>

<testsuites>
<testsuite name="Sips">
<directory>tests/Sips/Tests/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist addUncoveredFilesFromWhitelist="true">
<directory suffix=".php">lib</directory>
</whitelist>
</filter>
</phpunit>
Loading

0 comments on commit 7aaa395

Please sign in to comment.