Skip to content

Commit

Permalink
Sanitize dash and spaces from BillToPostCode and throw exceptions on …
Browse files Browse the repository at this point in the history
…other non-alphanumeric values, fixes #4
  • Loading branch information
einkoro committed Oct 7, 2015
1 parent 4957d71 commit 988b687
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions src/Message/AuthorizeRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public function getData()

$billingDetails = [
'BillToAddress' => $this->getCard()->getAddress1(),
'BillToZipPostCode' => $this->getCard()->getPostcode(),
'BillToZipPostCode' => $this->formatPostcode(),
'BillToFirstName' => $this->getCard()->getFirstName(),
'BillToLastName' => $this->getCard()->getLastName(),
'BillToCity' => $this->getCard()->getCity(),
Expand All @@ -114,7 +114,7 @@ public function getData()
// FAC only accepts two digit state abbreviations from the USA
if ( $billingDetails['BillToCountry'] == 840 )
{
$billingDetails['BillToState'] = $this->getState();
$billingDetails['BillToState'] = $this->formatState();
}

$data = [
Expand Down Expand Up @@ -165,7 +165,7 @@ protected function formatCountry()
*
* @return string State abbreviation
*/
public function getState()
public function formatState()
{
$state = $this->getCard()->getState();

Expand All @@ -177,6 +177,26 @@ public function getState()
return $state;
}

/**
* Returns the postal code sanitizing dashes and spaces and throws exceptions with other
* non-alphanumeric characters
*
* @throws Omnipay\Common\Exception\InvalidRequestException
*
* @return string Postal code
*/
public function formatPostcode()
{
$postal = preg_replace( '/[\s\-]/', '', $this->getCard()->getPostcode() );

if ( preg_match('/[^a-z0-9]/i', $postal) )
{
throw new InvalidRequestException("The postal code must be alpha-numeric.");
}

return $postal;
}

/**
* Returns endpoint for authorize requests
*
Expand Down

0 comments on commit 988b687

Please sign in to comment.