/**
  * Initialize the object one time
  * @return object
  */
 public static function getInstance()
 {
     if (!self::$c_instance) {
         self::$c_instance = new HCCoder_PayPalConfig();
     }
     return self::$c_instance;
 }
/**
 * Create admin menus
 */
function paypal_express_checkout_admin_menu()
{
    $config = HCCoder_PayPalConfig::getInstance();
    add_menu_page($config->getItem('plugin_name'), $config->getItem('plugin_name'), 'level_10', $config->getItem('plugin_id'), array('HCCoder_PayPalExpressCheckoutAdmin', 'adminIndex'), home_url('/wp-content/plugins/' . $config->getItem('plugin_id') . '/static/images/icon.png'));
    add_submenu_page($config->getItem('plugin_id'), 'Configuration', 'Configuration', 'level_10', $config->getItem('plugin_configuration_id'), array('HCCoder_PayPalExpressCheckoutAdmin', 'adminConfiguration'));
    add_submenu_page($config->getItem('plugin_id'), 'Shortcode', 'Shortcode', 'level_10', $config->getItem('plugin_shortcode_id'), array('HCCoder_PayPalExpressCheckoutAdmin', 'adminShortcode'));
    add_submenu_page($config->getItem('plugin_id'), 'Payments history', 'Payments history', 'level_10', $config->getItem('plugin_history_id'), array('HCCoder_PayPalExpressCheckoutAdmin', 'adminHistory'));
    add_submenu_page($config->getItem('plugin_id'), 'Help', 'Help', 'level_10', $config->getItem('plugin_help_id'), array('HCCoder_PayPalExpressCheckoutAdmin', 'adminHelp'));
}
 /**
  * Admin interface > payments history
  */
 public function adminHistory()
 {
     $config = HCCoder_PayPalConfig::getInstance();
     global $wpdb;
     $allowed_statuses = array('success', 'pending', 'failed');
     if (count($_POST) && isset($_POST['status']) && in_array($_POST['status'], $allowed_statuses) && isset($_POST['id']) && is_numeric($_POST['id']) && $_POST['id'] > 0) {
         $config_saved = TRUE;
         $update_data = array('status' => $_POST['status']);
         $where = array('id' => $_POST['id']);
         $update_format = array('%s');
         $wpdb->update('hccoder_paypal', $update_data, $where, $update_format);
     }
     if (isset($_GET['action']) && $_GET['action'] == 'details' && is_numeric($_GET['id']) && $_GET['id'] > 0) {
         $details = $wpdb->get_row('SELECT hccoder_paypal.id,
                             hccoder_paypal.amount,
                             hccoder_paypal.currency,
                             hccoder_paypal.status,
                             hccoder_paypal.firstname,
                             hccoder_paypal.lastname,
                             hccoder_paypal.email,
                             hccoder_paypal.description,
                             hccoder_paypal.summary,
                             hccoder_paypal.created
                           FROM
                             hccoder_paypal
                           WHERE
                             hccoder_paypal.id = ' . (int) $_GET['id']);
         require $config->getItem('views_path') . 'adminhistorydetails.php';
     } elseif (isset($_GET['action']) && $_GET['action'] == 'edit' && is_numeric($_GET['id']) && $_GET['id'] > 0) {
         $details = $wpdb->get_row('SELECT 
                             hccoder_paypal.status
                           FROM
                             hccoder_paypal
                           WHERE
                             hccoder_paypal.id = ' . (int) $_GET['id']);
         require $config->getItem('views_path') . 'adminhistoryedit.php';
     } else {
         $rows = $wpdb->get_results('SELECT hccoder_paypal.id,
                             hccoder_paypal.amount,
                             hccoder_paypal.currency,
                             hccoder_paypal.status,
                             hccoder_paypal.firstname,
                             hccoder_paypal.lastname,
                             hccoder_paypal.email,
                             hccoder_paypal.description,
                             hccoder_paypal.summary,
                             hccoder_paypal.created
                           FROM
                             hccoder_paypal
                           ORDER BY
                             hccoder_paypal.id DESC');
         require $config->getItem('views_path') . 'adminhistory.php';
     }
 }
 public function frontendIndex($atts)
 {
     if (!isset($atts['amount']) || (isset($atts['amount']) && !is_numeric($atts['amount']) || $atts['amount'] < 0)) {
         trigger_error('PayPal shortcode error: You need to specify the amount of the payment.', E_USER_ERROR);
     }
     $supported_currencies = array('AUD', 'CAD', 'CZK', 'DKK', 'EUR', 'HKD', 'HUF', 'JPY', 'NOK', 'NZD', 'PLN', 'GBP', 'SGD', 'SEK', 'CHF', 'USD');
     if (!isset($atts['currency']) || !in_array($atts['currency'], $supported_currencies)) {
         trigger_error('PayPal shortcode error: You need to specify the currency of the payment.', E_USER_ERROR);
     }
     ob_start();
     $config = HCCoder_PayPalConfig::getInstance();
     require $config->getItem('views_path') . 'frontendshortcode.php';
     return ob_get_clean();
 }
 /**
  * Close transaction
  */
 function DoExpressCheckout($result)
 {
     $config = HCCoder_PayPalConfig::getInstance();
     // FIELDS
     $fields = array('USER' => get_option('paypal_api_username'), 'PWD' => get_option('paypal_api_password'), 'SIGNATURE' => get_option('paypal_api_signature'), 'VERSION' => '74.0', 'PAYERID' => $result['PAYERID'], 'TOKEN' => $result['TOKEN'], 'LOCALECODE' => $result['LOCALECODE'], 'PAYMENTREQUEST_0_AMT' => $result['AMT'], 'PAYMENTREQUEST_0_CURRENCYCODE' => $result['CURRENCYCODE'], 'PAYMENTREQUEST_0_PAYMENTACTION' => 'Sale', 'METHOD' => 'DoExpressCheckoutPayment');
     $nb_fields = count($fields);
     $fields = http_build_query($fields);
     // $fields_string = '';
     // foreach ( $fields as $key => $value)
     // $fields_string .= $key.'='.$value.'&';
     // rtrim($fields_string,'&');
     // CURL
     $ch = curl_init();
     if (get_option('paypal_environment') == 'sandbox') {
         curl_setopt($ch, CURLOPT_URL, 'https://api-3t.sandbox.paypal.com/nvp');
     } elseif (get_option('paypal_environment') == 'live') {
         curl_setopt($ch, CURLOPT_URL, 'https://api-3t.paypal.com/nvp');
     }
     curl_setopt($ch, CURLOPT_POST, $nb_fields);
     // curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
     curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     //execute post
     $result = curl_exec($ch);
     //close connection
     curl_close($ch);
     parse_str($result, $result);
     if ($result['ACK'] == 'Success') {
         HCCoder_PayPalAPI::UpdatePayment($result, 'success');
     } else {
         HCCoder_PayPalAPI::UpdatePayment($result, 'failed');
     }
 }