Rev 6849 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |
<?php
namespace LeadersLinked\Controller;
use Laminas\Db\Adapter\AdapterInterface;
use Laminas\Cache\Storage\Adapter\AbstractAdapter;
use Laminas\Mvc\Controller\AbstractActionController;
use Laminas\Log\LoggerInterface;
use Laminas\View\Model\JsonModel;
use LeadersLinked\Model\Provider;
use LeadersLinked\Model\Transaction;
use LeadersLinked\Mapper\UserMapper;
use LeadersLinked\Mapper\TransactionMapper;
use PayPalCheckoutSdk\Core\SandboxEnvironment;
use PayPalCheckoutSdk\Core\ProductionEnvironment;
use PayPalCheckoutSdk\Orders\OrdersCaptureRequest;
use PayPalCheckoutSdk\Core\PayPalHttpClient;
use PayPalHttp\HttpException;
class PaypalController extends AbstractActionController
{
/**
*
* @var AdapterInterface
*/
private $adapter;
/**
*
* @var AbstractAdapter
*/
private $cache;
/**
*
* @var LoggerInterface
*/
private $logger;
/**
*
* @var array
*/
private $config;
/**
*
* @param AdapterInterface $adapter
* @param AbstractAdapter $cache
* @param LoggerInterface $logger
* @param array $config
*/
public function __construct($adapter, $cache , $logger, $config)
{
$this->adapter = $adapter;
$this->cache = $cache;
$this->logger = $logger;
$this->config = $config;
}
public function indexAction()
{
return new JsonModel(['success' => false, 'error' => 'Missing authentication']);
}
public function successAction()
{
$payerID = $this->params()->fromQuery('PayerID');
$token = $this->params()->fromQuery('token');
if(!empty($payerID) && !empty($token))
{
$sandbox = $this->config['leaderslinked.runmode.sandbox_paypal'];
if($sandbox) {
//$account_id = $this->config['leaderslinked.paypal.sandbox_account_id'];
$client_id = $this->config['leaderslinked.paypal.sandobx_client_id'];
$client_secret = $this->config['leaderslinked.paypal.sandbox_client_secret'];
$environment = new SandboxEnvironment($client_id, $client_secret);
} else {
// $account_id = $this->config['leaderslinked.paypal.production_account_id'];
$client_id = $this->config['leaderslinked.paypal.production_client_id'];
$client_secret = $this->config['leaderslinked.paypal.production_client_secret'];
$environment = new ProductionEnvironment($client_id, $client_secret);
}
try {
$request = new OrdersCaptureRequest($token);
// Call API with your client and get a response for your call
$client = new PayPalHttpClient($environment);
$response = $client->execute($request);
$external_ref = $response->result->id;
if($response->result->status == 'COMPLETED') {
$requestId = Provider::PAYPAL . '-' . $token;
$transaction = $this->cache->getItem($requestId);
if(!empty($transaction) )
{
$transaction = unserialize($transaction);
if($transaction instanceof Transaction)
{
$userMapper = UserMapper::getInstance($this->adapter);
$user = $userMapper->fetchOne($transaction->user_id);
$transaction->external_ref = $external_ref;
$transaction->previous = $user->balance;
$transaction->current = $user->balance + $transaction->amount;
$transaction->response = json_encode($response, JSON_PRETTY_PRINT);
$transaction->status = Transaction::STATUS_COMPLETED;
$transactionMapper = TransactionMapper::getInstance($this->adapter);
if($transactionMapper->insert($transaction)) {
$user->balance = $transaction->current;
$userMapper->update($user);
} else {
$flashMessenger = $this->plugin('FlashMessenger');
$flashMessenger->addErrorMessage('ERROR_TRANSACTION_NOT_SAVED');
}
}
}
}
} catch (HttpException $ex) {
$flashMessenger = $this->plugin('FlashMessenger');
$flashMessenger->addErrorMessage($ex->getMessage());
}
}
return $this->redirect()->toRoute('account-settings', [], ['query'=>['tab'=>'nav-transactions']]);
}
public function cancelAction()
{
$token = $this->params()->fromQuery('token');
if(!empty($token))
{
$requestId = Provider::PAYPAL . '-' . $token;
$transaction = $this->cache->getItem($requestId);
if(!empty($transaction))
{
$transaction = unserialize($transaction);
if($transaction instanceof Transaction)
{
$userMapper = UserMapper::getInstance($this->adapter);
$user = $userMapper->fetchOne($transaction->user_id);
$transaction->previous = $user->balance;
$transaction->current = $user->balance + $transaction->amount;
$transaction->status = Transaction::STATUS_CANCELLED;
$transactionMapper = TransactionMapper::getInstance($this->adapter);
if(!$transactionMapper->insert($transaction)) {
$flashMessenger = $this->plugin('FlashMessenger');
$flashMessenger->addErrorMessage('ERROR_TRANSACTION_NOT_SAVED');
}
}
}
}
return $this->redirect()->toRoute('account-settings', [], ['query'=>['tab'=>'nav-transactions']]);
}
}