public function mailNewApproveAffiliate($user_id, $user_pass)
 {
     add_filter('wp_mail_from', array($this, 'filterMailAddress'));
     add_filter('wp_mail_from_name', array($this, 'filterMailName'));
     $user = get_user_by('id', $user_id);
     $username = $user->user_login;
     $address = $user->user_email;
     $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
     $login_url = get_option(WPAM_PluginConfig::$AffLoginPageURL);
     //wp_login_url();
     $subject = "Affiliate Application for " . $blogname;
     //$message = "New affiliate registration for {blogname}: has been approved!. \n\nUsername: {affusername} \nPassword: {affpassword} \nLogin URL: {affloginurl}";
     $message = WPAM_MessageHelper::GetMessage('affiliate_application_approved_email');
     $tags = array("{blogname}", "{affusername}", "{affpassword}", "{affloginurl}");
     $vals = array($blogname, $username, $user_pass, $login_url);
     $body = str_replace($tags, $vals, $message);
     WPAM_Logger::log_debug($subject);
     WPAM_Logger::log_debug("Sending an email to " . $address);
     $mail_sent = wp_mail($address, $subject, $body);
     if ($mail_sent == true) {
         WPAM_Logger::log_debug("Email was sent successfully by WordPress");
     } else {
         WPAM_Logger::log_debug("Email could not be sent by WordPress");
     }
     remove_filter('wp_mail_from', array($this, 'filterMailAddress'));
     remove_filter('wp_mail_from_name', array($this, 'filterMailName'));
 }
function wpam_generate_refkey_from_affiliate_id($aff_id)
{
    $db = new WPAM_Data_DataAccess();
    $affiliateRepos1 = $db->getAffiliateRepository();
    $wpam_refkey = NULL;
    $affiliate = $affiliateRepos1->loadBy(array('affiliateId' => $aff_id, 'status' => 'active'));
    if ($affiliate === NULL) {
        //affiliate with this ID does not exist
        WPAM_Logger::log_debug("generate_refkey_from_affiliate_id function - affiliate ID " . $aff_id . " does not exist");
    } else {
        $default_creative_id = get_option(WPAM_PluginConfig::$DefaultCreativeId);
        if (!empty($default_creative_id)) {
            $creative = $db->getCreativesRepository()->load($default_creative_id);
            $linkBuilder = new WPAM_Tracking_TrackingLinkBuilder($affiliate, $creative);
            $strRefKey = $linkBuilder->getTrackingKey()->pack();
            $refKey = new WPAM_Tracking_TrackingKey();
            $refKey->unpack($strRefKey);
            $idGenerator = new WPAM_Tracking_UniqueIdGenerator();
            $trackTokenModel = new WPAM_Data_Models_TrackingTokenModel();
            $trackTokenModel->dateCreated = time();
            $trackTokenModel->sourceAffiliateId = $aff_id;
            $trackTokenModel->sourceCreativeId = $refKey->getCreativeId();
            $trackTokenModel->trackingKey = $idGenerator->generateId();
            $trackTokenModel->referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : NULL;
            /* add a new visit so it doesn't fail while awarding commission */
            $db->getTrackingTokenRepository()->insert($trackTokenModel);
            $db->getEventRepository()->quickInsert(time(), $trackTokenModel->trackingKey, 'visit');
            /* */
            $binConverter = new WPAM_Util_BinConverter();
            $wpam_refkey = $binConverter->binToString($trackTokenModel->trackingKey);
        }
    }
    return $wpam_refkey;
}
 public function processRequest($request)
 {
     if (isset($request['wpam_reset_logfile'])) {
         WPAM_Logger::reset_log_file();
         echo '<div class="updated fade"><p>Log file has been reset</p></div>';
     }
     if (isset($request['action']) && $request['action'] === 'submitSettings') {
         return $this->doFormSubmit($request);
     } else {
         return $this->getSettingsForm();
     }
 }
 private function executeRequest($method, array $fields)
 {
     $currency = WPAM_MoneyHelper::getCurrencyCode();
     $fields = array_merge($fields, array('USER' => $this->apiUser, 'PWD' => $this->apiPassword, 'VERSION' => '65.0', 'SIGNATURE' => $this->apiSignature, 'METHOD' => $method, 'CURRENCYCODE' => $currency));
     WPAM_Logger::log_debug('PayPal MassPay post data:');
     WPAM_Logger::log_debug_array($fields);
     $postData = http_build_query($fields, NULL, '&');
     $response = $this->executePayPalRequest($postData);
     WPAM_Logger::log_debug('PayPal MassPay response:');
     WPAM_Logger::log_debug_array($response);
     return new WPAM_PayPal_Response($response);
 }
 public function render_settings_page()
 {
     $request = $_REQUEST;
     $request = stripslashes_deep($request);
     if (isset($request['wpam_reset_logfile'])) {
         WPAM_Logger::reset_log_file();
         echo '<div class="updated fade"><p>Log file has been reset</p></div>';
     }
     if (isset($request['action']) && $request['action'] === 'submitSettings') {
         $this->doFormSubmit($request);
     } else {
         $this->getSettingsForm();
     }
     echo $this->response->render();
 }
 public static function log_debug($message, $level = 0, $section_break = false, $file_name = '')
 {
     //Check if logger is enabled
     if (!WPAM_Logger::is_logger_enabled()) {
         return;
     }
     //Log stuff
     $content = WPAM_Logger::get_debug_timestamp();
     //Timestamp
     $content .= WPAM_Logger::get_debug_status($level);
     //Debug status
     $content .= ' : ';
     $content .= $message . "\n";
     $content .= WPAM_Logger::get_section_break($section_break);
     WPAM_Logger::append_to_file($content, $file_name);
 }
 public static function refund_commission($txn_id)
 {
     WPAM_Logger::log_debug('Commission refund handler function has been invoked for PURCHASE LOG ID: ' . $txn_id);
     global $wpdb;
     $table = WPAM_TRANSACTIONS_TBL;
     $query = "\r\n        SELECT *\r\n        FROM " . WPAM_TRANSACTIONS_TBL . "\r\n        WHERE referenceId = %s\r\n        AND amount < 0\r\n        AND type = 'refund'\r\n        ";
     $txn_record = $wpdb->get_row($wpdb->prepare($query, $txn_id));
     if ($txn_record != null) {
         //found a refunded commission record
         WPAM_Logger::log_debug('Commission for this sale has already been refunded. PURCHASE LOG ID: ' . $txn_id);
         return;
     } else {
         //find the commission record
         $query = "\r\n            SELECT *\r\n            FROM " . WPAM_TRANSACTIONS_TBL . "\r\n            WHERE referenceId = %s\r\n            AND type = 'credit'\r\n            ";
         $txn_record = $wpdb->get_row($wpdb->prepare($query, $txn_id));
         if ($txn_record != null) {
             //found the original commission record
             $description = $txn_record->description;
             $description = str_replace("Credit", "Refund", $txn_record->description);
             $data = array();
             $data['dateModified'] = date("Y-m-d H:i:s", time());
             $data['dateCreated'] = date("Y-m-d H:i:s", time());
             $data['referenceId'] = $txn_id;
             $data['affiliateId'] = $txn_record->affiliateId;
             $data['type'] = 'refund';
             $data['description'] = $description;
             $data['amount'] = '-' . $txn_record->amount;
             $wpdb->insert($table, $data);
             WPAM_Logger::log_debug('Commission refunded (' . $txn_record->amount . ') for PURCHASE LOG ID: ' . $txn_id . ', Affiliate ID: ' . $txn_record->affiliateId);
             return;
         } else {
             WPAM_Logger::log_debug('No commission record found for PURCHASE LOG ID: ' . $txn_id . '. Commission cannot be refunded!');
             return;
         }
     }
 }
 function create_wpam_affiliate_record($fields)
 {
     global $wpdb;
     //Do some validation to make sure we have some minimum required info
     if (!isset($fields['email']) || empty($fields['email'])) {
         WPAM_Logger::log_debug("create_wpam_affiliate_record() - Error, email address is missing. Cannot create affiliate record!", 4);
         return;
     }
     if (!isset($fields['userId']) || empty($fields['userId'])) {
         WPAM_Logger::log_debug("create_wpam_affiliate_record() - Error, userId value is missing. Cannot create affiliate record!", 4);
         return;
     }
     //Check and set the default status values
     if (!isset($fields['status']) || empty($fields['status'])) {
         if (get_option(WPAM_PluginConfig::$AutoAffiliateApproveIsEnabledOption) == 1) {
             $fields['status'] = 'active';
         } else {
             $fields['status'] = 'applied';
         }
     }
     //Check and set default dateCreated value
     if (!isset($fields['dateCreated']) || empty($fields['dateCreated'])) {
         $fields['dateCreated'] = current_time('mysql');
         //date("Y-m-d H:i:s");
     }
     //Check and set default dateCreated value
     if (!isset($fields['uniqueRefKey']) || empty($fields['uniqueRefKey'])) {
         $idGenerator = new WPAM_Tracking_UniqueIdGenerator();
         $fields['uniqueRefKey'] = $idGenerator->generateId();
     }
     //Check and set default bountyType
     if (!isset($fields['bountyType']) || empty($fields['bountyType'])) {
         $fields['bountyType'] = get_option(WPAM_PluginConfig::$AffBountyType);
     }
     //Check and set default bountyAmount
     if (!isset($fields['bountyAmount']) || empty($fields['bountyAmount'])) {
         $fields['bountyAmount'] = get_option(WPAM_PluginConfig::$AffBountyAmount);
     }
     $wpdb->insert(WPAM_AFFILIATES_TBL, $fields);
 }
 public function edd_on_complete_purchase($payment_id)
 {
     WPAM_Logger::log_debug('Easy Digital Downlaods Integration - complete purchase hook triggered for Order ID: ' . $payment_id . '. Checking if affiliate commission needs to be awarded.');
     $payment_meta = edd_get_payment_meta($payment_id);
     $strRefKey = "";
     if (isset($payment_meta['wpam_refkey']) && !empty($payment_meta['wpam_refkey'])) {
         $strRefKey = $payment_meta['wpam_refkey'];
         WPAM_Logger::log_debug('Easy Digital Downlaods Integration - This purchase was referred by an affiliate, refkey: ' . $strRefKey);
     } else {
         WPAM_Logger::log_debug('Easy Digital Downlaods Integration - refkey not found in the payment_meta. This purchase was not referred by an affiliate');
         return;
     }
     $purchaseAmount = edd_get_payment_amount($payment_id);
     WPAM_Logger::log_debug('Easy Digital Downlaods Integration - Awarding commission for Order ID: ' . $payment_id . '. Purchase amt: ' . $purchaseAmount);
     $requestTracker = new WPAM_Tracking_RequestTracker();
     $requestTracker->handleCheckoutWithRefKey($payment_id, $purchaseAmount, $strRefKey);
 }
 public function jigoshopNewOrder($order_id)
 {
     $order = new jigoshop_order($order_id);
     $total = floatval($order->order_subtotal);
     if ($order->order_discount) {
         $total = $total - floatval($order->order_discount);
     }
     if ($total < 0) {
         $total = 0;
     }
     WPAM_Logger::log_debug('JigoShop Integration - new order received. Order ID: ' . order_id . '. Purchase amt: ' . $total);
     $requestTracker = new WPAM_Tracking_RequestTracker();
     $requestTracker->handleCheckout($order_id, $total);
 }
 public static function log_debug_array($array_to_write, $level = 0, $section_break = false, $file_name = '')
 {
     //Check if logger is enabled
     if (!WPAM_Logger::is_logger_enabled()) {
         return;
     }
     //Log stuff
     $content = WPAM_Logger::get_debug_timestamp();
     //Timestamp
     $content .= WPAM_Logger::get_debug_status($level);
     //Debug status
     $content .= ' : ';
     ob_start();
     print_r($array_to_write);
     $var = ob_get_contents();
     ob_end_clean();
     $content .= $var;
     $content .= WPAM_Logger::get_section_break($section_break);
     WPAM_Logger::append_to_file($content, $file_name);
 }