File indexing completed on 2024-05-12 05:58:38

0001 <?php
0002 /**
0003  *  ocs-webserver
0004  *
0005  *  Copyright 2016 by pling GmbH.
0006  *
0007  *    This file is part of ocs-webserver.
0008  *
0009  *    This program is free software: you can redistribute it and/or modify
0010  *    it under the terms of the GNU Affero General Public License as
0011  *    published by the Free Software Foundation, either version 3 of the
0012  *    License, or (at your option) any later version.
0013  *
0014  *    This program is distributed in the hope that it will be useful,
0015  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
0016  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0017  *    GNU Affero General Public License for more details.
0018  *
0019  *    You should have received a copy of the GNU Affero General Public License
0020  *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
0021  **/
0022 class GatewayController extends Zend_Controller_Action
0023 {
0024 
0025     public function indexAction()
0026     {
0027     }
0028 
0029     /**
0030      * Official OCS API to receive messages from PayPal.
0031      */
0032     public function paypalpayoutAction()
0033     {
0034         $this->_helper->layout->disableLayout();
0035         $this->_helper->viewRenderer->setNoRender(true);
0036         
0037         try {
0038             // It is really important to receive the information in this way. In some cases Zend can destroy the information
0039             // when parsing the data
0040             $rawPostData = file_get_contents('php://input');
0041             $ipnArray = $this->_parseRawMessage($rawPostData);
0042 
0043             //Save IPN in DB
0044             $ipnTable = new Default_Model_DbTable_PaypalIpn();
0045             $ipnTable->addFromIpnMessage($ipnArray, $rawPostData);
0046 
0047             Zend_Registry::get('logger')->info(__METHOD__ . ' - Start Process PayPal Payout IPN - ');
0048 
0049             Zend_Registry::get('logger')->info(__METHOD__ . ' - Start Process Payout IPN - ');
0050             $modelPayPal = new Default_Model_PayPal_PayoutIpnMessage();
0051             $modelPayPal->processIpn($rawPostData);
0052 
0053         } catch (Exception $exc) {
0054             //Do nothing...
0055             Zend_Registry::get('logger')->info(__METHOD__ . ' - Error by Processing PayPal Payout IPN - ExceptionTrace: '. $exc->getTraceAsString());
0056         }
0057             
0058     }
0059     
0060     
0061     /**
0062      * Official OCS API to receive messages from PayPal.
0063      */
0064     public function paypalAction()
0065     {
0066         $this->_helper->layout->disableLayout();
0067         $this->_helper->viewRenderer->setNoRender(true);
0068 
0069         // It is really important to receive the information in this way. In some cases Zend can destroy the information
0070         // when parsing the data
0071         $rawPostData = file_get_contents('php://input');
0072 
0073         Zend_Registry::get('logger')->info(__METHOD__ . ' - Start Process PayPal IPN - ');
0074         
0075         $ipnArray = $this->_parseRawMessage($rawPostData);
0076         
0077         //Save IPN in DB
0078         $ipnTable = new Default_Model_DbTable_PaypalIpn();
0079         $ipnTable->addFromIpnMessage($ipnArray, $rawPostData);
0080         
0081         //Switch betwee AdaptivePayment and Masspay
0082         if (isset($ipnArray['txn_type']) AND ($ipnArray['txn_type'] == 'masspay')) {
0083             Zend_Registry::get('logger')->info(__METHOD__ . ' - Start Process Masspay IPN - ');
0084             $modelPayPal = new Default_Model_PayPal_MasspayIpnMessage();
0085             $modelPayPal->processIpn($rawPostData);
0086         } else if (isset($ipnArray['txn_type']) AND ($ipnArray['txn_type'] == 'web_accept')) {
0087             Zend_Registry::get('logger')->info(__METHOD__ . ' - Start Process Support IPN - ');
0088             $modelPayPal = new Default_Model_PayPal_SupportIpnMessage();
0089             $modelPayPal->processIpn($rawPostData);
0090         } else if (isset($ipnArray['txn_type']) AND ($ipnArray['txn_type'] == 'subscr_signup')) {
0091             Zend_Registry::get('logger')->info(__METHOD__ . ' - Start Process SubscriptionSignup IPN - ');
0092             $modelPayPal = new Default_Model_PayPal_SubscriptionSignupIpnMessage();
0093             $modelPayPal->processIpn($rawPostData);
0094         } else if (isset($ipnArray['txn_type']) AND (($ipnArray['txn_type'] == 'subscr_cancel') || ($ipnArray['txn_type'] == 'subscr_failed')  || ($ipnArray['txn_type'] == 'recurring_payment_suspended_due_to_max_failed_paym'))) {
0095             Zend_Registry::get('logger')->info(__METHOD__ . ' - Start Process SubscriptionSignupCancel IPN - ');
0096             $modelPayPal = new Default_Model_PayPal_SubscriptionCancelIpnMessage();
0097             $modelPayPal->processIpn($rawPostData);
0098         } else if (isset($ipnArray['txn_type']) AND (($ipnArray['txn_type'] == 'subscr_eot'))) {
0099             Zend_Registry::get('logger')->info(__METHOD__ . ' - Subscription Ended Normaly, nothing to do -');
0100         } else if (isset($ipnArray['txn_type']) AND ($ipnArray['txn_type'] == 'subscr_payment')) {
0101             Zend_Registry::get('logger')->info(__METHOD__ . ' - Start Process SubscriptionPayment IPN - ');
0102             $modelPayPal = new Default_Model_PayPal_SubscriptionPaymentIpnMessage();
0103             $modelPayPal->processIpn($rawPostData);
0104         } else{
0105             Zend_Registry::get('logger')->info(__METHOD__ . ' - Start Process Normal IPN - ');
0106             $modelPayPal = new Default_Model_PayPal_IpnMessage();
0107             $modelPayPal->processIpn($rawPostData);
0108             
0109         }
0110 
0111     }
0112     
0113     private function _parseRawMessage($raw_post)
0114     {
0115         //log_message('error', "testing");
0116         if (empty($raw_post)) {
0117             return array();
0118         } # else:
0119         $parsedPost = array();
0120         $pairs = explode('&', $raw_post);
0121         foreach ($pairs as $pair) {
0122             list($key, $value) = explode('=', $pair, 2);
0123             $key = urldecode($key);
0124             $value = urldecode($value);
0125             # This is look for a key as simple as 'return_url' or as complex as 'somekey[x].property'
0126 //            preg_match('/(\w+)(?:\[(\d+)\])?(?:\.(\w+))?/', $key, $key_parts);
0127             preg_match('/(\w+)(?:(?:\[|\()(\d+)(?:\]|\)))?(?:\.(\w+))?/', $key, $key_parts);
0128             switch (count($key_parts)) {
0129                 case 4:
0130                     # Original key format: somekey[x].property
0131                     # Converting to $post[somekey][x][property]
0132                     if (false === isset($parsedPost[$key_parts[1]])) {
0133                         if (empty($key_parts[2]) && '0' != $key_parts[2]) {
0134                             $parsedPost[$key_parts[1]] = array($key_parts[3] => $value);
0135                         } else {
0136                             $parsedPost[$key_parts[1]] = array($key_parts[2] => array($key_parts[3] => $value));
0137                         }
0138                     } else {
0139                         if (false === isset($parsedPost[$key_parts[1]][$key_parts[2]])) {
0140                             if (empty($key_parts[2]) && '0' != $key_parts[2]) {
0141                                 $parsedPost[$key_parts[1]][$key_parts[3]] = $value;
0142                             } else {
0143                                 $parsedPost[$key_parts[1]][$key_parts[2]] = array($key_parts[3] => $value);
0144                             }
0145                         } else {
0146                             $parsedPost[$key_parts[1]][$key_parts[2]][$key_parts[3]] = $value;
0147                         }
0148                     }
0149                     break;
0150                 case 3:
0151                     # Original key format: somekey[x]
0152                     # Converting to $post[somekey][x]
0153                     if (!isset($parsedPost[$key_parts[1]])) {
0154                         $parsedPost[$key_parts[1]] = array();
0155                     }
0156                     $parsedPost[$key_parts[1]][$key_parts[2]] = $value;
0157                     break;
0158                 default:
0159                     # No special format
0160                     $parsedPost[$key] = $value;
0161                     break;
0162             }
0163             #switch
0164         }
0165         #foreach
0166 
0167         return $parsedPost;
0168     }
0169 
0170     public function dwollaAction()
0171     {
0172         $this->_helper->layout->disableLayout();
0173         $this->_helper->viewRenderer->setNoRender(true);
0174 
0175         // It is really important to receive the information in this way. In some cases Zend can destroy the information
0176         // when parsing the data
0177         $rawPostData = file_get_contents('php://input');
0178 
0179         Zend_Registry::get('logger')->info(__METHOD__ . ' - Start Process Dwolla IPN - ');
0180         Zend_Registry::get('logger')->debug(__METHOD__ . ' - rawpostdata - ' . print_r($rawPostData, true));
0181 
0182         $modelDwolla = new Default_Model_Dwolla_Callback();
0183         $modelDwolla->processCallback($rawPostData);
0184     }
0185 
0186     public function amazonAction()
0187     {
0188         $this->_helper->layout->disableLayout();
0189         $this->_helper->viewRenderer->setNoRender(true);
0190 
0191         // It is really important to receive the information in this way. In some cases Zend can destroy the information
0192         // when parsing the data
0193         $rawPostData = file_get_contents('php://input');
0194 
0195 //        $modelAmazon = new Default_Model_Amazon_IpnMessage();
0196 //        $modelAmazon->processCallback($rawPostData);
0197 
0198         Zend_Registry::get('logger')->info(__METHOD__ . ' - Start Process Amazon IPN - ');
0199         Zend_Registry::get('logger')->debug(__METHOD__ . ' - rawpostdata - ' . print_r($rawPostData, true));
0200     }
0201 
0202     public function dwollahookAction()
0203     {
0204         $this->_helper->layout->disableLayout();
0205         $this->_helper->viewRenderer->setNoRender(true);
0206 
0207         // It is really important to receive the information in this way. In some cases Zend can destroy the information
0208         // when parsing the data
0209         $rawPostData = file_get_contents('php://input');
0210 
0211         Zend_Registry::get('logger')->info(__METHOD__ . ' - Start Process Dwolla IPN - ');
0212         Zend_Registry::get('logger')->debug(__METHOD__ . ' - rawpostdata - ' . print_r($rawPostData, true));
0213     }
0214 
0215 }