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

0001 <?php
0002 
0003 /**
0004  *  ocs-webserver
0005  *
0006  *  Copyright 2016 by pling GmbH.
0007  *
0008  *    This file is part of ocs-webserver.
0009  *
0010  *    This program is free software: you can redistribute it and/or modify
0011  *    it under the terms of the GNU Affero General Public License as
0012  *    published by the Free Software Foundation, either version 3 of the
0013  *    License, or (at your option) any later version.
0014  *
0015  *    This program is distributed in the hope that it will be useful,
0016  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
0017  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0018  *    GNU Affero General Public License for more details.
0019  *
0020  *    You should have received a copy of the GNU Affero General Public License
0021  *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
0022  * */
0023 class Backend_MemberPayoutCliController extends Local_Controller_Action_CliAbstract
0024 {
0025 
0026     public static $ACTION_PAYOUT = "payout";
0027     public static $CONTEXT_ALL = "all";
0028     public static $CONTEXT_PREPARE = "prepare";
0029     public static $NVP_MODULE_ADAPTIVE_PAYMENT = "/AdaptivePayments";
0030     public static $NVP_ACTION_PAY = "/Pay";
0031 
0032     public static $PAYOUT_STATUS_NEW = 0;
0033     public static $PAYOUT_STATUS_REQUESTED = 1;
0034     public static $PAYOUT_STATUS_PROCESSED = 10;
0035     public static $PAYOUT_STATUS_COMPLETED = 100;
0036     public static $PAYOUT_STATUS_DENIED = 30;
0037     public static $PAYOUT_STATUS_ERROR = 99;
0038     public static $PAYOUT_STATUS_PAYPAL_API_ERROR = 999;
0039     public $headers;
0040     /** @var Zend_Config */
0041     protected $_config;
0042     /** @var \Zend_Log */
0043     protected $_logger;
0044 
0045     /**
0046      * Run php code as cronjob.
0047      * I.e.:
0048      * /usr/bin/php /var/www/pling.it/pling/scripts/cron.php -a /backend/member-payout-cli/run/action/payout/context/all >> /var/www/ocs-www/logs/masspay.log $
0049      *
0050      * @see Local_Controller_Action_CliInterface::runAction()
0051      */
0052     public function runAction()
0053     {
0054         $this->initVars();
0055 
0056         $this->initHeaders();
0057 
0058         echo "Start runAction\n";
0059         echo "AppEnv: " . APPLICATION_ENV . "\n";
0060         echo "SandboxActive: " . $this->_config->third_party->paypal->sandbox->active . "\n";
0061         echo "Endpoint: " . $this->_config->third_party->paypal->masspay->endpoint . "\n";
0062         echo "Test: " . $this->_config->third_party->paypal->test . "\n";
0063 
0064         $action = $this->getParam('action');
0065         $context = $this->getParam('context');
0066 
0067         echo "action: " . $action . "\n";
0068         echo "context: " . $context . "\n";
0069         if (isset($action) && $action == $this::$ACTION_PAYOUT && isset($context) && $context == $this::$CONTEXT_ALL) {
0070             $this->payoutMembers();
0071         } else if (isset($action) && $action == $this::$ACTION_PAYOUT && isset($context) && $context == $this::$CONTEXT_PREPARE) {
0072             $this->prepareMasspaymentTable();
0073         }
0074     }
0075 
0076     public function initVars()
0077     {
0078         //init
0079         $this->_config = Zend_Registry::get('config');
0080         $this->_logger = Zend_Registry::get('logger');
0081     }
0082 
0083     public function initHeaders()
0084     {
0085         echo "Start initHeaders";
0086 
0087         $this->headers = array(
0088             "X-PAYPAL-SECURITY-USERID: " . $this->_config->third_party->paypal->security->userid,
0089             "X-PAYPAL-SECURITY-PASSWORD: " . $this->_config->third_party->paypal->security->password,
0090             "X-PAYPAL-SECURITY-SIGNATURE: " . $this->_config->third_party->paypal->security->signature,
0091             "X-PAYPAL-REQUEST-DATA-FORMAT: NV",
0092             "X-PAYPAL-RESPONSE-DATA-FORMAT: NV",
0093             "X-PAYPAL-APPLICATION-ID: " . $this->_config->third_party->paypal->application->id,
0094         );
0095     }
0096 
0097     private function payoutMembers()
0098     {
0099         echo "payoutMembers()\n";
0100 
0101         //Select all members for payout and write them in the payout table, ignore allways inserted members
0102         //$this->prepareMasspaymentTable();
0103 
0104         //get payouts
0105         $allPayouts = $this->getPayouts();
0106 
0107         //send request for < 250 payouts
0108         $this->startMassPay($allPayouts);
0109     }
0110 
0111     private function prepareMasspaymentTable()
0112     {
0113         echo "prepareMasspaymentTable()\n";
0114         $db = Zend_Db_Table::getDefaultAdapter();
0115 
0116         $sql = "SELECT * FROM stat_dl_payment_last_month s WHERE s.amount >= 1";
0117 
0118         $stmt = $db->query($sql);
0119         $payouts = $stmt->fetchAll();
0120 
0121         echo "Select " . count($payouts) . " payouts. Sql: " . $sql . "\n";
0122 
0123         //Insert/Update users in table project_rating
0124         foreach ($payouts as $payout) {
0125             //Insert item in payment table
0126             //INSERT IGNORE INTO `pling`.`payout` (`yearmonth`, `member_id`, `amount`) VALUES ('201612', '223978', '181.0500');
0127             $sql =
0128                 "INSERT IGNORE INTO `member_payout` (`yearmonth`, `member_id`, `mail`, `paypal_mail`, `amount`, `num_downloads`, `created_at`) VALUES ('"
0129                 . $payout['yearmonth'] . "','" . $payout['member_id'] . "','" . $payout['mail'] . "','" . $payout['paypal_mail'] . "',"
0130                 . $payout['amount'] . "," . $payout['num_downloads'] . ", NOW()" . ")";
0131             $stmt = $db->query($sql);
0132             $stmt->execute();
0133         }
0134     }
0135 
0136     private function getPayouts()
0137     {
0138         echo "getPayouts";
0139         $db = Zend_Db_Table::getDefaultAdapter();
0140         $sql = "SELECT * FROM member_payout p WHERE p.status = " . $this::$PAYOUT_STATUS_NEW . " OR p.status = "
0141             . $this::$PAYOUT_STATUS_PAYPAL_API_ERROR;
0142         $stmt = $db->query($sql);
0143         $payouts = $stmt->fetchAll();
0144 
0145         $payoutsArray = array();
0146 
0147         foreach ($payouts as $payout) {
0148             $payoutsArray[] = $payout;
0149         }
0150 
0151         return $payoutsArray;
0152     }
0153 
0154     private function startMassPay($payoutsArray)
0155     {
0156         echo "startMassPay\n\n";
0157         if (!$payoutsArray || count($payoutsArray) == 0) {
0158             echo "Nothing to do...\n\n";
0159             die;
0160             //throw new Exception("Method startMassPay needs array of payouts.");
0161         }
0162         $payoutTable = new Default_Model_DbTable_MemberPayout();
0163         $log = $this->_logger;
0164 
0165         $log->info('********** Start PayPal Masspay **********\n');
0166         $log->info(__FUNCTION__);
0167         $log->debug(APPLICATION_ENV);
0168 
0169         echo('********** Start PayPal Masspay **********');
0170         echo(__FUNCTION__);
0171         echo(APPLICATION_ENV);
0172 
0173         foreach ($payoutsArray as $payout) {
0174             $amount = $payout['amount'];
0175             $mail = $payout['paypal_mail'];
0176             $id = $payout['id'];
0177             $yearmonth = $payout['yearmonth'];
0178             /*if($this->_config->third_party->paypal->sandbox->active) {
0179                 $mail = "paypal-buyer@pling.com";
0180             }*/
0181 
0182             $result = $this->sendPayout($mail, $amount, $id, $yearmonth);
0183 
0184             if ($result) {
0185 
0186                 echo "Result: " . print_r($result->getRawMessage());
0187                 $payKey = $result->getPaymentId();
0188 
0189                 if (is_array($result)) {
0190                     $successful = ($result->getStatus() != 'ERROR');
0191                 }
0192 
0193                 if ($successful) {
0194                     //mark payout as requested
0195                     $payoutTable->update(array("payment_reference_key"   => $payKey,
0196                                                "status"                  => $this::$PAYOUT_STATUS_REQUESTED,
0197                                                "timestamp_masspay_start" => new Zend_Db_Expr('Now()')
0198                     ), "id = " . $payout['id']);
0199                 } else {
0200                     //mark payout as failed
0201                     $payoutTable->update(array("payment_reference_key"   => $payKey,
0202                                                "status"                  => $this::$PAYOUT_STATUS_PAYPAL_API_ERROR,
0203                                                "timestamp_masspay_start" => new Zend_Db_Expr('Now()'),
0204                                                "payment_raw_error"       => print_r($result->getRawMessage(), true),
0205                                                "payment_status"          => 'ERROR'
0206                     ), "id = " . $payout['id']);
0207                 }
0208             } else {
0209 
0210                 echo "Result: PayPal-API-Error";
0211                 //mark payout as 999 = API-Error
0212                 $payoutTable->update(array("status"                  => $this::$PAYOUT_STATUS_PAYPAL_API_ERROR,
0213                                            "timestamp_masspay_start" => new Zend_Db_Expr('Now()')
0214                 ), "id = " . $payout['id']);
0215             }
0216         }
0217 
0218         return true;
0219     }
0220 
0221     private function sendPayout($receiverMail, $amount, $trackingId, $yearmonth)
0222     {
0223         $paymentGateway = $this->createPaymentGateway("paypal");
0224         $response = null;
0225         try {
0226             $response =
0227                 $paymentGateway->requestPaymentForPayout($this->_config->third_party->paypal->facilitator_fee_receiver, $receiverMail,
0228                     $amount, $trackingId, $yearmonth);
0229         } catch (Exception $e) {
0230             $log = $this->_logger;
0231             $log->info('Exception: payment error. Message: ' . print_r($e));
0232             echo('Exception: payment error. Message: ' . print_r($e));
0233 
0234             //throw new Zend_Controller_Action_Exception('payment error', 500, $e);
0235 
0236             //Set status to 999 (or we set the original paypal error code)
0237             //mark payout as requested
0238             return null;
0239         }
0240 
0241         return $response;
0242     }
0243 
0244     /**
0245      * @param string $paymentProvider
0246      *
0247      * @throws Zend_Controller_Exception
0248      * @return Local_Payment_GatewayInterface
0249      */
0250     protected function createPaymentGateway($paymentProvider)
0251     {
0252         $httpHost = "www.opendesktop.org";
0253         if ($this->_config->third_party->paypal->sandbox->active) {
0254             $httpHost = "www.pling.cc";
0255         }
0256         /** @var Zend_Config $config */
0257         $config = Zend_Registry::get('config');
0258         $helperBuildProductUrl = new Default_View_Helper_BuildProductUrl();
0259         switch ($paymentProvider) {
0260             case 'paypal':
0261                 $paymentGateway = new Default_Model_PayPal_Gateway($config->third_party->paypal);
0262                 $paymentGateway->setIpnNotificationUrl('http://' . $httpHost . '/gateway/paypalpayout');
0263                 //                $paymentGateway->setIpnNotificationUrl('http://' . $httpHost . '/gateway/paypal?XDEBUG_SESSION_START=1');
0264                 $paymentGateway->setCancelUrl('http://' . $httpHost);
0265                 $paymentGateway->setReturnUrl('http://' . $httpHost);
0266                 break;
0267 
0268             case 'dwolla':
0269                 $paymentGateway = new Default_Model_Dwolla_Gateway($config->third_party->dwolla);
0270                 $paymentGateway->setIpnNotificationUrl('http://' . $httpHost . '/gateway/dwolla');
0271                 //                $paymentGateway->setIpnNotificationUrl('http://' . $_SERVER ['HTTP_HOST'] . '/gateway/dwolla?XDEBUG_SESSION_START=1');
0272                 $paymentGateway->setReturnUrl($helperBuildProductUrl->buildProductUrl($this->_projectId, 'dwolla', null, true));
0273                 break;
0274 
0275             case 'amazon':
0276                 $paymentGateway = new Default_Model_Amazon_Gateway($config->third_party->amazon);
0277                 $paymentGateway->setIpnNotificationUrl('http://' . $httpHost . '/gateway/amazon');
0278                 //                $paymentGateway->setIpnNotificationUrl('http://' . $httpHost . '/gateway/amazon?XDEBUG_SESSION_START=1');
0279                 $paymentGateway->setCancelUrl($helperBuildProductUrl->buildProductUrl($this->_projectId, 'paymentcancel', null, true));
0280                 $paymentGateway->setReturnUrl($helperBuildProductUrl->buildProductUrl($this->_projectId, 'paymentok', null, true));
0281                 break;
0282 
0283             default:
0284                 throw new Zend_Controller_Exception('No known payment provider found in parameters.');
0285                 break;
0286         }
0287 
0288         return $paymentGateway;
0289     }
0290 
0291     public function isSuccessful($response)
0292     {
0293         //return $response['responseEnvelope']['ack'] == 'Success';
0294         return (strpos($response, 'ACK=Success') != false);
0295     }
0296 
0297 
0298 }