Skip to content

Commit

Permalink
Merge pull request #10 from AuthorizeNet/future
Browse files Browse the repository at this point in the history
Merge Future into Master
  • Loading branch information
brianmc committed Dec 29, 2014
2 parents 369917e + fe6b4a7 commit 58bfd5d
Show file tree
Hide file tree
Showing 11 changed files with 469 additions and 49 deletions.
33 changes: 4 additions & 29 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,34 +1,9 @@
# Cache and logs (Symfony2)
/app/cache/*
/app/logs/*
!app/cache/.gitkeep
!app/logs/.gitkeep

# Cache and logs (Symfony3)
/var/cache/*
/var/logs/*
!var/cache/.gitkeep
!var/logs/.gitkeep

# Parameters
/app/config/parameters.yml
/app/config/parameters.ini

# Managed by Composer
/app/bootstrap.php.cache
/var/bootstrap.php.cache
/bin/*
!bin/console
!bin/symfony_requirements
/vendor/

# Assets and user uploads
/web/bundles/
/web/uploads/

# PHPUnit
/app/phpunit.xml
/phpunit.xml

# Composer PHAR
/composer.phar
/composer.lock

# CyberSource config parameters
/lib/conf/cybs.ini
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
language: php
php:
- 5.6
- 5.5
- 5.4
- 5.3
before_script: composer install
69 changes: 52 additions & 17 deletions LICENSE

Large diffs are not rendered by default.

82 changes: 79 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,80 @@
cybersource-sdk-php
===================
#CyberSource PHP Client

This is the temporary private repo for the Cybs PHP SDK
This is the PHP client for the [CyberSource SOAP Toolkit API](http://www.cybersource.com/developers/getting_started/integration_methods/soap_toolkit_api).


##Prerequisites

- PHP 5.3 or above
- [curl](http://php.net/manual/en/book.curl.php), [openssl](http://php.net/manual/en/book.openssl.php), [soap](http://php.net/manual/en/book.soap.php) extensions must be enabled
- A CyberSource account. You can create an evaluation account [here](http://www.cybersource.com/register/).
- A CyberSource transaction key. You will need to set your merchant ID and transaction key in the ````cybs.ini```` file in ````lib/conf````. Instructions on obtaining a transaction key can be found [here](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.3.html).


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

- [test](https://ics2wstest.ic3.com/commerce/1.x/transactionProcessor/)
- [live](https://ics2ws.ic3.com/commerce/1.x/transactionProcessor/)

###Installing with Composer
You'll first need to make sure you have Composer installed. You can follow the instructions on the [official web site](https://getcomposer.org/download/). Once Composer is installed, you can enter the project root and run:
```
composer.phar install
```
Then, to use the client, you'll need to include the Composer-generated autoload file:

```php
require_once('/path/to/project/vendor/autoload.php');
```

###Manual installation
To use the client manually, include the CyberSource client in your project:

```php
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.

```php
$client = new CybsSoapClient();
$request = $client->createRequest();

$card = new stdClass();
$card->accountNumber = '4111111111111111';
$card->expirationMonth = '12';
$card->expirationYear = '2020';
$request->card = $card;

// Populate $request here with other necessary properties

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

```
php samples/Sale.php
```

The samples will output the response object for each request if successful. Note that the samples contain test data and should not be run in a live environment.

##Tests

In order to run tests, you'll need [PHPUnit](https://phpunit.de). You'll also need to use [Composer](https://getcomposer.org/) for autoloading. If you used Composer to install the client, this should already be set up. Otherwise, to use Composer for autoloading only, from the project root run
```
composer.phar dump-autoload
```

If you installed PHPUnit with Composer, run the tests from the project root with the command ````vendor/bin/phpunit````.

##Documentation

For more information about CyberSource services, see <http://www.cybersource.com/developers/documentation>

For all other support needs, see <http://www.cybersource.com/support>
30 changes: 30 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "cybersource/sdk-php",
"description": "CyberSource PHP SOAP client",
"keywords": [
"cybersource",
"payments",
"api"
],
"homepage": "https://github.com/CyberSource/cybersource-sdk-php",
"license": "https://github.com/CyberSource/cybersource-sdk-php/blob/master/LICENSE",
"authors": [
{
"name": "CyberSource",
"homepage": "https://github.com/CyberSource"
}
],
"require": {
"php": ">=5.3",
"ext-curl": "*",
"ext-openssl": "*",
"ext-soap": "*"
},
"require-dev": {
"phpunit/phpunit": "3.7.*"
},
"autoload": {
"classmap": ["lib/"]
},
"include-path": ["lib/conf/"]
}
123 changes: 123 additions & 0 deletions lib/CybsSoapClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php

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

/**
* CybsSoapClient
*
* An implementation of PHP's SOAPClient class for making CyberSource requests.
*/
class CybsSoapClient extends SoapClient
{
private $merchantId;
private $transactionKey;

function __construct($options=array())
{
$properties = parse_ini_file('cybs.ini');
$required = array('merchant_id', 'transaction_key', 'wsdl');

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

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

parent::__construct($properties['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;
}

/**
* Returns an object initialized with basic client information.
*
* @param string $merchantReferenceCode Desired reference code for the request
* @return stdClass An object initialized with the basic client info.
*/
public function createRequest($merchantReferenceCode)
{
$request = new stdClass();
$request->merchantID = $this->merchantId;
$request->merchantReferenceCode = $merchantReferenceCode;
$request->clientLibrary = "CyberSource PHP 1.0.0";
$request->clientLibraryVersion = phpversion();
$request->clientEnvironment = php_uname();
return $request;
}
}
5 changes: 5 additions & 0 deletions lib/conf/cybs.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
merchant_id = your_merchant_id
transaction_key = "your_transaction_key"

; Modify the URL to point to either a live or test WSDL file with the desired API version.
wsdl = "https://ics2wstest.ic3.com/commerce/1.x/transactionProcessor/CyberSourceTransaction_1.109.wsdl"
17 changes: 17 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false">
<testsuites>
<testsuite name="CyberSource PHP client test">
<directory suffix=".php">./test</directory>
</testsuite>
</testsuites>
</phpunit>
79 changes: 79 additions & 0 deletions samples/AuthFollowOnCapture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
// This sample demonstrates how to run an authorization request followed by a
// capture request.

// Using Composer-generated autoload file.
require __DIR__ . '/../vendor/autoload.php';
// Or, uncomment the line below if you're not using Composer autoloader.
// require_once(__DIR__ . '/../lib/CybsSoapClient.php');


// Before using this example, you can use your own reference code for the transaction.
$referenceCode = 'your_merchant_reference_code';

$client = new CybsSoapClient();
$request = $client->createRequest($referenceCode);

// This section contains a sample transaction request for the authorization
// service with complete billing, payment card, and purchase (two items) information.
$ccAuthService = new stdClass();
$ccAuthService->run = 'true';
$request->ccAuthService = $ccAuthService;

$billTo = new stdClass();
$billTo->firstName = 'John';
$billTo->lastName = 'Doe';
$billTo->street1 = '1295 Charleston Road';
$billTo->city = 'Mountain View';
$billTo->state = 'CA';
$billTo->postalCode = '94043';
$billTo->country = 'US';
$billTo->email = 'null@cybersource.com';
$billTo->ipAddress = '10.7.111.111';
$request->billTo = $billTo;

$card = new stdClass();
$card->accountNumber = '4111111111111111';
$card->expirationMonth = '12';
$card->expirationYear = '2020';
$request->card = $card;

$purchaseTotals = new stdClass();
$purchaseTotals->currency = 'USD';
$request->purchaseTotals = $purchaseTotals;

$item0 = new stdClass();
$item0->unitPrice = '12.34';
$item0->quantity = '2';
$item0->id = '0';

$item1 = new stdClass();
$item1->unitPrice = '56.78';
$item1->id = '1';

$request->item = array($item0, $item1);

$reply = $client->runTransaction($request);

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

if ($reply->decision != 'ACCEPT') {
print("\nFailed auth request.\n");
return;
}

// Build a capture using the request ID in the response as the auth request ID
$ccCaptureService = new stdClass();
$ccCaptureService->run = 'true';
$ccCaptureService->authRequestID = $reply->requestID;

$captureRequest = $client->createRequest($referenceCode);
$captureRequest->ccCaptureService = $ccCaptureService;
$captureRequest->item = array($item0, $item1);
$captureRequest->purchaseTotals = $purchaseTotals;

$captureReply = $client->runTransaction($captureRequest);

// This section will show all the reply fields.
print("\nCAPTRUE RESPONSE: " . print_r($captureReply, true));
Loading

0 comments on commit 58bfd5d

Please sign in to comment.