/**
  * validates a single shipping band
  */
 private static function validate_shipping_band($band, $band_index, $default)
 {
     $details = array();
     if (isset($band["name_" . $band_index]) && trim($band["name_" . $band_index]) !== "" && isset($band["band_id_" . $band_index]) && trim($band["band_id_" . $band_index]) !== "") {
         $regions = parent::get_shipping_regions();
         foreach ($regions as $region_code => $region_data) {
             // get shorthand for array indexes
             $s1 = "shipping_one_" . $region_code . "_" . $band_index;
             $sm = "shipping_multiple_" . $region_code . "_" . $band_index;
             if (isset($band[$s1]) && trim($band[$s1]) !== "" && isset($band[$sm]) && trim($band[$sm]) !== "") {
                 $details[$region_code] = array();
                 $details[$region_code]["shipping_one"] = SimplePayPalPluginAdmin::dec2($band[$s1]);
                 $details[$region_code]["shipping_multiple"] = SimplePayPalPluginAdmin::dec2($band[$sm]);
             }
         }
         if (count($details)) {
             $details["name"] = trim($band["name_" . $band_index]);
             $details["band_id"] = $band["band_id_" . $band_index];
             $details["default"] = $default == $band_index ? 1 : 0;
             $details["additional_free"] = isset($band["additional_free_" . $band_index]) ? 1 : 0;
         }
     }
     return $details;
 }
 /**
  * gets form controls for custom paypal box
  */
 public static function get_custom_paypal_box()
 {
     global $post;
     $paypal = self::get_paypal_meta($post->ID);
     $options = self::get_paypal_options();
     /* make sure options ahve been saved */
     if (empty(self::options_errors())) {
         /* Use nonce for verification */
         printf('<div class="paypal-options"><input type="hidden" name="paypal_meta" id="paypal_meta" value="%s" />', wp_create_nonce('paypal_meta'));
         /* left column */
         printf('<p><label for="paypal_name">%s: </label><input type="text" id="paypal_name" name="paypal_name" value="%s" size="25" /></p>', __('Name of item', 'sppp'), $paypal["name"]);
         printf('<p><label for="paypal_code">%s: </label><input type="text" id="paypal_code" name="paypal_code" value="%s" size="25" /></p>', __('Item code', 'sppp'), $paypal["code"]);
         printf('<p><label for="paypal_price">%s: </label><input type="text" id="paypal_price" name="paypal_price" value="%s" size="5" /></p>', __('Price', 'sppp'), $paypal["price"]);
         $chckd = isset($paypal["includes_vat"]) && $paypal["includes_vat"] === true ? ' checked' : '';
         printf('<p class="inc-vat"><label for="includes_vat" class="cbx"><input type="checkbox" class="vat-cbx" id="includes_vat" name="includes_vat" value="1"%s /> %s</label></p>', $chckd, __('Check this box if the price includes VAT', 'sppp'));
         $chckd = isset($paypal["exempt_vat"]) && $paypal["exempt_vat"] === true ? ' checked' : '';
         printf('<p class="vat-ex"><label for="exempt_vat" class="cbx"><input type="checkbox" class="vat-cbx" id="exempt_vat" name="exempt_vat" value="1"%s /> %s</label></p>', $chckd, __('Check this box if the item is VAT exempt', 'sppp'));
         $current_method = SimplePayPalPluginAdmin::get_selected_shipping_method();
         call_user_func_array($current_method["item_form_callback"], array($options, $paypal["shipping_settings"]));
         printf('<p><label for="paypal_stock">%s: </label><input type="text" id="paypal_stock" name="paypal_stock" value="%s" size="5" /></p>', __('Stock (either the number of items or a message about availability)', 'sppp'), $paypal["stock"]);
         print '<div class="clear">&nbsp;</div></div>';
     } else {
         printf('<p>Please <a href="%s">visit the Paypal Options page</a> to configure the plugin.</p>', admin_url('admin.php?page=sppp_options'));
     }
 }
 /**
  * function to process Instant Payment Notifications from Paypal
  */
 public static function processIPN()
 {
     $options = SimplePayPalPluginAdmin::get_paypal_options();
     $ppHost = isset($_POST['test_ipn']) ? $options["paypal_sandbox_url"] : $options["paypal_url"];
     $req = 'cmd=_notify-validate';
     $ipn_data = array();
     /* prepare echo */
     foreach ($_POST as $key => $value) {
         $value = urlencode(stripslashes($value));
         $req .= "&" . $key . "=" . $value;
         $ipn_data[$key] = urldecode($value);
     }
     /* Validate IPN with PayPal using curl */
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, $ppHost);
     curl_setopt($ch, CURLOPT_POST, true);
     curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded", "Content-Length: " . strlen($req)));
     curl_setopt($ch, CURLOPT_HEADER, false);
     curl_setopt($ch, CURLOPT_VERBOSE, true);
     curl_setopt($ch, CURLOPT_TIMEOUT, 30);
     $curl_result = @curl_exec($ch);
     $curl_err = curl_error($ch);
     $curl_info = curl_getinfo($ch);
     $ci = "";
     foreach ($curl_info as $k => $v) {
         $ci .= $k . " : " . $v . "\n";
     }
     /* are we verified? If so, let's process the IPN */
     if (strpos($curl_result, "VERIFIED") !== false) {
         /* decrease stock levels of items */
         $i = 1;
         while (isset($_POST["item_number" . $i])) {
             if (isset($_POST["quantity" . $i])) {
                 $paypal = SimplePaypalPluginAdmin::get_paypal_meta($_POST["item_number" . $i]);
                 if ($paypal["stock_no"] > 0) {
                     $paypal["stock"] = $paypal["stock_no"] - (int) $_POST["quantity" . $i];
                     if ($paypal["stock"] < 0) {
                         $paypal["stock"] = 0;
                     }
                     update_post_meta($_POST["item_number" . $i], 'sppp', $paypal);
                     update_post_meta($_POST["item_number" . $i], 'sppp-stock', $paypal["stock"]);
                 }
             }
             $i++;
         }
         /* store IPN in database */
         global $wpdb;
         $txn_id = isset($ipn_data["txn_id"]) ? $ipn_data["txn_id"] : '';
         $txn_type = isset($ipn_data["txn_type"]) ? $ipn_data["txn_type"] : '';
         $mc_gross = isset($ipn_data["mc_gross"]) ? $ipn_data["mc_gross"] : '';
         $tablename = self::get_payments_tablename();
         $wpdb->insert($tablename, array("payment_date" => time(), "payment_ipn" => serialize($ipn_data), "txn_id" => $txn_id, "txn_type" => $txn_type, "mc_gross" => $mc_gross), array("%d", "%s", "%s", "%s", "%s"));
     }
     if (is_email($options["paypal_ipn_email"])) {
         wp_mail($options["paypal_ipn_email"], "IPN CURL report", "CURL result: " . $curl_result . "\n\nCURL error: " . $curl_err . "\n\nCURL info: " . $ci . "\n\nIPN:\n\n" . $req, "From: " . $options["paypal_email"] . "\r\nReply-To: " . $options["paypal_email"] . "\r\n");
     }
     curl_close($ch);
 }