Skip to content

Commit 1770b8f

Browse files
committed
Add CybsNameValuePairClient for flat name-value pair requests. Add support for sending requests from XML strings
1 parent e8982b2 commit 1770b8f

6 files changed

Lines changed: 217 additions & 101 deletions

File tree

lib/CybsClient.php

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
3+
set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__) . '/conf');
4+
5+
/**
6+
* CybsClient
7+
*
8+
* An implementation of PHP's SOAPClient class for making either name-value pair
9+
* or XML CyberSource requests.
10+
*/
11+
class CybsClient extends SoapClient
12+
{
13+
const CLIENT_LIBRARY_VERSION = "CyberSource PHP 1.0.0";
14+
15+
private $merchantId;
16+
private $transactionKey;
17+
18+
function __construct($options=array(), $properties, $nvp=false)
19+
{
20+
$required = array('merchant_id', 'transaction_key');
21+
22+
if (!$properties) {
23+
throw new Exception('Unable to read cybs.ini.');
24+
}
25+
26+
if ($nvp === true) {
27+
array_push($required, 'nvp_wsdl');
28+
$wsdl = $properties['nvp_wsdl'];
29+
} else {
30+
array_push($required, 'wsdl');
31+
$wsdl = $properties['wsdl'];
32+
}
33+
34+
foreach ($required as $req) {
35+
if (empty($properties[$req])) {
36+
throw new Exception($req . ' not found in cybs.ini.');
37+
}
38+
}
39+
40+
parent::__construct($wsdl, $options);
41+
$this->merchantId = $properties['merchant_id'];
42+
$this->transactionKey = $properties['transaction_key'];
43+
44+
$nameSpace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd";
45+
46+
$soapUsername = new SoapVar(
47+
$this->merchantId,
48+
XSD_STRING,
49+
NULL,
50+
$nameSpace,
51+
NULL,
52+
$nameSpace
53+
);
54+
55+
$soapPassword = new SoapVar(
56+
$this->transactionKey,
57+
XSD_STRING,
58+
NULL,
59+
$nameSpace,
60+
NULL,
61+
$nameSpace
62+
);
63+
64+
$auth = new stdClass();
65+
$auth->Username = $soapUsername;
66+
$auth->Password = $soapPassword;
67+
68+
$soapAuth = new SoapVar(
69+
$auth,
70+
SOAP_ENC_OBJECT,
71+
NULL, $nameSpace,
72+
'UsernameToken',
73+
$nameSpace
74+
);
75+
76+
$token = new stdClass();
77+
$token->UsernameToken = $soapAuth;
78+
79+
$soapToken = new SoapVar(
80+
$token,
81+
SOAP_ENC_OBJECT,
82+
NULL,
83+
$nameSpace,
84+
'UsernameToken',
85+
$nameSpace
86+
);
87+
88+
$security =new SoapVar(
89+
$soapToken,
90+
SOAP_ENC_OBJECT,
91+
NULL,
92+
$nameSpace,
93+
'Security',
94+
$nameSpace
95+
);
96+
97+
$header = new SoapHeader($nameSpace, 'Security', $security, true);
98+
$this->__setSoapHeaders(array($header));
99+
}
100+
101+
/**
102+
* @return string The client's merchant ID.
103+
*/
104+
public function getMerchantId()
105+
{
106+
return $this->merchantId;
107+
}
108+
109+
/**
110+
* @return string The client's transaction key.
111+
*/
112+
public function getTransactionKey()
113+
{
114+
return $this->transactionKey;
115+
}
116+
}

lib/CybsNameValuePairClient.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
include 'CybsClient.php';
4+
5+
/**
6+
* CybsSoapClient
7+
*
8+
* An implementation of SOAPClient class for making CyberSource name-value pair
9+
* requests.
10+
*/
11+
class CybsNameValuePairClient extends CybsClient
12+
{
13+
14+
function __construct($options=array())
15+
{
16+
$properties = parse_ini_file('cybs.ini');
17+
parent::__construct($options, $properties, true);
18+
}
19+
20+
/**
21+
* Runs a transaction from a name-value pair array
22+
*
23+
* @param string $request An array of name-value pairs
24+
* @return string Response of name-value pairs delimited by a new line
25+
*/
26+
public function runTransaction($request)
27+
{
28+
if (!is_array($request)) {
29+
throw new Exception('Name-value pairs must be in array');
30+
}
31+
$nvpRequest = "";
32+
foreach($request as $k => $v) {
33+
$nvpRequest .= ($k . "=" . $v ."\n");
34+
}
35+
return parent::runTransaction($nvpRequest);
36+
}
37+
}

lib/CybsSoapClient.php

Lines changed: 22 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1,109 +1,19 @@
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
}

lib/conf/cybs.ini

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@ merchant_id = your_merchant_id
22
transaction_key = "your_transaction_key"
33

44
; Modify the URL to point to either a live or test WSDL file with the desired API version.
5-
wsdl = "https://ics2wstest.ic3.com/commerce/1.x/transactionProcessor/CyberSourceTransaction_1.109.wsdl"
5+
wsdl = "https://ics2wstest.ic3.com/commerce/1.x/transactionProcessor/CyberSourceTransaction_1.109.wsdl"
6+
7+
; Modify the URL to point to either a live or test WSDL file with the desired API version for the name-value pairs transaction API.
8+
nvp_wsdl = "https://ics2wstest.ic3.com/commerce/1.x/transactionProcessor/CyberSourceTransaction_NVP_1.120.wsdl"

samples/AuthFromNameValuePairs.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
// This sample demonstrates how to run an auth request for two items with a flat
3+
// name-value pair structure
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 CybsNameValuePairClient();
15+
16+
$request = array();
17+
$request['ccAuthService_run'] = 'true';
18+
$request['merchantID'] = 'dvaldez';
19+
$request['merchantReferenceCode'] = 'MRC-14344';
20+
$request['billTo_firstName'] = 'Jane';
21+
$request['billTo_lastName'] = 'Smith';
22+
$request['billTo_street1'] = '1295 Charleston Road';
23+
$request['billTo_city'] = 'Mountain View';
24+
$request['billTo_state'] = 'CA';
25+
$request['billTo_postalCode'] = '94043';
26+
$request['billTo_country'] = 'US';
27+
$request['billTo_email'] = 'jsmith@example.com';
28+
$request['card_accountNumber'] = '4111111111111111';
29+
$request['card_expirationMonth'] = '12';
30+
$request['card_expirationYear'] = '2019';
31+
$request['purchaseTotals_currency'] = 'USD';
32+
$request['item_0_unitPrice'] = '12.34';
33+
$request['item_1_unitPrice'] = '56.78';
34+
$reply = $client->runTransaction($request);
35+
36+
// This section will show all the reply fields.
37+
print("\nRESPONSE:\n" . $reply);

samples/AuthFromXml.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
$referenceCode = 'your_merchant_reference_code';
1313

1414
$client = new CybsSoapClient();
15-
$reply = $client->runTransactionFromXml(__DIR__ . '/xml/auth.xml', $referenceCode);
15+
$reply = $client->runTransactionFromFile(__DIR__ . '/xml/auth.xml', $referenceCode);
1616

1717
// This section will show all the reply fields.
1818
print("\nRESPONSE: " . print_r($reply, true));

0 commit comments

Comments
 (0)