Skip to content

Commit 0853bba

Browse files
committed
Merge pull request #1 from mikejestes/master
Global Gateway Support
2 parents 699486a + 95afb92 commit 0853bba

10 files changed

+477
-0
lines changed

src/GlobalGateway.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace Omnipay\FirstData;
4+
5+
use Omnipay\Common\AbstractGateway;
6+
7+
/**
8+
* Global Gateway
9+
*
10+
* This gateway is useful for testing. It simply authorizes any payment made using a valid
11+
* credit card number and expiry.
12+
*
13+
* Any card number which passes the Luhn algorithm and ends in an even number is authorized,
14+
* for example: 4242424242424242
15+
*
16+
* Any card number which passes the Luhn algorithm and ends in an odd number is declined,
17+
* for example: 4111111111111111
18+
*/
19+
class GlobalGateway extends AbstractGateway
20+
{
21+
public function getName()
22+
{
23+
return 'First Data Global';
24+
}
25+
26+
public function purchase(array $parameters = array())
27+
{
28+
return $this->createRequest('\Omnipay\FirstData\Message\GlobalPurchaseRequest', $parameters);
29+
}
30+
31+
public function authorize(array $parameters = array())
32+
{
33+
return $this->createRequest('\Omnipay\FirstData\Message\GlobalAuthorizeRequest', $parameters);
34+
}
35+
36+
public function getDefaultParameters()
37+
{
38+
return array(
39+
'gatewayid' => '',
40+
'password' => '',
41+
'testMode' => false,
42+
);
43+
}
44+
45+
public function getGatewayId()
46+
{
47+
return $this->getParameter('gatewayid');
48+
}
49+
50+
public function setGatewayId($value)
51+
{
52+
return $this->setParameter('gatewayid', $value);
53+
}
54+
55+
public function getPassword()
56+
{
57+
return $this->getParameter('password');
58+
}
59+
60+
public function setPassword($value)
61+
{
62+
return $this->setParameter('password', $value);
63+
}
64+
}
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
<?php
2+
3+
namespace Omnipay\FirstData\Message;
4+
5+
/**
6+
* First Data Abstract Request
7+
*/
8+
abstract class GlobalAbstractRequest extends \Omnipay\Common\Message\AbstractRequest
9+
{
10+
const API_VERSION = 'v11';
11+
12+
protected $liveEndpoint = 'https://api.globalgatewaye4.firstdata.com/transaction/';
13+
protected $testEndpoint = 'https://api.demo.globalgatewaye4.firstdata.com/transaction/';
14+
15+
/**
16+
* @var int - api transaction type
17+
*/
18+
protected $transactionType = '00';
19+
/**
20+
* Transaction types
21+
*/
22+
const TRAN_PURCHASE = '00';
23+
const TRAN_PREAUTH = '01';
24+
const TRAN_PREAUTHCOMPLETE = '02';
25+
const TRAN_FORCEDPOST = '03';
26+
const TRAN_REFUND = '04';
27+
const TRAN_PREAUTHONLY = '05';
28+
const TRAN_PAYPALORDER = '07';
29+
const TRAN_VOID = '13';
30+
const TRAN_TAGGEDPREAUTHCOMPLETE = '32';
31+
const TRAN_TAGGEDVOID = '33';
32+
const TRAN_TAGGEDREFUND = '34';
33+
const TRAN_CASHOUT = '83';
34+
const TRAN_ACTIVATION = '85';
35+
const TRAN_BALANCEINQUIRY = '86';
36+
const TRAN_RELOAD = '88';
37+
const TRAN_DEACTIVATION = '89';
38+
39+
protected static $cardTypes = array(
40+
'visa' => 'Visa',
41+
'mastercard' => 'Mastercard',
42+
'discover' => 'Discover',
43+
'amex' => 'American Express',
44+
'diners_club' => 'Diners Club',
45+
'jcb' => 'JCB',
46+
);
47+
48+
public function getGatewayid()
49+
{
50+
return $this->getParameter('gatewayid');
51+
}
52+
53+
public function setGatewayID($value)
54+
{
55+
return $this->setParameter('gatewayid', $value);
56+
}
57+
58+
public function getPassword()
59+
{
60+
return $this->getParameter('password');
61+
}
62+
63+
public function setPassword($value)
64+
{
65+
return $this->setParameter('password', $value);
66+
}
67+
68+
/**
69+
* Set transaction type
70+
* @param int $transactionType
71+
* @return object
72+
*/
73+
public function setTransactionType($transactionType)
74+
{
75+
$this->transactionType = $transactionType;
76+
return $this;
77+
}
78+
/**
79+
* Return transaction type
80+
* @return int
81+
*/
82+
public function getTransactionType()
83+
{
84+
return $this->transactionType;
85+
}
86+
87+
protected function getBaseData($method)
88+
{
89+
$data = array();
90+
$data['gateway_id'] = $this->getGatewayID();
91+
$data['password'] = $this->getPassword();
92+
$data['transaction_type'] = $this->getTransactionType();
93+
94+
return $data;
95+
}
96+
97+
protected function getHeaders()
98+
{
99+
return array(
100+
'Content-Type: application/json; charset=UTF-8;',
101+
'Accept: application/json'
102+
);
103+
}
104+
105+
public function getAVSHash()
106+
{
107+
$parts = array();
108+
$parts[] = $this->getCard()->getAddress1();
109+
$parts[] = $this->getCard()->getPostcode();
110+
$parts[] = $this->getCard()->getCity();
111+
$parts[] = $this->getCard()->getState();
112+
$parts[] = $this->getCard()->getCountry();
113+
return implode('|', $parts);
114+
}
115+
116+
public function getData()
117+
{
118+
$this->setTransactionType($this->action);
119+
$data = $this->getBaseData('DoDirectPayment');
120+
121+
$this->validate('amount', 'card');
122+
123+
$data['amount'] = $this->getAmount();
124+
$data['currency_code'] = $this->getCurrency();
125+
$data['reference_no'] = $this->getTransactionId();
126+
127+
// add credit card details
128+
$data['credit_card_type'] = self::getCardType($this->getCard()->getBrand());
129+
$data['cc_number'] = $this->getCard()->getNumber();
130+
$data['cardholder_name'] = $this->getCard()->getName();
131+
$data['cc_expiry'] = $this->getCard()->getExpiryDate('my');
132+
$data['cc_verification_str2'] = $this->getCard()->getCvv();
133+
$data['cc_verification_str1'] = $this->getAVSHash();
134+
$data['cvd_presence_ind'] = 1;
135+
$data['cvd_code'] = $this->getCard()->getCvv();
136+
137+
$data['client_ip'] = $this->getClientIp();
138+
$data['client_email'] = $this->getCard()->getEmail();
139+
$data['language'] = strtoupper($this->getCard()->getCountry());
140+
return $data;
141+
}
142+
143+
public function sendData($data)
144+
{
145+
$client = $this->httpClient->post(
146+
$this->getEndpoint(),
147+
$this->getHeaders(),
148+
$data
149+
);
150+
$client->getCurlOptions()->set(CURLOPT_PORT, 443);
151+
$httpResponse = $client->send();
152+
return $this->createResponse($httpResponse->getBody());
153+
}
154+
155+
protected function getEndpoint()
156+
{
157+
return $this->getTestMode() ? $this->testEndpoint.self::API_VERSION : $this->liveEndpoint.self::API_VERSION;
158+
}
159+
160+
protected function createResponse($data)
161+
{
162+
return $this->response = new GlobalResponse($this, $data);
163+
}
164+
165+
public static function getCardType($type)
166+
{
167+
if (isset(self::$cardTypes[$type])) {
168+
return self::$cardTypes[$type];
169+
}
170+
return $type;
171+
}
172+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Omnipay\FirstData\Message;
4+
5+
class GlobalAuthorizeRequest extends GlobalAbstractRequest
6+
{
7+
protected $action = self::TRAN_PREAUTH;
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Omnipay\FirstData\Message;
4+
5+
class GlobalPurchaseRequest extends GlobalAbstractRequest
6+
{
7+
protected $action = self::TRAN_PURCHASE;
8+
}

src/Message/GlobalResponse.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Omnipay\FirstData\Message;
4+
5+
use Omnipay\Common\Message\AbstractResponse;
6+
use Omnipay\Common\Message\RequestInterface;
7+
8+
/**
9+
* First Data Global Response
10+
*/
11+
class GlobalResponse extends AbstractResponse
12+
{
13+
public function __construct(RequestInterface $request, $data)
14+
{
15+
$this->request = $request;
16+
parse_str($data, $this->data);
17+
}
18+
19+
public function isSuccessful()
20+
{
21+
return ($this->data['transaction_approved'] == '1') ? true : false;
22+
}
23+
24+
public function getTransactionReference()
25+
{
26+
return $this->data['authorization_num'];
27+
}
28+
29+
public function getMessage()
30+
{
31+
return $this->data['exact_message'];
32+
}
33+
public function getCode()
34+
{
35+
return $this->data['exact_resp_code'];
36+
}
37+
}

tests/GlobalGatewayTest.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace Omnipay\FirstData;
4+
5+
use Omnipay\Tests\GatewayTestCase;
6+
7+
class GlobalGatewayTest extends GatewayTestCase
8+
{
9+
public function setUp()
10+
{
11+
parent::setUp();
12+
13+
$this->gateway = new GlobalGateway($this->getHttpClient(), $this->getHttpRequest());
14+
$this->gateway->setGatewayId('1234');
15+
$this->gateway->setPassword('abcde');
16+
17+
$this->options = array(
18+
'amount' => '13.00',
19+
'card' => $this->getValidCard(),
20+
'transactionId' => 'order2',
21+
'currency' => 'USD',
22+
'testMode' => true,
23+
);
24+
}
25+
26+
public function testProperties()
27+
{
28+
$this->assertEquals('1234', $this->gateway->getGatewayId());
29+
$this->assertEquals('abcde', $this->gateway->getPassword());
30+
}
31+
32+
public function testPurchaseSuccess()
33+
{
34+
$this->setMockHttpResponse('PurchaseSuccess.txt');
35+
36+
$response = $this->gateway->purchase($this->options)->send();
37+
38+
$this->assertTrue($response->isSuccessful());
39+
$this->assertFalse($response->isRedirect());
40+
$this->assertEquals('ET181147', $response->getTransactionReference());
41+
}
42+
43+
public function testAuthorizeSuccess()
44+
{
45+
$this->setMockHttpResponse('PurchaseSuccess.txt');
46+
47+
$response = $this->gateway->authorize($this->options)->send();
48+
49+
$this->assertTrue($response->isSuccessful());
50+
$this->assertFalse($response->isRedirect());
51+
$this->assertEquals('ET181147', $response->getTransactionReference());
52+
}
53+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Omnipay\FirstData\Message;
4+
5+
use Omnipay\Tests\TestCase;
6+
use Omnipay\FirstData\Message\GlobalPurchaseRequest;
7+
8+
class GlobalAuthorizeRequestTest extends TestCase
9+
{
10+
public function testPurchaseSuccess()
11+
{
12+
$request = new GlobalAuthorizeRequest($this->getHttpClient(), $this->getHttpRequest());
13+
$request->initialize(
14+
array(
15+
'amount' => '12.00',
16+
'card' => $this->getValidCard(),
17+
)
18+
);
19+
20+
$data = $request->getData();
21+
$this->assertEquals('01', $data['transaction_type']);
22+
$this->assertEquals('4111111111111111', $data['cc_number']);
23+
$this->assertEquals('Visa', $data['credit_card_type']);
24+
$this->assertEquals('12.00', $data['amount']);
25+
$this->assertEquals('123 Billing St|12345|Billstown|CA|US', $data['cc_verification_str1']);
26+
}
27+
}

0 commit comments

Comments
 (0)