Skip to content

Commit

Permalink
Added Normalizer and some string length limits.
Browse files Browse the repository at this point in the history
  • Loading branch information
ambroisemaupate committed May 19, 2017
1 parent 33bc18e commit a9bef5d
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ Gateway is provided default *Sogenactif* (Société Générale) testing credenti
| keyVersion | `1` |
| url | `https://payment-webinit.simu.sips-atos.com` |

Be careful, in *test* mode, `transactionReference` parameter is mandatory.

## Usage

### First step: offsite payment
Expand Down
1 change: 0 additions & 1 deletion src/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Omnipay\SipsPayPage;

use Omnipay\Common\AbstractGateway;
use Guzzle\Http\Client as HttpClient;

class Gateway extends AbstractGateway
{
Expand Down
5 changes: 4 additions & 1 deletion src/Message/PurchaseRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Omnipay\SipsPayPage\Composer\ShaComposer;
use Omnipay\SipsPayPage\Composer\ParameterComposer;
use Omnipay\SipsPayPage\Normalizer;

class PurchaseRequest extends AbstractRequest
{
Expand Down Expand Up @@ -314,6 +315,8 @@ public function getData()
}
}

$this->getCard()->validate();

$data = array_merge($data, $this->extractCardParameters(), [
'normalReturnUrl' => $this->getReturnUrl(),
'automaticResponseUrl' => $this->getNotifyUrl(),
Expand Down Expand Up @@ -351,7 +354,7 @@ protected function extractCardParameters()
'customerData.birthDate' => $card->getBirthday(),
// billing contact
'billingContact.firstname' => $card->getBillingFirstName(),
'billingContact.lastname' => $card->getBillingLastName(),
'billingContact.lastname' => $card->getBillingLastName(),
'billingContact.phone' => $card->getBillingPhone(),
'billingContact.title' => $card->getBillingTitle(),
'billingContact.email' => $card->getEmail(),
Expand Down
26 changes: 26 additions & 0 deletions src/Normalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Omnipay\SipsPayPage;

abstract class Normalizer
{
private static $invalid = array(
'Š'=>'S', 'š'=>'s', 'Đ'=>'Dj', 'đ'=>'dj', 'Ž'=>'Z', 'ž'=>'z',
'Č'=>'C', 'č'=>'c', 'Ć'=>'C', 'ć'=>'c', 'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A',
'Ä'=>'A', 'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E', 'Ê'=>'E', 'Ë'=>'E',
'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O',
'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U', 'Ú'=>'U', 'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y',
'Þ'=>'B', 'ß'=>'Ss', 'à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a',
'æ'=>'a', 'ç'=>'c', 'è'=>'e', 'é'=>'e', 'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i',
'î'=>'i', 'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o', 'ô'=>'o', 'õ'=>'o',
'ö'=>'o', 'ø'=>'o', 'ù'=>'u', 'ú'=>'u', 'û'=>'u', 'ý'=>'y', 'ý'=>'y', 'þ'=>'b',
'ÿ'=>'y', 'Ŕ'=>'R', 'ŕ'=>'r', "`" => "'", "´" => "'", "" => ",", "`" => "'",
"´" => "'", "" => "\"", "" => "\"", "´" => "'", "&acirc;€™" => "'", "{" => "",
"~" => "", "" => "-", "" => "'", ">" => " ", "<" => " "
);

public static function normalize($string)
{
return str_replace(array_keys(self::$invalid), array_values(self::$invalid), $string);
}
}
116 changes: 116 additions & 0 deletions src/OffsiteCreditCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,97 @@
*/
class OffsiteCreditCard extends CreditCard
{

/**
* Get the first part of the card billing name.
*
* @return string
*/
public function getBillingFirstName()
{
return Normalizer::normalize(parent::getBillingFirstName());
}

/**
* Get the last part of the card billing name.
*
* @return string
*/
public function getBillingLastName()
{
return Normalizer::normalize(parent::getBillingLastName());
}

/**
* Get the card billing company name.
*
* @return string
*/
public function getCompany()
{
return Normalizer::normalize(parent::getCompany());
}

/**
* Get the billing address, line 1.
*
* @return string
*/
public function getAddress1()
{
return Normalizer::normalize(parent::getAddress1());
}

/**
* Get the billing address, line 2.
*
* @return string
*/
public function getAddress2()
{
return Normalizer::normalize(parent::getAddress2());
}

/**
* Get the card billing title.
*
* @return string
*/
public function getBillingTitle()
{
return Normalizer::normalize(parent::getBillingTitle());
}

/**
* Get the billing address, line 1.
*
* @return string
*/
public function getBillingAddress1()
{
return Normalizer::normalize(parent::getBillingAddress1());
}

/**
* Get the billing address, line 2.
*
* @return string
*/
public function getBillingAddress2()
{
return Normalizer::normalize(parent::getBillingAddress2());
}

/**
* Get the billing city.
*
* @return string
*/
public function getBillingCity()
{
return Normalizer::normalize(parent::getBillingCity());
}

/**
* @{inheritdoc}
*/
Expand All @@ -27,5 +118,30 @@ public function validate()
if (!$this->getParameter('email')) {
throw new InvalidCreditCardException("The email parameter is required with OffsiteCreditCard.");
}

if (strlen($this->getParameter('email')) > 50) {
throw new InvalidCreditCardException("Email is too long.");
}


if ($this->getParameter('address1') &&
strlen($this->getParameter('address1')) > 50) {
throw new InvalidCreditCardException("Address1 is too long.");
}

if ($this->getParameter('address2') &&
strlen($this->getParameter('address2')) > 50) {
throw new InvalidCreditCardException("Address2 is too long.");
}

if ($this->getParameter('billingAddress1') &&
strlen($this->getParameter('billingAddress1')) > 50) {
throw new InvalidCreditCardException("Address1 is too long.");
}

if ($this->getParameter('billingAddress2') &&
strlen($this->getParameter('billingAddress2')) > 50) {
throw new InvalidCreditCardException("Address2 is too long.");
}
}
}
36 changes: 36 additions & 0 deletions tests/Message/PurchaseRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,40 @@ public function testSendSuccess()

$this->assertEquals($httpResponse->getBody(true), $response->getRedirectResponse()->getContent());
}

public function testCardAddress1Limit()
{
$this->setExpectedException(\Omnipay\Common\Exception\InvalidCreditCardException::class, 'Address1 is too long.');
$card = new OffsiteCreditCard();
$card->setEmail('[email protected]');
$card->setAddress1('uriset aursiet arusiet arusiet rausite nrautie rsauitersauiest i,auice');
$this->request->setCard($card);

/** @var \Omnipay\SipsPayPage\Message\PurchaseResponse $response */
$response = $this->request->send();

}

public function testCardEmail()
{
$this->setExpectedException(\Omnipay\Common\Exception\InvalidCreditCardException::class, 'The email parameter is required with OffsiteCreditCard.');
$card = new OffsiteCreditCard();
$this->request->setCard($card);

/** @var \Omnipay\SipsPayPage\Message\PurchaseResponse $response */
$response = $this->request->send();

}

public function testCardNormalize()
{
$card = new OffsiteCreditCard();
$card->setAddress1('11 rue Élie Rochette');
$card->setAddress2('éèàù');
$card->setFirstName('éèàù');

$this->assertEquals('11 rue Elie Rochette', $card->getAddress1());
$this->assertEquals('eeau', $card->getAddress2());
$this->assertEquals('eeau', $card->getFirstName());
}
}

0 comments on commit a9bef5d

Please sign in to comment.