Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions src/GlobalGateway.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace Omnipay\FirstData;

use Omnipay\Common\AbstractGateway;

/**
* Global Gateway
*
* This gateway is useful for testing. It simply authorizes any payment made using a valid
* credit card number and expiry.
*
* Any card number which passes the Luhn algorithm and ends in an even number is authorized,
* for example: 4242424242424242
*
* Any card number which passes the Luhn algorithm and ends in an odd number is declined,
* for example: 4111111111111111
*/
class GlobalGateway extends AbstractGateway
{
public function getName()
{
return 'First Data Global';
}

public function purchase(array $parameters = array())
{
return $this->createRequest('\Omnipay\FirstData\Message\GlobalPurchaseRequest', $parameters);
}

public function authorize(array $parameters = array())
{
return $this->createRequest('\Omnipay\FirstData\Message\GlobalAuthorizeRequest', $parameters);
}

public function getDefaultParameters()
{
return array(
'gatewayid' => '',
'password' => '',
'testMode' => false,
);
}

public function getGatewayId()
{
return $this->getParameter('gatewayid');
}

public function setGatewayId($value)
{
return $this->setParameter('gatewayid', $value);
}

public function getPassword()
{
return $this->getParameter('password');
}

public function setPassword($value)
{
return $this->setParameter('password', $value);
}
}
172 changes: 172 additions & 0 deletions src/Message/GlobalAbstractRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
<?php

namespace Omnipay\FirstData\Message;

/**
* First Data Abstract Request
*/
abstract class GlobalAbstractRequest extends \Omnipay\Common\Message\AbstractRequest
{
const API_VERSION = 'v11';

protected $liveEndpoint = 'https://api.globalgatewaye4.firstdata.com/transaction/';
protected $testEndpoint = 'https://api.demo.globalgatewaye4.firstdata.com/transaction/';

/**
* @var int - api transaction type
*/
protected $transactionType = '00';
/**
* Transaction types
*/
const TRAN_PURCHASE = '00';
const TRAN_PREAUTH = '01';
const TRAN_PREAUTHCOMPLETE = '02';
const TRAN_FORCEDPOST = '03';
const TRAN_REFUND = '04';
const TRAN_PREAUTHONLY = '05';
const TRAN_PAYPALORDER = '07';
const TRAN_VOID = '13';
const TRAN_TAGGEDPREAUTHCOMPLETE = '32';
const TRAN_TAGGEDVOID = '33';
const TRAN_TAGGEDREFUND = '34';
const TRAN_CASHOUT = '83';
const TRAN_ACTIVATION = '85';
const TRAN_BALANCEINQUIRY = '86';
const TRAN_RELOAD = '88';
const TRAN_DEACTIVATION = '89';

protected static $cardTypes = array(
'visa' => 'Visa',
'mastercard' => 'Mastercard',
'discover' => 'Discover',
'amex' => 'American Express',
'diners_club' => 'Diners Club',
'jcb' => 'JCB',
);

public function getGatewayid()
{
return $this->getParameter('gatewayid');
}

public function setGatewayID($value)
{
return $this->setParameter('gatewayid', $value);
}

public function getPassword()
{
return $this->getParameter('password');
}

public function setPassword($value)
{
return $this->setParameter('password', $value);
}

/**
* Set transaction type
* @param int $transactionType
* @return object
*/
public function setTransactionType($transactionType)
{
$this->transactionType = $transactionType;
return $this;
}
/**
* Return transaction type
* @return int
*/
public function getTransactionType()
{
return $this->transactionType;
}

protected function getBaseData($method)
{
$data = array();
$data['gateway_id'] = $this->getGatewayID();
$data['password'] = $this->getPassword();
$data['transaction_type'] = $this->getTransactionType();

return $data;
}

protected function getHeaders()
{
return array(
'Content-Type: application/json; charset=UTF-8;',
'Accept: application/json'
);
}

public function getAVSHash()
{
$parts = array();
$parts[] = $this->getCard()->getAddress1();
$parts[] = $this->getCard()->getPostcode();
$parts[] = $this->getCard()->getCity();
$parts[] = $this->getCard()->getState();
$parts[] = $this->getCard()->getCountry();
return implode('|', $parts);
}

public function getData()
{
$this->setTransactionType($this->action);
$data = $this->getBaseData('DoDirectPayment');

$this->validate('amount', 'card');

$data['amount'] = $this->getAmount();
$data['currency_code'] = $this->getCurrency();
$data['reference_no'] = $this->getTransactionId();

// add credit card details
$data['credit_card_type'] = self::getCardType($this->getCard()->getBrand());
$data['cc_number'] = $this->getCard()->getNumber();
$data['cardholder_name'] = $this->getCard()->getName();
$data['cc_expiry'] = $this->getCard()->getExpiryDate('my');
$data['cc_verification_str2'] = $this->getCard()->getCvv();
$data['cc_verification_str1'] = $this->getAVSHash();
$data['cvd_presence_ind'] = 1;
$data['cvd_code'] = $this->getCard()->getCvv();

$data['client_ip'] = $this->getClientIp();
$data['client_email'] = $this->getCard()->getEmail();
$data['language'] = strtoupper($this->getCard()->getCountry());
return $data;
}

public function sendData($data)
{
$client = $this->httpClient->post(
$this->getEndpoint(),
$this->getHeaders(),
$data
);
$client->getCurlOptions()->set(CURLOPT_PORT, 443);
$httpResponse = $client->send();
return $this->createResponse($httpResponse->getBody());
}

protected function getEndpoint()
{
return $this->getTestMode() ? $this->testEndpoint.self::API_VERSION : $this->liveEndpoint.self::API_VERSION;
}

protected function createResponse($data)
{
return $this->response = new GlobalResponse($this, $data);
}

public static function getCardType($type)
{
if (isset(self::$cardTypes[$type])) {
return self::$cardTypes[$type];
}
return $type;
}
}
8 changes: 8 additions & 0 deletions src/Message/GlobalAuthorizeRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Omnipay\FirstData\Message;

class GlobalAuthorizeRequest extends GlobalAbstractRequest
{
protected $action = self::TRAN_PREAUTH;
}
8 changes: 8 additions & 0 deletions src/Message/GlobalPurchaseRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Omnipay\FirstData\Message;

class GlobalPurchaseRequest extends GlobalAbstractRequest
{
protected $action = self::TRAN_PURCHASE;
}
37 changes: 37 additions & 0 deletions src/Message/GlobalResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Omnipay\FirstData\Message;

use Omnipay\Common\Message\AbstractResponse;
use Omnipay\Common\Message\RequestInterface;

/**
* First Data Global Response
*/
class GlobalResponse extends AbstractResponse
{
public function __construct(RequestInterface $request, $data)
{
$this->request = $request;
parse_str($data, $this->data);
}

public function isSuccessful()
{
return ($this->data['transaction_approved'] == '1') ? true : false;
}

public function getTransactionReference()
{
return $this->data['authorization_num'];
}

public function getMessage()
{
return $this->data['exact_message'];
}
public function getCode()
{
return $this->data['exact_resp_code'];
}
}
53 changes: 53 additions & 0 deletions tests/GlobalGatewayTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Omnipay\FirstData;

use Omnipay\Tests\GatewayTestCase;

class GlobalGatewayTest extends GatewayTestCase
{
public function setUp()
{
parent::setUp();

$this->gateway = new GlobalGateway($this->getHttpClient(), $this->getHttpRequest());
$this->gateway->setGatewayId('1234');
$this->gateway->setPassword('abcde');

$this->options = array(
'amount' => '13.00',
'card' => $this->getValidCard(),
'transactionId' => 'order2',
'currency' => 'USD',
'testMode' => true,
);
}

public function testProperties()
{
$this->assertEquals('1234', $this->gateway->getGatewayId());
$this->assertEquals('abcde', $this->gateway->getPassword());
}

public function testPurchaseSuccess()
{
$this->setMockHttpResponse('PurchaseSuccess.txt');

$response = $this->gateway->purchase($this->options)->send();

$this->assertTrue($response->isSuccessful());
$this->assertFalse($response->isRedirect());
$this->assertEquals('ET181147', $response->getTransactionReference());
}

public function testAuthorizeSuccess()
{
$this->setMockHttpResponse('PurchaseSuccess.txt');

$response = $this->gateway->authorize($this->options)->send();

$this->assertTrue($response->isSuccessful());
$this->assertFalse($response->isRedirect());
$this->assertEquals('ET181147', $response->getTransactionReference());
}
}
27 changes: 27 additions & 0 deletions tests/Message/GlobalAuthorizeRequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Omnipay\FirstData\Message;

use Omnipay\Tests\TestCase;
use Omnipay\FirstData\Message\GlobalPurchaseRequest;

class GlobalAuthorizeRequestTest extends TestCase
{
public function testPurchaseSuccess()
{
$request = new GlobalAuthorizeRequest($this->getHttpClient(), $this->getHttpRequest());
$request->initialize(
array(
'amount' => '12.00',
'card' => $this->getValidCard(),
)
);

$data = $request->getData();
$this->assertEquals('01', $data['transaction_type']);
$this->assertEquals('4111111111111111', $data['cc_number']);
$this->assertEquals('Visa', $data['credit_card_type']);
$this->assertEquals('12.00', $data['amount']);
$this->assertEquals('123 Billing St|12345|Billstown|CA|US', $data['cc_verification_str1']);
}
}
Loading