| 1 | 
           efrain | 
           1 | 
           <?php
  | 
        
        
            | 
            | 
           2 | 
           namespace LeadersLinked\Controller;
  | 
        
        
            | 
            | 
           3 | 
              | 
        
        
            | 
            | 
           4 | 
           use Laminas\Db\Adapter\AdapterInterface;
  | 
        
        
            | 
            | 
           5 | 
              | 
        
        
            | 
            | 
           6 | 
           use Laminas\Mvc\Controller\AbstractActionController;
  | 
        
        
            | 
            | 
           7 | 
           use Laminas\Log\LoggerInterface;
  | 
        
        
            | 
            | 
           8 | 
           use Laminas\View\Model\JsonModel;
  | 
        
        
            | 
            | 
           9 | 
           use LeadersLinked\Model\Provider;
  | 
        
        
            | 
            | 
           10 | 
           use LeadersLinked\Model\Transaction;
  | 
        
        
            | 
            | 
           11 | 
           use LeadersLinked\Mapper\UserMapper;
  | 
        
        
            | 
            | 
           12 | 
           use LeadersLinked\Mapper\TransactionMapper;
  | 
        
        
            | 
            | 
           13 | 
           use LeadersLinked\Cache\CacheInterface;
  | 
        
        
            | 
            | 
           14 | 
           use LeadersLinked\Cache\CacheImpl;
  | 
        
        
            | 
            | 
           15 | 
           use PayPalCheckoutSdk\Core\SandboxEnvironment;
  | 
        
        
            | 
            | 
           16 | 
           use PayPalCheckoutSdk\Core\ProductionEnvironment;
  | 
        
        
            | 
            | 
           17 | 
           use PayPalCheckoutSdk\Orders\OrdersCaptureRequest;
  | 
        
        
            | 
            | 
           18 | 
           use PayPalCheckoutSdk\Core\PayPalHttpClient;
  | 
        
        
            | 
            | 
           19 | 
           use PayPalHttp\HttpException;
  | 
        
        
            | 
            | 
           20 | 
           /*
  | 
        
        
            | 
            | 
           21 | 
           use PayPalCheckoutSdk\Core\SandboxEnvironment;
  | 
        
        
            | 
            | 
           22 | 
           use PayPalCheckoutSdk\Core\ProductionEnvironment;
  | 
        
        
            | 
            | 
           23 | 
           use PayPalCheckoutSdk\Orders\OrdersCaptureRequest;
  | 
        
        
            | 
            | 
           24 | 
           use PayPalCheckoutSdk\Core\PayPalHttpClient;
  | 
        
        
            | 
            | 
           25 | 
           use PayPalHttp\HttpException;
  | 
        
        
            | 
            | 
           26 | 
           */
  | 
        
        
            | 
            | 
           27 | 
              | 
        
        
            | 
            | 
           28 | 
           class PaypalController extends AbstractActionController
  | 
        
        
            | 
            | 
           29 | 
           {
  | 
        
        
            | 
            | 
           30 | 
               /**
  | 
        
        
            | 
            | 
           31 | 
                *
  | 
        
        
            | 
            | 
           32 | 
                * @var \Laminas\Db\Adapter\AdapterInterface
  | 
        
        
            | 
            | 
           33 | 
                */
  | 
        
        
            | 
            | 
           34 | 
               private $adapter;
  | 
        
        
            | 
            | 
           35 | 
              | 
        
        
            | 
            | 
           36 | 
               /**
  | 
        
        
            | 
            | 
           37 | 
                *
  | 
        
        
            | 
            | 
           38 | 
                * @var \LeadersLinked\Cache\CacheInterface
  | 
        
        
            | 
            | 
           39 | 
                */
  | 
        
        
            | 
            | 
           40 | 
               private $cache;
  | 
        
        
            | 
            | 
           41 | 
              | 
        
        
            | 
            | 
           42 | 
              | 
        
        
            | 
            | 
           43 | 
               /**
  | 
        
        
            | 
            | 
           44 | 
                *
  | 
        
        
            | 
            | 
           45 | 
                * @var \Laminas\Log\LoggerInterface
  | 
        
        
            | 
            | 
           46 | 
                */
  | 
        
        
            | 
            | 
           47 | 
               private $logger;
  | 
        
        
            | 
            | 
           48 | 
              | 
        
        
            | 
            | 
           49 | 
               /**
  | 
        
        
            | 
            | 
           50 | 
                *
  | 
        
        
            | 
            | 
           51 | 
                * @var array
  | 
        
        
            | 
            | 
           52 | 
                */
  | 
        
        
            | 
            | 
           53 | 
               private $config;
  | 
        
        
            | 
            | 
           54 | 
              | 
        
        
            | 
            | 
           55 | 
              | 
        
        
            | 
            | 
           56 | 
               /**
  | 
        
        
            | 
            | 
           57 | 
                *
  | 
        
        
            | 
            | 
           58 | 
                * @var \Laminas\Mvc\I18n\Translator
  | 
        
        
            | 
            | 
           59 | 
                */
  | 
        
        
            | 
            | 
           60 | 
               private $translator;
  | 
        
        
            | 
            | 
           61 | 
              | 
        
        
            | 
            | 
           62 | 
              | 
        
        
            | 
            | 
           63 | 
               /**
  | 
        
        
            | 
            | 
           64 | 
                *
  | 
        
        
            | 
            | 
           65 | 
                * @param \Laminas\Db\Adapter\AdapterInterface $adapter
  | 
        
        
            | 
            | 
           66 | 
                * @param \LeadersLinked\Cache\CacheInterface $cache
  | 
        
        
            | 
            | 
           67 | 
                * @param \Laminas\Log\LoggerInterface LoggerInterface $logger
  | 
        
        
            | 
            | 
           68 | 
                * @param array $config
  | 
        
        
            | 
            | 
           69 | 
                * @param \Laminas\Mvc\I18n\Translator $translator
  | 
        
        
            | 
            | 
           70 | 
                */
  | 
        
        
            | 
            | 
           71 | 
               public function __construct($adapter, $cache, $logger, $config, $translator)
  | 
        
        
            | 
            | 
           72 | 
               {
  | 
        
        
            | 
            | 
           73 | 
                   $this->adapter      = $adapter;
  | 
        
        
            | 
            | 
           74 | 
                   $this->cache        = $cache;
  | 
        
        
            | 
            | 
           75 | 
                   $this->logger       = $logger;
  | 
        
        
            | 
            | 
           76 | 
                   $this->config       = $config;
  | 
        
        
            | 
            | 
           77 | 
                   $this->translator   = $translator;
  | 
        
        
            | 
            | 
           78 | 
               }
  | 
        
        
            | 
            | 
           79 | 
              | 
        
        
            | 
            | 
           80 | 
              | 
        
        
            | 
            | 
           81 | 
               public function indexAction()
  | 
        
        
            | 
            | 
           82 | 
               {
  | 
        
        
            | 
            | 
           83 | 
                   return new JsonModel(['success' => false, 'error' => 'Missing authentication']);
  | 
        
        
            | 
            | 
           84 | 
               }
  | 
        
        
            | 
            | 
           85 | 
              | 
        
        
            | 
            | 
           86 | 
               public function successAction()
  | 
        
        
            | 
            | 
           87 | 
               {
  | 
        
        
            | 
            | 
           88 | 
              | 
        
        
            | 
            | 
           89 | 
                   $payerID    = $this->params()->fromQuery('PayerID');
  | 
        
        
            | 
            | 
           90 | 
                   $token      = $this->params()->fromQuery('token');
  | 
        
        
            | 
            | 
           91 | 
              | 
        
        
            | 
            | 
           92 | 
                   if(!empty($payerID) && !empty($token))
  | 
        
        
            | 
            | 
           93 | 
                   {
  | 
        
        
            | 
            | 
           94 | 
              | 
        
        
            | 
            | 
           95 | 
              | 
        
        
            | 
            | 
           96 | 
              | 
        
        
            | 
            | 
           97 | 
                       $sandbox = $this->config['leaderslinked.runmode.sandbox_paypal'];
  | 
        
        
            | 
            | 
           98 | 
                       if($sandbox) {
  | 
        
        
            | 
            | 
           99 | 
                           //$account_id     = $this->config['leaderslinked.paypal.sandbox_account_id'];
  | 
        
        
            | 
            | 
           100 | 
                           $client_id      = $this->config['leaderslinked.paypal.sandobx_client_id'];
  | 
        
        
            | 
            | 
           101 | 
                           $client_secret  = $this->config['leaderslinked.paypal.sandbox_client_secret'];
  | 
        
        
            | 
            | 
           102 | 
              | 
        
        
            | 
            | 
           103 | 
              | 
        
        
            | 
            | 
           104 | 
                           $environment = new SandboxEnvironment($client_id, $client_secret);
  | 
        
        
            | 
            | 
           105 | 
              | 
        
        
            | 
            | 
           106 | 
                       } else {
  | 
        
        
            | 
            | 
           107 | 
                           // $account_id     = $this->config['leaderslinked.paypal.production_account_id'];
  | 
        
        
            | 
            | 
           108 | 
                           $client_id      = $this->config['leaderslinked.paypal.production_client_id'];
  | 
        
        
            | 
            | 
           109 | 
                           $client_secret  = $this->config['leaderslinked.paypal.production_client_secret'];
  | 
        
        
            | 
            | 
           110 | 
              | 
        
        
            | 
            | 
           111 | 
              | 
        
        
            | 
            | 
           112 | 
                           $environment = new ProductionEnvironment($client_id, $client_secret);
  | 
        
        
            | 
            | 
           113 | 
                       }
  | 
        
        
            | 
            | 
           114 | 
              | 
        
        
            | 
            | 
           115 | 
                       try {
  | 
        
        
            | 
            | 
           116 | 
                           $request = new OrdersCaptureRequest($token);
  | 
        
        
            | 
            | 
           117 | 
                           // Call API with your client and get a response for your call
  | 
        
        
            | 
            | 
           118 | 
                           $client = new PayPalHttpClient($environment);
  | 
        
        
            | 
            | 
           119 | 
                           $response = $client->execute($request);
  | 
        
        
            | 
            | 
           120 | 
              | 
        
        
            | 
            | 
           121 | 
                           $external_ref = $response->result->id;
  | 
        
        
            | 
            | 
           122 | 
                           if($response->result->status == 'COMPLETED') {
  | 
        
        
            | 
            | 
           123 | 
              | 
        
        
            | 
            | 
           124 | 
                               $requestId = Provider::PAYPAL . '-' . $token;
  | 
        
        
            | 
            | 
           125 | 
              | 
        
        
            | 
            | 
           126 | 
                               $transaction = $this->cache->getItem($requestId);
  | 
        
        
            | 
            | 
           127 | 
                               if(!empty($transaction) )
  | 
        
        
            | 
            | 
           128 | 
                               {
  | 
        
        
            | 
            | 
           129 | 
                                   $transaction = unserialize($transaction);
  | 
        
        
            | 
            | 
           130 | 
                                   if($transaction instanceof  Transaction)
  | 
        
        
            | 
            | 
           131 | 
                                   {
  | 
        
        
            | 
            | 
           132 | 
                                       $userMapper = UserMapper::getInstance($this->adapter);
  | 
        
        
            | 
            | 
           133 | 
                                       $user = $userMapper->fetchOne($transaction->user_id);
  | 
        
        
            | 
            | 
           134 | 
              | 
        
        
            | 
            | 
           135 | 
              | 
        
        
            | 
            | 
           136 | 
                                       $transaction->external_ref = $external_ref;
  | 
        
        
            | 
            | 
           137 | 
                                       $transaction->previous = $user->balance;
  | 
        
        
            | 
            | 
           138 | 
                                       $transaction->current = $user->balance + $transaction->amount;
  | 
        
        
            | 
            | 
           139 | 
                                       $transaction->response = json_encode($response, JSON_PRETTY_PRINT);
  | 
        
        
            | 
            | 
           140 | 
                                       $transaction->status = Transaction::STATUS_COMPLETED;
  | 
        
        
            | 
            | 
           141 | 
              | 
        
        
            | 
            | 
           142 | 
                                       $transactionMapper = TransactionMapper::getInstance($this->adapter);
  | 
        
        
            | 
            | 
           143 | 
                                       if($transactionMapper->insert($transaction)) {
  | 
        
        
            | 
            | 
           144 | 
              | 
        
        
            | 
            | 
           145 | 
                                           $user->balance = $transaction->current;
  | 
        
        
            | 
            | 
           146 | 
                                           $userMapper->update($user);
  | 
        
        
            | 
            | 
           147 | 
              | 
        
        
            | 
            | 
           148 | 
                                       } else {
  | 
        
        
            | 
            | 
           149 | 
                                           $flashMessenger = $this->plugin('FlashMessenger');
  | 
        
        
            | 
            | 
           150 | 
                                           $flashMessenger->addErrorMessage('ERROR_TRANSACTION_NOT_SAVED');
  | 
        
        
            | 
            | 
           151 | 
                                       }
  | 
        
        
            | 
            | 
           152 | 
                                   }
  | 
        
        
            | 
            | 
           153 | 
                               }
  | 
        
        
            | 
            | 
           154 | 
              | 
        
        
            | 
            | 
           155 | 
                           }
  | 
        
        
            | 
            | 
           156 | 
                       } catch (HttpException $ex) {
  | 
        
        
            | 
            | 
           157 | 
              | 
        
        
            | 
            | 
           158 | 
                           $flashMessenger = $this->plugin('FlashMessenger');
  | 
        
        
            | 
            | 
           159 | 
                           $flashMessenger->addErrorMessage($ex->getMessage());
  | 
        
        
            | 
            | 
           160 | 
                       }
  | 
        
        
            | 
            | 
           161 | 
                   }
  | 
        
        
            | 
            | 
           162 | 
                   return $this->redirect()->toRoute('account-settings', [], ['query'=>['tab'=>'nav-transactions']]);
  | 
        
        
            | 
            | 
           163 | 
               }
  | 
        
        
            | 
            | 
           164 | 
              | 
        
        
            | 
            | 
           165 | 
               public function cancelAction()
  | 
        
        
            | 
            | 
           166 | 
               {
  | 
        
        
            | 
            | 
           167 | 
                   $token = $this->params()->fromQuery('token');
  | 
        
        
            | 
            | 
           168 | 
                   if(!empty($token))
  | 
        
        
            | 
            | 
           169 | 
                   {
  | 
        
        
            | 
            | 
           170 | 
                       $requestId = Provider::PAYPAL . '-' . $token;
  | 
        
        
            | 
            | 
           171 | 
              | 
        
        
            | 
            | 
           172 | 
                       $transaction = $this->cache->getItem($requestId);
  | 
        
        
            | 
            | 
           173 | 
                       if(!empty($transaction))
  | 
        
        
            | 
            | 
           174 | 
                       {
  | 
        
        
            | 
            | 
           175 | 
                           $transaction = unserialize($transaction);
  | 
        
        
            | 
            | 
           176 | 
              | 
        
        
            | 
            | 
           177 | 
                           if($transaction instanceof Transaction)
  | 
        
        
            | 
            | 
           178 | 
                           {
  | 
        
        
            | 
            | 
           179 | 
                               $userMapper = UserMapper::getInstance($this->adapter);
  | 
        
        
            | 
            | 
           180 | 
                               $user = $userMapper->fetchOne($transaction->user_id);
  | 
        
        
            | 
            | 
           181 | 
              | 
        
        
            | 
            | 
           182 | 
                               $transaction->previous = $user->balance;
  | 
        
        
            | 
            | 
           183 | 
                               $transaction->current = $user->balance + $transaction->amount;
  | 
        
        
            | 
            | 
           184 | 
                               $transaction->status = Transaction::STATUS_CANCELLED;
  | 
        
        
            | 
            | 
           185 | 
              | 
        
        
            | 
            | 
           186 | 
                               $transactionMapper = TransactionMapper::getInstance($this->adapter);
  | 
        
        
            | 
            | 
           187 | 
                               if(!$transactionMapper->insert($transaction)) {
  | 
        
        
            | 
            | 
           188 | 
                                   $flashMessenger = $this->plugin('FlashMessenger');
  | 
        
        
            | 
            | 
           189 | 
                                   $flashMessenger->addErrorMessage('ERROR_TRANSACTION_NOT_SAVED');
  | 
        
        
            | 
            | 
           190 | 
                               }
  | 
        
        
            | 
            | 
           191 | 
                           }
  | 
        
        
            | 
            | 
           192 | 
                       }
  | 
        
        
            | 
            | 
           193 | 
                   }
  | 
        
        
            | 
            | 
           194 | 
                   return $this->redirect()->toRoute('account-settings', [], ['query'=>['tab'=>'nav-transactions']]);
  | 
        
        
            | 
            | 
           195 | 
               }
  | 
        
        
            | 
            | 
           196 | 
           }
  |