From 4f84eb2b09bf51eabdc29b5eeec101e0260b1cb7 Mon Sep 17 00:00:00 2001
From: Camil Staps
Date: Thu, 28 Jul 2016 09:37:48 +0200
Subject: Braintree integration: first version
---
README.md | 6 +++++-
classes/Assignment.php | 8 ++++----
classes/Constants.php | 2 +-
classes/Discount.php | 8 ++++----
classes/Offer.php | 21 ++++++++++++++++++++-
classes/Payment.php | 2 +-
conf.php | 31 +++++++++++++++++++------------
conf.private.example.php | 1 +
css/businessadmin.css | 4 ++++
include/offers-overview.php | 5 ++++-
include/offers.php | 34 ++++++++++++++++++++++++++++------
include/pay.php | 24 +++++++++++-------------
index.php | 39 ++++++++++++++++++++-------------------
install/index.php | 6 +++++-
install/upgrade.php | 18 +++++++++++++++---
15 files changed, 142 insertions(+), 67 deletions(-)
diff --git a/README.md b/README.md
index c57267f..fd06d18 100644
--- a/README.md
+++ b/README.md
@@ -169,6 +169,10 @@ are listed by name and removal time. This way, you never really lose your file.
# Changelog
+### 0.5 (Jul 28, 2016)
+
+0.5 Braintree integration.
+
### 0.4 (Jul 26, 2016)
0.4.2 Moved `offer.payment_received` to a separate table `payments`.
@@ -177,7 +181,7 @@ are listed by name and removal time. This way, you never really lose your file.
### 0.3 (Jul 20, 2016)
-0.3 Discounts
+0.3 Discounts.
### 0.2 (Feb 10, 2015)
diff --git a/classes/Assignment.php b/classes/Assignment.php
index 27efb4a..2ceef94 100644
--- a/classes/Assignment.php
+++ b/classes/Assignment.php
@@ -24,8 +24,8 @@
/**
* An interface to the assignment table in the database
*/
-class Assignment extends Model {
- use Calculatable;
+class Assignment extends Model implements Calculatable {
+ use StandardCalculatable;
public
$table = 'assignment',
@@ -50,11 +50,11 @@ class Assignment extends Model {
return $pd->text($this->description);
}
- protected function calculateSubtotal() {
+ public function calculateSubtotal() {
return $this->hours * $this->price_per_hour;
}
- protected function calculateVAT() {
+ public function calculateVAT() {
return $this->calculateSubtotal() * $this->VAT_percentage / 100;
}
}
diff --git a/classes/Constants.php b/classes/Constants.php
index 3ffadd7..fb2435d 100644
--- a/classes/Constants.php
+++ b/classes/Constants.php
@@ -80,5 +80,5 @@ class Constants {
const password_cost = 10;
/** @const version Version of BusinessAdmin. Don't change this yourself! */
- const version = '0.4.2';
+ const version = '0.5';
}
diff --git a/classes/Discount.php b/classes/Discount.php
index 7216615..4084d43 100644
--- a/classes/Discount.php
+++ b/classes/Discount.php
@@ -24,8 +24,8 @@
/**
* An interface to the discount table in the database
*/
-class Discount extends Model {
- use Calculatable;
+class Discount extends Model implements Calculatable {
+ use StandardCalculatable;
public
$table = 'discount',
@@ -50,11 +50,11 @@ class Discount extends Model {
return $pd->text($this->description);
}
- protected function calculateSubtotal() {
+ public function calculateSubtotal() {
return - $this->value;
}
- protected function calculateVAT() {
+ public function calculateVAT() {
return $this->calculateSubtotal() * $this->VAT_percentage / 100;
}
}
diff --git a/classes/Offer.php b/classes/Offer.php
index 815f626..8fa9ba7 100644
--- a/classes/Offer.php
+++ b/classes/Offer.php
@@ -27,7 +27,7 @@
class Offer extends Model{
public
$table = 'offer',
- $fillable_columns = ['contactId', 'start_date', 'end_date', 'invoice_date', 'accepted', 'invoice_fileId'];
+ $fillable_columns = ['contactId', 'start_date', 'end_date', 'invoice_date', 'accepted', 'invoice_fileId', 'payment_key'];
protected function accessor($key, $value) {
switch ($key) {
@@ -56,6 +56,25 @@ class Offer extends Model{
}
}
+ /**
+ * A random max-63-char string that can be used as payment_key
+ *
+ * @return string The random string
+ */
+ public static function getRandomPaymentKey() {
+ return preg_replace('/[^\w]+/', '',
+ base64_encode(openssl_random_pseudo_bytes(45)));
+ }
+
+ /**
+ * Get whether the offer is eligible for online payment or not
+ *
+ * @return bool True iff it is eligible
+ */
+ public function getPaymentEligibility() {
+ return $this->payment_key != '';
+ }
+
/**
* Get the contact that this offer is linked to
*
diff --git a/classes/Payment.php b/classes/Payment.php
index e60539f..5bc08dc 100644
--- a/classes/Payment.php
+++ b/classes/Payment.php
@@ -27,7 +27,7 @@
class Payment extends Model {
public
$table = 'payment',
- $fillable_columns = ['offerId', 'date'];
+ $fillable_columns = ['offerId', 'date', 'braintree_id'];
/**
* Get the offer that this payment is linked to
diff --git a/conf.php b/conf.php
index e8ed000..d195a07 100644
--- a/conf.php
+++ b/conf.php
@@ -31,20 +31,16 @@ session_start();
error_reporting(0);
ini_set('display_errors', 0);
-/**
- * Autoload a class if it isn't loaded yet
- *
- * This function is automatically called by PHP if a class isn't loaded yet. It shouldn't be used manually.
- *
- * @param string $pClass The name of the class to load
- */
-function __autoload($pClass) {
- require_once("classes/$pClass.php");
-}
+set_include_path(dirname(__FILE__) . PATH_SEPARATOR . get_include_path());
-set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__));
+spl_autoload_register(function ($pClass) {
+ $path = dirname(__FILE__) . "/classes/$pClass.php";
+ if (file_exists($path)) {
+ require_once($path);
+ }
+});
-require_once('./conf.private.php');
+require_once('conf.private.php');
try {
$_pdo = new PDO("mysql:host=".DB_HOST.";port=".DB_PORT.";dbname=".DB_NAME.";charset=utf8", DB_USER, DB_PASS);
@@ -52,3 +48,14 @@ try {
} catch (PDOException $e) {
die("Down until PDO error fixed.");
}
+
+if (BRAINTREE_ENABLED) {
+ require_once('modules/braintree/lib/Braintree.php');
+
+ Braintree_Configuration::environment(BRAINTREE_ENVIRONMENT);
+ Braintree_Configuration::merchantId(BRAINTREE_MERCHANT);
+ Braintree_Configuration::publicKey(BRAINTREE_KEY_PUBLIC);
+ Braintree_Configuration::privateKey(BRAINTREE_KEY_PRIVATE);
+}
+
+require_once('classes/Calculatable.php'); // Some definitions that are required
diff --git a/conf.private.example.php b/conf.private.example.php
index 54f4133..a9275b1 100644
--- a/conf.private.example.php
+++ b/conf.private.example.php
@@ -8,6 +8,7 @@ define('DB_PORT', '3306');
// Braintree settings
define('BRAINTREE_ENABLED', true);
+define('BRAINTREE_ENVIRONMENT', 'sandbox');
define('BRAINTREE_MERCHANT', ...);
define('BRAINTREE_KEY_PUBLIC', ...);
define('BRAINTREE_KEY_PRIVATE', ...);
diff --git a/css/businessadmin.css b/css/businessadmin.css
index e898740..e2c714a 100644
--- a/css/businessadmin.css
+++ b/css/businessadmin.css
@@ -111,3 +111,7 @@ td .btn.btn-circle:last-child {
display: none;
}
}
+
+.payment-panel {
+ margin-top: 5%;
+}
diff --git a/include/offers-overview.php b/include/offers-overview.php
index 1118793..6d9bbc1 100644
--- a/include/offers-overview.php
+++ b/include/offers-overview.php
@@ -95,7 +95,10 @@ require_once('./login.php');
-
+
+
+
+
";
}
diff --git a/include/offers.php b/include/offers.php
index 2aa150d..8be7530 100644
--- a/include/offers.php
+++ b/include/offers.php
@@ -32,11 +32,12 @@ require('./header.php');
//------------------------------------------------------------------------------
// Check for GET variables
//
- // ?id= View information of the offer with id
- // ?toggle_accept= Toggle the accepted status of the offer with id
- // ?generate_invoice= Generate an invoice for the offer with id
- // ?trash_invoice= Trash the invoice file
- // ?delete= Delete the offer with id
+ // ?id= View information of the offer with id
+ // ?toggle_accept= Toggle the accepted status of the offer with id
+ // ?toggle_payment_eligibility= Toggle the payment eligibility of the offer with id
+ // ?generate_invoice= Generate an invoice for the offer with id
+ // ?trash_invoice= Trash the invoice file
+ // ?delete= Delete the offer with id
//------------------------------------------------------------------------------
// The header of the page
@@ -69,7 +70,7 @@ require('./header.php');
try {
$offer = new Offer($_pdo, $id);
$offer->accepted = !$offer->accepted;
- echo "
The status offer #{$offer->id} has been set to ".($offer->accepted ? "accepted" : "unaccepted").".
";
+ echo "
The status of offer #{$offer->id} has been set to ".($offer->accepted ? "accepted" : "unaccepted").".
";
} catch (PDOException $e) {
echo "
The status of the offer could not be changed due to a PDO error.