Ejemplo n.º 1
0
<?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);
}
Ejemplo n.º 2
0
 /**
  * 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();
         }
     }
 }
 /**
  * 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);
 }