$errors = '';
$result = false;
$paypal = new AgilePaypal();
$customer_isnew = 0;
$params = 'cmd=_notify-validate';
foreach ($_POST as $key => $value) {
    $params .= '&' . $key . '=' . urlencode(stripslashes($value));
}
if (Agile_Logging == 'on') {
    $paypal->log_message($params);
}
$recurring_txn_types = array('subscr_payment', 'recurring_payment_outstanding_payment', 'recurring_payment');
$interest_txn_types = array_merge(array('cart'), $recurring_txn_types);
$txn_type = $_POST['txn_type'];
if (!in_array($txn_type, $interest_txn_types)) {
    die($paypal->getL('Non interest txn_type: (None interest txn_type, ignored)'));
}
$paypalServer = 'www.' . (Configuration::get('AGILE_PAYPAL_SANDBOX') ? 'sandbox.' : '') . 'paypal.com';
if (function_exists('curl_exec')) {
    $ch = curl_init('https://' . $paypalServer . '/cgi-bin/webscr');
    if (!$ch) {
        $ch = curl_init('https://' . $paypalServer . '/cgi-bin/webscr/');
    }
    if (!$ch) {
        $errors .= $paypal->getL('connect') . ' ' . $paypal->getL('curlmethodfailed');
    } else {
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
 public function agilepaypal_subscribe()
 {
     global $cookie, $cart, $smarty, $defaultCountry;
     $error_msg = '';
     $paypal = new AgilePaypal();
     $cart = new Cart(intval($cookie->id_cart));
     $expresscheckoutkey = md5(_PS_VERSION_ . $cookie->id_cart);
     $cycle_base = Tools::getValue('sl_agilepaypalexpress_cycle_base');
     $cycle = Tools::getValue('sl_agilepaypalexpress_cycle');
     $cycle_list = array('D', 'W', 'M', 'Y');
     if (!in_array($cycle, $cycle_list)) {
         $error_msg .= $paypal->getL('Recurring cycle error: (invalid or recurring cycle)') . "<BR>";
     }
     $cycle_num = Tools::getValue('sl_agilepaypalexpress_cycle_num');
     $address_override = 0;
     $islogged = Context::getContext()->customer->isLogged();
     if ($islogged) {
         $customer = new Customer(intval($cart->id_customer));
         $addresses = $customer->getAddresses($cookie->id_lang);
         if (!empty($addresses)) {
             $id_address = 0;
             foreach ($addresses as $addr) {
                 if (intval($cart->id_address_delivery) == intval($addr['id_address'])) {
                     $id_address = intval($addr['id_address']);
                     break;
                 }
             }
             if ($id_address > 0) {
                 $address = new Address($id_address);
             } else {
                 $address = new Address($addresses[0]['id_address']);
             }
         }
     }
     if (!isset($address) or !$address->id_country) {
         $address = new Address();
         $address->id_country = intval(Tools::getValue('sl_expresscheckout_id_country'));
     }
     if (!isset($address) or !$address->id_country) {
         $address = new Address();
         $address->id_country = (int) Configuration::get('PS_COUNTRY_DEFAULT');
     }
     $country = new Country(intval($address->id_country));
     $zone = new Zone(intval($country->id_zone));
     $doSubmit = ($country->active == 1 and $zone->active == 1 or $cart->isVirtualCart());
     $state = NULL;
     if ($address->id_state) {
         $state = new State(intval($address->id_state));
     }
     if ($islogged) {
         $customer = new Customer(intval($cart->id_customer));
     } else {
         $customer = new Customer();
         $customer->secure_key = md5(uniqid(rand(), true));
     }
     $business = $paypal->getSellerPaypalEmailAddress();
     $header = Configuration::get('AGILE_PAYPAL_HEADER');
     $currency_order = new Currency(intval($cart->id_currency));
     $currency_module = new Currency((int) Configuration::get('AGILE_PAYPAL_CURRENCY'));
     if (!Validate::isEmail($business)) {
         $error_msg .= $paypal->getL('Paypal error: (invalid or undefined business account email)') . "<BR>";
     }
     if (!Validate::isLoadedObject($currency_module)) {
         $error_msg .= $paypal->getL('Currency Restriction: (Invalid currency restriction setting for this module)') . "<BR>";
     }
     $customercurrency = $cookie->id_currency;
     $defaultCountryAgile = $defaultCountry;
     $defaultCountry = $country;
     $the_rate = $currency_order->conversion_rate / $currency_module->conversion_rate;
     $cartproducts = $cart->getProducts();
     $product1st = $cartproducts[0];
     $all_total = $cart->getOrderTotal(true, Cart::BOTH);
     $business2 = $paypal->getSellerPaypalMicroEmailAddress();
     $micro_amount = floatval(Configuration::get('AGILE_PAYPAL_MICRO_AMOUNT'));
     if (isset($business2) and strlen(trim($business2)) > 0 and isset($micro_amount) and floatval($micro_amount) > 0 and floatval($all_total) <= $micro_amount) {
         $business = $business2;
     }
     if (_PS_VERSION_ > '1.5') {
         $shipping = Tools::ps_round(floatval($cart->getOrderTotal(true, Cart::ONLY_SHIPPING)), 2);
     } else {
         $shipping = Tools::ps_round(floatval($cart->getOrderShippingCost()) + floatval($cart->getOrderTotal(true, Cart::ONLY_WRAPPING)), 2);
     }
     if (!empty($error_msg)) {
         $doSubmit = 0;
     }
     $smarty->assign(array('redirect_text' => $paypal->getL($doSubmit == 1 ? 'Please wait, redirecting to Paypal... Thanks.' : 'Sorry, we do not ship to your country.'), 'cancel_text' => $paypal->getL('Cancel'), 'cart_text' => $paypal->getL('My cart'), 'return_text' => $paypal->getL('Return to shop'), 'paypal_url' => $paypal->getPaypalUrl(), 'address' => $address, 'country' => $country, 'state' => $state, 'doSubmit' => $doSubmit, 'baseUrl' => __PS_BASE_URI__, 'address_override' => $address_override, 'amount' => floatval($cart->getOrderTotal(true, Cart::BOTH_WITHOUT_SHIPPING)), 'customer' => $customer, 'all_total' => $all_total, 'shipping' => $shipping, 'discount' => abs($cart->getOrderTotal(true, Cart::ONLY_DISCOUNTS)), 'business' => $business, 'currency_module' => $currency_module, 'cart_id' => intval($cart->id), 'products' => $cartproducts, 'product1st' => $product1st, 'paypal_id' => intval($paypal->id), 'invoice' => intval(Configuration::get('AGILE_MS_PAYMENT_MODE')), 'header' => $header, 'cycle_base' => $cycle_base, 'cycle' => $cycle, 'cycle_num' => $cycle_num, 'expresscheckoutkey' => $expresscheckoutkey, 'PS_ALLOW_MOBILE_DEVICE' => 0, 'agile_url' => (_PS_VERSION_ > '1.4' ? Tools::getShopDomainSsl(true, true) : Tools::getHttpHost(true, true)) . __PS_BASE_URI__, 'agilepaypal_return_url' => $this->get_return_url(), 'error_msg' => $error_msg, 'the_rate' => $the_rate));
     $cookie->id_currency = $customercurrency;
     $defaultCountry = $defaultCountryAgile;
 }