Skip to content

Commit

Permalink
Merge pull request #34 from moddengine/cs-risk-xml
Browse files Browse the repository at this point in the history
Added support for risk management xml endpoint
  • Loading branch information
sudiptpa authored Aug 11, 2021
2 parents 943bc62 + 9c83fc8 commit cd341c6
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 0 deletions.
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,47 @@ The following gateways are provided by this package:
}

```


### NAB Transact SecureXML API with Risk Management

```php
use Omnipay\Omnipay;
use Omnipay\Common\CreditCard;

$gateway = Omnipay::create('NABTransact_SecureXML');
$gateway->setMerchantId('XYZ0010');
$gateway->setTransactionPassword('abcd1234');
$gateway->setTestMode(true);
$gateway->setRiskManagement(true);

$card = new CreditCard([
'firstName' => 'Sujip',
'lastName' => 'Thapa',
'number' => '4444333322221111',
'expiryMonth' => '06',
'expiryYear' => '2030',
'cvv' => '123',
]
);

$transaction = $gateway->purchase([
'amount' => '10.00',
'currency' => 'AUD',
'transactionId' => 'XYZ100',
'card' => $card,
'ip' => '1.1.1.1',
]
);

$response = $transaction->send();

if ($response->isSuccessful()) {
echo sprintf('Transaction %s was successful!', $response->getTransactionReference());
} else {
echo sprintf('Transaction %s failed: %s', $response->getTransactionReference(), $response->getMessage());
}

### NAB Transact DirectPost v2

```php
Expand Down
71 changes: 71 additions & 0 deletions src/Message/SecureXMLRiskPurchaseRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace Omnipay\NABTransact\Message;

/**
* NABTransact SecureXML Purchase Request.
*/
class SecureXMLRiskPurchaseRequest extends SecureXMLAbstractRequest
{
/**
* @var string
*/
public $liveEndpoint = 'https://transact.nab.com.au/riskmgmt/payment';

/**
* @var string
*/
public $testEndpoint = 'https://demo.transact.nab.com.au/riskmgmt/payment';

/**
* @var int
*/
protected $txnType = 0;

/**
* @var array
*/
protected $requiredFields = ['amount', 'card', 'transactionId', 'ip'];

public function setIp($value)
{
$this->setParameter('ip', $value);
}

public function getIp()
{
return $this->getParameter('ip');
}

/**
* @return string
*/
public function getData()
{
$xml = $this->getBasePaymentXMLWithCard();

$buyer = $xml->addChild('BuyerInfo');
$buyer->addChild('ip', $this->getIp('ip'));
$card = $this->getCard();
if ($firstName = $card->getFirstName()) {
$buyer->addChild('firstName', $firstName);
}
if ($lastName = $card->getLastName()) {
$buyer->addChild('firstName', $lastName);
}
if ($postCode = $card->getBillingPostcode()) {
$buyer->addChild('zipcode', $postCode);
}
if ($city = $card->getBillingCity()) {
$buyer->addChild('town', $city);
}
if ($country = $card->getBillingCountry()) {
$buyer->addChild('billingCountry', $country);
}
if ($email = $card->getEmail()) {
$buyer->addChild('emailAddress', $email);
}

return $xml;
}
}
20 changes: 20 additions & 0 deletions src/SecureXMLGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,22 @@ public function setMerchantId($value)
return $this->setParameter('merchantId', $value);
}

/**
* @return string
*/
public function getRiskManagement()
{
return $this->getParameter('riskManagement');
}

/**
* @param $value
*/
public function setRiskManagement($value)
{
return $this->setParameter('riskManagement', $value);
}

/**
* @return string
*/
Expand Down Expand Up @@ -82,6 +98,10 @@ public function capture(array $parameters = [])
*/
public function purchase(array $parameters = [])
{
if ($this->getRiskManagement()) {
return $this->createRequest('\Omnipay\NABTransact\Message\SecureXMLRiskPurchaseRequest', $parameters);
}

return $this->createRequest('\Omnipay\NABTransact\Message\SecureXMLPurchaseRequest', $parameters);
}

Expand Down
14 changes: 14 additions & 0 deletions tests/SecureXMLGatewayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,20 @@ public function testPurchase()
$this->assertSame('10.00', $request->getAmount());
}

public function testPurchaseRiskManaged()
{
$gateway = clone $this->gateway;
$gateway->setRiskManagement(true);
$request = $gateway->purchase(['card' => $this->getValidCard(), 'transactionId' => 'Test1234', 'ip' => '1.1.1.1', 'amount' => '25.00']);

$this->assertInstanceOf('\Omnipay\NABTransact\Message\SecureXMLRiskPurchaseRequest', $request);
$this->assertSame('25.00', $request->getAmount());
$this->assertContains(
'<BuyerInfo><ip>1.1.1.1</ip><firstName>Example</firstName><firstName>User</firstName><zipcode>12345</zipcode><town>Billstown</town><billingCountry>US</billingCountry></BuyerInfo>',
(string) $request->getData()->asXml()
);
}

public function testRefund()
{
$request = $this->gateway->refund(['amount' => '10.00', 'transactionId' => 'Order-YKHU67']);
Expand Down

0 comments on commit cd341c6

Please sign in to comment.