11<?php
22
3- set_include_path ( get_include_path () . PATH_SEPARATOR . dirname ( __FILE__ ) . ' /conf ' ) ;
3+ include ' CybsClient.php ' ;
44
55/**
66 * CybsSoapClient
77 *
88 * An implementation of PHP's SOAPClient class for making CyberSource requests.
99 */
10- class CybsSoapClient extends SoapClient
10+ class CybsSoapClient extends CybsClient
1111{
12- const CLIENT_LIBRARY_VERSION = "CyberSource PHP 1.0.0 " ;
13-
14- private $ merchantId ;
15- private $ transactionKey ;
1612
1713 function __construct ($ options =array ())
1814 {
1915 $ properties = parse_ini_file ('cybs.ini ' );
20- $ required = array ('merchant_id ' , 'transaction_key ' , 'wsdl ' );
21-
22- if (!$ properties ) {
23- throw new Exception ('Unable to read cybs.ini. ' );
24- }
25-
26- foreach ($ required as $ req ) {
27- if (empty ($ properties [$ req ])) {
28- throw new Exception ($ req . ' not found in cybs.ini. ' );
29- }
30- }
31-
32- parent ::__construct ($ properties ['wsdl ' ], $ options );
33- $ this ->merchantId = $ properties ['merchant_id ' ];
34- $ this ->transactionKey = $ properties ['transaction_key ' ];
35-
36- $ nameSpace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd " ;
37-
38- $ soapUsername = new SoapVar (
39- $ this ->merchantId ,
40- XSD_STRING ,
41- NULL ,
42- $ nameSpace ,
43- NULL ,
44- $ nameSpace
45- );
46-
47- $ soapPassword = new SoapVar (
48- $ this ->transactionKey ,
49- XSD_STRING ,
50- NULL ,
51- $ nameSpace ,
52- NULL ,
53- $ nameSpace
54- );
55-
56- $ auth = new stdClass ();
57- $ auth ->Username = $ soapUsername ;
58- $ auth ->Password = $ soapPassword ;
59-
60- $ soapAuth = new SoapVar (
61- $ auth ,
62- SOAP_ENC_OBJECT ,
63- NULL , $ nameSpace ,
64- 'UsernameToken ' ,
65- $ nameSpace
66- );
67-
68- $ token = new stdClass ();
69- $ token ->UsernameToken = $ soapAuth ;
70-
71- $ soapToken = new SoapVar (
72- $ token ,
73- SOAP_ENC_OBJECT ,
74- NULL ,
75- $ nameSpace ,
76- 'UsernameToken ' ,
77- $ nameSpace
78- );
79-
80- $ security =new SoapVar (
81- $ soapToken ,
82- SOAP_ENC_OBJECT ,
83- NULL ,
84- $ nameSpace ,
85- 'Security ' ,
86- $ nameSpace
87- );
88-
89- $ header = new SoapHeader ($ nameSpace , 'Security ' , $ security , true );
90- $ this ->__setSoapHeaders (array ($ header ));
91- }
92-
93- /**
94- * @return string The client's merchant ID.
95- */
96- public function getMerchantId ()
97- {
98- return $ this ->merchantId ;
99- }
100-
101- /**
102- * @return string The client's transaction key.
103- */
104- public function getTransactionKey ()
105- {
106- return $ this ->transactionKey ;
16+ parent ::__construct ($ options , $ properties );
10717 }
10818
10919 public function simpleXmlToCybsRequest ($ simpleXml ) {
@@ -122,7 +32,6 @@ public function simpleXmlToCybsRequest($simpleXml) {
12232 if ($ key == "@attributes " ) {
12333 // Each attribute in the '@attributes' array should
12434 // instead be a property of the parent element.
125- // copyAttributes($simpleXml, $array);
12635 foreach ($ array as $ k => $ value ) {
12736 $ request ->$ k = $ value ;
12837 }
@@ -149,7 +58,7 @@ public function simpleXmlToCybsRequest($simpleXml) {
14958 public function createRequest ($ merchantReferenceCode )
15059 {
15160 $ request = new stdClass ();
152- $ request ->merchantID = $ this ->merchantId ;
61+ $ request ->merchantID = $ this ->getMerchantId () ;
15362 $ request ->merchantReferenceCode = $ merchantReferenceCode ;
15463 $ request ->clientLibrary = self ::CLIENT_LIBRARY_VERSION ;
15564 $ request ->clientLibraryVersion = phpversion ();
@@ -158,18 +67,32 @@ public function createRequest($merchantReferenceCode)
15867 }
15968
16069 /**
161- * Runs a transaction from an XML file
70+ * Runs a transaction from an XML string
16271 *
16372 * @param string $filePath The path to the XML file
16473 * @param string $merchantReferenceCode Desired reference code for the request
16574 * @return stdClass An object representation of the transaction response.
16675 */
167- public function runTransactionFromXml ($ filePath , $ merchantReferenceCode )
76+ public function runTransactionFromXml ($ xml , $ merchantReferenceCode )
16877 {
16978 $ request = $ this ->createRequest ($ merchantReferenceCode );
170- $ xml = simplexml_load_string (file_get_contents ( $ filePath ) );
171- $ xmlRequest = $ this ->simpleXmlToCybsRequest ($ xml );
79+ $ simpleXml = simplexml_load_string ($ xml );
80+ $ xmlRequest = $ this ->simpleXmlToCybsRequest ($ simpleXml );
17281 $ mergedRequest = (object ) array_merge ((array ) $ request , (array ) $ xmlRequest );
17382 return $ this ->runTransaction ($ mergedRequest );
17483 }
84+
85+ /**
86+ * Runs a transaction from an XML file
87+ *
88+ * @param string $filePath The path to the XML file
89+ * @param string $merchantReferenceCode Desired reference code for the request
90+ * @return stdClass An object representation of the transaction response.
91+ */
92+ public function runTransactionFromFile ($ filePath , $ merchantReferenceCode )
93+ {
94+ $ request = $ this ->createRequest ($ merchantReferenceCode );
95+ $ xml = file_get_contents ($ filePath );
96+ return $ this ->runTransactionFromXml ($ xml , $ merchantReferenceCode );
97+ }
17598}
0 commit comments