Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 6749 | Ir a la última revisión | | Ultima modificación | Ver Log |

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