File indexing completed on 2025-05-04 05:29:11
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_PayoutIpnMessage extends Local_Payment_PayPal_AdaptivePayment_Ipn 0024 { 0025 0026 /** @var \Default_Model_Pling */ 0027 protected $_tablePayout; 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->_tablePayout = new Default_Model_DbTable_MemberPayout(); 0042 } 0043 0044 0045 /** 0046 * @param $rawData 0047 * @throws Exception 0048 */ 0049 public function processIpn($rawData) 0050 { 0051 Zend_Registry::get('logger')->info(__METHOD__ . ' - Start Process Payout IPN in Default_Model_PayPal_PayoutIpnMessage - '); 0052 if (false === $this->verifyIpnOrigin($rawData)) { 0053 $this->_logger->err(__FUNCTION__ . '::Abort IPN processing. IPN not verified: ' . $rawData); 0054 return; 0055 } 0056 0057 $this->_dataIpn = $this->_parseRawMessage($rawData); 0058 $this->_logger->info(__FUNCTION__ . '::_dataIpn: ' . print_r($this->_dataIpn, true) . "\n"); 0059 0060 $this->_ipnMessage = Local_Payment_PayPal_Response::buildResponse($this->_dataIpn); 0061 $this->_logger->info(__FUNCTION__ . '::_ipnMessage: ' . print_r($this->_ipnMessage, true) . "\n"); 0062 0063 if (false === $this->validateTransaction()) { 0064 $this->_logger->err(__FUNCTION__ . '::Abort IPN processing. Transaction not valid:' . $rawData); 0065 return; 0066 } 0067 0068 $this->processPaymentStatus(); 0069 0070 } 0071 0072 protected function processPaymentStatus() 0073 { 0074 Zend_Registry::get('logger')->info(__METHOD__ . ' - Status: ' .$this->_ipnMessage->getTransactionStatus()); 0075 0076 switch ($this->_ipnMessage->getTransactionStatus()) { 0077 case 'COMPLETED': 0078 $this->_statusCompleted(); 0079 break; 0080 case 'INCOMPLETE': 0081 $this->_statusIncomplete(); 0082 break; 0083 case 'CREATED': 0084 $this->_statusCreated(); 0085 break; 0086 case 'DENIED': 0087 $this->_statusDenied(); 0088 break; 0089 case 'REVERSED': 0090 $this->_statusReserved(); 0091 break; 0092 case 'REFUNDED': 0093 $this->_statusRefunded(); 0094 break; 0095 case 'FAILED': 0096 $this->_statusFailed(); 0097 break; 0098 case 'PARTIALLY_REFUNDED': 0099 $this->_statusPartiallyRefunded(); 0100 break; 0101 case 'ERROR': 0102 $this->_statusError(); 0103 break; 0104 case 'REVERSALERROR': 0105 $this->_statusReversalError(); 0106 break; 0107 case 'PROCESSING': 0108 $this->_statusProcessing(); 0109 break; 0110 case 'PENDING': 0111 $this->_statusPending(); 0112 break; 0113 default: 0114 0115 //No normal transaction status, so look into status_for_sender_txn 0116 switch ($this->_ipnMessage->getTransactionForSenderStatus()) { 0117 case 'COMPLETED': 0118 $this->_statusCompleted(); 0119 break; 0120 case 'INCOMPLETE': 0121 $this->_statusIncomplete(); 0122 break; 0123 case 'CREATED': 0124 $this->_statusCreated(); 0125 break; 0126 case 'DENIED': 0127 $this->_statusDenied(); 0128 break; 0129 case 'REVERSED': 0130 $this->_statusReserved(); 0131 break; 0132 case 'REFUNDED': 0133 $this->_statusRefunded(); 0134 break; 0135 case 'FAILED': 0136 $this->_statusFailed(); 0137 break; 0138 case 'PARTIALLY_REFUNDED': 0139 $this->_statusPartiallyRefunded(); 0140 break; 0141 case 'ERROR': 0142 $this->_statusError(); 0143 break; 0144 case 'REVERSALERROR': 0145 $this->_statusReversalError(); 0146 break; 0147 case 'PROCESSING': 0148 $this->_statusProcessing(); 0149 break; 0150 case 'PENDING': 0151 $this->_statusPending(); 0152 break; 0153 default: 0154 throw new Local_Payment_Exception('2. Unknown transaction status from PayPal: IPN = ' . print_r($this->_ipnMessage)); 0155 } 0156 0157 throw new Local_Payment_Exception('1. Unknown transaction status from PayPal: IPN = ' . print_r($this->_ipnMessage)); 0158 } 0159 } 0160 0161 protected function validateTransaction() 0162 { 0163 $this->_dataIpn = $this->_tablePayout->fetchPayoutFromResponse($this->_ipnMessage)->toArray(); 0164 if (empty($this->_dataIpn)) { 0165 $this->_logger->err(__METHOD__ . ' - ' . 'No transaction found for IPN message.' . PHP_EOL); 0166 return false; 0167 } 0168 0169 $ckAmount = $this->_checkAmount(); 0170 $ckEmail = $this->_checkEmail($this->_dataIpn['paypal_mail']); 0171 0172 return $ckAmount AND $ckEmail; 0173 } 0174 0175 protected function _checkAmount() 0176 { 0177 $amount = isset($this->_dataIpn['amount']) ? $this->_dataIpn['amount'] : 0; 0178 $receiver_amount = (float)$amount - (float)$this->_config->facilitator_fee; 0179 $currency = new Zend_Currency('en_US'); 0180 $this->_logger->info(__METHOD__ . ' - ' . $this->_ipnMessage->getTransactionAmount() . ' == ' . $currency->getShortName() . ' ' . number_format((float)$amount, 2, '.', '')); 0181 return ($this->_ipnMessage->getTransactionAmount()) == $currency->getShortName() . ' ' . number_format((float)$amount, 2, '.', ''); 0182 } 0183 0184 protected function _checkEmail() 0185 { 0186 //$email = isset($this->_dataIpn['email']) ? $this->_dataIpn['email'] : ''; 0187 //$this->_logger->info(__METHOD__ . ' - ' . $this->_ipnMessage->getTransactionReceiver() . ' == ' . $email); 0188 //return $this->_ipnMessage->getTransactionReceiver() == $email; 0189 0190 return true; 0191 } 0192 0193 protected function _statusCompleted() 0194 { 0195 Zend_Registry::get('logger')->info(__METHOD__); 0196 $this->_processTransactionStatusCompleted(); 0197 } 0198 protected function _statusDenied() 0199 { 0200 Zend_Registry::get('logger')->info(__METHOD__); 0201 $this->_processTransactionStatusDenied(); 0202 } 0203 protected function _statusReserved() 0204 { 0205 Zend_Registry::get('logger')->info(__METHOD__); 0206 $this->_processTransactionStatusReserved(); 0207 } 0208 protected function _statusRefunded() 0209 { 0210 Zend_Registry::get('logger')->info(__METHOD__); 0211 $this->_processTransactionStatusRefunded(); 0212 } 0213 protected function _statusPending() 0214 { 0215 Zend_Registry::get('logger')->info(__METHOD__); 0216 $this->_processTransactionStatusPending(); 0217 } 0218 protected function _statusFailed() 0219 { 0220 Zend_Registry::get('logger')->info(__METHOD__); 0221 $this->_processTransactionStatusFailed(); 0222 } 0223 0224 protected function processTransactionStatus() 0225 { 0226 Zend_Registry::get('logger')->info(__METHOD__ . ' - IPN Status: ' .$this->_ipnMessage->getTransactionStatus()); 0227 $this->_tablePayout->updatePayoutTransactionStatusFromResponse($this->_ipnMessage); 0228 0229 0230 switch (strtoupper($this->_ipnMessage->getTransactionStatus())) { 0231 case 'COMPLETED': 0232 $this->_processTransactionStatusCompleted(); 0233 break; 0234 case 'PENDING': 0235 $this->_processTransactionStatusPending(); 0236 break; 0237 case 'DENIED': 0238 $this->_processTransactionStatusDenied(); 0239 break; 0240 case 'REVERSED': 0241 $this->_processTransactionStatusReserved(); 0242 break; 0243 case 'REFUNDED': 0244 $this->_processTransactionStatusRefunded(); 0245 break; 0246 case 'FAILED': 0247 $this->_processTransactionStatusFailed(); 0248 break; 0249 default: 0250 switch (strtoupper($this->_ipnMessage->getTransactionForSenderStatus())) { 0251 case 'PENDING': 0252 $this->_processTransactionStatusPending(); 0253 break; 0254 default: 0255 throw new Local_Payment_Exception('Unknown transaction status from PayPal: TransactionStatus = ' . $this->_ipnMessage->getTransactionStatus() . ' --- TransactionForSenderStatus = ' . $this->_ipnMessage->getTransactionForSenderStatus); 0256 } 0257 throw new Local_Payment_Exception('Unknown transaction status from PayPal: TransactionStatus = ' . $this->_ipnMessage->getTransactionStatus() . ' --- TransactionForSenderStatus = ' . $this->_ipnMessage->getTransactionForSenderStatus); 0258 } 0259 } 0260 0261 protected function _statusError() 0262 { 0263 $this->_tablePayout->deactivatePayoutFromResponse($this->_ipnMessage); 0264 } 0265 0266 protected function _processTransactionStatusCompleted() 0267 { 0268 Zend_Registry::get('logger')->info(__METHOD__); 0269 $this->_tablePayout->setPayoutStatusCompletedFromResponse($this->_ipnMessage); 0270 } 0271 0272 protected function _processTransactionStatusPending() 0273 { 0274 $this->_tablePayout->setPayoutStatusPendingFromResponse($this->_ipnMessage); 0275 } 0276 0277 protected function _processTransactionStatusRefunded() 0278 { 0279 $this->_tablePayout->setPayoutStatusRefundFromResponse($this->_ipnMessage); 0280 } 0281 0282 protected function _processTransactionStatusDenied() 0283 { 0284 $this->_tablePayout->setPayoutStatusDeniedFromResponse($this->_ipnMessage); 0285 } 0286 0287 protected function _processTransactionStatusReserved() 0288 { 0289 $this->_tablePayout->setPayoutStatusReservedFromResponse($this->_ipnMessage); 0290 } 0291 0292 protected function _processTransactionStatusFailed() 0293 { 0294 $this->_tablePayout->setPayoutStatusFailedFromResponse($this->_ipnMessage); 0295 } 0296 }