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