Skip to content

Commit eaadf8a

Browse files
author
Craig Christenson
committed
Merge pull request #10 from 2Checkout/0.3.0
0.3.0
2 parents 1e2d53f + 91f6315 commit eaadf8a

22 files changed

Lines changed: 601 additions & 587 deletions

README.md

Lines changed: 65 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
2Checkout PHP Library
22
=====================
33

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

66
To use, download or clone the repository.
77

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

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

25-
Full documentation for each binding is provided in the [Wiki](https://github.com/2checkout/2checkout-php/wiki).
25+
26+
Credentials and Options
27+
-----------------
28+
29+
Methods are provided to set the following credentials and options.
30+
31+
```php
32+
<?php
33+
34+
// Your sellerId(account number) and privateKey are required to make the Payment API Authorization call.
35+
Twocheckout::privateKey('BE632CB0-BB29-11E3-AFB6-D99C28100996');
36+
Twocheckout::sellerId('901248204');
37+
38+
// Your username and password are required to make any Admin API call.
39+
Twocheckout::username('testlibraryapi901248204');
40+
Twocheckout::password('testlibraryapi901248204PASS');
41+
42+
// If you want to turn off SSL verification (Please don't do this in your production environment)
43+
Twocheckout::verifySSL(false); // this is set to true by default
44+
45+
// To use your sandbox account set sandbox to true
46+
Twocheckout::sandbox(true);
47+
48+
// All methods return an Array by default or you can set the format to 'json' to get a JSON response.
49+
Twocheckout::format('json');
50+
51+
```
52+
53+
54+
55+
Full documentation for each binding is provided in the [2Checkout Documentation](https://www.2checkout.com/documentation/libraries/php).
2656

2757
Example Purchase API Usage
2858
-----------------
@@ -31,12 +61,15 @@ Example Purchase API Usage
3161

3262
```php
3363
<?php
34-
Twocheckout::setApiCredentials('1817037', '3508079E-5383-44D4-BF69-DC619C0D9811');
64+
65+
Twocheckout::privateKey('BE632CB0-BB29-11E3-AFB6-D99C28100996');
66+
Twocheckout::sellerId('901248204');
67+
3568
try {
3669
$charge = Twocheckout_Charge::auth(array(
37-
"sellerId" => "1817037",
70+
"sellerId" => "901248204",
3871
"merchantOrderId" => "123",
39-
"token" => 'Y2U2OTdlZjMtOGQzMi00MDdkLWJjNGQtMGJhN2IyOTdlN2Ni',
72+
"token" => 'MjFiYzIzYjAtYjE4YS00ZmI0LTg4YzYtNDIzMTBlMjc0MDlk',
4073
"currency" => 'USD',
4174
"total" => '10.00',
4275
"billingAddr" => array(
@@ -59,7 +92,7 @@ try {
5992
"email" => 'testingtester@2co.com',
6093
"phoneNumber" => '555-555-5555'
6194
)
62-
), 'array');
95+
));
6396
$this->assertEquals('APPROVED', $charge['response']['responseCode']);
6497
} catch (Twocheckout_Error $e) {
6598
$this->assertEquals('Unauthorized', $e->getMessage());
@@ -147,9 +180,18 @@ Example Admin API Usage
147180

148181
```php
149182
<?php
150-
Twocheckout::setCredentials("APIuser1817037", "APIpass1817037");
151-
$args = array('sale_id' => 4834917619);
152-
Twocheckout_Sale::stop($args, 'array');
183+
184+
Twocheckout::username('testlibraryapi901248204');
185+
Twocheckout::password('testlibraryapi901248204PASS');
186+
187+
$args = array(
188+
'sale_id' => 4834917619
189+
);
190+
try {
191+
$result = Twocheckout_Sale::stop($args);
192+
} catch (Twocheckout_Error $e) {
193+
$e->getMessage();
194+
}
153195
```
154196

155197
*Example Response:*
@@ -200,11 +242,12 @@ Example Return Usage:
200242

201243
```php
202244
<?php
245+
203246
$params = array();
204247
foreach ($_REQUEST as $k => $v) {
205248
$params[$k] = $v;
206249
}
207-
$passback = Twocheckout_Return::check($params, "tango", 'array');
250+
$passback = Twocheckout_Return::check($params, "tango");
208251
```
209252

210253
*Example Response:*
@@ -223,11 +266,12 @@ Example INS Usage:
223266

224267
```php
225268
<?php
269+
226270
$params = array();
227271
foreach ($_POST as $k => $v) {
228272
$params[$k] = $v;
229273
}
230-
$passback = Twocheckout_Notification::check($params, "tango", 'array');
274+
$passback = Twocheckout_Notification::check($params, "tango");
231275
```
232276

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

251-
Twocheckout::setCredentials("APIuser1817037", "APIpass1817037");
295+
Twocheckout::username('testlibraryapi901248204');
296+
Twocheckout::password('testlibraryapi901248204PASS');
252297

253298
$params = array(
254-
'sale_id' => 4774380224,
255-
'category' => 1,
256-
'comment' => 'Order never sent.'
299+
'sale_id' => 4774380224,
300+
'category' => 1,
301+
'comment' => 'Order never sent.'
257302
);
258303
try {
259-
$sale = Twocheckout_Sale::refund($params, 'array');
304+
$sale = Twocheckout_Sale::refund($params);
260305
} catch (Twocheckout_Error $e) {
261-
$e->getMessage();
306+
$e->getMessage();
262307
}
263308
```
264309

265-
Full documentation for each binding is provided in the [Wiki](https://github.com/2checkout/2checkout-php/wiki).
310+
Full documentation for each binding is provided in the [2Checkout Documentation](https://www.2checkout.com/documentation/libraries/php).

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
"ext-curl": "*",
1818
"ext-json": "*"
1919
},
20+
"require-dev": {
21+
"phpunit/phpunit": "3.7.*"
22+
},
2023
"autoload": {
2124
"psr-0": {
2225
"Twocheckout": "lib/"

lib/Twocheckout.php

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,53 @@
22

33
abstract class Twocheckout
44
{
5-
public static $user;
6-
public static $pass;
7-
public static $format = "json";
8-
public static $apiBaseUrl = "https://www.2checkout.com/api/";
95
public static $sid;
106
public static $privateKey;
11-
public static $apiUrl;
12-
public static $environment;
7+
public static $username;
8+
public static $password;
9+
public static $sandbox;
10+
public static $verifySSL = true;
11+
public static $baseUrl = 'https://www.2checkout.com';
1312
public static $error;
14-
const VERSION = '0.2.1';
15-
16-
static function setCredentials($user, $pass, $mode='', $environment="production")
17-
{
18-
self::$user = $user;
19-
self::$pass = $pass;
20-
if ($mode == 'sandbox') {
21-
self::$apiBaseUrl = 'https://sandbox.2checkout.com/api/';
13+
public static $format = 'array';
14+
const VERSION = '0.3.0';
15+
16+
public static function sellerId($value = null) {
17+
self::$sid = $value;
18+
}
19+
20+
public static function privateKey($value = null) {
21+
self::$privateKey = $value;
22+
}
23+
24+
public static function username($value = null) {
25+
self::$username = $value;
26+
}
27+
28+
public static function password($value = null) {
29+
self::$password = $value;
30+
}
31+
32+
public static function sandbox($value = null) {
33+
if ($value == 1 || $value == true) {
34+
self::$sandbox = true;
35+
self::$baseUrl = 'https://sandbox.2checkout.com';
36+
} else {
37+
self::$sandbox = false;
38+
self::$baseUrl = 'https://www.2checkout.com';
2239
}
23-
self::$environment = $environment;
2440
}
2541

26-
static function setApiCredentials($sid, $privateKey, $mode='', $environment="production")
27-
{
28-
self::$sid = $sid;
29-
self::$privateKey = $privateKey;
30-
if ($mode == 'sandbox') {
31-
self::$apiUrl = 'https://sandbox.2checkout.com/checkout/api/1/'.$sid.'/rs/authService';
42+
public static function verifySSL($value = null) {
43+
if ($value == 0 || $value == false) {
44+
self::$verifySSL = false;
3245
} else {
33-
self::$apiUrl = 'https://www.2checkout.com/checkout/api/1/'.$sid.'/rs/authService';
46+
self::$verifySSL = true;
3447
}
35-
self::$environment = $environment;
48+
}
49+
50+
public static function format($value = null) {
51+
self::$format = $value;
3652
}
3753
}
3854

lib/Twocheckout/Api/TwocheckoutAccount.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,23 @@
33
class Twocheckout_Company extends Twocheckout
44
{
55

6-
public static function retrieve($format='json')
6+
public static function retrieve()
77
{
88
$request = new Twocheckout_Api_Requester();
9-
$urlSuffix = 'acct/detail_company_info';
10-
$result = $request->do_call($urlSuffix);
11-
return Twocheckout_Util::return_resp($result, $format);
9+
$urlSuffix = '/api/acct/detail_company_info';
10+
$result = $request->doCall($urlSuffix);
11+
return Twocheckout_Util::returnResponse($result);
1212
}
1313
}
1414

1515
class Twocheckout_Contact extends Twocheckout
1616
{
1717

18-
public static function retrieve($format='json')
18+
public static function retrieve()
1919
{
2020
$request = new Twocheckout_Api_Requester();
21-
$urlSuffix = 'acct/detail_contact_info';
22-
$result = $request->do_call($urlSuffix);
23-
return Twocheckout_Util::return_resp($result, $format);
21+
$urlSuffix = '/api/acct/detail_contact_info';
22+
$result = $request->doCall($urlSuffix);
23+
return Twocheckout_Util::returnResponse($result);
2424
}
2525
}

lib/Twocheckout/Api/TwocheckoutApi.php

Lines changed: 28 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,65 +2,54 @@
22

33
class Twocheckout_Api_Requester
44
{
5-
public $apiBaseUrl;
5+
public $baseUrl;
66
public $environment;
77
private $user;
88
private $pass;
9+
private $sid;
10+
private $privateKey;
911

1012
function __construct() {
11-
$this->user = Twocheckout::$user;
12-
$this->pass = Twocheckout::$pass;
13-
$this->apiBaseUrl = Twocheckout::$apiBaseUrl;
14-
$this->environment = Twocheckout::$environment;
15-
$this->privateKey = Twocheckout::$privateKey;
16-
$this->apiUrl = Twocheckout::$apiUrl;
13+
$this->user = Twocheckout::$username;
14+
$this->pass = Twocheckout::$password;
15+
$this->sid = Twocheckout::$sid;
16+
$this->baseUrl = Twocheckout::$baseUrl;
17+
$this->verifySSL = Twocheckout::$verifySSL;
18+
$this->privateKey = Twocheckout::$privateKey;
1719
}
1820

19-
function do_call($urlSuffix, $data=array())
21+
function doCall($urlSuffix, $data=array())
2022
{
21-
$url = $this->apiBaseUrl . $urlSuffix;
23+
$url = $this->baseUrl . $urlSuffix;
2224
$ch = curl_init($url);
23-
if ($this->environment === "development") {
24-
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
25-
}
26-
curl_setopt($ch, CURLOPT_HEADER, 0);
27-
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: application/json"));
28-
curl_setopt($ch, CURLOPT_USERAGENT, "2Checkout PHP/0.1.0%s");
29-
curl_setopt($ch, CURLOPT_POST, 0);
30-
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
31-
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
32-
curl_setopt($ch, CURLOPT_USERPWD, "{$this->user}:{$this->pass}");
33-
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
34-
$resp = curl_exec($ch);
35-
curl_close($ch);
36-
if ($resp === FALSE) {
37-
throw new Twocheckout_Error("cURL call failed", "403");
25+
if (isset($data['api'])) {
26+
unset( $data['api'] );
27+
$data['privateKey'] = $this->privateKey;
28+
$data['sellerId'] = $this->sid;
29+
$data = json_encode($data);
30+
$header = array("content-type:application/json","content-length:".strlen($data));
31+
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
3832
} else {
39-
return utf8_encode($resp);
33+
$header = array("Accept: application/json");
34+
curl_setopt($ch, CURLOPT_HEADER, 0);
35+
curl_setopt($ch, CURLOPT_POST, 0);
36+
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
37+
curl_setopt($ch, CURLOPT_USERPWD, "{$this->user}:{$this->pass}");
4038
}
41-
}
42-
43-
function do_auth_call($data)
44-
{
45-
$data['privateKey'] = $this->privateKey;
46-
$data = json_encode($data);
47-
$header = array("content-type:application/JSON","content-length:".strlen($data));
48-
$url = $this->apiUrl;
49-
$ch = curl_init($url);
50-
if ($this->environment === "development") {
39+
if ($this->verifySSL == false) {
5140
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
5241
}
53-
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
54-
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
5542
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
5643
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
44+
curl_setopt($ch, CURLOPT_USERAGENT, "2Checkout PHP/0.1.0%s");
45+
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
5746
$resp = curl_exec($ch);
5847
curl_close($ch);
5948
if ($resp === FALSE) {
6049
throw new Twocheckout_Error("cURL call failed", "403");
6150
} else {
62-
return $resp;
51+
return utf8_encode($resp);
6352
}
64-
}
53+
}
6554

6655
}

0 commit comments

Comments
 (0)