Skip to content

Commit 9336b82

Browse files
committed
Merge pull request #6 from Orteko/securexml
SecureXML API integration
2 parents 8b10a49 + eb104ba commit 9336b82

23 files changed

+843
-27
lines changed

src/Message/AbstractRequest.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\SecurePay\Message;
4+
5+
/**
6+
* SecurePay Direct Post Abstract Request.
7+
*/
8+
abstract class AbstractRequest extends \Omnipay\Common\Message\AbstractRequest
9+
{
10+
public $testEndpoint;
11+
public $liveEndpoint;
12+
13+
public function getMerchantId()
14+
{
15+
return $this->getParameter('merchantId');
16+
}
17+
18+
public function setMerchantId($value)
19+
{
20+
return $this->setParameter('merchantId', $value);
21+
}
22+
23+
public function getTransactionPassword()
24+
{
25+
return $this->getParameter('transactionPassword');
26+
}
27+
28+
public function setTransactionPassword($value)
29+
{
30+
return $this->setParameter('transactionPassword', $value);
31+
}
32+
33+
public function getEndpoint()
34+
{
35+
return $this->getTestMode() ? $this->testEndpoint : $this->liveEndpoint;
36+
}
37+
}

src/Message/DirectPostAbstractRequest.php

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,11 @@
22

33
namespace Omnipay\SecurePay\Message;
44

5-
use Omnipay\Common\Message\AbstractRequest;
6-
75
/**
86
* SecurePay Direct Post Abstract Request
97
*/
108
abstract class DirectPostAbstractRequest extends AbstractRequest
119
{
1210
public $testEndpoint = 'https://api.securepay.com.au/test/directpost/authorise';
1311
public $liveEndpoint = 'https://api.securepay.com.au/live/directpost/authorise';
14-
15-
public function getMerchantId()
16-
{
17-
return $this->getParameter('merchantId');
18-
}
19-
20-
public function setMerchantId($value)
21-
{
22-
return $this->setParameter('merchantId', $value);
23-
}
24-
25-
public function getTransactionPassword()
26-
{
27-
return $this->getParameter('transactionPassword');
28-
}
29-
30-
public function setTransactionPassword($value)
31-
{
32-
return $this->setParameter('transactionPassword', $value);
33-
}
34-
35-
public function getEndpoint()
36-
{
37-
return $this->getTestMode() ? $this->testEndpoint : $this->liveEndpoint;
38-
}
3912
}
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
<?php
2+
3+
namespace Omnipay\SecurePay\Message;
4+
5+
/**
6+
* SecurePay SecureXML Abstract Request.
7+
*/
8+
abstract class SecureXMLAbstractRequest extends AbstractRequest
9+
{
10+
public $testEndpoint = 'https://test.securepay.com.au/xmlapi/payment';
11+
public $liveEndpoint = 'https://api.securepay.com.au/xmlapi/payment';
12+
13+
protected $requestType = 'Payment';
14+
protected $requiredFields = array();
15+
16+
/**
17+
* Set the messageID on the request.
18+
*
19+
* This is returned intact on any response so you could add a local
20+
* database ID here to ease in matching data later.
21+
*/
22+
public function setMessageId($value)
23+
{
24+
return $this->setParameter('messageId', $value);
25+
}
26+
27+
/**
28+
* Get the messageID for the request.
29+
*
30+
* @return string User-supplied messageID or generated one based on
31+
* timestamp.
32+
*/
33+
public function getMessageId()
34+
{
35+
$messageId = $this->getParameter('messageId');
36+
37+
if (empty($messageId)) {
38+
$this->setMessageId(substr(md5(microtime()), 0, 30));
39+
}
40+
41+
return $this->getParameter('messageId');
42+
}
43+
44+
public function sendData($data)
45+
{
46+
$httpResponse = $this->httpClient->post($this->getEndpoint(), null, $data->asXML())->send();
47+
48+
return $this->response = new SecureXMLResponse($this, $httpResponse->xml());
49+
}
50+
51+
/**
52+
* XML Template of a SecurePayMessage.
53+
*
54+
* As per section 5.1 of the documentation, these elements are common to
55+
* all requests.
56+
*
57+
* @return \SimpleXMLElement SecurePayMessage template.
58+
*/
59+
protected function getBaseXML()
60+
{
61+
foreach ($this->requiredFields as $field) {
62+
$this->validate($field);
63+
}
64+
65+
$xml = new \SimpleXMLElement('<SecurePayMessage/>');
66+
67+
$messageInfo = $xml->addChild('MessageInfo');
68+
$messageInfo->addChild('messageID', $this->getMessageId());
69+
$messageInfo->addChild('messageTimestamp', $this->generateTimestamp());
70+
$messageInfo->addChild('timeoutValue', 60);
71+
$messageInfo->addChild('apiVersion', 'xml-4.2');
72+
73+
$merchantInfo = $xml->addChild('MerchantInfo');
74+
$merchantInfo->addChild('merchantID', $this->getMerchantId());
75+
$merchantInfo->addChild('password', $this->getTransactionPassword());
76+
77+
$xml->addChild('RequestType', $this->requestType); // Not related to the transaction type
78+
79+
return $xml;
80+
}
81+
82+
/**
83+
* XML template of a SecurePayMessage Payment.
84+
*
85+
* @return \SimpleXMLElement SecurePayMessage with transaction details.
86+
*/
87+
protected function getBasePaymentXML()
88+
{
89+
$xml = $this->getBaseXML();
90+
91+
$payment = $xml->addChild('Payment');
92+
$txnList = $payment->addChild('TxnList');
93+
$txnList->addAttribute('count', 1); // One transaction per request supported by current API.
94+
95+
$transaction = $txnList->addChild('Txn');
96+
$transaction->addAttribute('ID', 1); // One transaction per request supported by current API.
97+
$transaction->addChild('txnType', $this->txnType);
98+
$transaction->addChild('txnSource', 23); // Must always be 23 for SecureXML.
99+
$transaction->addChild('amount', $this->getAmountInteger());
100+
$transaction->addChild('currency', $this->getCurrency());
101+
$transaction->addChild('purchaseOrderNo', $this->getTransactionId());
102+
103+
return $xml;
104+
}
105+
106+
/**
107+
* @return \SimpleXMLElement SecurePayMessage with transaction and card
108+
* details.
109+
*/
110+
protected function getBasePaymentXMLWithCard()
111+
{
112+
$this->getCard()->validate();
113+
114+
$xml = $this->getBasePaymentXML();
115+
116+
$card = $xml->Payment->TxnList->Txn->addChild('CreditCardInfo');
117+
$card->addChild('cardNumber', $this->getCard()->getNumber());
118+
$card->addChild('cvv', $this->getCard()->getCvv());
119+
$card->addChild('expiryDate', $this->getCard()->getExpiryDate('m/y'));
120+
121+
return $xml;
122+
}
123+
124+
/**
125+
* Generates a SecureXML timestamp.
126+
*
127+
* SecureXML requires a specific timestamp format as per appendix E of the
128+
* documentation.
129+
*
130+
* @return string SecureXML formatted timestamp.
131+
*/
132+
protected function generateTimestamp()
133+
{
134+
$date = new \DateTime();
135+
136+
// API requires the timezone offset in minutes
137+
return $date->format(sprintf('YmdHis000%+04d', $date->format('Z') / 60));
138+
}
139+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Omnipay\SecurePay\Message;
4+
5+
/**
6+
* SecurePay SecureXML Authorize Request.
7+
*
8+
* Verify that the amount is available and hold for capture.
9+
*
10+
* Returns a 'preauthID' value that must be supplied in any subsequent capture
11+
* request.
12+
*/
13+
class SecureXMLAuthorizeRequest extends SecureXMLAbstractRequest
14+
{
15+
protected $txnType = 10; // Preauthorise, as per Appendix A of documentation.
16+
protected $requiredFields = array('amount', 'card', 'transactionId');
17+
18+
public function getData()
19+
{
20+
return $this->getBasePaymentXMLWithCard();
21+
}
22+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Omnipay\SecurePay\Message;
4+
5+
/**
6+
* SecurePay SecureXML Capture Request.
7+
*
8+
* Capture a partial or full amount that has been held by a prior authorize
9+
* request.
10+
*
11+
* The transactionId (purchaseOrderNo) and preauthID must match the prior
12+
* authorize transaction for the capture to succeed.
13+
*/
14+
class SecureXMLCaptureRequest extends SecureXMLAbstractRequest
15+
{
16+
protected $txnType = 11; // Preauthorise Complete, as per Appendix A of documentation.
17+
protected $requiredFields = array('amount', 'transactionId', 'preauthId');
18+
19+
public function getData()
20+
{
21+
$xml = $this->getBasePaymentXML();
22+
23+
$xml->Payment->TxnList->Txn->addChild('preauthID', $this->getPreauthId());
24+
25+
return $xml;
26+
}
27+
28+
/**
29+
* Set the preauthId that was returned as part of the original authorize
30+
* request.
31+
*/
32+
public function setPreauthId($value)
33+
{
34+
return $this->setParameter('preauthId', $value);
35+
}
36+
37+
/**
38+
* @return string The preauthId from the authorize request that this
39+
* capture matches.
40+
*/
41+
public function getPreauthId()
42+
{
43+
return $this->getParameter('preauthId');
44+
}
45+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Omnipay\SecurePay\Message;
4+
5+
/**
6+
* SecurePay SecureXML Echo Request.
7+
*
8+
* Echo requests are used to verify that the SecurePay payment server is
9+
* available.
10+
*
11+
* The status code returned in the response will be '000' if the service is up.
12+
*/
13+
class SecureXMLEchoTestRequest extends SecureXMLAbstractRequest
14+
{
15+
protected $requestType = 'Echo';
16+
17+
public function getData()
18+
{
19+
return $this->getBaseXML();
20+
}
21+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Omnipay\SecurePay\Message;
4+
5+
/**
6+
* SecurePay SecureXML Purchase Request.
7+
*/
8+
class SecureXMLPurchaseRequest extends SecureXMLAbstractRequest
9+
{
10+
protected $txnType = 0; // Standard Payment, as per Appendix A of documentation.
11+
protected $requiredFields = array('amount', 'card', 'transactionId');
12+
13+
public function getData()
14+
{
15+
return $this->getBasePaymentXMLWithCard();
16+
}
17+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Omnipay\SecurePay\Message;
4+
5+
/**
6+
* SecurePay SecureXML Refund Request.
7+
*
8+
* Refund a partial or full amount from a prior purchase.
9+
*
10+
* The transactionReference (txnID) and transactionId (purchaseOrderNo) must
11+
* match the original transaction for a refund to be successful.
12+
*/
13+
class SecureXMLRefundRequest extends SecureXMLAbstractRequest
14+
{
15+
protected $txnType = 4; // Refund, as per Appendix A of documentation.
16+
protected $requiredFields = array('amount', 'transactionId', 'transactionReference');
17+
18+
public function getData()
19+
{
20+
$xml = $this->getBasePaymentXML();
21+
$xml->Payment->TxnList->Txn->addChild('txnID', $this->getTransactionReference());
22+
23+
return $xml;
24+
}
25+
}

0 commit comments

Comments
 (0)