static function getInstance()
 {
     if (empty(self::$_this)) {
         self::$_this = new WP_UnocoinBitcoin_Config();
         self::$_this->loadConfig();
         return self::$_this;
     }
     return self::$_this;
 }
function unocoin_estore_filter_store_action_page($content)
{
    global $post;
    if ($post->post_name == 'wpbc-order-info') {
        if (isset($_REQUEST['wpbc_co']) && $_REQUEST['gateway'] == 'unocoin') {
            if (isset($_REQUEST['result']) && $_REQUEST['result'] == 'error' && isset($_REQUEST['error_type'])) {
                $content = '<p>There was an error with the transaction processing. Please see below for details</p>';
                $errortype = strip_tags($_REQUEST['error_type']);
                $content .= 'ErrorType: ' . $errortype;
                return $content;
            }
            if (isset($_REQUEST['wpbc_unocoin_gateway']) && isset($_REQUEST['order_id'])) {
                $order_id = base64_decode(strip_tags($_REQUEST['order_id']));
                $content = '<p>Thank you for your payment. You should receive an email with your order summary</p>';
                return $content;
            }
            if (isset($_REQUEST['wpbc_user_info_submit']) && $_REQUEST['wpbc_user_info_submit'] == "yes") {
                $config = WP_UnocoinBitcoin_Config::getInstance();
                $api_key = $config->getValue('wpbc_unocoin_api_key');
                if (empty($api_key)) {
                    echo "You need to enter your unocoin credentials in the plugin settings";
                    return;
                }
                $transaction_speed = $config->getValue('wpbc_unocoin_transaction_speed');
                if (!$transaction_speed) {
                    $transaction_speed = "low";
                }
                $item_name = strip_tags($_REQUEST["item_name"]);
                $currency = strip_tags($_REQUEST["currency"]);
                $cart_id = "wpbc" . uniqid();
                $amount = strip_tags($_REQUEST["price"]);
                $price = number_format($amount, 2, '.', '');
                // Create AccessCode Request Object
                $first_name = !empty($_REQUEST["fname"]) ? strip_tags(str_replace("'", "`", $_REQUEST["fname"])) : '';
                $last_name = !empty($_REQUEST["lname"]) ? strip_tags(str_replace("'", "`", $_REQUEST["lname"])) : '';
                $address = !empty($_REQUEST["address"]) ? strip_tags(str_replace("'", "`", $_REQUEST["address"])) : '';
                $city = !empty($_REQUEST["city"]) ? strip_tags(str_replace("'", "`", $_REQUEST["city"])) : '';
                $state = !empty($_REQUEST["state"]) ? strip_tags(str_replace("'", "`", $_REQUEST["state"])) : '';
                $zip = !empty($_REQUEST["zip"]) ? strip_tags(str_replace("'", "`", $_REQUEST["zip"])) : '';
                $country = !empty($_REQUEST["country"]) ? strip_tags(str_replace("'", "`", $_REQUEST["country"])) : '';
                $email = !empty($_REQUEST["email"]) ? strip_tags(str_replace("'", "`", $_REQUEST["email"])) : '';
                $phone = !empty($_REQUEST["phone"]) ? strip_tags(str_replace("'", "`", $_REQUEST["phone"])) : '';
                //------------------------
                $options = array();
                $options['buyerName'] = $first_name . " " . $last_name;
                $options['buyerAddress1'] = $address;
                $options['buyerCity'] = $city;
                $options['buyerState'] = $state;
                $options['buyerZip'] = $zip;
                $options['buyerCountry'] = $country;
                $options['buyerEmail'] = $email;
                if ($phone != "") {
                    $options['buyerPhone'] = $phone;
                }
                include_once "lib/unocoin_lib.php";
                $options['itemDesc'] = $item_name;
                $options['currency'] = $currency;
                $return_url = $config->getValue('wpbc_unocoin_form_page_url');
                $order_id = base64_encode($cart_id);
                $notification_url = add_query_arg(array('wpbc_unocoin_callback' => '1'), $return_url);
                $redirect_url = add_query_arg(array('wpbc_unocoin_gateway' => '1', 'order_id' => $order_id), $return_url);
                $options['notificationURL'] = $notification_url;
                //pass sessionid along so that it can be used to populate the transaction results page
                $options['redirectURL'] = $redirect_url;
                $options['transactionSpeed'] = $transaction_speed;
                $options['apiKey'] = $api_key;
                //create a new order
                $wpbc_bitcoin_orders = array('post_title' => 'WPBC Bitcoin Order', 'post_type' => 'wpbc_bitcoin_orders', 'post_content' => '', 'post_status' => 'trash');
                // Insert the post into the database
                $post_id = wp_insert_post($wpbc_bitcoin_orders);
                if ($post_id) {
                    $updated_wpbc_order = array('ID' => $post_id, 'post_title' => $cart_id, 'post_type' => 'wpbc_bitcoin_orders');
                    wp_update_post($updated_wpbc_order);
                    update_post_meta($post_id, 'wpbc_order_id', $cart_id);
                    update_post_meta($post_id, 'wpbc_first_name', $first_name);
                    update_post_meta($post_id, 'wpbc_last_name', $last_name);
                    update_post_meta($post_id, 'wpbc_email_address', $email);
                    update_post_meta($post_id, 'wpbc_total_amount', $price);
                    update_post_meta($post_id, 'wpbc_address', $address);
                    update_post_meta($post_id, 'wpbc_city', $city);
                    update_post_meta($post_id, 'wpbc_state', $state);
                    update_post_meta($post_id, 'wpbc_zip', $zip);
                    update_post_meta($post_id, 'wpbc_country', $country);
                    if ($phone != "") {
                        update_post_meta($post_id, 'wpbc_phone', $phone);
                    }
                    $status = "In Progress";
                    update_post_meta($post_id, 'wpbc_order_status', $status);
                    $item_details = "Item Name: " . $item_name . ", Price: " . $price . " " . $currency;
                    update_post_meta($post_id, 'wpbc_items_ordered', $item_details);
                }
                //
                $options['posData'] = $post_id;
                $options['fullNotifications'] = true;
                $invoice = unocoinCreateInvoice($cart_id, $price, $cart_id, $options);
                if (isset($invoice['error'])) {
                    $error_url = add_query_arg(array('wpbc_co' => '1', 'gateway' => 'unocoin', 'result' => 'error', 'error_type' => $invoice["error"]), $error_url);
                    wpbc_redirect_to_url($error_url);
                } else {
                    //invoice created successfully
                    wpbc_redirect_to_url($invoice['url']);
                }
            } else {
                //This is a checkout request via Google Wallet for digital goods. Lets process it.
                $atts = array();
                $content = wpbc_order_form::display_form_handler($atts);
            }
        }
    }
    return $content;
}
function wp_unocoinbitcoin_display_general_settings_menu()
{
    $config = WP_UnocoinBitcoin_Config::getInstance();
    if (isset($_POST['wp_unocoinbitcoin_settings_update'])) {
        $config->setValue('wpbc_unocoin_enabled', $_POST["wpbc_unocoin_enabled"] == '1' ? 1 : '');
        $config->setValue('wpbc_unocoin_api_key', trim($_POST["wpbc_unocoin_api_key"]));
        $config->setValue('wpbc_unocoin_transaction_speed', trim($_POST["wpbc_unocoin_transaction_speed"]));
        $config->setValue('wpbc_unocoin_form_page_url', trim($_POST["wpbc_unocoin_form_page_url"]));
        $config->setValue('wpbc_unocoin_form_page_title', trim($_POST["wpbc_unocoin_form_page_title"]));
        echo '<div class="updated"><p>Settings updated!</p></div>';
        $config->saveConfig();
    }
    ?>

    <div class="postbox">
    <h3><label for="title">Usage Instruction</label></h3>
    <div class="inside">
    <p>You have successfully installed the WordPress Unocoin-Bitcoin plugin.</p>
    <p><strong>Step 1)</strong> Enter your unocoim credentials in the "Settings" section</p>
    <p><strong>Step 1)</strong> Insert a shortcode for your product to create a Buy Now button:</p>
    <p><code>[wpbc_buy_now item_name="test product" price="5.00" currency="INR"]</code></p>
    <p>Check the <a href="https://www.unocoin.com/merchants/overview" target="_blank">WP Unocoin-Bitcoin plugin page</a> for more information</p>
    </div></div>

    <div class="postbox">
    <h3><label for="title">unocoin Settings</label></h3>
    <div class="inside">

    <form method="post" action="">
    <table class="form-table" width="100%" border="0" cellspacing="0" cellpadding="6">

    <tr valign="top"><td width="25%" align="left">
    Enable unocoin
    </td><td align="left">
    <input name="wpbc_unocoin_enabled" type="checkbox"<?php 
    if ($config->getValue('wpbc_unocoin_enabled') != '') {
        echo ' checked="checked"';
    }
    ?>
 value="1"/>
    <p class="description">Check this to enable unocoin gateway.</p>
    </td></tr>

    <tr valign="top"><td width="25%" align="left">
    API Key
    </td><td align="left">
    <input name="wpbc_unocoin_api_key" type="text" size="80" value="<?php 
    echo $config->getValue('wpbc_unocoin_api_key');
    ?>
"/>
    <p class="description">Your unocoin merchant api key</p>
    </td></tr>
    
    <tr valign="top"><td width="25%" align="left">
    Transaction Speed
    </td><td align="left">
    <?php 
    $txn_speed = $config->getValue('wpbc_unocoin_transaction_speed');
    ?>
    <select name="wpbc_unocoin_transaction_speed" >
        <option <?php 
    echo $txn_speed === 'low' ? 'selected="selected"' : '';
    ?>
 value="low">Low</option>
        <option <?php 
    echo $txn_speed === 'medium' ? 'selected="selected"' : '';
    ?>
 value="medium">Medium</option>
        <option <?php 
    echo $txn_speed === 'high' ? 'selected="selected"' : '';
    ?>
 value="high">High</option>
    </select>
        <br /><i>Speed at which the bitcoin transaction registers as "confirmed" to the store. This overrides your merchant settings on the unocoin website</i><br /><br />
    </td></tr>
    
    <tr valign="top"><td width="25%" align="left">
    Order Information Page
    </td><td align="left">
    <input name="wpbc_unocoin_form_page_url" type="text" size="80" value="<?php 
    echo $config->getValue('wpbc_unocoin_form_page_url');
    ?>
"/>
    <br /><i>URL of the page where order information will be collected from your users.</i>
    </td></tr>
    
    <tr valign="top"><td width="25%" align="left">
    Order Information Page Title
    </td><td align="left">
    <input name="wpbc_unocoin_form_page_title" type="text" size="80" value="<?php 
    echo $config->getValue('wpbc_unocoin_form_page_title');
    ?>
"/>
    <br /><i>Title of the page where order information will be collected from your users.</i>
    </td></tr>
    
    </table>
    
    <div class="submit">
        <input type="submit" name="wp_unocoinbitcoin_settings_update" class="button-primary" value="Update" />
    </div>
     
    </form>
    </div></div>

    <div style="background: none repeat scroll 0 0 #FFF6D5;border: 1px solid #D1B655;color: #3F2502;margin: 10px 0;padding: 5px 5px 5px 10px;text-shadow: 1px 1px #FFFFFF;">	
    <p>If you need a feature rich plugin to sell your digital and physical products using bitcoin then checkout our <a target="_blank" href="http://unocoin.com">WP eStore Plugin</a>
    </p>
    </div>
	
<?php 
}
        function wpbc_buy_now_handler($atts)
        {
            extract(shortcode_atts(array('item_name' => '', 'currency' => 'INR', 'price' => '', 'image' => '', 'gateway' => 'unocoin'), $atts));
            if (empty($image)) {
                $image = WP_UNOCOINBITCOIN_PLUGIN_URL . '/images/wpbc_co.png';
            }
            $config = WP_UnocoinBitcoin_Config::getInstance();
            $order_page = $config->getValue('wpbc_unocoin_form_page_url');
            $action_url = add_query_arg(array('wpbc_co' => '1', 'gateway' => $gateway), $order_page);
            $output = <<<EOT
            <form action="{$action_url}" method="post">
            <input type="hidden" name="item_name" value="{$item_name}">
            <input type="hidden" name="price" value="{$price}">
            <input type="hidden" name="currency" value="{$currency}">
            <input type="image" src="{$image}" name="submit">
            </form>
EOT;
            return $output;
        }
function wpbc_unocoin_handle_post_payment()
{
    //handle notification
    if (isset($_REQUEST['wpbc_unocoin_callback']) && $_REQUEST['wpbc_unocoin_callback'] == "1") {
        include_once "lib/Unocoin_lib.php";
        $config = WP_UnocoinBitcoin_Config::getInstance();
        $api_key = $config->getValue('wpbc_unocoin_api_key');
        $response = unocoinVerifyNotification($api_key);
        //verify transaction
        if (isset($response['error'])) {
            //verification error
            return;
        }
        //transaction verified
        $post_id = $response['posData'];
        if (empty($post_id)) {
            return;
        }
        $order_status = get_post_meta($post_id, 'wpbc_order_status', true);
        if ($order_status == "confirmed") {
            //order has been processed once already
            return;
        }
        //update information
        if ($order_status != "In Progress") {
            $updated_wpbc_order = array('ID' => $post_id, 'post_status' => 'publish', 'post_type' => 'wpbc_bitcoin_orders');
            wp_update_post($updated_wpbc_order);
        }
        $trn_status = $response['status'];
        $to_address = get_post_meta($post_id, 'wpbc_email_address', true);
        $subject = "";
        $body = "";
        $admin_email = get_bloginfo('admin_email');
        $from_address = get_bloginfo('name') . " <" . $admin_email . ">";
        $headers = 'From: ' . $from_address . "\r\n";
        switch ($trn_status) {
            //For low and medium transaction speeds, the order status is set to "Order Received". The customer receives
            //an initial email stating that the transaction has been paid.
            case 'paid':
                update_post_meta($post_id, 'wpbc_order_status', $trn_status);
                $subject = "Payment Received";
                $body = "Thank you! Your payment has been received, but the transaction has not been confirmed on the bitcoin network. " . "You will receive another email when the transaction has been confirmed.";
                wp_mail($to_address, $subject, $body, $headers);
                update_post_meta($post_id, 'wpbc_buyer_email_sent', 'Email sent to: ' . $to_address . " for order status: " . $trn_status);
                break;
                //For low and medium transaction speeds, the order status will not change. For high transaction speed, the order
                //status is set to "Order Received" here. For all speeds, an email will be sent stating that the transaction has
                //been confirmed.
            //For low and medium transaction speeds, the order status will not change. For high transaction speed, the order
            //status is set to "Order Received" here. For all speeds, an email will be sent stating that the transaction has
            //been confirmed.
            case 'confirmed':
                //display initial "thank you" if transaction speed is high, as the 'paid' status is skipped on high speed
                update_post_meta($post_id, 'wpbc_order_status', $trn_status);
                if ($config->getValue('eStore_unocoin_transaction_speed') == "high") {
                    $subject = "Payment Received";
                    $body = "Thank you! Your payment has been received, and the transaction has been confirmed on the bitcoin network. " . "You will receive another email when the transaction is complete.";
                } else {
                    $subject = "Transaction Confirmed";
                    $body = "Your transaction has now been confirmed on the bitcoin network. " . "You will receive another email when the transaction is complete.";
                }
                wp_mail($to_address, $subject, $body, $headers);
                update_post_meta($post_id, 'wpbc_buyer_email_sent', 'Email sent to: ' . $to_address . " for order status: " . $trn_status);
                break;
                //The purchase receipt email is sent upon the invoice status changing to "complete", and the order
                //status is changed to Accepted Payment
            //The purchase receipt email is sent upon the invoice status changing to "complete", and the order
            //status is changed to Accepted Payment
            case 'complete':
                update_post_meta($post_id, 'wpbc_order_status', $trn_status);
                $subject = "Transaction Complete";
                $body = "Thank you for your payment. You have ordered the following item(s):\n\n ";
                $body .= get_post_meta($post_id, 'wpbc_items_ordered', true);
                wp_mail($to_address, $subject, $body, $headers);
                update_post_meta($post_id, 'wpbc_buyer_email_sent', 'Email sent to: ' . $to_address . " for order status: " . $trn_status);
                break;
        }
    }
}