/**
  * 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');
     }
 }
if (isset($_GET['func']) && $_GET['func'] == 'confirm' && isset($_GET['token']) && isset($_GET['PayerID'])) {
    $result = HCCoder_PayPalAPI::ConfirmExpressCheckout();
    if (isset($_SESSION['RETURN_URL'])) {
        $url = $_SESSION['RETURN_URL'];
        unset($_SESSION['RETURN_URL']);
        header('Location: ' . $url);
        exit;
    }
    $_SESSION['result'] = serialize($result);
    if (is_numeric(get_option('paypal_success_page')) && get_option('paypal_success_page') > 0) {
        header('Location: ' . get_permalink(get_option('paypal_success_page')));
    } else {
        header('Location: ' . home_url());
    }
    exit;
}
if (!count($_POST)) {
    trigger_error('Payment error code: #00001', E_USER_ERROR);
}
$allowed_func = array('start');
if (count($_POST) && (!isset($_POST['func']) || !in_array($_POST['func'], $allowed_func))) {
    trigger_error('Payment error code: #00002', E_USER_ERROR);
}
if (count($_POST) && (!isset($_POST['AMT']) || !is_numeric($_POST['AMT']) || $_POST['AMT'] < 0)) {
    trigger_error('Payment error code: #00003', E_USER_ERROR);
}
switch ($_POST['func']) {
    case 'start':
        HCCoder_PayPalAPI::StartExpressCheckout();
        break;
}