public function requestPaymentAction()
 {
     // Changed By Adam 28/07/2014
     if (!Mage::helper('affiliateplus')->isAffiliateModuleEnabled()) {
         return $this->_redirectUrl(Mage::getBaseUrl());
     }
     if (!Mage::helper('magenotification')->checkLicenseKeyFrontController($this)) {
         return;
     }
     if ($this->_getAccountHelper()->accountNotLogin()) {
         return $this->_redirect('affiliateplus/account/login');
     }
     //if(!$this->getRequest()->isPost())
     //return $this->_redirect('affiliateplus/index/paymentForm');
     if ($this->_getAccountHelper()->disableWithdrawal()) {
         $this->_getCoreSession()->addError($this->__('Request withdrawal not allowed at this time.'));
         if (!$this->_getAccountHelper()->disableStoreCredit()) {
             return $this->_redirect('affiliateplus/index/payments');
         }
         return $this->_redirect('affiliateplus/index/listTransaction');
     }
     $paymentCodes = $this->_getPaymentHelper()->getAvailablePaymentCode();
     if (!count($paymentCodes)) {
         $this->_getCoreSession()->addError($this->__('There is no payment method on file for your account. Please update your details or contact us to solve the problem.'));
         return $this->_redirect('affiliateplus/index/payments');
     } elseif (count($paymentCodes) == 1) {
         $paymentCode = $this->getRequest()->getParam('payment_method');
         if (!$paymentCode) {
             $paymentCode = current($paymentCodes);
         }
     } else {
         $paymentCode = $this->getRequest()->getParam('payment_method');
     }
     if (!$paymentCode) {
         $this->_getCoreSession()->addNotice($this->__('Please chose an available payment method!'));
         return $this->_redirect('affiliateplus/index/paymentForm', $this->getRequest()->getPost());
     }
     if (!in_array($paymentCode, $paymentCodes)) {
         $this->_getCoreSession()->addError($this->__('This payment method is not available, please choose an alternative payment method.'));
         return $this->_redirect('affiliateplus/index/paymentForm', $this->getRequest()->getPost());
     }
     $account = $this->_getAccountHelper()->getAccount();
     $store = Mage::app()->getStore();
     $amount = $this->getRequest()->getParam('amount');
     $amount = $amount / $store->convertPrice(1);
     if ($amount < $this->_getConfigHelper()->getPaymentConfig('payment_release')) {
         $this->_getCoreSession()->addNotice($this->__('The minimum balance required to request withdrawal is %s', Mage::helper('core')->currency($this->_getConfigHelper()->getPaymentConfig('payment_release'), true, false)));
         return $this->_redirect('affiliateplus/index/paymentForm');
     }
     if ($amountInclTax = $this->getRequest()->getParam('amount_incl_tax')) {
         if ($amountInclTax > $amount && $amountInclTax > $account->getBalance()) {
             $this->_getCoreSession()->addError($this->__('The withdrawal requested cannot exceed your current balance (%s).', Mage::helper('core')->currency($account->getBalance(), true, false)));
             return $this->_redirect('affiliateplus/index/paymentForm');
         }
     }
     if ($amount > $account->getBalance()) {
         $this->_getCoreSession()->addError($this->__('The withdrawal requested cannot exceed your current balance (%s).', Mage::helper('core')->currency($account->getBalance(), true, false)));
         return $this->_redirect('affiliateplus/index/paymentForm');
     }
     $payment = Mage::getModel('affiliateplus/payment')->setPaymentMethod($paymentCode)->setAmount($amount)->setAccountId($account->getId())->setAccountName($account->getName())->setAccountEmail($account->getEmail())->setRequestTime(now())->setStatus(1)->setIsRequest(1)->setIsPayerFee(0);
     if ($this->_getConfigHelper()->getPaymentConfig('who_pay_fees') == 'payer' && $paymentCode == 'paypal') {
         $payment->setIsPayerFee(1);
     }
     if ($payment->hasWaitingPayment()) {
         $this->_getCoreSession()->addError($this->__('You are having a pending request!'));
         return $this->_redirect('affiliateplus/index/payments');
     }
     if ($this->_getConfigHelper()->getSharingConfig('balance') == 'store') {
         $payment->setStoreIds($store->getId());
     }
     $paymentMethod = $payment->getPayment();
     $paymentObj = new Varien_Object(array('payment_code' => $paymentCode, 'required' => true));
     Mage::dispatchEvent("affiliateplus_request_payment_action_{$paymentCode}", array('payment_obj' => $paymentObj, 'payment' => $payment, 'payment_method' => $paymentMethod, 'request' => $this->getRequest()));
     $paymentCode = $paymentObj->getPaymentCode();
     if ($paymentCode == 'paypal') {
         $paypalEmail = $this->getRequest()->getParam('paypal_email');
         //Change paypal email for affiliate account
         if ($paypalEmail && $paypalEmail != $account->getPaypalEmail()) {
             $accountModel = Mage::getModel('affiliateplus/account')->setStoreId($store->getId())->load($account->getId());
             try {
                 $accountModel->setPaypalEmail($paypalEmail)->setId($account->getId())->save();
             } catch (Exception $e) {
             }
         }
         $paypalEmail = $paypalEmail ? $paypalEmail : $account->getPaypalEmail();
         if ($paypalEmail) {
             $paymentMethod->setEmail($paypalEmail);
             $paymentObj->setRequired(false);
         }
     }
     if ($paymentObj->getRequired()) {
         $this->_getCoreSession()->addNotice($this->__('Please fill out all required fields in the form below.'));
         return $this->_redirect('affiliateplus/index/paymentForm', $this->getRequest()->getPost());
     }
     // Save request payment for affiliate account
     try {
         $payment->save();
         $paymentMethod->savePaymentMethodInfo();
         $payment->sendMailRequestPaymentToSales();
         $this->_getCoreSession()->addSuccess($this->__('Your request has been sent to admin for approval.'));
     } catch (Exception $e) {
         $this->_getCoreSession()->addError($e->getMessage());
     }
     return $this->_redirect('affiliateplus/index/payments');
 }
 public function requestPaymentAction()
 {
     if (!Mage::helper('magenotification')->checkLicenseKeyFrontController($this)) {
         return;
     }
     if ($this->_getAccountHelper()->accountNotLogin()) {
         return $this->_redirect('affiliateplus/account/login');
     }
     if (!$this->getRequest()->isPost()) {
         return $this->_redirect('affiliateplus/index/paymentForm');
     }
     $paymentCodes = $this->_getPaymentHelper()->getAvailablePaymentCode();
     if (!count($paymentCodes)) {
         $this->_getCoreSession()->addError($this->__('There is no payment method in the system. Please contact us to solve the problem.'));
         return $this->_redirect('affiliateplus/index/payments');
     } elseif (count($paymentCodes) == 1) {
         $paymentCode = $this->getRequest()->getParam('payment_method');
         if (!$paymentCode) {
             $paymentCode = current($paymentCodes);
         }
     } else {
         $paymentCode = $this->getRequest()->getParam('payment_method');
     }
     if (!$paymentCode) {
         $this->_getCoreSession()->addNotice($this->__('Please chose an available payment method!'));
         return $this->_redirect('affiliateplus/index/paymentForm', $this->getRequest()->getPost());
     }
     if (!in_array($paymentCode, $paymentCodes)) {
         $this->_getCoreSession()->addError($this->__('This payment method not available, please chose an available payment method!'));
         return $this->_redirect('affiliateplus/index/paymentForm', $this->getRequest()->getPost());
     }
     $account = $this->_getAccountHelper()->getAccount();
     $store = Mage::app()->getStore();
     $amount = $this->getRequest()->getParam('amount');
     if ($amount < $this->_getConfigHelper()->getPaymentConfig('payment_release')) {
         $baseCurrency = $store->getBaseCurrency();
         $this->_getCoreSession()->addNotice($this->__('Minimum balance to request payment is %s', $baseCurrency->format($this->_getConfigHelper()->getPaymentConfig('payment_release'), array(), false)));
         return $this->_redirect('affiliateplus/index/paymentForm');
     }
     if ($amount > $account->getBalance()) {
         $this->_getCoreSession()->addError($this->__('The amount of the payment request cannot exceed your balance: %s.', $baseCurrency->format($account->getBalance(), array(), false)));
         return $this->_redirect('affiliateplus/index/paymentForm');
     }
     $payment = Mage::getModel('affiliateplus/payment')->setPaymentMethod($paymentCode)->setAmount($amount)->setAccountId($account->getId())->setAccountName($account->getName())->setAccountEmail($account->getEmail())->setRequestTime(now())->setStatus(1)->setIsRequest(1)->setIsPayerFee(0);
     if ($this->_getConfigHelper()->getPaymentConfig('who_pay_fees') == 'payer' && $paymentCode == 'paypal') {
         $payment->setIsPayerFee(1);
     }
     if ($payment->hasWaitingPayment()) {
         $this->_getCoreSession()->addError($this->__('You are having a waiting request!'));
         return $this->_redirect('affiliateplus/index/payments');
     }
     if ($this->_getConfigHelper()->getSharingConfig('balance') == 'store') {
         $payment->setStoreIds($store->getId());
     }
     $paymentMethod = $payment->getPayment();
     $paymentObj = new Varien_Object(array('payment_code' => $paymentCode, 'required' => true));
     Mage::dispatchEvent("affiliateplus_request_payment_action_{$paymentCode}", array('payment_obj' => $paymentObj, 'payment' => $payment, 'payment_method' => $paymentMethod, 'request' => $this->getRequest()));
     $paymentCode = $paymentObj->getPaymentCode();
     if ($paymentCode == 'paypal') {
         $paypalEmail = $this->getRequest()->getParam('paypal_email');
         //Change paypal email for affiliate account
         if ($paypalEmail && $paypalEmail != $account->getPaypalEmail()) {
             $accountModel = Mage::getModel('affiliateplus/account')->setStoreId($store->getId())->load($account->getId());
             try {
                 $accountModel->setPaypalEmail($paypalEmail)->setId($account->getId())->save();
             } catch (Exception $e) {
             }
         }
         $paypalEmail = $paypalEmail ? $paypalEmail : $account->getPaypalEmail();
         if ($paypalEmail) {
             $paymentMethod->setEmail($paypalEmail);
             $paymentObj->setRequired(false);
         }
     }
     if ($paymentObj->getRequired()) {
         $this->_getCoreSession()->addNotice($this->__('Please complete required fields in form below.'));
         return $this->_redirect('affiliateplus/index/paymentForm', $this->getRequest()->getPost());
     }
     // Save request payment for affiliate account
     try {
         $payment->save();
         $paymentMethod->savePaymentMethodInfo();
         $payment->sendMailRequestPaymentToSales();
         $this->_getCoreSession()->addSuccess($this->__('Your request has been sent successfully!'));
     } catch (Exception $e) {
         $this->_getCoreSession()->addError($e->getMessage());
     }
     return $this->_redirect('affiliateplus/index/payments');
 }