|
1 | 1 | <?php |
2 | 2 |
|
3 | | -set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__) . '/conf'); |
| 3 | +include 'CybsClient.php'; |
4 | 4 |
|
5 | 5 | /** |
6 | 6 | * CybsSoapClient |
7 | 7 | * |
8 | 8 | * An implementation of PHP's SOAPClient class for making CyberSource requests. |
9 | 9 | */ |
10 | | -class CybsSoapClient extends SoapClient |
| 10 | +class CybsSoapClient extends CybsClient |
11 | 11 | { |
12 | | - private $merchantId; |
13 | | - private $transactionKey; |
14 | 12 |
|
15 | 13 | function __construct($options=array()) |
16 | 14 | { |
17 | 15 | $properties = parse_ini_file('cybs.ini'); |
18 | | - $required = array('merchant_id', 'transaction_key', 'wsdl'); |
19 | | - |
20 | | - if (!$properties) { |
21 | | - throw new Exception('Unable to read cybs.ini.'); |
22 | | - } |
| 16 | + parent::__construct($options, $properties); |
| 17 | + } |
| 18 | + |
| 19 | + public function simpleXmlToCybsRequest($simpleXml) { |
23 | 20 |
|
24 | | - foreach ($required as $req) { |
25 | | - if (empty($properties[$req])) { |
26 | | - throw new Exception($req . ' not found in cybs.ini.'); |
| 21 | + $vars = get_object_vars($simpleXml); |
| 22 | + $request = new stdClass(); |
| 23 | + foreach(array_keys($vars) as $key) { |
| 24 | + $element = $vars[$key]; |
| 25 | + if ($key == 'comment') { |
| 26 | + continue; |
| 27 | + } |
| 28 | + if (is_string($element)) { |
| 29 | + $request->$key = $element; |
| 30 | + } else if (is_array($element)) { |
| 31 | + $array = $element; |
| 32 | + if ($key == "@attributes") { |
| 33 | + // Each attribute in the '@attributes' array should |
| 34 | + // instead be a property of the parent element. |
| 35 | + foreach($array as $k => $value) { |
| 36 | + $request->$k = $value; |
| 37 | + } |
| 38 | + } else { |
| 39 | + $newArray = array(); |
| 40 | + foreach($array as $k => $value) { |
| 41 | + $newArray[$k] = $this->simpleXmlToCybsRequest($value); |
| 42 | + } |
| 43 | + $request->$key = $newArray; |
| 44 | + } |
| 45 | + } else if ($element instanceof SimpleXMLElement) { |
| 46 | + $request->$key = $this->simpleXmlToCybsRequest($element); |
27 | 47 | } |
28 | 48 | } |
29 | | - |
30 | | - parent::__construct($properties['wsdl'], $options); |
31 | | - $this->merchantId = $properties['merchant_id']; |
32 | | - $this->transactionKey = $properties['transaction_key']; |
33 | | - |
34 | | - $nameSpace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"; |
35 | | - |
36 | | - $soapUsername = new SoapVar( |
37 | | - $this->merchantId, |
38 | | - XSD_STRING, |
39 | | - NULL, |
40 | | - $nameSpace, |
41 | | - NULL, |
42 | | - $nameSpace |
43 | | - ); |
44 | | - |
45 | | - $soapPassword = new SoapVar( |
46 | | - $this->transactionKey, |
47 | | - XSD_STRING, |
48 | | - NULL, |
49 | | - $nameSpace, |
50 | | - NULL, |
51 | | - $nameSpace |
52 | | - ); |
53 | | - |
54 | | - $auth = new stdClass(); |
55 | | - $auth->Username = $soapUsername; |
56 | | - $auth->Password = $soapPassword; |
57 | | - |
58 | | - $soapAuth = new SoapVar( |
59 | | - $auth, |
60 | | - SOAP_ENC_OBJECT, |
61 | | - NULL, $nameSpace, |
62 | | - 'UsernameToken', |
63 | | - $nameSpace |
64 | | - ); |
65 | | - |
66 | | - $token = new stdClass(); |
67 | | - $token->UsernameToken = $soapAuth; |
68 | | - |
69 | | - $soapToken = new SoapVar( |
70 | | - $token, |
71 | | - SOAP_ENC_OBJECT, |
72 | | - NULL, |
73 | | - $nameSpace, |
74 | | - 'UsernameToken', |
75 | | - $nameSpace |
76 | | - ); |
77 | | - |
78 | | - $security =new SoapVar( |
79 | | - $soapToken, |
80 | | - SOAP_ENC_OBJECT, |
81 | | - NULL, |
82 | | - $nameSpace, |
83 | | - 'Security', |
84 | | - $nameSpace |
85 | | - ); |
86 | | - |
87 | | - $header = new SoapHeader($nameSpace, 'Security', $security, true); |
88 | | - $this->__setSoapHeaders(array($header)); |
| 49 | + return $request; |
89 | 50 | } |
90 | 51 |
|
91 | 52 | /** |
92 | | - * @return string The client's merchant ID. |
| 53 | + * Returns an object initialized with basic client information. |
| 54 | + * |
| 55 | + * @param string $merchantReferenceCode Desired reference code for the request |
| 56 | + * @return stdClass An object initialized with the basic client info. |
93 | 57 | */ |
94 | | - public function getMerchantId() |
| 58 | + public function createRequest($merchantReferenceCode) |
95 | 59 | { |
96 | | - return $this->merchantId; |
| 60 | + $request = new stdClass(); |
| 61 | + $request->merchantID = $this->getMerchantId(); |
| 62 | + $request->merchantReferenceCode = $merchantReferenceCode; |
| 63 | + $request->clientLibrary = self::CLIENT_LIBRARY_VERSION; |
| 64 | + $request->clientLibraryVersion = phpversion(); |
| 65 | + $request->clientEnvironment = php_uname(); |
| 66 | + return $request; |
97 | 67 | } |
98 | 68 |
|
99 | 69 | /** |
100 | | - * @return string The client's transaction key. |
| 70 | + * Runs a transaction from an XML string |
| 71 | + * |
| 72 | + * @param string $filePath The path to the XML file |
| 73 | + * @param string $merchantReferenceCode Desired reference code for the request |
| 74 | + * @return stdClass An object representation of the transaction response. |
101 | 75 | */ |
102 | | - public function getTransactionKey() |
| 76 | + public function runTransactionFromXml($xml, $merchantReferenceCode) |
103 | 77 | { |
104 | | - return $this->transactionKey; |
| 78 | + $request = $this->createRequest($merchantReferenceCode); |
| 79 | + $simpleXml = simplexml_load_string($xml); |
| 80 | + $xmlRequest = $this->simpleXmlToCybsRequest($simpleXml); |
| 81 | + $mergedRequest = (object) array_merge((array) $request, (array) $xmlRequest); |
| 82 | + return $this->runTransaction($mergedRequest); |
105 | 83 | } |
106 | 84 |
|
107 | 85 | /** |
108 | | - * Returns an object initialized with basic client information. |
| 86 | + * Runs a transaction from an XML file |
109 | 87 | * |
| 88 | + * @param string $filePath The path to the XML file |
110 | 89 | * @param string $merchantReferenceCode Desired reference code for the request |
111 | | - * @return stdClass An object initialized with the basic client info. |
| 90 | + * @return stdClass An object representation of the transaction response. |
112 | 91 | */ |
113 | | - public function createRequest($merchantReferenceCode) |
| 92 | + public function runTransactionFromFile($filePath, $merchantReferenceCode) |
114 | 93 | { |
115 | | - $request = new stdClass(); |
116 | | - $request->merchantID = $this->merchantId; |
117 | | - $request->merchantReferenceCode = $merchantReferenceCode; |
118 | | - $request->clientLibrary = "CyberSource PHP 1.0.0"; |
119 | | - $request->clientLibraryVersion = phpversion(); |
120 | | - $request->clientEnvironment = php_uname(); |
121 | | - return $request; |
| 94 | + $request = $this->createRequest($merchantReferenceCode); |
| 95 | + $xml = file_get_contents($filePath); |
| 96 | + return $this->runTransactionFromXml($xml, $merchantReferenceCode); |
122 | 97 | } |
123 | 98 | } |
0 commit comments