Skip to content

Commit 0f19074

Browse files
committed
Merge pull request #17 from CyberSource/nvp-support
Added support for NVP & XML String
2 parents a96a781 + 8d9e0de commit 0f19074

File tree

11 files changed

+372
-92
lines changed

11 files changed

+372
-92
lines changed

README.md

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ If you want to install SDK from Packagist,add the following dependency to your a
2323

2424
##Installation
2525

26-
You can install the client either via [Composer](https://getcomposer.org/) or manually. Before installing, make sure to configure the merchant ID, transaction key, and the WSDL file URL in ````cybs.ini````. By default, the WSDL file for the client is for API version 1.109 (the latest when this package was created). Available WSDL file URLs can be browsed at the following locations:
26+
You can install the client either via [Composer](https://getcomposer.org/) or manually. Before installing, make sure to configure the merchant ID, transaction key, and the appropriate WSDL file URL in ````cybs.ini````. By default, the WSDL file for the client is for API version 1.120 (the latest when this package was updated). Available WSDL file URLs can be browsed at the following locations:
2727

2828
- [test](https://ics2wstest.ic3.com/commerce/1.x/transactionProcessor/)
2929
- [live](https://ics2ws.ic3.com/commerce/1.x/transactionProcessor/)
@@ -48,7 +48,10 @@ require_once('/path/to/project/lib/CybsSoapClient.php');
4848

4949

5050
##Getting Started
51-
The PHP client will generate the request message headers for you, and will contain the methods specified by the WSDL file. The main method you'll use is ````runTransaction()````. To run a transaction, you'll first need to construct a client to generate a request object, which you can populate with the necessary fields (see [documentation](http://www.cybersource.com/developers/integration_methods/simple_order_and_soap_toolkit_api/soap_api/html/wwhelp/wwhimpl/js/html/wwhelp.htm#href=Intro.04.4.html) for sample requests). The object will be converted into XML, so the properties of the object will need to correspond to the correct XML format.
51+
The PHP client will generate the request message headers for you, and will contain the methods specified by the WSDL file.
52+
53+
###Creating a simple request
54+
The main method you'll use is ````runTransaction()````. To run a transaction, you'll first need to construct a client to generate a request object, which you can populate with the necessary fields (see [documentation](http://www.cybersource.com/developers/integration_methods/simple_order_and_soap_toolkit_api/soap_api/html/wwhelp/wwhimpl/js/html/wwhelp.htm#href=Intro.04.4.html) for sample requests). The object will be converted into XML, so the properties of the object will need to correspond to the correct XML format.
5255

5356
```php
5457
$client = new CybsSoapClient();
@@ -65,6 +68,37 @@ $request->card = $card;
6568
$reply = $client->runTransaction($request);
6669
```
6770

71+
###Creating a request from XML
72+
You can create a request from XML either in a file or from an XML string. The XML request format is described in the **Using XML** section [here](http://apps.cybersource.com/library/documentation/dev_guides/Simple_Order_API_Clients/Client_SDK_SO_API.pdf). Here's how to run a transaction from an XML file:
73+
74+
```php
75+
$referenceCode = 'your_merchant_reference_code';
76+
$client = new CybsSoapClient();
77+
$reply = $client->runTransactionFromFile('path/to/my.xml', $referenceCode);
78+
```
79+
80+
Or, you can create your own XML string and use that instead:
81+
82+
```php
83+
$xml = "";
84+
// Populate $xml
85+
$client = new CybsSoapClient();
86+
$client->runTransactionFromXml($xml);
87+
```
88+
89+
###Using name-value pairs
90+
In order to run transactions using name-value pairs, make sure to set the value for the WSDL for the NVP transaction processor in ````cybs.ini````. Then use the ````CybsNameValuePairClient```` as so:
91+
92+
```php
93+
$client = new CybsNameValuePairClient();
94+
$request = array();
95+
$request['ccAuthService_run'] = 'true';
96+
$request['merchantID'] = 'my_merchant_id';
97+
$request['merchantReferenceCode'] = $'my_reference_code';
98+
// Populate $request
99+
$reply = $client->runTransaction($request);
100+
```
101+
68102
##Running the Samples
69103
After configuring your merchant ID and transaction key in ````cybs.ini````, the samples in the ````samples```` directory can be run from the project root. For example:
70104

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: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
if (!array_key_exists('merchantID', $request)) {
32+
$request['merchantID'] = $this->getMerchantId();
33+
}
34+
$nvpRequest = "";
35+
foreach($request as $k => $v) {
36+
$nvpRequest .= ($k . "=" . $v ."\n");
37+
}
38+
return parent::runTransaction($nvpRequest);
39+
}
40+
}

0 commit comments

Comments
 (0)