File: /var/www/vhosts/creativefellows.nl/test.creativefellows.nl/tekenmappen/classes/ShopController.php
<?php
class ShopController extends CassetteController{
function __construct($db_connection,$router=null,$view=null,$settings=null)
{
$this->db = $db_connection;
$this->router = $router;
$this->view = $view;
$this->settings = $settings;
$this->client_settings = $this->getClientSettings();
$this->config = new BaseController($settings,$this->client_settings);
}
/*
* Add product to the session
*/
public function setBasket($request, $response, $args)
{
// get data
$post = $request->getParsedBody();
$this->cart = new CartController("cart",$post["uid"]);
$this->cart->set("pid",$post["pid"]);
$this->cart->set("uid",$post["uid"]);
$this->cart->set("amount",$post["amount"]);
$this->cart->set("article",$post["article"]);
$this->cart->set("size",$post["size"]);
$this->cart->set("type",$post["type"]);
$this->cart->set("material",$post["article_material"]);
$this->cart->set("price",$post["price"]);
$this->cart->set("img",$post["article_img"]);
$this->getBasket($request, $response, $args);
}
/*
* View shopping basket
*/
public function getBasket($request, $response, $args)
{
// get url parameters
$this->section_name = isset($args["name"]) ? $args["name"] : 0;
$this->category_name = isset($args["category"]) ? $args["category"] : 0;
$this->entry_name = isset($args["entry"]) ? $args["entry"] : 0;
$this->get_uid = isset($args["uid"]) ? $args["uid"] : 0;
// get topbar navigation
$navigation = $this->getMainNavigation(true,$this->section_name);
$this->cart = new CartController("cart");
$mollie = new \Mollie\Api\MollieApiClient();
$mollie->setApiKey($this->config->getMollieApiKey());
//$this->session->output();
// return the page
return $this->view->render($response, "shop/view.basket.php", array(
"cassette" => $this,
"config" => $this->config,
"router" => $this->router,
"page" => $page,
"navigation" => $navigation,
"section_name" => $this->section_name,
"cart" => $this->cart,
"payments_methods" => $mollie->methods->all()
));
}
/*
* Remove a product from basket
*/
public function removeBasket($request, $response, $args)
{
// get data
$post = $request->getParsedBody();
// access the cart
$this->cart = new CartController("cart");
// unset product
$this->cart->unset($post["uid"]);
// return order totals
$this->returnTotals($request, $response, $args);
}
/*
* Let the user confirm its input
*/
public function confirmBasket($request, $response, $args)
{
// get data
$post = $request->getParsedBody();
// user date to session
$this->setPostToSession($post);
// get url parameters
$this->section_name = isset($args["name"]) ? $args["name"] : 0;
$this->category_name = isset($args["category"]) ? $args["category"] : 0;
$this->entry_name = isset($args["entry"]) ? $args["entry"] : 0;
$this->get_uid = isset($args["uid"]) ? $args["uid"] : 0;
// get topbar navigation
$navigation = $this->getMainNavigation(true,$this->section_name);
$this->cart = new CartController("cart");
$mollie = new \Mollie\Api\MollieApiClient();
$mollie->setApiKey($this->config->getMollieApiKey());
// return the page
return $this->view->render($response, "shop/view.confirm.php", array(
"cassette" => $this,
"config" => $this->config,
"router" => $this->router,
"page" => $page,
"navigation" => $navigation,
"section_name" => $this->section_name,
"cart" => $this->cart,
"payments_methods" => $mollie->methods->all()
));
}
/*
* Insert order in database
*/
public function insertData($request, $response, $args)
{
// create new cliet
$client = new ClientController($this->db);
$client->create();
$client_id = $client->getClientId();
// current cart product
$cart = new CartController("cart");
$cart->setOrderTotals( $this->config->getDeliveryCosts(), $this->config->getGlobalTax() );
// session user data
$user = new CartController("user");
$payment_method = $user->getSingle("betaalmethode");
// insert order and products
$order = new OrderController($this->db,$this->client_settings);
$invoice_number = $order->getInvoiceNumber();
$order->create(
$client_id,
$invoice_number,
$cart->getSubtotal(),
$cart->getTaxTotal(),
$cart->getDelivery(),
$user->getSingle("betaalmethode"),
$user->getSingle("opmerkingen"),
$cart
);
// create order email
$order->setEmail($client,$cart);
// request payment url
$this->requestPayment($cart,$order,$payment_method);
}
/*
* Requst a payment url for mollie
*/
private function requestPayment($cart,$order,$payment_method)
{
$mollie = new \Mollie\Api\MollieApiClient();
$mollie->setApiKey($this->config->getMollieApiKey());
/*
* Determine the url parts
*/
$protocol = isset($_SERVER['HTTPS']) && strcasecmp('off', $_SERVER['HTTPS']) !== 0 ? "https" : "http";
$hostname = $_SERVER['HTTP_HOST'];
$path = dirname(isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : $_SERVER['PHP_SELF']);
//die("{$protocol}://{$hostname}". $this->router->pathFor('cassette.verifyPayment'));
$payment = $mollie->payments->create(array(
"amount" => [
"currency" => "EUR",
"value" => $cart->getTotal()
],
"method" => $payment_method == 0 ? "ideal" : "banktransfer",
"description" => $order->getInvoiceId(),
"redirectUrl" => "{$protocol}://{$hostname}{$path}/4?order_id=". $order->getInvoiceId() ."&hash=". $this->config->getHash( $order->getInvoiceId() ),
"webhookUrl" => ($this->config->mollieIsLive() == true) ? "{$protocol}://{$hostname}". $this->router->pathFor('cassette.verifyPayment') : "",
"metadata" => array(
"order_id" => $order->getBestellingId()
)
));
/*
* Update DB with transaction_id
*/
$order->updateTransactionId($payment->id,$order->getBestellingId());
/*
* Send the customer off to complete the payment.
*/
header("Location: " . $payment->getCheckoutUrl(), true, 303);
}
/*
* Page to return to after Mollie Payment
*/
public function paymentReturn($request, $response, $args)
{
// get data
$hash = $request->getQueryParam('hash');
$order_id = $request->getQueryParam('order_id');
// check if hash is ok
if($hash !== $this->config->getHash($order_id)) return $this->get404Page($request, $response, $args);
// insert order and products
$order = new OrderController($this->db,$this->client_settings);
// get user
$order->setOrderById($order_id);
// get client
$client = new ClientController($this->db);
$client_id = $client->setClientById( $order->getClientId() );
// Clear current session
$cart = new CartController("cart");
//$cart->clearAll("cart");
//$cart->clearAll("user");
//
// get url parameters
$this->section_name = isset($args["name"]) ? $args["name"] : 0;
$this->category_name = isset($args["category"]) ? $args["category"] : 0;
$this->entry_name = isset($args["entry"]) ? $args["entry"] : 0;
$this->get_uid = isset($args["uid"]) ? $args["uid"] : 0;
// get topbar navigation
$navigation = $this->getMainNavigation(true,$this->section_name);
// return the page
return $this->view->render($response, "shop/view.return.php", array(
"cassette" => $this,
"config" => $this->config,
"router" => $this->router,
"page" => $page,
"navigation" => $navigation,
"section_name" => $this->section_name,
"cart" => $cart,
"client" => $client,
"order_id" => $order_id,
"order_status" => $order->getStatus()
));
}
/*
* Verify a payment
*/
public function verifyPayment($request, $response, $args)
{
// post data
$post = $request->getParsedBody();
// mollie object
$mollie = new \Mollie\Api\MollieApiClient();
$mollie->setApiKey($this->config->getMollieApiKey());
$payment = $mollie->payments->get($post["id"]);
$order_id = $payment->metadata->order_id;
// insert order and products
$order = new OrderController($this->db,$this->client_settings);
$order->setOrder($order_id);
if($payment->isPaid() == TRUE)
{
// update payment status to payed
$order->updatePaymentStatus(1);
// Set client
$client = new ClientController($this->db);
$client_id = $client->setClientById( $order->getClientId() );
// send copy to client
$mail = new MailController($this->client_settings);
$mail->sendMail(
$client->getEmail(),
"Bestelling ". $this->getClientSetting("bedrijfsnaam") ." - ".$order->getOrderId(),
$order->getHTML(),
true
);
return $response->withStatus(200);
}
elseif($payment->isOpen() == FALSE)
{
/*
* Cancel order
*/
$order->setOrderStatus(3);
}
return $response->withStatus(200);
}
/*
* Set client data in session
*/
private function setPostToSession($post)
{
// access the cart
$this->cart = new CartController("user");
foreach($post as $par => $value)
{
$this->cart->setSingle($par,$value);
}
}
/*
* Update a product property in current session
*/
public function patchProductBasket($request, $response, $args)
{
// get data
$post = $request->getParsedBody();
// access the cart
$this->cart = new CartController("cart",$post["uid"]);
// unset product
$this->cart->set("amount",$post["amount"]);
// return order totals
$this->returnTotals($request, $response, $args);
}
/*
* Get order totals
*/
private function returnTotals($request, $response, $args)
{
// return totals
$totals = $this->cart->getOrderTotals( $this->config->getDeliveryCosts(), $this->config->getGlobalTax(),true);
// return data
return $response->withHeader('Content-Type', 'application/json')->withStatus(200)->write( json_encode($totals) );
}
}
?>