Skip to content

Commit 39c431c

Browse files
committed
Corrected cart session for adding multiple items, cleaned up code
1 parent cdedc88 commit 39c431c

File tree

4 files changed

+49
-56
lines changed

4 files changed

+49
-56
lines changed

examples/cart/Cart.php

Lines changed: 34 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,19 @@
33
* Checkfront Sample Code: Browse inventory & create booking.
44
*
55
* This is sample code is for demonstration only and should not be used in production
6-
* without modifcation. It does not adequtly secure your OAuth tokens.
6+
* without modifcation. It does not adequtly secure your OAuth tokens.
77
*
8-
* see:
9-
*
10-
* API Documenation: http://www.checkfront.com/developers/api/
11-
* API Error Codes: http://www.checkfront.com/developers/api-error
12-
* PHP SDK - https://github.com/Checkfront/PHP-SDK
13-
* CQL Documenation: http://www.checkfront.com/developers/api-cql/
8+
* see:
9+
*
10+
* API Documenation: http://api.checkfront.com/
11+
* PHP SDK - https://github.com/htmlgraphic/Checkfront-PHP-SDK
1412
*
1513
*/
1614

1715
/*
1816
* @access public
1917
* @package Checkfront
2018
*/
21-
2219
ini_set('session.hash_bits_per_character', 5);
2320
include('../../lib/Checkfront.php');
2421

@@ -30,12 +27,12 @@ public function __construct($data) {
3027
parent::__construct($data,session_id());
3128
}
3229

33-
/* DUMMY Data store. This sample stores oauth tokens in a text file...
34-
* This is NOT reccomened in production. Ideally, this would be in an encryped
35-
* database or other secure source. Never expose the client_secret or access / refresh
30+
/* DUMMY Data store. This sample stores oauth tokens in a text file...
31+
* This is NOT reccomened in production. Ideally, this would be in an encryped
32+
* database or other secure source. Never expose the client_secret or access / refresh
3633
* tokens.
3734
*
38-
* store() is called from the parent::CheckfrontAPI when fetching or setting access tokens.
35+
* store() is called from the parent::Checkfront when fetching or setting access tokens.
3936
*
4037
* When an array is passed, the store should save and return the access tokens.
4138
* When no array is passed, it should fetch and return the stored access tokens.
@@ -54,44 +51,40 @@ final protected function store($data=array()) {
5451
}
5552

5653
public function session($session_id,$data=array()) {
57-
session_id($session_id);
58-
if(!empty($data)) $_SESSION = $data;
54+
session_id($session_id);
55+
if(!empty($data)) $_SESSION = $data;
5956
}
6057
}
6158

62-
/*
63-
* You need to create a new application in your Checkfront Account under
64-
Manage / Extend / Api and supply the details below.
59+
/*
60+
You need to create a new application in your Checkfront Account under
61+
Manage / Developer / Api and supply the details below.
6562
66-
This example bybasses the oauth authorization redirect by supplying "oob"
67-
(Out Of Bounds) as the redirect_uri, and by generating the access and
68-
refresh tokens from within Checkfront.
63+
This example bybasses the oauth authorization redirect by supplying "oob"
64+
(Out Of Bounds) as the redirect_uri, and by generating the access and
65+
refresh tokens from within Checkfront.
6966
70-
For more infomration on your endpoints see:
71-
http://www.checkfront.com/developers/api/#endpoints
67+
For more infomration on your endpoints see:
68+
http://api.checkfront.com/ref/index.html
7269
*/
7370

7471
// a general class that wraps the api along with some custom calls
7572
class Booking {
76-
7773
public $cart = array();
78-
public $session = array();
7974

8075
function __construct() {
8176
// apply a session_id to the request if one is specified
82-
if (!empty($_GET['cart_id'])) session_id($_GET['cart_id']);
77+
if (!empty($_GET['cart_id'])) { session_id($_GET['cart_id']); }
8378
session_start();
79+
8480
// create api connection to Checkfront
85-
// you can generate a token pair under Manage / Developer in your account
86-
$this->Checkfront = new Checkfront(
81+
$this->Checkfront = new CheckfrontAPI(
8782
array(
88-
'host' => 'your-company.checkfront.com',
89-
'auth_type' => 'token',
90-
'api_key' => '',
91-
'api_secret' => '',
92-
'account_id' => 'off',
93-
)
94-
);
83+
'host' => getenv('HOST'),
84+
'api_key' => getenv('CONSUMER_KEY'),
85+
'api_secret' => getenv('CONSUMER_SECRET'),
86+
'auth_type' => 'token'
87+
));
9588

9689
// init shopping cart
9790
$this->cart();
@@ -118,16 +111,14 @@ public function form() {
118111

119112
// get cart session
120113
public function cart() {
121-
if(!empty($_SESSION)) {
122-
$response = $this->Checkfront->get('booking/session');
123-
if(!empty($response['booking']['session']['item'])) {
124-
foreach($response['booking']['session']['item'] as $line_id => $data) {
125-
// store for later
126-
$this->cart[$line_id] = $data;
127-
}
114+
$response = $this->Checkfront->get('booking/session');
115+
if(!empty($response['booking']['session']['item'])) {
116+
foreach($response['booking']['session']['item'] as $line_id => $data) {
117+
// store for later
118+
$this->cart[$line_id] = $data;
128119
}
129-
$this->Checkfront->set_session($response['booking']['session']['id'], $response['booking']['session']);
130120
}
121+
$this->Checkfront->set_session($response['booking']['session']['id'], $response['booking']['session']);
131122
}
132123

133124
// create a booking using the session and the posted form fields
@@ -142,5 +133,5 @@ public function create($form) {
142133
public function clear() {
143134
$response = $this->Checkfront->get('booking/session/clear');
144135
session_destroy();
145-
}
136+
}
146137
}

examples/cart/create.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
$response = $Booking->create($_POST);
1111

1212
if($response['request']['status'] == 'OK') {
13-
// successful transactions will return a url to be redirected to for payment or an invoice.
13+
// Successful transactions will return a url redirected to
14+
// for payment or an invoice. Update to a public URL to view receipt.
1415
header("Location: {$response['request']['data']['url']}");
1516
exit;
1617

@@ -32,7 +33,7 @@
3233
</style>
3334
</head>
3435
<body>
35-
<h1>Checkfront Shopping Cart Demo</h1>
36+
<h1>Reservations Booking Demo</h1>
3637
<form method="post" action="<?php echo $_SERVER['SCRIPT_NAME']?>">
3738
<fieldset>
3839
<?php

examples/cart/index.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
<style style="text/css">body { font:90% "Helvetica Neue",Helvetica,Arial,sans-serif; }</style>
2929
</head>
3030
<body>
31-
<h1>Checkfront Shopping Cart Demo</h1>
32-
<p>This is a bare bones example of how to query inventory items from the Checkfront API, add them to a booking session, and create a new booking. This is a shopping cart style demo that allows you to add and remove multiple items to a booking before proceeding.</p>
31+
<h1>Reservations Booking Demo</h1>
32+
<p>This is a bare bones example of how to query inventory items from the Checkfront API, add them to a booking session, and create a new booking. This is a shopping cart style demo that allows you to add and remove multiple items to a booking before proceeding.</p>
3333
<!-- it may be preferred to set this in your local session -->
3434
<div style="width: 500px; float: left;">
3535
<h3>Available Items</h3>
@@ -44,8 +44,7 @@
4444
'end_date'=>$date,
4545
// change these booking parameters to suit your setup:
4646
'param'=>array( 'guests' => 1 )
47-
)
48-
);
47+
));
4948

5049
if(count($items)) {
5150
$c = 0;
@@ -58,7 +57,7 @@
5857
echo "&nbsp; <small style='color: #999;'>SLIP: {$item['rate']['slip']}</small><br />";
5958
echo '</li>';
6059
// Let's only show 5 for the sake of the demo;
61-
if($c++ == 5) break;
60+
if($c++ == 20) break;
6261
}
6362
}
6463
?>
@@ -76,15 +75,17 @@
7675
foreach($Booking->cart as $line_id => $item) {
7776
echo "<li style='padding: 5px'><strong>{$item['name']}</strong> ({$item['rate']['qty']})<br />";
7877
echo $item['rate']['total'];
79-
if($item['date']['summary']) echo '<br /><span style="font-size: .9em; color: #444">' . $item['date']['summary'] . '</span>';
78+
if($item['date']['summary']) {
79+
echo '<br /><span style="font-size: .9em; color: #444">' . $item['date']['summary'] . '</span>';
80+
}
8081
echo "</li>";
8182
}
82-
echo "<li><span style='font-family:monospace;font-size: .9em; color: #333'>Sub-total: {$_SESSION['sub_total']}<br/>";
83+
echo "<li><span style='font-family:monospace;font-size: .9em; color: #333'>Sub-total: {$_SESSION['sub_total']}<br/>";
8384
echo "Tax: {$_SESSION['tax_total']}<br/>";
8485
echo "Total: {$_SESSION['total']}</span></li>";
8586
echo '<li><input type="submit" name="create" value=" Book Now " /></li>';
8687
echo '<li><input type="button" name="cancel" value=" Clear All " onClick="window.location=\'' . $_SERVER['SCRIPT_NAME'] . '?reset=1\';" /></li>';
87-
88+
8889
} else {
8990
echo '<p>EMPTY</p>';
9091
}

lib/Checkfront.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class Checkfront {
5050
public $error = array();
5151
private $api_timeout = '30';
5252

53-
// change to the name of your app / integration (shows in log and invoice footer). maxlength=15
53+
// change to the name of your app / integration (shows in log and invoice footer). maxlength=15
5454
private $app_id = 'API';
5555

5656
// checkfront host
@@ -76,13 +76,13 @@ class Checkfront {
7676
private $account_id;
7777

7878

79-
// over-ride the ip address of the API call. Set this if you wish to track
79+
// over-ride the ip address of the API call. Set this if you wish to track
8080
// another API when creating or updating a booking.
8181
private $client_ip;
8282
private $session_id;
8383

8484

85-
// use to store API keys, ideally via an extended class. Not used for token pair auth
85+
// use to store API keys, ideally via an extended class. Not used for token pair auth
8686
protected function store($data=array()) {
8787
}
8888

0 commit comments

Comments
 (0)