paypal.php
15.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
<?php
class ControllerExtensionPaymentPayPal extends Controller {
private $error = array();
public function index() {
$this->load->language('extension/payment/paypal');
$this->document->setTitle($this->language->get('heading_title'));
$this->load->model('extension/payment/paypal');
$this->load->model('setting/setting');
if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
$this->model_setting_setting->editSetting('payment_paypal', $this->request->post);
$this->session->data['success'] = $this->language->get('success_save');
$this->response->redirect($this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=payment', true));
}
$data['breadcrumbs'] = array();
$data['breadcrumbs'][] = array(
'text' => $this->language->get('text_home'),
'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'], true)
);
$data['breadcrumbs'][] = array(
'text' => $this->language->get('text_extensions'),
'href' => $this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=payment', true)
);
$data['breadcrumbs'][] = array(
'text' => $this->language->get('heading_title'),
'href' => $this->url->link('extension/payment/paypal', 'user_token=' . $this->session->data['user_token'], true)
);
$data['action'] = $this->url->link('extension/payment/paypal', 'user_token=' . $this->session->data['user_token'], true);
$data['cancel'] = $this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=payment', true);
$data['partner_url'] = str_replace('&', '%26', $this->url->link('extension/payment/paypal', 'user_token=' . $this->session->data['user_token'], true));
$data['callback_url'] = str_replace('&', '&', $this->url->link('extension/payment/paypal/callback', 'user_token=' . $this->session->data['user_token'], true));
$data['configure_smart_button_url'] = $this->url->link('extension/payment/paypal/configureSmartButton', 'user_token=' . $this->session->data['user_token'], true);
if (isset($this->request->server['HTTPS']) && (($this->request->server['HTTPS'] == 'on') || ($this->request->server['HTTPS'] == '1'))) {
$data['server'] = HTTPS_SERVER;
$data['catalog'] = HTTPS_CATALOG;
} else {
$data['server'] = HTTP_SERVER;
$data['catalog'] = HTTP_CATALOG;
}
// Setting
$_config = new Config();
$_config->load('paypal');
$data['setting'] = $_config->get('paypal_setting');
if (isset($this->session->data['environment']) && isset($this->session->data['authorization_code']) && isset($this->session->data['shared_id']) && isset($this->session->data['seller_nonce']) && isset($this->request->get['merchantIdInPayPal'])) {
$environment = $this->session->data['environment'];
require_once DIR_SYSTEM . 'library/paypal/paypal.php';
$paypal_info = array(
'client_id' => $this->session->data['shared_id'],
'environment' => $environment
);
$paypal = new PayPal($paypal_info);
$token_info = array(
'grant_type' => 'authorization_code',
'code' => $this->session->data['authorization_code'],
'code_verifier' => $this->session->data['seller_nonce']
);
$paypal->setAccessToken($token_info);
$result = $paypal->getSellerCredentials($data['setting']['partner'][$environment]['partner_id']);
if (isset($result['client_id']) && isset($result['client_secret'])) {
$client_id = $result['client_id'];
$secret = $result['client_secret'];
}
$webhook_info = array(
'url' => $data['catalog'] . 'index.php?route=extension/payment/paypal/webhook',
'event_types' => array(
array('name' => 'PAYMENT.AUTHORIZATION.CREATED'),
array('name' => 'PAYMENT.AUTHORIZATION.VOIDED'),
array('name' => 'PAYMENT.CAPTURE.COMPLETED'),
array('name' => 'PAYMENT.CAPTURE.DENIED'),
array('name' => 'PAYMENT.CAPTURE.PENDING'),
array('name' => 'PAYMENT.CAPTURE.REFUNDED'),
array('name' => 'PAYMENT.CAPTURE.REVERSED'),
array('name' => 'CHECKOUT.ORDER.COMPLETED'),
array('name' => 'CHECKOUT.ORDER.APPROVED')
)
);
$result = $paypal->createWebhook($webhook_info);
$webhook_id = '';
if (isset($result['id'])) {
$webhook_id = $result['id'];
}
if ($paypal->hasErrors()) {
$error_messages = array();
$errors = $paypal->getErrors();
foreach ($errors as $error) {
if (isset($error['name']) && ($error['name'] == 'CURLE_OPERATION_TIMEOUTED')) {
$error['message'] = $this->language->get('error_timeout');
}
if (isset($error['details'][0]['description'])) {
$error_messages[] = $error['details'][0]['description'];
} else {
$error_messages[] = $error['message'];
}
$this->model_extension_payment_paypal->log($error, $error['message']);
}
$this->error['warning'] = implode(' ', $error_messages);
}
$merchant_id = $this->request->get['merchantIdInPayPal'];
unset($this->session->data['authorization_code']);
unset($this->session->data['shared_id']);
unset($this->session->data['seller_nonce']);
}
if (isset($environment)) {
$data['environment'] = $environment;
} elseif (isset($this->request->post['payment_paypal_environment'])) {
$data['environment'] = $this->request->post['payment_paypal_environment'];
} elseif ($this->config->get('payment_paypal_environment')) {
$data['environment'] = $this->config->get('payment_paypal_environment');
} else {
$data['environment'] = 'production';
}
$data['seller_nonce'] = $this->token(50);
$data['configure_url'] = array(
'production' => array(
'ppcp' => 'https://www.paypal.com/bizsignup/partner/entry?partnerId=' . $data['setting']['partner']['production']['partner_id'] . '&partnerClientId=' . $data['setting']['partner']['production']['client_id'] . '&features=PAYMENT,REFUND&product=ppcp&integrationType=FO&returnToPartnerUrl=' . $data['partner_url'] . '&displayMode=minibrowser&sellerNonce=' . $data['seller_nonce'],
'express_checkout' => 'https://www.paypal.com/bizsignup/partner/entry?partnerId=' . $data['setting']['partner']['production']['partner_id'] . '&partnerClientId=' . $data['setting']['partner']['production']['client_id'] . '&features=PAYMENT,REFUND&product=EXPRESS_CHECKOUT&integrationType=FO&returnToPartnerUrl=' . $data['partner_url'] . '&displayMode=minibrowser&sellerNonce=' . $data['seller_nonce']
),
'sandbox' => array(
'ppcp' => 'https://www.sandbox.paypal.com/bizsignup/partner/entry?partnerId=' . $data['setting']['partner']['sandbox']['partner_id'] . '&partnerClientId=' . $data['setting']['partner']['sandbox']['client_id'] . '&features=PAYMENT,REFUND&product=ppcp&integrationType=FO&returnToPartnerUrl=' . $data['partner_url'] . '&displayMode=minibrowser&sellerNonce=' . $data['seller_nonce'],
'express_checkout' => 'https://www.sandbox.paypal.com/bizsignup/partner/entry?partnerId=' . $data['setting']['partner']['sandbox']['partner_id'] . '&partnerClientId=' . $data['setting']['partner']['sandbox']['client_id'] . '&features=PAYMENT,REFUND&product=EXPRESS_CHECKOUT&integrationType=FO&returnToPartnerUrl=' . $data['partner_url'] . '&displayMode=minibrowser&sellerNonce=' . $data['seller_nonce']
)
);
$data['help_checkout_express'] = sprintf($this->language->get('help_checkout_express'), $data['configure_url'][$data['environment']]['express_checkout']);
if (isset($client_id)) {
$data['client_id'] = $client_id;
} elseif (isset($this->request->post['payment_paypal_client_id'])) {
$data['client_id'] = $this->request->post['payment_paypal_client_id'];
} else {
$data['client_id'] = $this->config->get('payment_paypal_client_id');
}
if (isset($secret)) {
$data['secret'] = $secret;
} elseif (isset($this->request->post['payment_paypal_secret'])) {
$data['secret'] = $this->request->post['payment_paypal_secret'];
} else {
$data['secret'] = $this->config->get('payment_paypal_secret');
}
if (isset($merchant_id)) {
$data['merchant_id'] = $merchant_id;
} elseif (isset($this->request->post['payment_paypal_merchant_id'])) {
$data['merchant_id'] = $this->request->post['payment_paypal_merchant_id'];
} else {
$data['merchant_id'] = $this->config->get('payment_paypal_merchant_id');
}
$data['text_connect'] = sprintf($this->language->get('text_connect'), $data['client_id'], $data['secret'], $data['merchant_id']);
if (isset($webhook_id)) {
$data['webhook_id'] = $webhook_id;
} elseif (isset($this->request->post['payment_paypal_webhook_id'])) {
$data['webhook_id'] = $this->request->post['payment_paypal_webhook_id'];
} else {
$data['webhook_id'] = $this->config->get('payment_paypal_webhook_id');
}
if (isset($this->request->post['payment_paypal_debug'])) {
$data['debug'] = $this->request->post['payment_paypal_debug'];
} else {
$data['debug'] = $this->config->get('payment_paypal_debug');
}
if (isset($this->request->post['payment_paypal_transaction_method'])) {
$data['transaction_method'] = $this->request->post['payment_paypal_transaction_method'];
} else {
$data['transaction_method'] = $this->config->get('payment_paypal_transaction_method');
}
if (isset($this->request->post['payment_paypal_total'])) {
$data['total'] = $this->request->post['payment_paypal_total'];
} else {
$data['total'] = $this->config->get('payment_paypal_total');
}
$this->load->model('localisation/order_status');
$data['order_statuses'] = $this->model_localisation_order_status->getOrderStatuses();
if (isset($this->request->post['payment_paypal_geo_zone_id'])) {
$data['geo_zone_id'] = $this->request->post['payment_paypal_geo_zone_id'];
} else {
$data['geo_zone_id'] = $this->config->get('payment_paypal_geo_zone_id');
}
$this->load->model('localisation/geo_zone');
$data['geo_zones'] = $this->model_localisation_geo_zone->getGeoZones();
if (isset($this->request->post['payment_paypal_status'])) {
$data['status'] = $this->request->post['payment_paypal_status'];
} else {
$data['status'] = $this->config->get('payment_paypal_status');
}
if (isset($this->request->post['payment_paypal_sort_order'])) {
$data['sort_order'] = $this->request->post['payment_paypal_sort_order'];
} else {
$data['sort_order'] = $this->config->get('payment_paypal_sort_order');
}
if (isset($this->request->post['payment_paypal_currency_code'])) {
$data['currency_code'] = $this->request->post['payment_paypal_currency_code'];
} else {
$data['currency_code'] = $this->config->get('payment_paypal_currency_code');
}
if (isset($this->request->post['payment_paypal_currency_value'])) {
$data['currency_value'] = $this->request->post['payment_paypal_currency_value'];
} elseif ($this->config->get('payment_paypal_currency_value')) {
$data['currency_value'] = $this->config->get('payment_paypal_currency_value');
} else {
$data['currency_value'] = '1';
}
if (isset($this->request->post['payment_paypal_setting'])) {
$data['setting'] = array_replace_recursive((array)$data['setting'], (array)$this->request->post['payment_paypal_setting']);
} else {
$data['setting'] = array_replace_recursive((array)$data['setting'], (array)$this->config->get('payment_paypal_setting'));
}
if ($data['client_id'] && $data['secret']) {
require_once DIR_SYSTEM . 'library/paypal/paypal.php';
$paypal_info = array(
'client_id' => $data['client_id'],
'secret' => $data['secret'],
'environment' => $data['environment']
);
$paypal = new PayPal($paypal_info);
$token_info = array(
'grant_type' => 'client_credentials'
);
$paypal->setAccessToken($token_info);
$data['client_token'] = $paypal->getClientToken();
if ($paypal->hasErrors()) {
$error_messages = array();
$errors = $paypal->getErrors();
foreach ($errors as $error) {
if (isset($error['name']) && ($error['name'] == 'CURLE_OPERATION_TIMEOUTED')) {
$error['message'] = $this->language->get('error_timeout');
}
if (isset($error['details'][0]['description'])) {
$error_messages[] = $error['details'][0]['description'];
} else {
$error_messages[] = $error['message'];
}
$this->model_extension_payment_paypal->log($error, $error['message']);
}
$this->error['warning'] = implode(' ', $error_messages);
}
}
if (isset($this->error['warning'])) {
$data['error_warning'] = $this->error['warning'];
} else {
$data['error_warning'] = '';
}
$data['header'] = $this->load->controller('common/header');
$data['column_left'] = $this->load->controller('common/column_left');
$data['footer'] = $this->load->controller('common/footer');
$this->response->setOutput($this->load->view('extension/payment/paypal', $data));
}
public function callback() {
if (isset($this->request->post['environment']) && isset($this->request->post['authorization_code']) && isset($this->request->post['shared_id']) && isset($this->request->post['seller_nonce'])) {
$this->session->data['environment'] = $this->request->post['environment'];
$this->session->data['authorization_code'] = $this->request->post['authorization_code'];
$this->session->data['shared_id'] = $this->request->post['shared_id'];
$this->session->data['seller_nonce'] = $this->request->post['seller_nonce'];
}
$data['error'] = $this->error;
$this->response->setOutput(json_encode($data));
}
public function configureSmartButton() {
$this->load->model('extension/payment/paypal');
$this->model_extension_payment_paypal->configureSmartButton();
$this->response->redirect($this->url->link('extension/module/paypal_smart_button', 'user_token=' . $this->session->data['user_token'], true));
}
protected function validate() {
if (!$this->user->hasPermission('modify', 'extension/payment/paypal')) {
$this->error['warning'] = $this->language->get('error_permission');
}
require_once DIR_SYSTEM . 'library/paypal/paypal.php';
$paypal_info = array(
'client_id' => $this->request->post['payment_paypal_client_id'],
'secret' => $this->request->post['payment_paypal_secret'],
'environment' => $this->request->post['payment_paypal_environment']
);
$paypal = new PayPal($paypal_info);
$token_info = array(
'grant_type' => 'client_credentials'
);
$paypal->setAccessToken($token_info);
if ($paypal->hasErrors()) {
$error_messages = array();
$errors = $paypal->getErrors();
foreach ($errors as $error) {
if (isset($error['name']) && ($error['name'] == 'CURLE_OPERATION_TIMEOUTED')) {
$error['message'] = $this->language->get('error_timeout');
}
if (isset($error['details'][0]['description'])) {
$error_messages[] = $error['details'][0]['description'];
} else {
$error_messages[] = $error['message'];
}
$this->model_extension_payment_paypal->log($error, $error['message']);
}
$this->error['warning'] = implode(' ', $error_messages);
}
return !$this->error;
}
private function token($length = 32) {
// Create random token
$string = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
$max = strlen($string) - 1;
$token = '';
for ($i = 0; $i < $length; $i++) {
$token .= $string[mt_rand(0, $max)];
}
return $token;
}
}