<?php
/*
Copyright 2012 Neil Harvey - wordpressLAB
*/

/********************************************************************
 * Change product transaction log status to active
 ********************************************************************/
add_action('wp_ajax_rt_react_transaction', 'rt_react_transaction');
function rt_react_transaction() {
    global $wpdb;
    $table_name = $wpdb->prefix . "wpsa_product_transaction";
    $row_id = (int)$_POST['row_id'];
    $wpdb->query("UPDATE $table_name SET `status` = 'active' WHERE `id` = $row_id ");
}

/********************************************************************
 * Change product transaction log status to inactive
 ********************************************************************/
add_action('wp_ajax_rt_deact_transaction', 'rt_deact_transaction');
function rt_deact_transaction() {
    global $wpdb;
    $table_name = $wpdb->prefix . "wpsa_product_transaction";
    $row_id = (int)$_POST['row_id'];
    $wpdb->query("UPDATE $table_name SET `status` = 'inactive' WHERE `id` = $row_id ");
}

/********************************************************************
 * To send mail of "product download link"
 ********************************************************************/
function rt_sa_send_download_link( $product_ids, $pypal_transaction_detail, $reset_flag = false) {
    global $wpdb;
    //error_log(serialize($pypal_transaction_detail));
    $email_to = trim($pypal_transaction_detail['payer_email']);
    $txn_id= trim($pypal_transaction_detail['txn_id']);
    $reglink='<a class="register_link" href="' .wp_login_url(). '?action=register&email_id='.$email_to.'&txn_id='.$txn_id.'">Register</a> here!';
    $rt_option = get_option('rt_option_products');
    $sql = "SELECT id, 	name, 	description, 	price, 	file_name,  message_template FROM ".$wpdb->prefix."wpsa_product_details WHERE id IN ($product_ids)";
    $results = $wpdb->get_results($sql);
    
    foreach ($results as $result) {
        $file_temp_links = '';
        //added by santosh
        $wpsa_gross_sell = '';
        $wpsa_discount = '';
        if($pypal_transaction_detail['mc_gross'] !='' ) $wpsa_gross_sell = $pypal_transaction_detail['mc_gross'];
        if($pypal_transaction_detail['discount'] !='' ) $wpsa_discount = $pypal_transaction_detail['discount'];

        $file_temp_links = create_temp_file_to_download($result->id, '', $wpsa_gross_sell, $wpsa_discount).'';
        //$file_temp_links = create_temp_file_to_download($result->id).'';
        //end
        $rt_message_template = $result->message_template;
        $rt_option = unserialize($rt_message_template);

        $subject = $rt_option['subject'];

        //$message = '<html><head><title></title></head><body>';
        $message = $rt_option['msg'];
        
        $message = str_replace('%first_name%', $pypal_transaction_detail['first_name'], $message);
        $message = str_replace('%last_name%', $pypal_transaction_detail['last_name'], $message);
        
        $message = str_replace('%user_email%', $email_to, $message);
        $message = str_replace('%blogname%', get_option('blogname'), $message);

        $message = str_replace('%product_name%', $result->name, $message);
        $message = str_replace('%description%', $result->description, $message);
        $message = str_replace('%price%', $result->price, $message);

        $message = str_replace('%download_link%', $file_temp_links , $message);
        $message = str_replace('%register_link%', $reglink , $message);

        $siteurl = get_option('siteurl');
        $message = str_replace('%siteurl%', $siteurl, $message);

        if ($reset_flag == true) {
            $message .= "\n" . 'This is a Temporary Reset Link';
        }
        
        //$message .= '</body></html>';

        //$message = nl2br($message);

        $headers  = 'MIME-Version: 1.0' . "\r\n";
        $headers .= 'Content-type: text/plain; charset=iso-8859-1' . "\r\n";
        $headers .= 'From: '.$rt_option['fromname'].' <'.$rt_option['from'].'>' . "\r\n";

        wp_mail($email_to, $subject, $message, $headers);
		
    }
}

/********************************************************************
    Code for paypal form added after clicking "Buy Now " button
********************************************************************/
add_action('wp_ajax_product_form', 'rt_products_buy_button');
add_action('wp_ajax_nopriv_product_form', 'rt_products_buy_button');
function rt_products_buy_button() {
    $wp_sa_setting = get_option('wp_sa_setting');
    $item_ids = $_POST['item_id'];
    $item_prices = $_POST['item_price'];
    $item_names = $_POST['item_name'];
    $item_ids = explode('|',$item_ids);
    $item_prices = explode('|',$item_prices);
    $item_names = explode('|',$item_names);
    $custom_code = microtime(TRUE);

    setcookie('check_time_wpsa', $custom_code, time()+3600, COOKIEPATH, COOKIE_DOMAIN, false, true);
    setcookie('check_type_wpsa', 'pro', time()+3600, COOKIEPATH, COOKIE_DOMAIN, false, true);

    $cancel_return_path = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
    $return_path = $wp_sa_setting['wp_sa_thank_you_page'];
    if ($wp_sa_setting['wp_sa_test'] == "checked")
        $form_action = 'https://www.sandbox.paypal.com/cgi-bin/webscr';
    else
        $form_action = 'https://www.paypal.com/cgi-bin/webscr';
    ?>
<div>
    <form name="cart" id="rt_sa_product_hidden_form" action="<?php echo $form_action;?>" method="post">
        <input type="hidden" name="cmd" value="_cart" />
        <input type="hidden" name="return" value="<?php echo $wp_sa_setting['wp_sa_thank_you_page'];?>"/>
        <input type="hidden" name="upload" value="1" />
        <input type="hidden" name="business" value="<?php echo $wp_sa_setting['wp_sa_payment_email'];?>"/>
        <input type="hidden" name="currency_code" value="<?php echo $wp_sa_setting['wp_sa_currency_code']; ?>"/>
        <input type="hidden" name="rm" value="2">
        <input type="hidden" name="notify_url" value="<?php echo WP_SA_IPN_URL; ?>"/>
        <input type="hidden" name="no_note" value="1"/>
        <input type="hidden" name="cancel_return" value="<?php echo $wp_sa_setting['wp_sa_sub_payment_url'];?>"/>
        <?php
        $item_id_count = 1;
        foreach ($item_ids as $item_id) {
            ?>
            <input type="hidden" name="item_number_<?php echo $item_id_count?>" value="<?echo $item_id?>"/>
            <?php
            $item_id_count++;
        }
        $item_name_count = 1;
        foreach ($item_names as $item_name) {
            ?>
            <input type="hidden" name="item_name_<?php echo $item_name_count?>" value="<?echo $item_name?>"/>
            <input type="hidden" name="os0_<?php echo $item_name_count?>" value="<?php echo $_SERVER['REMOTE_ADDR'];?>"/>
            <input type="hidden" name="on0_<?php echo $item_name_count?>" value="RemoteIP">
            <?php
            $item_name_count++;
        }
        $item_price_count = 1;
        foreach ($item_prices as $item_price) {
            ?>
            <input type="hidden" name="amount_<?php echo $item_price_count?>" value="<?echo $item_price?>"/>
            <?php
            $item_price_count++;
        }
        do_action('wpsa_product_paypal_form');
        ?>
        <input type="hidden" name="custom" value="<?php echo $custom_code.'|'.$_COOKIE['referal_url']; ?>"/>
        <input type="image" name="submit" border="0" src="http://www.paypal.com/en_US/i/btn/btn_buynow_LG.gif" alt="PayPal - The safer, easier way to pay online"/>
    </form>
</div>
    <?php
}
?>