99 */
1010class CybsSoapClient extends SoapClient
1111{
12+ const CLIENT_LIBRARY_VERSION = "CyberSource PHP 1.0.0 " ;
13+
1214 private $ merchantId ;
1315 private $ transactionKey ;
1416
@@ -104,6 +106,40 @@ public function getTransactionKey()
104106 return $ this ->transactionKey ;
105107 }
106108
109+ public function simpleXmlToCybsRequest ($ simpleXml ) {
110+
111+ $ vars = get_object_vars ($ simpleXml );
112+ $ request = new stdClass ();
113+ foreach (array_keys ($ vars ) as $ key ) {
114+ $ element = $ vars [$ key ];
115+ if ($ key == 'comment ' ) {
116+ continue ;
117+ }
118+ if (is_string ($ element )) {
119+ $ request ->$ key = $ element ;
120+ } else if (is_array ($ element )) {
121+ $ array = $ element ;
122+ if ($ key == "@attributes " ) {
123+ // Each attribute in the '@attributes' array should
124+ // instead be a property of the parent element.
125+ // copyAttributes($simpleXml, $array);
126+ foreach ($ array as $ k => $ value ) {
127+ $ request ->$ k = $ value ;
128+ }
129+ } else {
130+ $ newArray = array ();
131+ foreach ($ array as $ k => $ value ) {
132+ $ newArray [$ k ] = $ this ->simpleXmlToCybsRequest ($ value );
133+ }
134+ $ request ->$ key = $ newArray ;
135+ }
136+ } else if ($ element instanceof SimpleXMLElement) {
137+ $ request ->$ key = $ this ->simpleXmlToCybsRequest ($ element );
138+ }
139+ }
140+ return $ request ;
141+ }
142+
107143 /**
108144 * Returns an object initialized with basic client information.
109145 *
@@ -115,9 +151,25 @@ public function createRequest($merchantReferenceCode)
115151 $ request = new stdClass ();
116152 $ request ->merchantID = $ this ->merchantId ;
117153 $ request ->merchantReferenceCode = $ merchantReferenceCode ;
118- $ request ->clientLibrary = " CyberSource PHP 1.0.0 " ;
154+ $ request ->clientLibrary = self :: CLIENT_LIBRARY_VERSION ;
119155 $ request ->clientLibraryVersion = phpversion ();
120156 $ request ->clientEnvironment = php_uname ();
121157 return $ request ;
122158 }
159+
160+ /**
161+ * Runs a transaction from an XML file
162+ *
163+ * @param string $filePath The path to the XML file
164+ * @param string $merchantReferenceCode Desired reference code for the request
165+ * @return stdClass An object representation of the transaction response.
166+ */
167+ public function runTransactionFromXml ($ filePath , $ merchantReferenceCode )
168+ {
169+ $ request = $ this ->createRequest ($ merchantReferenceCode );
170+ $ xml = simplexml_load_string (file_get_contents ($ filePath ));
171+ $ xmlRequest = $ this ->simpleXmlToCybsRequest ($ xml );
172+ $ mergedRequest = (object ) array_merge ((array ) $ request , (array ) $ xmlRequest );
173+ return $ this ->runTransaction ($ mergedRequest );
174+ }
123175}
0 commit comments