Issues (2037)

plugin/buycourses/src/process_confirm.php (1 issue)

1
<?php
2
/* For license terms, see /license.txt */
3
4
/**
5
 * Process purchase confirmation script for the Buy Courses plugin.
6
 *
7
 * @package chamilo.plugin.buycourses
8
 */
9
require_once '../config.php';
10
11
$plugin = BuyCoursesPlugin::create();
12
13
$saleId = $_SESSION['bc_sale_id'];
14
$couponId = (!empty($_SESSION['bc_coupon_id']) ?? '');
15
16
if (empty($saleId)) {
17
    api_not_allowed(true);
18
}
19
20
$sale = $plugin->getSale($saleId);
21
22
$coupon = [];
23
if (!empty($couponId)) {
24
    $coupon = $plugin->getCoupon($couponId, $sale['product_type'], $sale['product_id']);
25
}
26
27
$userInfo = api_get_user_info($sale['user_id']);
28
29
if (empty($sale)) {
30
    api_not_allowed(true);
31
}
32
33
$currency = $plugin->getCurrency($sale['currency_id']);
34
$globalParameters = $plugin->getGlobalParameters();
35
36
switch ($sale['payment_type']) {
37
    case BuyCoursesPlugin::PAYMENT_TYPE_PAYPAL:
38
        $paypalParams = $plugin->getPaypalParams();
39
40
        $pruebas = $paypalParams['sandbox'] == 1;
41
        $paypalUsername = $paypalParams['username'];
42
        $paypalPassword = $paypalParams['password'];
43
        $paypalSignature = $paypalParams['signature'];
44
45
        require_once "paypalfunctions.php";
46
47
        $i = 0;
48
        $extra = "&L_PAYMENTREQUEST_0_NAME0={$sale['product_name']}";
49
        $extra .= "&L_PAYMENTREQUEST_0_AMT0={$sale['price']}";
50
        $extra .= "&L_PAYMENTREQUEST_0_QTY0=1";
51
52
        $expressCheckout = CallShortcutExpressCheckout(
53
            $sale['price'],
54
            $currency['iso_code'],
55
            'paypal',
56
            api_get_path(WEB_PLUGIN_PATH).'buycourses/src/success.php',
57
            api_get_path(WEB_PLUGIN_PATH).'buycourses/src/error.php',
58
            $extra
59
        );
60
61
        if ($expressCheckout["ACK"] !== 'Success') {
62
            $erroMessage = vsprintf(
63
                $plugin->get_lang('ErrorOccurred'),
64
                [$expressCheckout['L_ERRORCODE0'], $expressCheckout['L_LONGMESSAGE0']]
65
            );
66
            Display::addFlash(
67
                Display::return_message($erroMessage, 'error', false)
68
            );
69
            header('Location: ../index.php');
70
            exit;
71
        }
72
73
        if (!empty($globalParameters['sale_email'])) {
74
            $messageConfirmTemplate = new Template();
75
            $messageConfirmTemplate->assign('user', $userInfo);
76
            $messageConfirmTemplate->assign(
77
                'sale',
78
                [
79
                    'date' => $sale['date'],
80
                    'product' => $sale['product_name'],
81
                    'currency' => $currency['iso_code'],
82
                    'price' => $sale['price'],
83
                    'reference' => $sale['reference'],
84
                ]
85
            );
86
87
            api_mail_html(
88
                '',
89
                $globalParameters['sale_email'],
90
                $plugin->get_lang('bc_subject'),
91
                $messageConfirmTemplate->fetch('buycourses/view/message_confirm.tpl')
92
            );
93
        }
94
95
        RedirectToPayPal($expressCheckout["TOKEN"]);
96
        break;
97
    case BuyCoursesPlugin::PAYMENT_TYPE_TRANSFER:
98
        $buyingCourse = false;
99
        $buyingSession = false;
100
101
        switch ($sale['product_type']) {
102
            case BuyCoursesPlugin::PRODUCT_TYPE_COURSE:
103
                $buyingCourse = true;
104
                $course = $plugin->getCourseInfo($sale['product_id'], $coupon);
105
                break;
106
            case BuyCoursesPlugin::PRODUCT_TYPE_SESSION:
107
                $buyingSession = true;
108
                $session = $plugin->getSessionInfo($sale['product_id'], $coupon);
109
                break;
110
        }
111
112
        $transferAccounts = $plugin->getTransferAccounts();
113
        $infoEmailExtra = $plugin->getTransferInfoExtra()['tinfo_email_extra'];
114
115
        $form = new FormValidator(
116
            'success',
117
            'POST',
118
            api_get_self(),
119
            null,
120
            null,
121
            FormValidator::LAYOUT_INLINE
122
        );
123
124
        if ($form->validate()) {
125
            $formValues = $form->getSubmitValues();
126
127
            if (isset($formValues['cancel'])) {
128
                $plugin->cancelSale($sale['id']);
129
130
                unset($_SESSION['bc_sale_id']);
131
                unset($_SESSION['bc_coupon_id']);
132
133
                header('Location: '.api_get_path(WEB_PLUGIN_PATH).'buycourses/index.php');
134
                exit;
135
            }
136
137
            $messageTemplate = new Template();
138
            $messageTemplate->assign('user', $userInfo);
139
            $messageTemplate->assign(
140
                'sale',
141
                [
142
                    'date' => $sale['date'],
143
                    'product' => $sale['product_name'],
144
                    'currency' => $currency['iso_code'],
145
                    'price' => $sale['price'],
146
                    'reference' => $sale['reference'],
147
                ]
148
            );
149
            $messageTemplate->assign('transfer_accounts', $transferAccounts);
150
            $messageTemplate->assign('info_email_extra', $infoEmailExtra);
151
152
            MessageManager::send_message_simple(
153
                $userInfo['user_id'],
154
                $plugin->get_lang('bc_subject'),
155
                $messageTemplate->fetch('buycourses/view/message_transfer.tpl')
156
            );
157
158
            if (!empty($globalParameters['sale_email'])) {
159
                $messageConfirmTemplate = new Template();
160
                $messageConfirmTemplate->assign('user', $userInfo);
161
                $messageConfirmTemplate->assign(
162
                    'sale',
163
                    [
164
                        'date' => $sale['date'],
165
                        'product' => $sale['product_name'],
166
                        'currency' => $currency['iso_code'],
167
                        'price' => $sale['price'],
168
                        'reference' => $sale['reference'],
169
                    ]
170
                );
171
172
                api_mail_html(
173
                    '',
174
                    $globalParameters['sale_email'],
175
                    $plugin->get_lang('bc_subject'),
176
                    $messageConfirmTemplate->fetch('buycourses/view/message_confirm.tpl')
177
                );
178
            }
179
180
            Display::addFlash(
181
                Display::return_message(
182
                    sprintf(
183
                        $plugin->get_lang('PurchaseStatusX'),
184
                        $plugin->get_lang('PendingReasonByTransfer')
185
                    ),
186
                    'success',
187
                    false
188
                )
189
            );
190
191
            unset($_SESSION['bc_sale_id']);
192
            unset($_SESSION['bc_coupon_id']);
193
            header('Location: '.api_get_path(WEB_PLUGIN_PATH).'buycourses/src/course_catalog.php');
194
            exit;
195
        }
196
197
        $form->addButton(
198
            'confirm',
199
            $plugin->get_lang('ConfirmOrder'),
200
            'check',
201
            'success',
202
            'default',
203
            null,
204
            ['id' => 'confirm']
205
        );
206
        $form->addButtonCancel($plugin->get_lang('CancelOrder'), 'cancel');
207
208
        $template = new Template();
209
210
        if ($buyingCourse) {
211
            $template->assign('course', $course);
212
        } elseif ($buyingSession) {
213
            $template->assign('session', $session);
214
        }
215
216
        $template->assign('buying_course', $buyingCourse);
217
        $template->assign('buying_session', $buyingSession);
218
        $template->assign('terms', $globalParameters['terms_and_conditions']);
219
        $template->assign('title', $sale['product_name']);
220
        $template->assign('price', $sale['price']);
221
        $template->assign('currency', $sale['currency_id']);
222
        $template->assign('user', $userInfo);
223
        $template->assign('transfer_accounts', $transferAccounts);
224
        $template->assign('form', $form->returnForm());
225
        $template->assign('is_bank_transfer', true);
226
227
        $content = $template->fetch('buycourses/view/process_confirm.tpl');
228
229
        $template->assign('content', $content);
230
        $template->display_one_col_template();
231
        break;
232
    case BuyCoursesPlugin::PAYMENT_TYPE_CULQI:
233
        // We need to include the main online script, acording to the Culqi documentation the JS needs to be loeaded
234
        // directly from the main url "https://integ-pago.culqi.com" because a local copy of this JS is not supported
235
        $htmlHeadXtra[] = '<script src="//integ-pago.culqi.com/js/v1"></script>';
236
237
        $buyingCourse = false;
238
        $buyingSession = false;
239
240
        switch ($sale['product_type']) {
241
            case BuyCoursesPlugin::PRODUCT_TYPE_COURSE:
242
                $buyingCourse = true;
243
                $course = $plugin->getCourseInfo($sale['product_id'], $coupon);
244
                break;
245
            case BuyCoursesPlugin::PRODUCT_TYPE_SESSION:
246
                $buyingSession = true;
247
                $session = $plugin->getSessionInfo($sale['product_id'], $coupon);
248
                break;
249
        }
250
251
        $form = new FormValidator(
252
            'success',
253
            'POST',
254
            api_get_self(),
255
            null,
256
            null,
257
            FormValidator::LAYOUT_INLINE
258
        );
259
260
        if ($form->validate()) {
261
            $formValues = $form->getSubmitValues();
262
263
            if (isset($formValues['cancel'])) {
264
                $plugin->cancelSale($sale['id']);
265
266
                unset($_SESSION['bc_sale_id']);
267
                unset($_SESSION['bc_coupon_id']);
268
269
                Display::addFlash(
270
                    Display::return_message(
271
                        $plugin->get_lang('OrderCanceled'),
272
                        'warning',
273
                        false
274
                    )
275
                );
276
277
                header('Location: '.api_get_path(WEB_PLUGIN_PATH).'buycourses/index.php');
278
                exit;
279
            }
280
        }
281
        $form->addButton(
282
            'confirm',
283
            $plugin->get_lang('ConfirmOrder'),
284
            'check',
285
            'success',
286
            'default',
287
            null,
288
            ['id' => 'confirm']
289
        );
290
        $form->addButton(
291
            'cancel',
292
            $plugin->get_lang('CancelOrder'),
293
            'times',
294
            'danger',
295
            'default',
296
            null,
297
            ['id' => 'cancel']
298
        );
299
300
        $template = new Template();
301
302
        if ($buyingCourse) {
303
            $template->assign('course', $course);
304
        } elseif ($buyingSession) {
305
            $template->assign('session', $session);
306
        }
307
308
        $template->assign('buying_course', $buyingCourse);
309
        $template->assign('buying_session', $buyingSession);
310
        $template->assign('terms', $globalParameters['terms_and_conditions']);
311
        $template->assign('title', $sale['product_name']);
312
        $template->assign('price', floatval($sale['price']));
313
        $template->assign('currency', $plugin->getSelectedCurrency());
314
        $template->assign('user', $userInfo);
315
        $template->assign('sale', $sale);
316
        $template->assign('form', $form->returnForm());
317
        $template->assign('is_culqi_payment', true);
318
        $template->assign('culqi_params', $culqiParams = $plugin->getCulqiParams());
319
320
        $content = $template->fetch('buycourses/view/process_confirm.tpl');
321
322
        $template->assign('content', $content);
323
        $template->display_one_col_template();
324
325
        break;
326
    case BuyCoursesPlugin::PAYMENT_TYPE_TPV_REDSYS:
327
        $tpvRedsysParams = $plugin->getTpvRedsysParams();
328
329
        require_once '../resources/apiRedsys.php';
330
        $tpv = new RedsysAPI();
331
332
        $merchantcode = $tpvRedsysParams['merchantcode'];
333
        $terminal = $tpvRedsysParams['terminal'];
334
        $currency = $tpvRedsysParams['currency'];
335
        $transactionType = "0";
336
        $urlMerchant = api_get_path(WEB_PLUGIN_PATH).'buycourses/src/tpv_response.php';
337
        $urlSuccess = api_get_path(WEB_PLUGIN_PATH).'buycourses/src/tpv_success.php';
338
        $urlFailed = api_get_path(WEB_PLUGIN_PATH).'buycourses/src/tpv_error.php';
339
        $order = str_pad(strval($saleId), 4, "0", STR_PAD_LEFT);
340
        $amount = $sale['price'] * 100;
341
        $description = $plugin->get_lang('OrderReference').": ".$sale['reference'];
342
        $tpv->setParameter("DS_MERCHANT_AMOUNT", $amount);
343
        $tpv->setParameter("DS_MERCHANT_ORDER", $order);
344
        $tpv->setParameter("DS_MERCHANT_MERCHANTCODE", $merchantcode);
345
        $tpv->setParameter("DS_MERCHANT_CURRENCY", $currency);
346
        $tpv->setParameter("DS_MERCHANT_TRANSACTIONTYPE", $transactionType);
347
        $tpv->setParameter("DS_MERCHANT_TERMINAL", $terminal);
348
        $tpv->setParameter("DS_MERCHANT_MERCHANTURL", $urlMerchant);
349
        $tpv->setParameter("DS_MERCHANT_URLOK", $urlSuccess);
350
        $tpv->setParameter("DS_MERCHANT_URLKO", $urlFailed);
351
        $tpv->setParameter("DS_MERCHANT_PRODUCTDESCRIPTION", $description);
352
353
        $version = "HMAC_SHA256_V1";
354
        $kc = $tpvRedsysParams['kc'];
355
356
        $urlTpv = $tpvRedsysParams['url_redsys'];
357
        $sandboxFlag = $tpvRedsysParams['sandbox'] == 1;
358
        if ($sandboxFlag === true) {
359
            $urlTpv = $tpvRedsysParams['url_redsys_sandbox'];
360
        }
361
362
        $params = $tpv->createMerchantParameters();
363
        $signature = $tpv->createMerchantSignature($kc);
364
365
        echo '<form name="tpv_chamilo" action="'.$urlTpv.'" method="POST">';
366
        echo '<input type="hidden" name="Ds_SignatureVersion" value="'.$version.'" />';
367
        echo '<input type="hidden" name="Ds_MerchantParameters" value="'.$params.'" />';
368
        echo '<input type="hidden" name="Ds_Signature" value="'.$signature.'" />';
369
        echo '</form>';
370
371
        echo '<SCRIPT language=javascript>';
372
        echo 'document.tpv_chamilo.submit();';
373
        echo '</script>';
374
375
        break;
376
    case BuyCoursesPlugin::PAYMENT_TYPE_STRIPE:
377
        $buyingCourse = false;
378
        $buyingSession = false;
379
380
        switch ($sale['product_type']) {
381
            case BuyCoursesPlugin::PRODUCT_TYPE_COURSE:
382
                $buyingCourse = true;
383
                $course = $plugin->getCourseInfo($sale['product_id'], $coupon);
384
                break;
385
            case BuyCoursesPlugin::PRODUCT_TYPE_SESSION:
386
                $buyingSession = true;
387
                $session = $plugin->getSessionInfo($sale['product_id'], $coupon);
388
                break;
389
        }
390
391
        $form = new FormValidator(
392
            'success',
393
            'POST',
394
            api_get_self(),
395
            null,
396
            null,
397
            FormValidator::LAYOUT_INLINE
398
        );
399
400
        if ($form->validate()) {
401
            $formValues = $form->getSubmitValues();
402
403
            if (isset($formValues['cancel'])) {
404
                $plugin->cancelSale($sale['id']);
405
406
                unset($_SESSION['bc_sale_id']);
407
                unset($_SESSION['bc_coupon_id']);
408
409
                header('Location: '.api_get_path(WEB_PLUGIN_PATH).'buycourses/index.php');
410
                exit;
411
            }
412
413
            $stripeParams = $plugin->getStripeParams();
414
            $currency = $plugin->getCurrency($sale['currency_id']);
415
416
            \Stripe\Stripe::setApiKey($stripeParams['secret_key']);
417
            \Stripe\Stripe::setAppInfo("ChamiloBuyCoursesPlugin");
418
419
            $session = \Stripe\Checkout\Session::create([
420
                'payment_method_types' => ['card'],
421
                'line_items' => [[
422
                    'price_data' => [
423
                        'unit_amount_decimal' => $sale['price'] * 100,
424
                        'currency' => $currency['iso_code'],
425
                        'product_data' => [
426
                            'name' => $sale['product_name'],
427
                        ],
428
                    ],
429
                    'quantity' => 1,
430
                ]],
431
                'customer_email' => $_SESSION['_user']['email'],
432
                'mode' => 'payment',
433
                'success_url' => api_get_path(WEB_PLUGIN_PATH).'buycourses/src/stripe_success.php',
434
                'cancel_url' => api_get_path(WEB_PLUGIN_PATH).'buycourses/src/stripe_cancel.php',
435
            ]);
436
437
            if (!empty($session)) {
438
                $plugin->updateSaleReference($saleId, $session->id);
439
440
                unset($_SESSION['bc_coupon_id']);
441
442
                header('HTTP/1.1 301 Moved Permanently');
443
                header('Location: '.$session->url);
444
            } else {
445
                Display::addFlash(
446
                    Display::return_message(
447
                        $plugin->get_lang('ErrorOccurred'),
448
                         'error',
449
                         false
450
                        )
451
                );
452
                header('Location: ../index.php');
453
            }
454
455
            exit;
456
        }
457
458
        $form->addButton(
459
            'confirm',
460
            $plugin->get_lang('ConfirmOrder'),
461
            'check',
462
            'success',
463
            'default',
464
            null,
465
            ['id' => 'confirm']
466
        );
467
        $form->addButtonCancel($plugin->get_lang('CancelOrder'), 'cancel');
468
469
        $template = new Template();
470
471
        if ($buyingCourse) {
472
            $template->assign('course', $course);
473
        } elseif ($buyingSession) {
474
            $template->assign('session', $session);
475
        }
476
477
        $template->assign('buying_course', $buyingCourse);
478
        $template->assign('buying_session', $buyingSession);
479
        $template->assign('terms', $globalParameters['terms_and_conditions']);
480
        $template->assign('title', $sale['product_name']);
481
        $template->assign('price', $sale['price']);
482
        $template->assign('currency', $sale['currency_id']);
483
        $template->assign('user', $userInfo);
484
        $template->assign('transfer_accounts', $transferAccounts);
485
        $template->assign('form', $form->returnForm());
486
        $template->assign('is_bank_transfer', false);
487
488
        $content = $template->fetch('buycourses/view/process_confirm.tpl');
489
490
        $template->assign('content', $content);
491
        $template->display_one_col_template();
492
493
        break;
494
495
    case BuyCoursesPlugin::PAYMENT_TYPE_TPV_CECABANK:
496
        $buyingCourse = false;
497
        $buyingSession = false;
498
499
        switch ($sale['product_type']) {
500
            case BuyCoursesPlugin::PRODUCT_TYPE_COURSE:
501
                $buyingCourse = true;
502
                $course = $plugin->getCourseInfo($sale['product_id']);
503
                break;
504
            case BuyCoursesPlugin::PRODUCT_TYPE_SESSION:
505
                $buyingSession = true;
506
                $session = $plugin->getSessionInfo($sale['product_id']);
507
                break;
508
        }
509
510
        $cecabankParams = $plugin->getcecabankParams();
511
        $currency = $plugin->getCurrency($sale['currency_id']);
512
513
        $form = new FormValidator(
514
            'success',
515
            'POST',
516
            api_get_self(),
517
            null,
518
            null,
519
            FormValidator::LAYOUT_INLINE
520
        );
521
522
        if ($form->validate()) {
523
            $formValues = $form->getSubmitValues();
524
525
            if (isset($formValues['cancel'])) {
526
                $plugin->cancelSale($sale['id']);
527
528
                unset($_SESSION['bc_sale_id']);
529
                unset($_SESSION['bc_coupon_id']);
530
531
                header('Location: '.api_get_path(WEB_PLUGIN_PATH).'buycourses/index.php');
532
                exit;
533
            }
534
535
            $urlTpv = $cecabankParams['url'];
536
            $currency = $plugin->getCurrency($sale['currency_id']);
537
            $signature = $plugin->getCecabankSignature($sale['reference'], $sale['price']);
538
539
            echo '<form name="tpv_chamilo" action="'.$urlTpv.'" method="POST">';
540
            echo '<input type="hidden" name="MerchantID" value="'.$cecabankParams['merchant_id'].'" />';
541
            echo '<input type="hidden" name="AcquirerBIN" value="'.$cecabankParams['acquirer_bin'].'" />';
542
            echo '<input type="hidden" name="TerminalID" value="'.$cecabankParams['terminal_id'].'" />';
543
            echo '<input type="hidden" name="URL_OK" value="'.api_get_path(WEB_PLUGIN_PATH).'buycourses/src/cecabank_success.php'.'" />';
544
            echo '<input type="hidden" name="URL_NOK" value="'.api_get_path(WEB_PLUGIN_PATH).'buycourses/src/cecabank_cancel.php'.'" />';
545
            echo '<input type="hidden" name="Firma" value="'.$signature.'" />';
0 ignored issues
show
Are you sure $signature of type array can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

545
            echo '<input type="hidden" name="Firma" value="'./** @scrutinizer ignore-type */ $signature.'" />';
Loading history...
546
            echo '<input type="hidden" name="Cifrado" value="'.$cecabankParams['cypher'].'" />';
547
            echo '<input type="hidden" name="Num_operacion" value="'.$sale['reference'].'" />';
548
            echo '<input type="hidden" name="Importe" value="'.($sale['price'] * 100).'" />';
549
            echo '<input type="hidden" name="TipoMoneda" value="978" />';
550
            echo '<input type="hidden" name="Exponente" value="'.$cecabankParams['exponent'].'" />';
551
            echo '<input type="hidden" name="Pago_soportado" value="'.$cecabankParams['supported_payment'].'" />';
552
            echo '</form>';
553
554
            echo '<SCRIPT language=javascript>';
555
            echo 'document.tpv_chamilo.submit();';
556
            echo '</script>';
557
558
            exit;
559
        }
560
561
        $form->addButton(
562
            'confirm',
563
            $plugin->get_lang('ConfirmOrder'),
564
            'check',
565
            'success',
566
            'default',
567
            null,
568
            ['id' => 'confirm']
569
        );
570
        $form->addButtonCancel($plugin->get_lang('CancelOrder'), 'cancel');
571
572
        $template = new Template();
573
574
        if ($buyingCourse) {
575
            $template->assign('course', $course);
576
        } elseif ($buyingSession) {
577
            $template->assign('session', $session);
578
        }
579
580
        $template->assign('buying_course', $buyingCourse);
581
        $template->assign('buying_session', $buyingSession);
582
        $template->assign('terms', $globalParameters['terms_and_conditions']);
583
        $template->assign('title', $sale['product_name']);
584
        $template->assign('price', $sale['price']);
585
        $template->assign('currency', $sale['currency_id']);
586
        $template->assign('user', $userInfo);
587
        $template->assign('transfer_accounts', $transferAccounts);
588
        $template->assign('form', $form->returnForm());
589
        $template->assign('is_bank_transfer', false);
590
591
        $content = $template->fetch('buycourses/view/process_confirm.tpl');
592
593
        $template->assign('content', $content);
594
        $template->display_one_col_template();
595
596
        break;
597
}
598