Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 6849 | | Comparar con el anterior | 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;
6849 efrain 5
 
1 www 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;
6849 efrain 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;
6749 efrain 20
/*
1 www 21
use PayPalCheckoutSdk\Core\SandboxEnvironment;
22
use PayPalCheckoutSdk\Core\ProductionEnvironment;
23
use PayPalCheckoutSdk\Orders\OrdersCaptureRequest;
24
use PayPalCheckoutSdk\Core\PayPalHttpClient;
25
use PayPalHttp\HttpException;
6749 efrain 26
*/
1 www 27
 
28
class PaypalController extends AbstractActionController
29
{
30
    /**
31
     *
6866 efrain 32
     * @var \Laminas\Db\Adapter\AdapterInterface
1 www 33
     */
34
    private $adapter;
35
 
36
    /**
37
     *
6866 efrain 38
     * @var \LeadersLinked\Cache\CacheInterface
1 www 39
     */
6866 efrain 40
    private $cache;
41
 
42
 
43
    /**
44
     *
45
     * @var \Laminas\Log\LoggerInterface
46
     */
1 www 47
    private $logger;
6866 efrain 48
 
1 www 49
    /**
50
     *
51
     * @var array
52
     */
53
    private $config;
54
 
6866 efrain 55
 
1 www 56
    /**
6866 efrain 57
     *
58
     * @var \Laminas\Mvc\I18n\Translator
6849 efrain 59
     */
6866 efrain 60
    private $translator;
6849 efrain 61
 
6866 efrain 62
 
6849 efrain 63
    /**
1 www 64
     *
6866 efrain 65
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
66
     * @param \LeadersLinked\Cache\CacheInterface $cache
67
     * @param \Laminas\Log\LoggerInterface LoggerInterface $logger
1 www 68
     * @param array $config
6866 efrain 69
     * @param \Laminas\Mvc\I18n\Translator $translator
1 www 70
     */
6866 efrain 71
    public function __construct($adapter, $cache, $logger, $config, $translator)
1 www 72
    {
73
        $this->adapter      = $adapter;
6866 efrain 74
        $this->cache        = $cache;
1 www 75
        $this->logger       = $logger;
76
        $this->config       = $config;
6866 efrain 77
        $this->translator   = $translator;
1 www 78
    }
79
 
80
 
81
    public function indexAction()
82
    {
83
        return new JsonModel(['success' => false, 'error' => 'Missing authentication']);
84
    }
85
 
86
    public function successAction()
87
    {
6849 efrain 88
 
1 www 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
 
6849 efrain 126
                    $transaction = $this->cache->getItem($requestId);
1 www 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
        }
6849 efrain 162
        return $this->redirect()->toRoute('account-settings', [], ['query'=>['tab'=>'nav-transactions']]);
1 www 163
    }
164
 
165
    public function cancelAction()
166
    {
167
        $token = $this->params()->fromQuery('token');
168
        if(!empty($token))
169
        {
170
            $requestId = Provider::PAYPAL . '-' . $token;
171
 
6849 efrain 172
            $transaction = $this->cache->getItem($requestId);
1 www 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
}