Skip to content

Commit

Permalink
Merge pull request #17 from CyberSource/nvp-support
Browse files Browse the repository at this point in the history
Added support for NVP & XML String
  • Loading branch information
brianmc committed Sep 23, 2015
2 parents a96a781 + 8d9e0de commit 0f19074
Show file tree
Hide file tree
Showing 11 changed files with 372 additions and 92 deletions.
38 changes: 36 additions & 2 deletions README.md
Expand Up @@ -23,7 +23,7 @@ If you want to install SDK from Packagist,add the following dependency to your a

##Installation

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:
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:

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


##Getting Started
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.
The PHP client will generate the request message headers for you, and will contain the methods specified by the WSDL file.

###Creating a simple request
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.

```php
$client = new CybsSoapClient();
Expand All @@ -65,6 +68,37 @@ $request->card = $card;
$reply = $client->runTransaction($request);
```

###Creating a request from XML
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:

```php
$referenceCode = 'your_merchant_reference_code';
$client = new CybsSoapClient();
$reply = $client->runTransactionFromFile('path/to/my.xml', $referenceCode);
```

Or, you can create your own XML string and use that instead:

```php
$xml = "";
// Populate $xml
$client = new CybsSoapClient();
$client->runTransactionFromXml($xml);
```

###Using name-value pairs
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:

```php
$client = new CybsNameValuePairClient();
$request = array();
$request['ccAuthService_run'] = 'true';
$request['merchantID'] = 'my_merchant_id';
$request['merchantReferenceCode'] = $'my_reference_code';
// Populate $request
$reply = $client->runTransaction($request);
```

##Running the Samples
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:

Expand Down
116 changes: 116 additions & 0 deletions lib/CybsClient.php
@@ -0,0 +1,116 @@
<?php

set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__) . '/conf');

/**
* CybsClient
*
* An implementation of PHP's SOAPClient class for making either name-value pair
* or XML CyberSource requests.
*/
class CybsClient extends SoapClient
{
const CLIENT_LIBRARY_VERSION = "CyberSource PHP 1.0.0";

private $merchantId;
private $transactionKey;

function __construct($options=array(), $properties, $nvp=false)
{
$required = array('merchant_id', 'transaction_key');

if (!$properties) {
throw new Exception('Unable to read cybs.ini.');
}

if ($nvp === true) {
array_push($required, 'nvp_wsdl');
$wsdl = $properties['nvp_wsdl'];
} else {
array_push($required, 'wsdl');
$wsdl = $properties['wsdl'];
}

foreach ($required as $req) {
if (empty($properties[$req])) {
throw new Exception($req . ' not found in cybs.ini.');
}
}

parent::__construct($wsdl, $options);
$this->merchantId = $properties['merchant_id'];
$this->transactionKey = $properties['transaction_key'];

$nameSpace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd";

$soapUsername = new SoapVar(
$this->merchantId,
XSD_STRING,
NULL,
$nameSpace,
NULL,
$nameSpace
);

$soapPassword = new SoapVar(
$this->transactionKey,
XSD_STRING,
NULL,
$nameSpace,
NULL,
$nameSpace
);

$auth = new stdClass();
$auth->Username = $soapUsername;
$auth->Password = $soapPassword;

$soapAuth = new SoapVar(
$auth,
SOAP_ENC_OBJECT,
NULL, $nameSpace,
'UsernameToken',
$nameSpace
);

$token = new stdClass();
$token->UsernameToken = $soapAuth;

$soapToken = new SoapVar(
$token,
SOAP_ENC_OBJECT,
NULL,
$nameSpace,
'UsernameToken',
$nameSpace
);

$security =new SoapVar(
$soapToken,
SOAP_ENC_OBJECT,
NULL,
$nameSpace,
'Security',
$nameSpace
);

$header = new SoapHeader($nameSpace, 'Security', $security, true);
$this->__setSoapHeaders(array($header));
}

/**
* @return string The client's merchant ID.
*/
public function getMerchantId()
{
return $this->merchantId;
}

/**
* @return string The client's transaction key.
*/
public function getTransactionKey()
{
return $this->transactionKey;
}
}
40 changes: 40 additions & 0 deletions lib/CybsNameValuePairClient.php
@@ -0,0 +1,40 @@
<?php

include 'CybsClient.php';

/**
* CybsSoapClient
*
* An implementation of SOAPClient class for making CyberSource name-value pair
* requests.
*/
class CybsNameValuePairClient extends CybsClient
{

function __construct($options=array())
{
$properties = parse_ini_file('cybs.ini');
parent::__construct($options, $properties, true);
}

/**
* Runs a transaction from a name-value pair array
*
* @param string $request An array of name-value pairs
* @return string Response of name-value pairs delimited by a new line
*/
public function runTransaction($request)
{
if (!is_array($request)) {
throw new Exception('Name-value pairs must be in array');
}
if (!array_key_exists('merchantID', $request)) {
$request['merchantID'] = $this->getMerchantId();
}
$nvpRequest = "";
foreach($request as $k => $v) {
$nvpRequest .= ($k . "=" . $v ."\n");
}
return parent::runTransaction($nvpRequest);
}
}

0 comments on commit 0f19074

Please sign in to comment.