Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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