Skip to content

Commit e8982b2

Browse files
committed
Add functionality for sending requests from XML
1 parent ad90cd4 commit e8982b2

3 files changed

Lines changed: 113 additions & 1 deletion

File tree

lib/CybsSoapClient.php

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
*/
1010
class 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
}

samples/AuthFromXml.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
// This sample demonstrates how to run a sale request, which combines an
3+
// authorization with a capture in one request.
4+
5+
// Using Composer-generated autoload file.
6+
require __DIR__ . '/../vendor/autoload.php';
7+
// Or, uncomment the line below if you're not using Composer autoloader.
8+
// require_once(__DIR__ . '/../lib/CybsSoapClient.php');
9+
10+
11+
// Before using this example, you can use your own reference code for the transaction.
12+
$referenceCode = 'your_merchant_reference_code';
13+
14+
$client = new CybsSoapClient();
15+
$reply = $client->runTransactionFromXml(__DIR__ . '/xml/auth.xml', $referenceCode);
16+
17+
// This section will show all the reply fields.
18+
print("\nRESPONSE: " . print_r($reply, true));

samples/xml/auth.xml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<requestMessage
3+
xmlns="urn:schemas-cybersource-com:transaction-data-_1.109_"> -->
4+
<merchantReferenceCode>your_merchant_reference_code</merchantReferenceCode>
5+
<billTo>
6+
<firstName>John</firstName>
7+
<lastName>Doe</lastName>
8+
<street1>1295 Charleston Road</street1>
9+
<city>Mountain View</city>
10+
<state>CA</state>
11+
<postalCode>94043</postalCode>
12+
<country>US</country>
13+
<phoneNumber>650-965-6000</phoneNumber>
14+
<email>nobody@cybersource.com</email>
15+
<ipAddress>10.7.7.7</ipAddress>
16+
</billTo>
17+
<shipTo>
18+
<firstName>Jane</firstName>
19+
<lastName>Doe</lastName>
20+
<street1>100 Elm Street</street1>
21+
<city>San Mateo</city>
22+
<state>CA</state>
23+
<postalCode>94401</postalCode>
24+
<country>US</country>
25+
</shipTo>
26+
<item id="0">
27+
<unitPrice>12.34</unitPrice>
28+
</item>
29+
<item id="1">
30+
<unitPrice>56.78</unitPrice>
31+
</item>
32+
<purchaseTotals>
33+
<currency>USD</currency>
34+
</purchaseTotals>
35+
<card>
36+
<accountNumber>4111111111111111</accountNumber>
37+
<expirationMonth>12</expirationMonth>
38+
<expirationYear>2020</expirationYear>
39+
</card>
40+
<ccAuthService run="true">
41+
</ccAuthService>
42+
</requestMessage>

0 commit comments

Comments
 (0)