public function balanceAction()
 {
     $session = Mage::getSingleton('core/session');
     if ($this->getRequest()->getMethod() == "POST") {
         try {
             // get our gift code from the submitted form
             $gcCode = (string) $this->getRequest()->getParam('giftcard_code');
             $apiUrl = "https://ps1.merchantware.net/Merchantware/ws/ExtensionServices/v4/Giftcard.asmx?WSDL";
             $merchName = $this->getConfigData("name");
             $siteId = $this->getConfigData("site_id");
             $key = $this->getConfigData("key");
             // Create our soap client
             $client = new SoapClient($apiUrl, array("trace" => 1));
             // Here is the data required to make this API call
             $card = array("merchantName" => $merchName, "merchantSiteId" => $siteId, "merchantKey" => $key, "cardNumber" => $gcCode);
             // Make the API call
             $result = $client->BalanceInquiryKeyed($card);
             // was approved
             if (strtolower($result->BalanceInquiryKeyedResult->ApprovalStatus) == "approved") {
                 // approved
                 if ($result->BalanceInquiryKeyedResult->CardBalance > 0) {
                     $session->addSuccess("Your gift card balance is: " . Mage::helper('core')->currency($result->BalanceInquiryKeyedResult->CardBalance, true, false));
                 } else {
                     $error = $this->__('The gift card you entered has a zero balance.');
                     $session->addError($error);
                 }
             } else {
                 if (strtolower($result->BalanceInquiryKeyedResult->ApprovalStatus) == "declined") {
                     // not approved
                     $error = $this->__('The gift card you entered was declined.');
                     $session->addError($error);
                 } else {
                     // error
                     $error = $this->__('There was an error verifying your gift card: ') . $result->BalanceInquiryKeyedResult->ErrorMessage;
                     $session->addError($error);
                 }
             }
         } catch (Exception $e) {
             $session->addError($this->__('There was an error connecting to the gift card gateway: ') . $e->getMessage());
         }
     }
     $this->loadLayout(array('default'));
     $this->renderLayout();
 }
Example #2
0
 public function salesOrderPaymentPlaceStart($observer)
 {
     $quote = Mage::getSingleton('checkout/session')->getQuote();
     $address = $quote->getShippingAddress();
     $gcDiscount = $address->getGiftcardDiscount();
     $gcTotal = Mage::getSingleton('core/session')->getGiftcardAmount();
     $gcCode = Mage::getSingleton('core/session')->getGiftcardCode();
     if ($gcDiscount > 0) {
         $apiUrl = "https://ps1.merchantware.net/Merchantware/ws/ExtensionServices/v4/Giftcard.asmx?WSDL";
         $merchName = $this->getConfigData("name");
         $siteId = $this->getConfigData("site_id");
         $key = $this->getConfigData("key");
         // Create our soap client
         $client = new SoapClient($apiUrl, array("trace" => 1));
         // Here is the data required to make this API call
         $card = array("merchantName" => $merchName, "merchantSiteId" => $siteId, "merchantKey" => $key, "cardNumber" => $gcCode);
         // Make the API call
         $result = $client->BalanceInquiryKeyed($card);
         // was approved
         if (strtolower($result->BalanceInquiryKeyedResult->ApprovalStatus) == "approved") {
             // approved
             // make sure they card has enough balance to cover the discount
             if ($gcDiscount > $result->BalanceInquiryKeyedResult->CardBalance) {
                 Mage::getSingleton('core/session')->addError($this->__('There was an error with your gift card. The amount changed during checkout.'));
                 $url = Mage::getUrl('checkout/cart');
                 //eg to redirect to cart page
                 $response = Mage::app()->getFrontController()->getResponse();
                 $response->setRedirect($url);
                 $controllerAction = $observer->getEvent()->getControllerAction();
                 $result = array();
                 $result['error'] = '-1';
                 $result['message'] = 'Error with Gift Card';
                 $controllerAction->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
                 exit;
             }
         } else {
             if (strtolower($result->BalanceInquiryKeyedResult->ApprovalStatus) == "declined") {
                 // not approved
                 Mage::getSingleton('core/session')->addError($this->__('The gift card you entered was declined.'));
                 $url = Mage::getUrl('checkout/cart');
                 //eg to redirect to cart page
                 $response = Mage::app()->getFrontController()->getResponse();
                 $response->setRedirect($url);
                 $controllerAction = $observer->getEvent()->getControllerAction();
                 $result = array();
                 $result['error'] = '-1';
                 $result['message'] = 'Error with Gift Card';
                 $controllerAction->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
                 exit;
             } else {
                 // error
                 Mage::getSingleton('core/session')->addError($this->__('There was an error verifying your gift card: ') . $result->BalanceInquiryKeyedResult->ErrorMessage);
                 $url = Mage::getUrl('checkout/cart');
                 //eg to redirect to cart page
                 $response = Mage::app()->getFrontController()->getResponse();
                 $response->setRedirect($url);
                 $controllerAction = $observer->getEvent()->getControllerAction();
                 $result = array();
                 $result['error'] = '-1';
                 $result['message'] = 'Error with Gift Card';
                 $controllerAction->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
                 exit;
             }
         }
     }
     return $this;
 }