|
| 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 | +} |
0 commit comments