22
33namespace Omnipay \AuthorizeNet \Message ;
44
5+ use Omnipay \AuthorizeNet \Model \CardReference ;
6+ use Omnipay \AuthorizeNet \Model \TransactionReference ;
57use Omnipay \Common \CreditCard ;
68use Omnipay \Common \Exception \InvalidRequestException ;
79use Omnipay \Common \Message \AbstractRequest ;
1113 */
1214abstract class AIMAbstractRequest extends AbstractRequest
1315{
16+ protected $ requestType = 'createTransactionRequest ' ;
1417 protected $ action = null ;
1518
1619 public function getApiLoginId ()
@@ -72,16 +75,84 @@ private function getDuplicateWindow()
7275 return $ this ->getParameter ('duplicateWindow ' ); // Maps x_duplicate_window
7376 }
7477
75- protected function addExtraOptions ( \ SimpleXMLElement $ data )
78+ public function getLiveEndpoint ( )
7679 {
77- if (!is_null ($ this ->getDuplicateWindow ())) {
78- $ extraOptions = $ data ->addChild ('extraOptions ' );
79- $ node = dom_import_simplexml ($ extraOptions );
80- $ nodeOwner = $ node ->ownerDocument ;
81- $ duplicateWindowStr = sprintf ("x_duplicate_window=%s " , $ this ->getDuplicateWindow ());
82- $ node ->appendChild ($ nodeOwner ->createCDATASection ($ duplicateWindowStr ));
80+ return $ this ->getParameter ('liveEndpoint ' );
81+ }
82+
83+ public function setLiveEndpoint ($ value )
84+ {
85+ return $ this ->setParameter ('liveEndpoint ' , $ value );
86+ }
87+
88+ public function getDeveloperEndpoint ()
89+ {
90+ return $ this ->getParameter ('developerEndpoint ' );
91+ }
92+
93+ public function setDeveloperEndpoint ($ value )
94+ {
95+ return $ this ->setParameter ('developerEndpoint ' , $ value );
96+ }
97+
98+ public function getEndpoint ()
99+ {
100+ return $ this ->getDeveloperMode () ? $ this ->getDeveloperEndpoint () : $ this ->getLiveEndpoint ();
101+ }
102+
103+ /**
104+ * @return TransactionReference
105+ */
106+ public function getTransactionReference ()
107+ {
108+ return $ this ->getParameter ('transactionReference ' );
109+ }
110+
111+ public function setTransactionReference ($ value )
112+ {
113+ if (substr ($ value , 0 , 1 ) === '{ ' ) {
114+ // Value is a complex key containing the transaction ID and other properties
115+ $ transactionRef = new TransactionReference ($ value );
116+ } else {
117+ // Value just contains the transaction ID
118+ $ transactionRef = new TransactionReference ();
119+ $ transactionRef ->setTransId ($ value );
83120 }
84- return $ data ;
121+ return $ this ->setParameter ('transactionReference ' , $ transactionRef );
122+ }
123+
124+ /**
125+ * @param string|CardReference $value
126+ * @return AbstractRequest
127+ */
128+ public function setCardReference ($ value )
129+ {
130+ if (!($ value instanceof CardReference)) {
131+ $ value = new CardReference ($ value );
132+ }
133+ return parent ::setCardReference ($ value );
134+ }
135+
136+ /**
137+ * @param bool $serialize Determines whether the return value will be a string or object
138+ * @return string|CardReference
139+ */
140+ public function getCardReference ($ serialize = true )
141+ {
142+ $ value = parent ::getCardReference ();
143+ if ($ serialize ) {
144+ $ value = (string )$ value ;
145+ }
146+ return $ value ;
147+ }
148+
149+ public function sendData ($ data )
150+ {
151+ $ headers = array ('Content-Type ' => 'text/xml; charset=utf-8 ' );
152+ $ data = $ data ->saveXml ();
153+ $ httpResponse = $ this ->httpClient ->post ($ this ->getEndpoint (), $ headers , $ data )->send ();
154+
155+ return $ this ->response = new AIMResponse ($ this , $ httpResponse ->getBody ());
85156 }
86157
87158 /**
@@ -90,27 +161,35 @@ protected function addExtraOptions(\SimpleXMLElement $data)
90161 */
91162 public function getBaseData ()
92163 {
93- $ data = new \SimpleXMLElement ('<createTransactionRequest /> ' );
164+ $ data = new \SimpleXMLElement ('< ' . $ this -> requestType . ' /> ' );
94165 $ data ->addAttribute ('xmlns ' , 'AnetApi/xml/v1/schema/AnetApiSchema.xsd ' );
166+ $ this ->addAuthentication ($ data );
167+ $ this ->addReferenceId ($ data );
168+ $ this ->addTransactionType ($ data );
169+ return $ data ;
170+ }
95171
96- // Credentials
172+ protected function addAuthentication (\SimpleXMLElement $ data )
173+ {
97174 $ data ->merchantAuthentication ->name = $ this ->getApiLoginId ();
98175 $ data ->merchantAuthentication ->transactionKey = $ this ->getTransactionKey ();
176+ }
99177
100- // User-assigned transaction ID
178+ protected function addReferenceId (\SimpleXMLElement $ data )
179+ {
101180 $ txnId = $ this ->getTransactionId ();
102181 if (!empty ($ txnId )) {
103182 $ data ->refId = $ this ->getTransactionId ();
104183 }
184+ }
105185
106- // Transaction type
186+ protected function addTransactionType (\SimpleXMLElement $ data )
187+ {
107188 if (!$ this ->action ) {
108189 // The extending class probably hasn't specified an "action"
109190 throw new InvalidRequestException ();
110191 }
111192 $ data ->transactionRequest ->transactionType = $ this ->action ;
112-
113- return $ data ;
114193 }
115194
116195 /**
@@ -165,37 +244,15 @@ protected function addTestModeSetting(\SimpleXMLElement $data)
165244 return $ data ;
166245 }
167246
168- public function sendData ($ data )
169- {
170- $ headers = array ('Content-Type ' => 'text/xml; charset=utf-8 ' );
171- $ data = $ data ->saveXml ();
172- $ httpResponse = $ this ->httpClient ->post ($ this ->getEndpoint (), $ headers , $ data )->send ();
173-
174- return $ this ->response = new AIMResponse ($ this , $ httpResponse ->getBody ());
175- }
176-
177- public function getLiveEndpoint ()
178- {
179- return $ this ->getParameter ('liveEndpoint ' );
180- }
181-
182- public function setLiveEndpoint ($ value )
183- {
184- return $ this ->setParameter ('liveEndpoint ' , $ value );
185- }
186-
187- public function getDeveloperEndpoint ()
188- {
189- return $ this ->getParameter ('developerEndpoint ' );
190- }
191-
192- public function setDeveloperEndpoint ($ value )
193- {
194- return $ this ->setParameter ('developerEndpoint ' , $ value );
195- }
196-
197- public function getEndpoint ()
247+ protected function addExtraOptions (\SimpleXMLElement $ data )
198248 {
199- return $ this ->getDeveloperMode () ? $ this ->getDeveloperEndpoint () : $ this ->getLiveEndpoint ();
249+ if (!is_null ($ this ->getDuplicateWindow ())) {
250+ $ extraOptions = $ data ->addChild ('extraOptions ' );
251+ $ node = dom_import_simplexml ($ extraOptions );
252+ $ nodeOwner = $ node ->ownerDocument ;
253+ $ duplicateWindowStr = sprintf ("x_duplicate_window=%s " , $ this ->getDuplicateWindow ());
254+ $ node ->appendChild ($ nodeOwner ->createCDATASection ($ duplicateWindowStr ));
255+ }
256+ return $ data ;
200257 }
201258}
0 commit comments