Example #1
1
 static function enroll($is_manager)
 {
     global $wpdb, $user_ID, $user_email;
     $_course = new NamasteLMSCourseModel();
     $message = '';
     // enroll in course
     $course = $_course->select($_POST['course_id']);
     // course fee?
     $fee = get_post_meta($course->ID, 'namaste_fee', true);
     // When fee is paid, enrollment is automatic so this is just fine here
     if (!empty($fee)) {
         $fee = apply_filters('namaste-coupon-applied', $fee);
     }
     // coupon code from other plugin?
     if ($fee > 0 and !$is_manager) {
         wp_die("You can't enroll yourself in a course when there is a fee");
     }
     $enroll_mode = get_post_meta($course->ID, 'namaste_enroll_mode', true);
     // if already enrolled, just skip this altogether
     if (!NamasteLMSStudentModel::is_enrolled($user_ID, $course->ID)) {
         // depending on mode, status will be either 'pending' or 'enrolled'
         $status = $enroll_mode == 'free' ? 'enrolled' : 'pending';
         $_course->enroll($user_ID, $course->ID, $status);
         if ($enroll_mode == 'free') {
             $message = sprintf(__('You enrolled in "%s"', 'namaste'), $course->post_title);
         } else {
             $message = __('Thank you for your interest in enrolling this course. A manager will review your application.', 'namaste');
         }
     } else {
         $message = __('You have already enrolled in this course', 'namaste');
     }
     return $message;
 }
Example #2
0
 static function pay($currency)
 {
     global $wpdb, $user_ID, $user_email;
     $_course = new NamasteLMSCourseModel();
     $token = $_POST['stripeToken'];
     $course = get_post($_POST['course_id']);
     $fee = get_post_meta($course->ID, 'namaste_fee', true);
     $fee = apply_filters('namaste-coupon-applied', $fee);
     // coupon code from other plugin?
     try {
         $customer = Stripe_Customer::create(array('email' => $user_email, 'card' => $token));
         $charge = Stripe_Charge::create(array('customer' => $customer->id, 'amount' => $fee * 100, 'currency' => $currency));
     } catch (Exception $e) {
         wp_die($e->getMessage());
     }
     // !!!!in the next version avoid this copy-paste
     // almost the same code is in models/payment.php for the paypal payments
     $wpdb->query($wpdb->prepare("INSERT INTO " . NAMASTE_PAYMENTS . " SET \n\t\t\t\t\t\tcourse_id=%d, user_id=%s, date=CURDATE(), amount=%s, status='completed', paycode=%s, paytype='stripe'", $_POST['course_id'], $user_ID, $fee, $token));
     do_action('namaste-paid', $user_ID, $fee, "course", $_POST['course_id']);
     // enroll accordingly to course settings - this will be placed in a method once we
     // have more payment options
     $enroll_mode = get_post_meta($course->ID, 'namaste_enroll_mode', true);
     if (!NamasteLMSStudentModel::is_enrolled($user_ID, $course->ID)) {
         $status = $enroll_mode == 'free' ? 'enrolled' : 'pending';
         $_course->enroll($user_ID, $course->ID, $status);
     }
 }
 private static function enroll($userId, $courseId)
 {
     $_course = new NamasteLMSCourseModel();
     // enroll in course
     $course = $_course->select($courseId);
     $enroll_mode = get_post_meta($course->ID, 'namaste_enroll_mode', true);
     // if already enrolled, just skip this altogether
     $_course->enroll($userId, $course->ID, 'enrolled');
 }
Example #4
0
 static function paypal_ipn($wp)
 {
     global $wpdb;
     echo "<!-- NAMASTECOMMENT paypal IPN -->";
     $paypal_email = get_option("namaste_paypal_id");
     // read the post from PayPal system and add 'cmd'
     $req = 'cmd=_notify-validate';
     foreach ($_POST as $key => $value) {
         $value = urlencode(stripslashes($value));
         $req .= "&{$key}={$value}";
     }
     $paypal_host = 'www.paypal.com';
     $paypal_sandbox = get_option('namaste_paypal_sandbox');
     if ($paypal_sandbox == '1') {
         $paypal_host = 'www.sandbox.paypal.com';
     }
     // post back to PayPal system to validate
     $header = "";
     $header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
     $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
     $header .= "Host: " . $paypal_host . "\r\n";
     $header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
     $fp = fsockopen($paypal_host, 80, $errno, $errstr, 30);
     if ($fp) {
         fputs($fp, $header . $req);
         $pp_response = '';
         while (!feof($fp)) {
             $res = fgets($fp, 1024);
             $pp_response .= $res;
             if (strstr($res, "VERIFIED") or $paypal_sandbox == '1') {
                 // check the payment_status is Completed
                 // check that txn_id has not been previously processed
                 // check that receiver_email is your Primary PayPal email
                 // process payment
                 $payment_completed = false;
                 $txn_id_okay = false;
                 $receiver_okay = false;
                 $payment_currency_okay = false;
                 $payment_amount_okay = false;
                 if ($_POST['payment_status'] == "Completed") {
                     $payment_completed = true;
                 } else {
                     self::log_and_exit("Payment status: {$_POST['payment_status']}");
                 }
                 // check txn_id
                 $txn_exists = $wpdb->get_var($wpdb->prepare("SELECT paycode FROM " . NAMASTE_PAYMENTS . "\n\t\t\t\t\t   WHERE paytype='paypal' AND paycode=%s", $_POST['txn_id']));
                 if (empty($txn_id)) {
                     $txn_id_okay = true;
                 } else {
                     self::log_and_exit("TXN ID exists: {$txn_id}");
                 }
                 // check receiver email
                 if ($_POST['business'] == $paypal_email) {
                     $receiver_okay = true;
                 } else {
                     self::log_and_exit("Business email is wrong: {$_POST['business']}");
                 }
                 // check payment currency
                 if ($_POST['mc_currency'] == get_option("namaste_currency")) {
                     $payment_currency_okay = true;
                 } else {
                     self::log_and_exit("Currency is {$_POST['mc_currency']}");
                 }
                 // check amount
                 $fee = get_post_meta($_GET['course_id'], 'namaste_fee', true);
                 $fee = apply_filters('namaste-coupon-applied', $fee);
                 // coupon code from other plugin?
                 if ($_POST['mc_gross'] >= $fee) {
                     $payment_amount_okay = true;
                 } else {
                     self::log_and_exit("Wrong amount: {$_POST['mc_gross']} when price is {$fee}");
                 }
                 // everything OK, insert payment and enroll
                 if ($payment_completed and $txn_id_okay and $receiver_okay and $payment_currency_okay and $payment_amount_okay) {
                     $wpdb->query($wpdb->prepare("INSERT INTO " . NAMASTE_PAYMENTS . " SET \n\t\t\t\t\t\t\tcourse_id=%d, user_id=%s, date=CURDATE(), amount=%s, status='completed', paycode=%s, paytype='paypal'", $_GET['course_id'], $_GET['user_id'], $fee, $_POST['txn_id']));
                     do_action('namaste-paid', $_GET['user_id'], $fee, "course", $_GET['course_id']);
                     // enroll accordingly to course settings - this will be placed in a method once we
                     // have more payment options
                     $enroll_mode = get_post_meta($_GET['course_id'], 'namaste_enroll_mode', true);
                     if (!NamasteLMSStudentModel::is_enrolled($_GET['user_id'], $_GET['course_id'])) {
                         $_course = new NamasteLMSCourseModel();
                         $status = $enroll_mode == 'free' ? 'enrolled' : 'pending';
                         $_course->enroll($_GET['user_id'], $_GET['course_id'], $status);
                     }
                     exit;
                 }
             }
         }
         fclose($fp);
         self::log_and_exit("Paypal result is not VERIFIED: {$pp_response}");
     } else {
         self::log_and_exit("Can't connect to Paypal");
     }
     exit;
 }