File indexing completed on 2025-08-03 05:34:04

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 abstract class Local_Payment_PayPal_AdaptivePayment_Gateway
0024     extends Local_Payment_PayPal_Base
0025     implements Local_Payment_GatewayInterface
0026 {
0027 
0028     const ATTRIBUTE_FALSE = 'false';
0029     const ATTRIBUTE_TRUE = 'true';
0030     const ATTRIBUTE_FEE_PAYER_SENDER = 'SENDER';
0031     const ATTRIBUTE_FEE_PAYER_PRIMARY = 'PRIMARYRECEIVER';
0032     const ATTRIBUTE_FEE_PAYER_EACH = 'EACHRECEIVER';
0033     const ATTRIBUTE_FEE_PAYER_SECONDARY = 'SECONDARYONLY';
0034     const ATTRIBUTE_CURRENCY_USD = 'USD';
0035     const ATTRIBUTE_ACTION_PAY = 'PAY';
0036 
0037     const SUCCESS = 'Success';
0038     const VERIFIED = 'VERIFIED';
0039     const RESPONSE_ENVELOPE_ACK = 'responseEnvelope_ack';
0040     const ACCOUNT_STATUS = 'accountStatus';
0041 
0042     const API_ADAPTIVE_PAYMENTS = 'AdaptivePayments';
0043 
0044     const OPERATION_PAY = 'Pay';
0045 
0046     /** @var \Zend_Config */
0047     protected $_config;
0048     /** @var \Zend_Log */
0049     protected $_logger;
0050 
0051     protected $_ipnNotificationUrl;
0052     protected $_cancelUrl;
0053     protected $_returnUrl;
0054 
0055     /** @var  \Local_Payment_PayPal_UserData */
0056     protected $_paymentUserData;
0057     /** @var  array */
0058     protected $_dataIpn;
0059 
0060     /**
0061      * @param float $amount
0062      * @param string $requestMsg
0063      * @throws Local_Payment_Exception
0064      * @return Local_Payment_PayPal_AdaptivePayment_ResponsePay | mixed
0065      */
0066     public function requestPayment($amount, $requestMsg = null, $senderMail = null)
0067     {
0068         $log = $this->_logger;
0069 
0070         $log->info('********** Start PayPal Payment **********');
0071         $log->info(__FUNCTION__);
0072         $log->debug(APPLICATION_ENV);
0073 
0074         if (empty($this->_returnUrl)) {
0075             throw new Local_Payment_Exception('return url was not set.');
0076         }
0077 
0078         if (empty($this->_cancelUrl)) {
0079             throw new Local_Payment_Exception('cancel return url was not set.');
0080         }
0081 
0082         if (empty($this->_ipnNotificationUrl)) {
0083             throw new Local_Payment_Exception('ipn notification url was not set.');
0084         }
0085 
0086         $receiver_amount = $amount - (float)$this->_config->facilitator_fee;
0087 
0088         $bodyParameter = array(
0089             'requestEnvelope.errorLanguage' => "en_US",
0090             'actionType' => self::ATTRIBUTE_ACTION_PAY,
0091             'currencyCode' => self::ATTRIBUTE_CURRENCY_USD,
0092             'feesPayer' => self::ATTRIBUTE_FEE_PAYER_EACH,
0093             'receiverList.receiver(0).email' => $this->_paymentUserData->getPaymentUserId(),
0094             'receiverList.receiver(0).amount' => $receiver_amount,
0095             'receiverList.receiver(0).primary' => self::ATTRIBUTE_FALSE,
0096             'requestEnvelope.detailLevel' => 'ReturnAll',
0097             'reverseAllParallelPaymentsOnError' => self::ATTRIBUTE_TRUE,
0098             'clientDetails.ipAddress' => $_SERVER ['REMOTE_ADDR'],
0099             'clientDetails.deviceId' => $_SERVER ['SERVER_ADDR'],
0100             'clientDetails.applicationId' => $this->_config->client->application_id,
0101             'clientDetails.partnerName' => $this->_config->client->partner_name,
0102             'cancelUrl' => $this->_cancelUrl,
0103             'returnUrl' => $this->_returnUrl,
0104             'ipnNotificationUrl' => $this->_ipnNotificationUrl,
0105             'memo' => $requestMsg
0106         );
0107         
0108         if($senderMail) {
0109             $bodyParameter['senderEmail'] = $senderMail;
0110         }
0111 
0112         if ((float)$this->_config->facilitator_fee > 0.00 AND isset($this->_config->facilitator_fee_receiver)) {
0113             $bodyParameter['receiverList.receiver(1).email'] = $this->_config->facilitator_fee_receiver;
0114             $bodyParameter['receiverList.receiver(1).amount'] = $this->_config->facilitator_fee;
0115             $bodyParameter['receiverList.receiver(1).primary'] = self::ATTRIBUTE_FALSE;
0116         }
0117 
0118         $response = $this->_makeRequest($bodyParameter, self::API_ADAPTIVE_PAYMENTS, self::OPERATION_PAY);
0119 
0120         $log->info('********** Finished PayPal Payment **********');
0121 
0122         $paypalResponse = new Local_Payment_PayPal_AdaptivePayment_ResponsePayRequest($response);
0123         if (false === $paypalResponse->isSuccessful()) {
0124             throw new Local_Payment_Exception('PayPal payment request failed. Request response:' . print_r($paypalResponse->getRawMessage(), true));
0125         }
0126 
0127         return $paypalResponse;
0128     }
0129     
0130     /**
0131      * @param float $amount
0132      * @param string $requestMsg
0133      * @throws Local_Payment_Exception
0134      * @return Local_Payment_PayPal_AdaptivePayment_ResponsePay | mixed
0135      */
0136     public function requestPaymentForPayout($senderMail, $receiverMail, $amount, $trackingId, $yearmonth)
0137     {
0138         $log = $this->_logger;
0139 
0140         $log->info('********** Start PayPal Payment for Payout **********');
0141         $log->info(__FUNCTION__);
0142         
0143         $log->debug(APPLICATION_ENV);
0144 
0145         if (empty($this->_returnUrl)) {
0146             throw new Local_Payment_Exception('return url was not set.');
0147         }
0148 
0149         if (empty($this->_cancelUrl)) {
0150             throw new Local_Payment_Exception('cancel return url was not set.');
0151         }
0152 
0153         if (empty($this->_ipnNotificationUrl)) {
0154             throw new Local_Payment_Exception('ipn notification url was not set.');
0155         }
0156 
0157         $receiver_amount = $amount - (float)$this->_config->facilitator_fee;
0158 
0159         $bodyParameter = array(
0160             'requestEnvelope.errorLanguage' => "en_US",
0161             'senderEmail' => $senderMail,
0162             'actionType' => self::ATTRIBUTE_ACTION_PAY,
0163             'currencyCode' => self::ATTRIBUTE_CURRENCY_USD,
0164             'feesPayer' => self::ATTRIBUTE_FEE_PAYER_EACH,
0165             //'trackingId' => $trackingId,
0166             'receiverList.receiver(0).email' => $receiverMail,
0167             'receiverList.receiver(0).amount' => $receiver_amount,
0168             'receiverList.receiver(0).primary' => self::ATTRIBUTE_FALSE,
0169             'requestEnvelope.detailLevel' => 'ReturnAll',
0170             'reverseAllParallelPaymentsOnError' => self::ATTRIBUTE_TRUE,
0171             'clientDetails.applicationId' => $this->_config->client->application_id,
0172             'clientDetails.partnerName' => $this->_config->client->partner_name,
0173             'cancelUrl' => $this->_cancelUrl,
0174             'returnUrl' => $this->_returnUrl,
0175             'ipnNotificationUrl' => $this->_ipnNotificationUrl,
0176             'memo' => 'OpenDesktop.org payout for month: '.$yearmonth
0177         );
0178         
0179         $response = $this->_makeRequest($bodyParameter, self::API_ADAPTIVE_PAYMENTS, self::OPERATION_PAY);
0180 
0181         $log->info('********** Finished PayPal Payment for Payout **********');
0182 
0183         $paypalResponse = new Local_Payment_PayPal_AdaptivePayment_ResponsePayRequest($response);
0184         if (false === $paypalResponse->isSuccessful()) {
0185             throw new Local_Payment_Exception('PayPal payment request failed. Request response:' . print_r($paypalResponse->getRawMessage(), true));
0186         }
0187 
0188         return $paypalResponse;
0189     }
0190 
0191     /**
0192      * @param Local_Payment_UserDataInterface $userData
0193      * @throws Exception
0194      */
0195     public function setUserDataStore($userData)
0196     {
0197         if (false === ($userData instanceof Local_Payment_UserDataInterface)) {
0198             throw new Exception('Wrong data type for user data');
0199         }
0200         $this->_paymentUserData = $userData;
0201     }
0202 
0203     /**
0204      * @return Local_Payment_PayPal_UserData
0205      */
0206     public function getUserDataStore()
0207     {
0208         return $this->_paymentUserData;
0209     }
0210 
0211     /**
0212      * @return string
0213      */
0214     public function getCancelUrl()
0215     {
0216         return $this->_cancelUrl;
0217     }
0218 
0219     /**
0220      * @param string $cancelUrl
0221      */
0222     public function setCancelUrl($cancelUrl)
0223     {
0224         $this->_cancelUrl = $cancelUrl;
0225     }
0226 
0227     /**
0228      * @return string
0229      */
0230     public function getIpnNotificationUrl()
0231     {
0232         return $this->_ipnNotificationUrl;
0233     }
0234 
0235     /**
0236      * @param string $ipnNotificationUrl
0237      */
0238     public function setIpnNotificationUrl($ipnNotificationUrl)
0239     {
0240         $this->_ipnNotificationUrl = $ipnNotificationUrl;
0241     }
0242 
0243     /**
0244      * @return string
0245      */
0246     public function getReturnUrl()
0247     {
0248         return $this->_returnUrl;
0249     }
0250 
0251     /**
0252      * @param string $returnUrl
0253      */
0254     public function setReturnUrl($returnUrl)
0255     {
0256         $this->_returnUrl = $returnUrl;
0257     }
0258 
0259     public function getCheckoutEndpoint()
0260     {
0261         return trim($this->_config->form->endpoint . '/webapps/adaptivepayment/flow/pay');
0262     }
0263 
0264 }