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

Commit 574aaed

Browse files
authored
Merge pull request Strikewood#8 from CaswellWC/master
Updates the iso3166 package to the league package
2 parents 6455666 + 530f6db commit 574aaed

File tree

6 files changed

+120
-39
lines changed

6 files changed

+120
-39
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,6 @@ bin
4646
.Trashes
4747
ehthumbs.db
4848
Thumbs.db
49+
50+
# phpstorm
51+
.idea

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"require": {
2020
"php": ">=5.4",
2121
"omnipay/common": "~2.3",
22-
"alcohol/iso3166": "~2.0"
22+
"league/iso3166": "~1.0.1"
2323
},
2424
"require-dev": {
2525
"omnipay/tests": "~2.0"

src/CreditCard.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22

33
namespace Omnipay\FirstAtlanticCommerce;
44

5+
use League\ISO3166\ISO3166;
56
use Omnipay\Common\CreditCard as BaseCreditCard;
67
use Omnipay\Common\Exception\InvalidCreditCardException;
8+
use Omnipay\Common\Exception\InvalidRequestException;
9+
use Omnipay\Common\Helper;
10+
711

812
class CreditCard extends BaseCreditCard
913
{
@@ -66,4 +70,36 @@ public function validate()
6670
}
6771
}
6872
}
73+
74+
/**
75+
* Returns the country as the numeric ISO 3166-1 code
76+
*
77+
* @throws InvalidRequestException
78+
*
79+
* @return int ISO 3166-1 numeric country
80+
*/
81+
public function getNumericCountry()
82+
{
83+
$country = $this->getCountry();
84+
85+
if ( !is_null($country) && !is_numeric($country) )
86+
{
87+
$iso3166 = new ISO3166();
88+
89+
if ( strlen($country) == 2 )
90+
{
91+
$country = $iso3166->getByAlpha2($country)['numeric'];
92+
}
93+
elseif ( strlen($country) == 3 )
94+
{
95+
$country = $iso3166->getByAlpha3($country)['numeric'];
96+
}
97+
else
98+
{
99+
throw new InvalidRequestException("The country parameter must be ISO 3166-1 numeric, aplha2 or alpha3.");
100+
}
101+
}
102+
103+
return $country;
104+
}
69105
}

src/Message/AbstractRequest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Omnipay\FirstAtlanticCommerce\Message;
44

55
use Omnipay\Common\Message\AbstractRequest as BaseAbstractRequest;
6+
use Omnipay\Common\Message\ResponseInterface;
67
use Omnipay\FirstAtlanticCommerce\CreditCard;
78
use Omnipay\FirstAtlanticCommerce\ParameterTrait;
89
use SimpleXMLElement;
@@ -132,7 +133,7 @@ protected function xmlSerialize($data, $xml = null)
132133
*
133134
* @param CreditCard $value
134135
*
135-
* @return AbstractRequest Provides a fluent interface
136+
* @return \Omnipay\Common\Message\AbstractRequest Provides a fluent interface
136137
*/
137138
public function setCard($value)
138139
{
@@ -143,4 +144,14 @@ public function setCard($value)
143144

144145
return $this->setParameter('card', $value);
145146
}
147+
148+
/**
149+
* Get the card.
150+
*
151+
* @return CreditCard
152+
*/
153+
public function getCard()
154+
{
155+
return $this->getParameter('card');
156+
}
146157
}

src/Message/AuthorizeRequest.php

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
namespace Omnipay\FirstAtlanticCommerce\Message;
44

5-
use Alcohol\ISO3166;
65
use Omnipay\Common\Exception\InvalidRequestException;
7-
use Omnipay\FirstAtlanticCommerce\Message\AbstractRequest;
86

97
/**
108
* FACPG2 Authorize Request
@@ -105,7 +103,7 @@ public function getData()
105103
'BillToFirstName' => $this->getCard()->getFirstName(),
106104
'BillToLastName' => $this->getCard()->getLastName(),
107105
'BillToCity' => $this->getCard()->getCity(),
108-
'BillToCountry' => $this->formatCountry(),
106+
'BillToCountry' => $this->getCard()->getNumericCountry(),
109107
'BillToEmail' => $this->getCard()->getEmail(),
110108
'BillToTelephone' => $this->getCard()->getPhone(),
111109
'BillToFax' => $this->getCard()->getFax()
@@ -126,42 +124,10 @@ public function getData()
126124
return $data;
127125
}
128126

129-
/**
130-
* Returns the country as the numeric ISO 3166-1 code
131-
*
132-
* @throws Omnipay\Common\Exception\InvalidRequestException
133-
*
134-
* @return int ISO 3166-1 numeric country
135-
*/
136-
protected function formatCountry()
137-
{
138-
$country = $this->getCard()->getCountry();
139-
140-
if ( !is_null($country) && !is_numeric($country) )
141-
{
142-
$iso3166 = new ISO3166;
143-
144-
if ( strlen($country) == 2 )
145-
{
146-
$country = $iso3166->getByAlpha2($country)['numeric'];
147-
}
148-
elseif ( strlen($country) == 3 )
149-
{
150-
$country = $iso3166->getByAlpha3($country)['numeric'];
151-
}
152-
else
153-
{
154-
throw new InvalidRequestException("The country parameter must be ISO 3166-1 numeric, aplha2 or alpha3.");
155-
}
156-
}
157-
158-
return $country;
159-
}
160-
161127
/**
162128
* Returns the billing state if its a US abbreviation or throws an exception
163129
*
164-
* @throws Omnipay\Common\Exception\InvalidRequestException
130+
* @throws InvalidRequestException
165131
*
166132
* @return string State abbreviation
167133
*/
@@ -181,7 +147,7 @@ public function formatState()
181147
* Returns the postal code sanitizing dashes and spaces and throws exceptions with other
182148
* non-alphanumeric characters
183149
*
184-
* @throws Omnipay\Common\Exception\InvalidRequestException
150+
* @throws InvalidRequestException
185151
*
186152
* @return string Postal code
187153
*/

tests/AuthorizeTest.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace tests;
4+
5+
6+
use Omnipay\FirstAtlanticCommerce\Gateway;
7+
use Omnipay\Tests\GatewayTestCase;
8+
9+
class AuthorizeTest extends GatewayTestCase
10+
{
11+
12+
/** @var Gateway */
13+
protected $gateway;
14+
15+
/**
16+
* Setup the gateway to be used for testing.
17+
*/
18+
public function setUp()
19+
{
20+
parent::setUp();
21+
22+
$this->gateway = new Gateway($this->getHttpClient(), $this->getHttpRequest());
23+
$this->gateway->setMerchantId('123');
24+
$this->gateway->setMerchantPassword('abc123');
25+
}
26+
27+
/**
28+
* Test the country formatting functionality
29+
*/
30+
public function testFormatCountry()
31+
{
32+
//Alpha2
33+
$card = $this->getValidCard();
34+
$requestData = $this->getRequestData($card);
35+
$this->assertEquals(840, $requestData['BillingDetails']['BillToCountry']);
36+
37+
//number
38+
$card['billingCountry'] = 840;
39+
$requestData = $this->getRequestData($card);
40+
$this->assertEquals(840, $requestData['BillingDetails']['BillToCountry']);
41+
42+
//Alpha3
43+
$card['billingCountry'] = 'USA';
44+
$requestData = $this->getRequestData($card);
45+
$this->assertEquals(840, $requestData['BillingDetails']['BillToCountry']);
46+
}
47+
48+
/**
49+
* @param $card
50+
*
51+
* @return array
52+
*/
53+
private function getRequestData($card)
54+
{
55+
$request = $this->gateway->authorize([
56+
'amount' => '10.00',
57+
'currency' => 'USD',
58+
'transactionId' => uniqid(),
59+
'card' => $card
60+
]);
61+
$requestData = $request->getData();
62+
return $requestData;
63+
}
64+
65+
}

0 commit comments

Comments
 (0)