/** * Sends an email notification with the details of the transaction error * @param string $subject email subject line * @param array $data transaction data * @return void */ protected function send_error_to_admin($subject, $data = array()) { global $CFG; require_once $CFG->libdir . '/eventslib.php'; $admin = get_admin(); $site = get_site(); $message = sprintf("%s: Transaction #%d failed. %s \n\n", $site->fullname, $this->_transaction->get_id(), $subject); foreach ($data as $key => $value) { $message .= "{$key} => {$value}\n"; } // Update the transaction with the error message details $this->_transaction->set_error($message); $eventdata = new stdClass(); $eventdata->component = 'local_moodec'; $eventdata->name = 'payment_gateway'; $eventdata->userfrom = $admin; $eventdata->userto = $admin; $eventdata->subject = $this->_gatewayName . " ERROR: " . $subject; $eventdata->fullmessage = $message; $eventdata->fullmessageformat = FORMAT_PLAIN; $eventdata->fullmessagehtml = ''; $eventdata->smallmessage = ''; message_send($eventdata); }
/** * Constructor will check if any SESSION or COOKIE exists and will load that data if so. * Otherwise, an empty cart is initialised */ function __construct() { // By default, the cart is empty $this->_products = array(); $this->_cartTotal = 0; $this->_transactionId = null; $sessionTime = 0; $sessionProducts = array(); $sessionCartTotal = 0; $sessionVersion = ''; $sessionData = array(); $cookieTime = 0; $cookieProducts = array(); $cookieCartTotal = 0; $cookieVersion = ''; $cookieData = array(); // SESSION is preferred over the COOKIE if (isset($_SESSION[self::STORAGE_ID])) { list($sessionVersion, $sessionData) = unserialize($_SESSION[self::STORAGE_ID]); // we check if the version is current, before setting extracting the data // this way if it's old, we won't throw an error for a missing field if ($sessionVersion === self::CART_VERSION) { list($sessionProducts, $sessionCartTotal, $sessionTransactionId, $sessionTime) = $sessionData; } else { // if the session is not the current cart version, we destroy it unset($_SESSION[self::STORAGE_ID]); } } // Now we do the same for the cookie if (isset($_COOKIE[self::STORAGE_ID])) { list($cookieVersion, $cookieData) = unserialize($_COOKIE[self::STORAGE_ID]); if ($sessionVersion === self::CART_VERSION) { // Get cart from the cookies list($cookieProducts, $cookieCartTotal, $cookieTransactionId, $cookieTime) = $cookieData; } else { // if the cookie is not the current cart version, we destroy it unset($_COOKIE[self::STORAGE_ID]); } } // Check which is newer; the session, or the cookie if ($cookieTime < $sessionTime) { // If session is newer, then we set the cart properties from the session $this->_products = $sessionProducts; $this->_cartTotal = $sessionCartTotal; $this->_transactionId = $sessionTransactionId; $this->_lastUpdated = $sessionTime; } else { if (!!$cookieTime) { // otherwise, we set the cart to the cookie vars $this->_products = $cookieProducts; $this->_cartTotal = $cookieCartTotal; $this->_transactionId = $cookieTransactionId; $this->_lastUpdated = $cookieTime; } } // We run through all the products in the cart and load the info foreach ($this->_products as $pID => $vID) { $newProduct = local_moodec_get_product($pID); // If any product is variable, but the variation ID is stored as 0 // That means the product WAS simple, but has been changed. // Therefore we clear the cart, as this could cause errors. if ($newProduct->get_type() === PRODUCT_TYPE_VARIABLE && $vID === 0) { $this->clear(); break; } } // When we create the cart, check if there is a transaction associated with it, // and if there is, check if it is complete. // If the transaction is complete, then this cart has been purchased, // so we need to clear it. if (!!$this->get_transaction_id()) { try { $transaction = new MoodecTransaction($this->get_transaction_id()); if ($transaction->get_status() === MoodecTransaction::STATUS_COMPLETE) { $this->clear(); } } catch (Exception $e) { // If there is an exception, then we want to clear the transaction // so we will be given a new one $this->_transactionId = null; $this->clear(); } } }
<?php /** * Moodec DPS Gateway * * @package local * @subpackage local_moodec * @author Thomas Threadgold * @copyright 2015 LearningWorks Ltd * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ require_once dirname(__FILE__) . '/../../../../config.php'; require_once $CFG->dirroot . '/local/moodec/lib.php'; $transactionID = required_param('id', PARAM_INT); // plugin instance id require_login(); $transaction = new MoodecTransaction($transactionID); // If the transaction is already completed, we do not want to do it again if ($transaction->get_status() === MoodecTransaction::STATUS_COMPLETE) { redirect(new moodle_url($CFG->wwwroot . '/local/moodec/pages/cart.php')); } $gateway = new MoodecGatewayDPS($transaction); $response = $gateway->begin(); // abort if DPS returns an invalid response if ($response->attributes()->valid != '1') { print_error('error_dpsinitiate', 'local_moodec'); } else { // otherwise, redirect to the DPS provided URI redirect($response->URI); }
$cart = new MoodecCart(); if ($cart->is_empty()) { redirect(new moodle_url($CFG->wwwroot . '/local/moodec/pages/cart.php')); } // Check if the products in the cart are valid, store the ones that are not // (so we can notify the user they've been removed) $removedProducts = $cart->refresh(); // Check if a transaction has already been made for this cart if (!!$cart->get_transaction_id()) { // Get the existing transaction if the cart has recorded one $transaction = new MoodecTransaction($cart->get_transaction_id()); // We reset the transaction, in case the items in the cart have changed $transaction->reset(); } else { // Otherwise create a new transaction $transaction = new MoodecTransaction(); } // Set the transactionId in the cart to that of the transaction // We do this in case the transaction reset created a new transaction // Or, to add the transaction id to the cart if one didn't already exist $cart->set_transaction_id($transaction->get_id()); // We need to add all the products in the cart to the transaction foreach ($cart->get() as $pID => $vID) { // Get the product in the cart $product = local_moodec_get_product($pID); // Add the product to the transaction, relative to variation if ($product->get_type() === PRODUCT_TYPE_VARIABLE) { $transaction->add($pID, $product->get_variation($vID)->get_price(), $vID); } else { $transaction->add($pID, $product->get_price()); }
/** * This function is called for each data row to allow processing of the * purchase_date value. * * @param object $values Contains object with all the values of record. * @return $string Return status formatted like as per the transaction class */ function col_status($values) { $t = new MoodecTransaction((int) $values->id); return $t->get_status(true); }
require_once $CFG->dirroot . '/local/moodec/lib.php'; $params = array(); // Get the ID of the user to be displayed $transactionID = optional_param('id', 0, PARAM_INT); if (!!$transactionID) { $params['id'] = $transactionID; } $context = context_system::instance(); // Set PAGE variables $PAGE->set_context($context); $PAGE->set_url($CFG->wwwroot . '/local/moodec/pages/transaction/view.php', $params); $PAGE->navbar->add(get_string('transactions_title', 'local_moodec'), new moodle_url($CFG->wwwroot . '/local/moodec/pages/transaction/index.php')); $PAGE->navbar->add(get_string('transaction_view_title', 'local_moodec', array('id' => $transactionID))); // Force the user to login/create an account to access this page require_login(); $transaction = new MoodecTransaction($transactionID); if ((int) $USER->id !== $transaction->get_user_id()) { require_capability('local/moodec:viewalltransactions', $context); } // Check if the theme has a moodec pagelayout defined, otherwise use standard if (array_key_exists('moodec_transaction_view', $PAGE->theme->layouts)) { $PAGE->set_pagelayout('moodec_transaction_view'); } else { if (array_key_exists('moodec', $PAGE->theme->layouts)) { $PAGE->set_pagelayout('moodec'); } else { $PAGE->set_pagelayout('standard'); } } // Get the renderer for this page $renderer = $PAGE->get_renderer('local_moodec');