Exemple #1
0
// Require this for curl class
require_once $CFG->libdir . '/filelib.php';
/// Keep out casual intruders
if (empty($_POST) or !empty($_GET)) {
    print_error("Sorry, you can not use the script that way.");
}
/// Read all the data from PayPal and get it ready for later;
/// we expect only valid UTF-8 encoding, it is the responsibility
/// of user to set it up properly in PayPal business account,
/// it is documented in docs wiki.
$req = 'cmd=_notify-validate';
$data = new stdClass();
foreach ($_POST as $key => $value) {
    $req .= "&{$key}=" . urlencode($value);
    $data->{$key} = $value;
}
// GET THE PAYPAL GATEWAY TO BE USED
// THE CUSTOM FIELD IS THE TRANSACTION ID
$gateway = new MoodecGatewayPaypal((int) $data->custom);
// CONFIRM NOTIFICATION WITH PAYPAL
$c = new curl();
$options = array('returntransfer' => true, 'httpheader' => array('application/x-www-form-urlencoded', "Host: www.paypal.com"), 'timeout' => 30, 'CURLOPT_HTTP_VERSION' => CURL_HTTP_VERSION_1_1);
$location = $gateway->get_url();
$result = $c->post($location, $req, $options);
// Read the response from Paypal
if (0 < strlen($result) && strcmp($result, "VERIFIED") == 0) {
    // If we are here, it means the payment was a valid paypal transaction
    // So now we get the gateway to validate and handle the transaction info
    $gateway->handle($data);
}
exit;
 /**
  * Returns the HTML for the Moodec cart review on the checkout page
  * @param 	array 		cart
  * @param 	bool 		is it the checkout page
  * @return 	string 		the HTML output
  */
 function cart_review($cart, $removedProducts = array())
 {
     global $CFG, $USER;
     // Require Moodec lib
     require_once $CFG->dirroot . '/local/moodec/lib.php';
     // OPEN cart-overview
     $html = '<div class="cart-overview">';
     // Output cart review message
     $html .= sprintf('<p class="cart-review__message">%s</p>', get_string('checkout_message', 'local_moodec'));
     if (!!$removedProducts && is_array($removedProducts)) {
         $html .= '<div class="cart-review__wrapper">';
         $html .= sprintf('<p class="cart-review__message--removed">%s</p>', get_string('checkout_removed_courses_label', 'local_moodec'));
         $html .= '<ul>';
         foreach ($removedProducts as $p) {
             $thisProduct = local_moodec_get_product($p);
             $html .= sprintf('<li class="cart-review__item--removed">%s</li>', $thisProduct->get_fullname());
         }
         $html .= '</ul></div>';
     }
     if ($cart->is_empty() === false) {
         $html .= '<ul class="products">';
         // Go through each product in the cart
         foreach ($cart->get() as $pID => $vID) {
             $product = local_moodec_get_product($pID);
             $html .= '<li class="product-item">';
             // Product title and variation
             $html .= sprintf('<h4 class="product-title"><a href="%s">%s</a></h4>', new moodle_url($CFG->wwwroot . '/local/moodec/pages/product.php', array('id' => $product->get_id())), $product->get_type() === PRODUCT_TYPE_SIMPLE ? $product->get_fullname() : $product->get_fullname() . ' - ' . $product->get_variation($vID)->get_name());
             // Product price
             $html .= sprintf('<div class="product-price">%s%.02f</div>', local_moodec_get_currency_symbol(get_config('local_moodec', 'currency')), $product->get_type() === PRODUCT_TYPE_SIMPLE ? $product->get_price() : $product->get_variation($vID)->get_price());
             $html .= '</li>';
         }
         $html .= '</ul>';
         // Output cart summary section
         $html .= '<div class="cart-summary">';
         // Cart total price
         $html .= sprintf('<h3 class="cart-total__label">%s</h3><h3 class="cart-total">%s%01.2f</h3>', get_string('cart_total', 'local_moodec'), local_moodec_get_currency_symbol(get_config('local_moodec', 'currency')), $cart->get_total(false));
         $html .= '</div>';
         // Return to store button
         $html .= $this->return_to_store_action();
         // Render all active Gateway options
         if (!!get_config('local_moodec', 'payment_dps_enable')) {
             $gatewayDPS = new MoodecGatewayDPS($cart->get_transaction_id());
             $html .= $gatewayDPS->render();
         }
         if (!!get_config('local_moodec', 'payment_paypal_enable')) {
             $gatewayPaypal = new MoodecGatewayPaypal($cart->get_transaction_id());
             $html .= $gatewayPaypal->render();
         }
     } else {
         // Empty cart message
         $html .= sprintf('<p class="cart-mesage--empty">%s</p>', get_string('cart_empty_message', 'local_moodec'));
         // Return to store button
         $html .= $this->return_to_store_action();
     }
     // CLOSE cart-overview
     $html .= '</div>';
     return $html;
 }