Commit fa1c1f16 authored by Mal's avatar Mal

Added automatic downloads in cart module when user returns from payment page.

Bug fixes including problem with url formatting with fancy url's.
parent 154ed89c
......@@ -35,10 +35,11 @@ class Cart extends Base {
$image = $cart_items["image"];
$full = $cart_items["full"];
if ($image == "" && $full == "") {
$object["content"] = '<i>No full text to display.<i>';
$object["content"] =
'<i>No extra content to display for this item.<i>';
}
else {
$object["content"] = '<img src="'.$image.'">'.$full;
$object["content"] = '<img src="'.$image.'"><br>'.$full;
}
}
$result->close();
......@@ -63,6 +64,8 @@ class Cart extends Base {
$total_price = 0;
$total_weight = 0;
$order_verified = true;
// Initialise the cart session array to store downloadable items.
$_SESSION['cart'] = array();
$us_cart = json_decode($_POST['cart'], true);
foreach ($us_cart as $us_item => $us_values) {
$item = $mysqli->escape_string($us_item);
......@@ -237,11 +240,34 @@ class Cart extends Base {
}
public function Content($id) {
$content = "";
// TODO: There is no checking done on paypal return values, that requires
// supporting PDT and maybe IPN in the future.
if ($_GET["payment"] == "done") {
$content = '<div class="cart-payment-message">'.
'Thankyou for your purchase!</div>';
// Check if there are any items that need downloading.
$count = count($_SESSION['cart']);
if ($count != 0) {
$download = "download";
if ($count > 1) {
$download = "downloads";
}
$content .= '<div class="cart-download-message">'.
'Your '.$download.' will begin shortly.</div>'.
'<ul class="cart-download-list hidden">';
for ($i = 0; $i < $count; $i++) {
$content .= '<li>'.$_SESSION['cart'][$i].'</li>';
}
$content .= '</ul>';
}
}
$mysqli = connect_db();
$query = 'SELECT name, image, short, price, variable, minimum, download '.
'FROM cart_items WHERE user="'.$this->owner.'" AND available="1" '.
'ORDER BY item_order';
$content = '<form id="cart-form">';
$content .= '<form id="cart-form">';
if ($result = $mysqli->query($query)) {
$count = 0;
while ($cart_items = $result->fetch_assoc()) {
......@@ -458,8 +484,8 @@ class Cart extends Base {
$verified = false;
$weight = 0;
$mysqli = connect_db();
$query = 'SELECT weight, price, variable, minimum, available FROM '.
'cart_items WHERE user="'.$this->owner.'" AND name="'.$item.'"';
$query = 'SELECT weight, price, variable, minimum, download, available '.
'FROM cart_items WHERE user="'.$this->owner.'" AND name="'.$item.'"';
if ($result = $mysqli->query($query)) {
if ($cart_items = $result->fetch_assoc()) {
// First make sure the item is available.
......@@ -479,6 +505,12 @@ class Cart extends Base {
}
}
$weight = (float)$cart_items["weight"];
// If the item has been verified and the price is not zero, check if
// this item should be automatically downloaded when the buyer returns
// to the page and therefore stored in the session.
if ($verified && $price != 0 && $cart_items["download"] != "") {
$_SESSION['cart'][] = $cart_items["download"];
}
}
$result->close();
}
......@@ -596,7 +628,7 @@ class Cart extends Base {
"Please check that the transaction has completed, before shipping to:\n".
$address."\n".$city."\n".$postcode."\n".$state."\n".$country;
$headers = 'From: noreply@'.$_SERVER['HTTP_HOST'];
//mail($business, "order received", $message, $headers);
mail($business, "order received", $message, $headers);
// Maximum item_name string to paypal is 127 chars.
if (strlen($items) > 127) {
......@@ -604,9 +636,9 @@ class Cart extends Base {
$items .= "...";
}
$url = "http://".$_SERVER['SERVER_NAME'].$this->Url();
$url = "http://".$_SERVER['SERVER_NAME'].$this->Url("payment=done");
return
'<form action="https://www.paypal.com/us/cgi-bin/webscr" method="post">'.
'<form action="https://www.paypal.com/cgi-bin/webscr" method="post">'.
'<input type="hidden" name="cmd" value="_ext-enter">'.
'<input type="hidden" name="redirect_cmd" value="_xclick">'.
'<input type="hidden" name="business" value="'.$business.'">'.
......
......@@ -85,7 +85,7 @@ class Gift extends Base {
}
else {
$content = '<div class="manage"><a href="'.
$this->Url("&mode=manage").'">Edit your gift registry</a></div>';
$this->Url("mode=manage").'">Edit your gift registry</a></div>';
}
$result->close();
}
......@@ -313,7 +313,7 @@ class Gift extends Base {
$mysqli->close();
$server = $_SERVER['SERVER_NAME'];
$url = $this->Url("&claim_id=".$id."&claim_email=".$email);
$url = $this->Url("claim_id=".$id."&claim_email=".$email);
$body .= "\n\nPrint a copy of your gift here:\n\n".$server.$url.
"\n\nIf you want to change your gift use the following link:\n\n".
$server.$url."&mode=change";
......
......@@ -22,6 +22,7 @@ if (!this.dobrado.cart) {
// Don't run if the module isn't on the page.
if ($(".cart").length === 0) return;
checkDownloads();
$(".cart-item .item-add").button().click(cartAdd);
$(".cart-item .item-quantity").val("0");
$(".cart-item .item-quantity").spinner({ min: 0,
......@@ -65,6 +66,16 @@ if (!this.dobrado.cart) {
}).click(cartEditor);
});
function checkDownloads() {
$(".cart-download-list li").each(function(i) {
// Set the downloads to start at 10 second intervals.
var file = $(this).html();
setTimeout(function() {
location.href = "/php/download.php?file=" + file;
}, i*10000);
});
}
function cartPrice(item) {
var price = 0;
var checkMinimum = false;
......@@ -94,7 +105,7 @@ if (!this.dobrado.cart) {
}
function cartAdd() {
var name = $(this).siblings(".item-name").html();
var name = $(this).siblings(".item-name").children("a").html();
var price = cartPrice($(this).siblings(".item-price"));
// If the price is zero and the item has a download link, display it.
if (price == 0) {
......@@ -130,7 +141,8 @@ if (!this.dobrado.cart) {
function cartQuantity(event, ui) {
// The quantity input is wrapped to create the spinner input,
// so need to go up the dom to find the item's details.
var name = $(this).parents(".cart-item").children(".item-name").html();
var name =
$(this).parents(".cart-item").children(".item-name").children("a").html();
var price =
cartPrice($(this).parents(".cart-item").children(".item-price"));
var quantity = 0;
......
<?php
// Dobrado Content Management System
// Copyright (C) 2012 Malcolm Blaney
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
session_start();
// TODO: At the moment only the public directory is supported.
// Next step is to support uploads to a directory named 'private' that Apache
// will not be allowed to serve from.
$regex = "/^public\/[a-zA-Z0-9_-]{1,200}\.[a-z0-9]{1,10}$/";
$file = $_GET["file"];
if (!preg_match($regex, $file)) return;
// Make sure this file is in the cart.
if (!$_SESSION['cart']) return;
$in_cart = false;
for ($i = 0; $i < count($_SESSION['cart']); $i++) {
if ($_SESSION['cart'][$i] == $file) {
$in_cart = true;
break;
}
}
if (!$in_cart) return;
if ($_SESSION['owner'] != "admin") {
$file = $_SESSION['owner']."/".$file;
}
$file = "../".$file;
if (file_exists($file)) {
header("Cache-Control: private");
header("Content-Type: application/octet");
header("Content-Length: ".filesize($file));
header("Content-Disposition: attachment; filename=".basename($file));
readfile($file);
}
?>
......@@ -291,8 +291,22 @@ abstract class Base {
}
protected function Url($part = "") {
$page = $_SESSION['page'].$part;
$url = $this->config->fancy_url() ? "/".$page : "/index.php?page=".$page;
$url = "";
$page = $_SESSION['page'];
if ($this->config->fancy_url()) {
$url = "/".$page;
if ($part != "") {
$url .= "?".$part;
}
}
else {
$url = "/index.php?page=".$page;
if ($part != "") {
$url .= "&".$part;
}
}
return $this->owner == "admin" ? $url : "/".$this->owner.$url;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment