File indexing completed on 2024-05-26 05:59:17

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 
0023 class Default_Model_PayPal_IpnMessage extends Local_Payment_PayPal_AdaptivePayment_Ipn
0024 {
0025 
0026     /** @var \Default_Model_Pling */
0027     protected $_tablePling;
0028 
0029     function __construct($config = null, $logger = null)
0030     {
0031         if (null == $logger) {
0032             $logger = Zend_Registry::get('logger');
0033         }
0034 
0035         if (null == $config) {
0036             $config = Zend_Registry::get('config');
0037         }
0038 
0039         parent::__construct($config->third_party->paypal, $logger);
0040 
0041         $this->_tablePling = new Default_Model_Pling();
0042     }
0043 
0044     protected function validateTransaction()
0045     {
0046         if(!$this->_ipnMessage) {
0047             return false;
0048         } else {
0049             $this->_dataIpn = $this->_tablePling->fetchPlingFromResponse($this->_ipnMessage)->toArray();
0050             if (empty($this->_dataIpn)) {
0051                 $this->_logger->err(__METHOD__ . ' - ' . 'No transaction found for IPN message.' . PHP_EOL);
0052                 return false;
0053             }
0054 
0055             $tableProject = new Default_Model_Project();
0056             $member = $tableProject->find($this->_dataIpn['project_id'])->current()->findDependentRowset('Default_Model_DbTable_Member', 'Owner')->current();
0057 
0058             return $this->_checkAmount() AND $this->_checkEmail($member->paypal_mail);
0059         }
0060     }
0061 
0062     protected function _checkAmount()
0063     {
0064         $amount = isset($this->_dataIpn['amount']) ? $this->_dataIpn['amount'] : 0;
0065         $receiver_amount = (float)$amount - (float)$this->_config->facilitator_fee;
0066         $currency = new Zend_Currency('en_US');
0067         $this->_logger->debug(__METHOD__ . ' - ' . $this->_ipnMessage->getTransactionAmount() . ' == ' . $currency->getShortName() . ' ' . $receiver_amount);
0068         return $this->_ipnMessage->getTransactionAmount() == $currency->getShortName() . ' ' . $amount;
0069     }
0070 
0071     protected function _checkEmail()
0072     {
0073         $email = isset($this->_dataIpn['email']) ? $this->_dataIpn['email'] : '';
0074         $this->_logger->debug(__METHOD__ . ' - ' . $this->_ipnMessage->getTransactionReceiver() . ' == ' . $email);
0075         return $this->_ipnMessage->getTransactionReceiver() == $email;
0076     }
0077 
0078     protected function _statusCompleted()
0079     {
0080         $this->processTransactionStatus();
0081     }
0082 
0083     protected function _statusError()
0084     {
0085         $this->_tablePling->deactivatePlingsFromResponse($this->_ipnMessage);
0086     }
0087 
0088     protected function _processTransactionStatusCompleted()
0089     {
0090         $this->_tablePling->activatePlingsFromResponse($this->_ipnMessage);
0091     }
0092 
0093     protected function _processTransactionStatusPending()
0094     {
0095         $this->_tablePling->activatePlingsFromResponse($this->_ipnMessage);
0096     }
0097 
0098     protected function _processTransactionStatusRefunded()
0099     {
0100         $this->_tablePling->deactivatePlingsFromResponse($this->_ipnMessage);
0101     }
0102 
0103     protected function _processTransactionStatusDenied()
0104     {
0105         $this->_tablePling->deactivatePlingsFromResponse($this->_ipnMessage);
0106     }
0107 
0108 }