Skip to content
This repository has been archived by the owner on Aug 26, 2020. It is now read-only.

Commit

Permalink
Merge pull request Strikewood#8 from CaswellWC/master
Browse files Browse the repository at this point in the history
Updates the iso3166 package to the league package
  • Loading branch information
dcaswel authored Feb 7, 2017
2 parents 6455666 + 530f6db commit 574aaed
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 39 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,6 @@ bin
.Trashes
ehthumbs.db
Thumbs.db

# phpstorm
.idea
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"require": {
"php": ">=5.4",
"omnipay/common": "~2.3",
"alcohol/iso3166": "~2.0"
"league/iso3166": "~1.0.1"
},
"require-dev": {
"omnipay/tests": "~2.0"
Expand Down
36 changes: 36 additions & 0 deletions src/CreditCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

namespace Omnipay\FirstAtlanticCommerce;

use League\ISO3166\ISO3166;
use Omnipay\Common\CreditCard as BaseCreditCard;
use Omnipay\Common\Exception\InvalidCreditCardException;
use Omnipay\Common\Exception\InvalidRequestException;
use Omnipay\Common\Helper;


class CreditCard extends BaseCreditCard
{
Expand Down Expand Up @@ -66,4 +70,36 @@ public function validate()
}
}
}

/**
* Returns the country as the numeric ISO 3166-1 code
*
* @throws InvalidRequestException
*
* @return int ISO 3166-1 numeric country
*/
public function getNumericCountry()
{
$country = $this->getCountry();

if ( !is_null($country) && !is_numeric($country) )
{
$iso3166 = new ISO3166();

if ( strlen($country) == 2 )
{
$country = $iso3166->getByAlpha2($country)['numeric'];
}
elseif ( strlen($country) == 3 )
{
$country = $iso3166->getByAlpha3($country)['numeric'];
}
else
{
throw new InvalidRequestException("The country parameter must be ISO 3166-1 numeric, aplha2 or alpha3.");
}
}

return $country;
}
}
13 changes: 12 additions & 1 deletion src/Message/AbstractRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Omnipay\FirstAtlanticCommerce\Message;

use Omnipay\Common\Message\AbstractRequest as BaseAbstractRequest;
use Omnipay\Common\Message\ResponseInterface;
use Omnipay\FirstAtlanticCommerce\CreditCard;
use Omnipay\FirstAtlanticCommerce\ParameterTrait;
use SimpleXMLElement;
Expand Down Expand Up @@ -132,7 +133,7 @@ protected function xmlSerialize($data, $xml = null)
*
* @param CreditCard $value
*
* @return AbstractRequest Provides a fluent interface
* @return \Omnipay\Common\Message\AbstractRequest Provides a fluent interface
*/
public function setCard($value)
{
Expand All @@ -143,4 +144,14 @@ public function setCard($value)

return $this->setParameter('card', $value);
}

/**
* Get the card.
*
* @return CreditCard
*/
public function getCard()
{
return $this->getParameter('card');
}
}
40 changes: 3 additions & 37 deletions src/Message/AuthorizeRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

namespace Omnipay\FirstAtlanticCommerce\Message;

use Alcohol\ISO3166;
use Omnipay\Common\Exception\InvalidRequestException;
use Omnipay\FirstAtlanticCommerce\Message\AbstractRequest;

/**
* FACPG2 Authorize Request
Expand Down Expand Up @@ -105,7 +103,7 @@ public function getData()
'BillToFirstName' => $this->getCard()->getFirstName(),
'BillToLastName' => $this->getCard()->getLastName(),
'BillToCity' => $this->getCard()->getCity(),
'BillToCountry' => $this->formatCountry(),
'BillToCountry' => $this->getCard()->getNumericCountry(),
'BillToEmail' => $this->getCard()->getEmail(),
'BillToTelephone' => $this->getCard()->getPhone(),
'BillToFax' => $this->getCard()->getFax()
Expand All @@ -126,42 +124,10 @@ public function getData()
return $data;
}

/**
* Returns the country as the numeric ISO 3166-1 code
*
* @throws Omnipay\Common\Exception\InvalidRequestException
*
* @return int ISO 3166-1 numeric country
*/
protected function formatCountry()
{
$country = $this->getCard()->getCountry();

if ( !is_null($country) && !is_numeric($country) )
{
$iso3166 = new ISO3166;

if ( strlen($country) == 2 )
{
$country = $iso3166->getByAlpha2($country)['numeric'];
}
elseif ( strlen($country) == 3 )
{
$country = $iso3166->getByAlpha3($country)['numeric'];
}
else
{
throw new InvalidRequestException("The country parameter must be ISO 3166-1 numeric, aplha2 or alpha3.");
}
}

return $country;
}

/**
* Returns the billing state if its a US abbreviation or throws an exception
*
* @throws Omnipay\Common\Exception\InvalidRequestException
* @throws InvalidRequestException
*
* @return string State abbreviation
*/
Expand All @@ -181,7 +147,7 @@ public function formatState()
* Returns the postal code sanitizing dashes and spaces and throws exceptions with other
* non-alphanumeric characters
*
* @throws Omnipay\Common\Exception\InvalidRequestException
* @throws InvalidRequestException
*
* @return string Postal code
*/
Expand Down
65 changes: 65 additions & 0 deletions tests/AuthorizeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace tests;


use Omnipay\FirstAtlanticCommerce\Gateway;
use Omnipay\Tests\GatewayTestCase;

class AuthorizeTest extends GatewayTestCase
{

/** @var Gateway */
protected $gateway;

/**
* Setup the gateway to be used for testing.
*/
public function setUp()
{
parent::setUp();

$this->gateway = new Gateway($this->getHttpClient(), $this->getHttpRequest());
$this->gateway->setMerchantId('123');
$this->gateway->setMerchantPassword('abc123');
}

/**
* Test the country formatting functionality
*/
public function testFormatCountry()
{
//Alpha2
$card = $this->getValidCard();
$requestData = $this->getRequestData($card);
$this->assertEquals(840, $requestData['BillingDetails']['BillToCountry']);

//number
$card['billingCountry'] = 840;
$requestData = $this->getRequestData($card);
$this->assertEquals(840, $requestData['BillingDetails']['BillToCountry']);

//Alpha3
$card['billingCountry'] = 'USA';
$requestData = $this->getRequestData($card);
$this->assertEquals(840, $requestData['BillingDetails']['BillToCountry']);
}

/**
* @param $card
*
* @return array
*/
private function getRequestData($card)
{
$request = $this->gateway->authorize([
'amount' => '10.00',
'currency' => 'USD',
'transactionId' => uniqid(),
'card' => $card
]);
$requestData = $request->getData();
return $requestData;
}

}

0 comments on commit 574aaed

Please sign in to comment.