File indexing completed on 2024-05-12 06:02: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 Local_Validate_Paypal extends Zend_Validate_Abstract
0024 {
0025 
0026     const INVALID_URL = 'invalidPayPal';
0027     const INVALID_FIRSTNAME = 'invalidFirstname';
0028     const INVALID_NAME = 'invalidName';
0029 
0030     protected $_messageTemplates = array(
0031         self::INVALID_URL => "Cannot determine PayPal Account status. Please check that name and firstname fit to your PayPal email address.",
0032         self::INVALID_FIRSTNAME => "Your firstname is needed to check your PayPal Account.",
0033         self::INVALID_NAME => "Your name is needed to check your PayPal Account."
0034     );
0035 
0036     /**
0037      * @var string
0038      */
0039     private $fieldFirstname;
0040 
0041     /**
0042      * @var string
0043      */
0044     private $fieldName;
0045 
0046     function __construct($fieldFirstname, $fieldName)
0047     {
0048         $this->fieldFirstname = $fieldFirstname;
0049         $this->fieldName = $fieldName;
0050     }
0051 
0052     public function isValid($value, $context = null)
0053     {
0054         $valueString = ( string )$value;
0055         $this->_setValue($valueString);
0056 
0057         if (empty($context[$this->fieldFirstname])) {
0058             $this->_error(self::INVALID_FIRSTNAME);
0059             return false;
0060         }
0061 
0062         if (empty($context[$this->fieldName])) {
0063             $this->_error(self::INVALID_NAME);
0064             return false;
0065         }
0066 
0067         if (!$this->verifyPayPalAccount($value, $context)) {
0068             $this->_error(self::INVALID_URL);
0069             return false;
0070         }
0071         return true;
0072     }
0073 
0074     private function verifyPayPalAccount($mail, $context)
0075     {
0076         $data = array('mail' => $mail);
0077         $data = array_merge($data, $context);
0078 
0079         $config = Zend_Registry::get('config');
0080         $paymentProvider = new Local_Payment_PayPal_AdaptiveAccounts_Account($config->third_party->paypal);
0081         $userData = new Local_Payment_PayPal_UserData();
0082         $userData->generateFromArray($data);
0083         $result = $paymentProvider->verifyAccount($userData);
0084 
0085         return $result;
0086     }
0087 
0088 }