Skip to content

Commit

Permalink
Merge pull request #10 from 2Checkout/0.3.0
Browse files Browse the repository at this point in the history
0.3.0
  • Loading branch information
Craig Christenson committed Apr 30, 2014
2 parents 1e2d53f + 91f6315 commit eaadf8a
Show file tree
Hide file tree
Showing 22 changed files with 601 additions and 587 deletions.
85 changes: 65 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
2Checkout PHP Library
=====================

This library provides developers with a simple set of bindings to the 2Checkout purchase routine, Instant Notification Service and Back Office API.
This library provides developers with a simple set of bindings to the 2Checkout Payment API, Hosted Checkout, Instant Notification Service and Admin API.

To use, download or clone the repository.

Expand All @@ -15,14 +15,44 @@ Require in your php script.
require_once("/path/to/2checkout-php/lib/Twocheckout.php");
```

JSON is returned by default or you can add 'array' as an additional argument to each call to get an Array.
All methods return an Array by default or you can set the format to 'json' to get a JSON response.
**Example:**
```php
<?php
Twocheckout_Charge::auth($args, 'array');
Twocheckout::format('json');
```

Full documentation for each binding is provided in the [Wiki](https://github.com/2checkout/2checkout-php/wiki).

Credentials and Options
-----------------

Methods are provided to set the following credentials and options.

```php
<?php

// Your sellerId(account number) and privateKey are required to make the Payment API Authorization call.
Twocheckout::privateKey('BE632CB0-BB29-11E3-AFB6-D99C28100996');
Twocheckout::sellerId('901248204');

// Your username and password are required to make any Admin API call.
Twocheckout::username('testlibraryapi901248204');
Twocheckout::password('testlibraryapi901248204PASS');

// If you want to turn off SSL verification (Please don't do this in your production environment)
Twocheckout::verifySSL(false); // this is set to true by default

// To use your sandbox account set sandbox to true
Twocheckout::sandbox(true);

// All methods return an Array by default or you can set the format to 'json' to get a JSON response.
Twocheckout::format('json');

```



Full documentation for each binding is provided in the [2Checkout Documentation](https://www.2checkout.com/documentation/libraries/php).

Example Purchase API Usage
-----------------
Expand All @@ -31,12 +61,15 @@ Example Purchase API Usage

```php
<?php
Twocheckout::setApiCredentials('1817037', '3508079E-5383-44D4-BF69-DC619C0D9811');

Twocheckout::privateKey('BE632CB0-BB29-11E3-AFB6-D99C28100996');
Twocheckout::sellerId('901248204');

try {
$charge = Twocheckout_Charge::auth(array(
"sellerId" => "1817037",
"sellerId" => "901248204",
"merchantOrderId" => "123",
"token" => 'Y2U2OTdlZjMtOGQzMi00MDdkLWJjNGQtMGJhN2IyOTdlN2Ni',
"token" => 'MjFiYzIzYjAtYjE4YS00ZmI0LTg4YzYtNDIzMTBlMjc0MDlk',
"currency" => 'USD',
"total" => '10.00',
"billingAddr" => array(
Expand All @@ -59,7 +92,7 @@ try {
"email" => '[email protected]',
"phoneNumber" => '555-555-5555'
)
), 'array');
));
$this->assertEquals('APPROVED', $charge['response']['responseCode']);
} catch (Twocheckout_Error $e) {
$this->assertEquals('Unauthorized', $e->getMessage());
Expand Down Expand Up @@ -147,9 +180,18 @@ Example Admin API Usage

```php
<?php
Twocheckout::setCredentials("APIuser1817037", "APIpass1817037");
$args = array('sale_id' => 4834917619);
Twocheckout_Sale::stop($args, 'array');

Twocheckout::username('testlibraryapi901248204');
Twocheckout::password('testlibraryapi901248204PASS');

$args = array(
'sale_id' => 4834917619
);
try {
$result = Twocheckout_Sale::stop($args);
} catch (Twocheckout_Error $e) {
$e->getMessage();
}
```

*Example Response:*
Expand Down Expand Up @@ -200,11 +242,12 @@ Example Return Usage:

```php
<?php

$params = array();
foreach ($_REQUEST as $k => $v) {
$params[$k] = $v;
}
$passback = Twocheckout_Return::check($params, "tango", 'array');
$passback = Twocheckout_Return::check($params, "tango");
```

*Example Response:*
Expand All @@ -223,11 +266,12 @@ Example INS Usage:

```php
<?php

$params = array();
foreach ($_POST as $k => $v) {
$params[$k] = $v;
}
$passback = Twocheckout_Notification::check($params, "tango", 'array');
$passback = Twocheckout_Notification::check($params, "tango");
```

*Example Response:*
Expand All @@ -248,18 +292,19 @@ Twocheckout_Error exceptions are thrown by if an error has returned. It is best
```php
<?php

Twocheckout::setCredentials("APIuser1817037", "APIpass1817037");
Twocheckout::username('testlibraryapi901248204');
Twocheckout::password('testlibraryapi901248204PASS');

$params = array(
'sale_id' => 4774380224,
'category' => 1,
'comment' => 'Order never sent.'
'sale_id' => 4774380224,
'category' => 1,
'comment' => 'Order never sent.'
);
try {
$sale = Twocheckout_Sale::refund($params, 'array');
$sale = Twocheckout_Sale::refund($params);
} catch (Twocheckout_Error $e) {
$e->getMessage();
$e->getMessage();
}
```

Full documentation for each binding is provided in the [Wiki](https://github.com/2checkout/2checkout-php/wiki).
Full documentation for each binding is provided in the [2Checkout Documentation](https://www.2checkout.com/documentation/libraries/php).
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
"ext-curl": "*",
"ext-json": "*"
},
"require-dev": {
"phpunit/phpunit": "3.7.*"
},
"autoload": {
"psr-0": {
"Twocheckout": "lib/"
Expand Down
62 changes: 39 additions & 23 deletions lib/Twocheckout.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,53 @@

abstract class Twocheckout
{
public static $user;
public static $pass;
public static $format = "json";
public static $apiBaseUrl = "https://www.2checkout.com/api/";
public static $sid;
public static $privateKey;
public static $apiUrl;
public static $environment;
public static $username;
public static $password;
public static $sandbox;
public static $verifySSL = true;
public static $baseUrl = 'https://www.2checkout.com';
public static $error;
const VERSION = '0.2.1';

static function setCredentials($user, $pass, $mode='', $environment="production")
{
self::$user = $user;
self::$pass = $pass;
if ($mode == 'sandbox') {
self::$apiBaseUrl = 'https://sandbox.2checkout.com/api/';
public static $format = 'array';
const VERSION = '0.3.0';

public static function sellerId($value = null) {
self::$sid = $value;
}

public static function privateKey($value = null) {
self::$privateKey = $value;
}

public static function username($value = null) {
self::$username = $value;
}

public static function password($value = null) {
self::$password = $value;
}

public static function sandbox($value = null) {
if ($value == 1 || $value == true) {
self::$sandbox = true;
self::$baseUrl = 'https://sandbox.2checkout.com';
} else {
self::$sandbox = false;
self::$baseUrl = 'https://www.2checkout.com';
}
self::$environment = $environment;
}

static function setApiCredentials($sid, $privateKey, $mode='', $environment="production")
{
self::$sid = $sid;
self::$privateKey = $privateKey;
if ($mode == 'sandbox') {
self::$apiUrl = 'https://sandbox.2checkout.com/checkout/api/1/'.$sid.'/rs/authService';
public static function verifySSL($value = null) {
if ($value == 0 || $value == false) {
self::$verifySSL = false;
} else {
self::$apiUrl = 'https://www.2checkout.com/checkout/api/1/'.$sid.'/rs/authService';
self::$verifySSL = true;
}
self::$environment = $environment;
}

public static function format($value = null) {
self::$format = $value;
}
}

Expand Down
16 changes: 8 additions & 8 deletions lib/Twocheckout/Api/TwocheckoutAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@
class Twocheckout_Company extends Twocheckout
{

public static function retrieve($format='json')
public static function retrieve()
{
$request = new Twocheckout_Api_Requester();
$urlSuffix = 'acct/detail_company_info';
$result = $request->do_call($urlSuffix);
return Twocheckout_Util::return_resp($result, $format);
$urlSuffix = '/api/acct/detail_company_info';
$result = $request->doCall($urlSuffix);
return Twocheckout_Util::returnResponse($result);
}
}

class Twocheckout_Contact extends Twocheckout
{

public static function retrieve($format='json')
public static function retrieve()
{
$request = new Twocheckout_Api_Requester();
$urlSuffix = 'acct/detail_contact_info';
$result = $request->do_call($urlSuffix);
return Twocheckout_Util::return_resp($result, $format);
$urlSuffix = '/api/acct/detail_contact_info';
$result = $request->doCall($urlSuffix);
return Twocheckout_Util::returnResponse($result);
}
}
67 changes: 28 additions & 39 deletions lib/Twocheckout/Api/TwocheckoutApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,65 +2,54 @@

class Twocheckout_Api_Requester
{
public $apiBaseUrl;
public $baseUrl;
public $environment;
private $user;
private $pass;
private $sid;
private $privateKey;

function __construct() {
$this->user = Twocheckout::$user;
$this->pass = Twocheckout::$pass;
$this->apiBaseUrl = Twocheckout::$apiBaseUrl;
$this->environment = Twocheckout::$environment;
$this->privateKey = Twocheckout::$privateKey;
$this->apiUrl = Twocheckout::$apiUrl;
$this->user = Twocheckout::$username;
$this->pass = Twocheckout::$password;
$this->sid = Twocheckout::$sid;
$this->baseUrl = Twocheckout::$baseUrl;
$this->verifySSL = Twocheckout::$verifySSL;
$this->privateKey = Twocheckout::$privateKey;
}

function do_call($urlSuffix, $data=array())
function doCall($urlSuffix, $data=array())
{
$url = $this->apiBaseUrl . $urlSuffix;
$url = $this->baseUrl . $urlSuffix;
$ch = curl_init($url);
if ($this->environment === "development") {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
}
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: application/json"));
curl_setopt($ch, CURLOPT_USERAGENT, "2Checkout PHP/0.1.0%s");
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "{$this->user}:{$this->pass}");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$resp = curl_exec($ch);
curl_close($ch);
if ($resp === FALSE) {
throw new Twocheckout_Error("cURL call failed", "403");
if (isset($data['api'])) {
unset( $data['api'] );
$data['privateKey'] = $this->privateKey;
$data['sellerId'] = $this->sid;
$data = json_encode($data);
$header = array("content-type:application/json","content-length:".strlen($data));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
} else {
return utf8_encode($resp);
$header = array("Accept: application/json");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "{$this->user}:{$this->pass}");
}
}

function do_auth_call($data)
{
$data['privateKey'] = $this->privateKey;
$data = json_encode($data);
$header = array("content-type:application/JSON","content-length:".strlen($data));
$url = $this->apiUrl;
$ch = curl_init($url);
if ($this->environment === "development") {
if ($this->verifySSL == false) {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
}
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_USERAGENT, "2Checkout PHP/0.1.0%s");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$resp = curl_exec($ch);
curl_close($ch);
if ($resp === FALSE) {
throw new Twocheckout_Error("cURL call failed", "403");
} else {
return $resp;
return utf8_encode($resp);
}
}
}

}
Loading

0 comments on commit eaadf8a

Please sign in to comment.