success.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. /* For license terms, see /license.txt */
  3. /**
  4. * Success page for the purchase of a course in the Buy Courses plugin.
  5. *
  6. * @package chamilo.plugin.buycourses
  7. */
  8. require_once '../config.php';
  9. $plugin = BuyCoursesPlugin::create();
  10. $paypalEnabled = $plugin->get('paypal_enable') === 'true';
  11. if (!$paypalEnabled) {
  12. api_not_allowed(true);
  13. }
  14. $sale = $plugin->getSale($_SESSION['bc_sale_id']);
  15. if (empty($sale)) {
  16. api_not_allowed(true);
  17. }
  18. $buyingCourse = false;
  19. $buyingSession = false;
  20. switch ($sale['product_type']) {
  21. case BuyCoursesPlugin::PRODUCT_TYPE_COURSE:
  22. $buyingCourse = true;
  23. $course = $plugin->getCourseInfo($sale['product_id']);
  24. break;
  25. case BuyCoursesPlugin::PRODUCT_TYPE_SESSION:
  26. $buyingSession = true;
  27. $session = $plugin->getSessionInfo($sale['product_id']);
  28. break;
  29. }
  30. $paypalParams = $plugin->getPaypalParams();
  31. $pruebas = $paypalParams['sandbox'] == 1;
  32. $paypalUsername = $paypalParams['username'];
  33. $paypalPassword = $paypalParams['password'];
  34. $paypalSignature = $paypalParams['signature'];
  35. require_once "paypalfunctions.php";
  36. $form = new FormValidator(
  37. 'success',
  38. 'POST',
  39. api_get_self(),
  40. null,
  41. null,
  42. FormValidator::LAYOUT_INLINE
  43. );
  44. $form->addButton('confirm', $plugin->get_lang('ConfirmOrder'), 'check', 'success');
  45. $form->addButtonCancel($plugin->get_lang('CancelOrder'), 'cancel');
  46. if ($form->validate()) {
  47. $formValues = $form->getSubmitValues();
  48. if (isset($formValues['cancel'])) {
  49. $plugin->cancelSale($sale['id']);
  50. unset($_SESSION['bc_sale_id']);
  51. header('Location: '.api_get_path(WEB_PLUGIN_PATH).'buycourses/index.php');
  52. exit;
  53. }
  54. $confirmPayments = ConfirmPayment($sale['price']);
  55. if ($confirmPayments['ACK'] !== 'Success') {
  56. $erroMessage = vsprintf(
  57. $plugin->get_lang('ErrorOccurred'),
  58. [$expressCheckout['L_ERRORCODE0'], $confirmPayments['L_LONGMESSAGE0']]
  59. );
  60. Display::addFlash(
  61. Display::return_message($erroMessage, 'error', false)
  62. );
  63. header('Location: ../index.php');
  64. exit;
  65. }
  66. $transactionId = $confirmPayments['PAYMENTINFO_0_TRANSACTIONID'];
  67. $transactionType = $confirmPayments['PAYMENTINFO_0_TRANSACTIONTYPE'];
  68. switch ($confirmPayments['PAYMENTINFO_0_PAYMENTSTATUS']) {
  69. case 'Completed':
  70. $saleIsCompleted = $plugin->completeSale($sale['id']);
  71. if ($saleIsCompleted) {
  72. Display::addFlash(
  73. $plugin->getSubscriptionSuccessMessage($sale)
  74. );
  75. $plugin->storePayouts($sale['id']);
  76. break;
  77. }
  78. Display::addFlash(
  79. Display::return_message($plugin->get_lang('ErrorContactPlatformAdmin'), 'error')
  80. );
  81. break;
  82. case 'Pending':
  83. switch ($confirmPayments["PAYMENTINFO_0_PENDINGREASON"]) {
  84. case 'address':
  85. $purchaseStatus = $plugin->get_lang('PendingReasonByAddress');
  86. break;
  87. case 'authorization':
  88. $purchaseStatus = $plugin->get_lang('PendingReasonByAuthorization');
  89. break;
  90. case 'echeck':
  91. $purchaseStatus = $plugin->get_lang('PendingReasonByEcheck');
  92. break;
  93. case 'intl':
  94. $purchaseStatus = $plugin->get_lang('PendingReasonByIntl');
  95. break;
  96. case 'multicurrency':
  97. $purchaseStatus = $plugin->get_lang('PendingReasonByMulticurrency');
  98. break;
  99. case 'order':
  100. $purchaseStatus = $plugin->get_lang('PendingReasonByOrder');
  101. break;
  102. case 'paymentreview':
  103. $purchaseStatus = $plugin->get_lang('PendingReasonByPaymentReview');
  104. break;
  105. case 'regulatoryreview':
  106. $purchaseStatus = $plugin->get_lang('PendingReasonByRegulatoryReview');
  107. break;
  108. case 'unilateral':
  109. $purchaseStatus = $plugin->get_lang('PendingReasonByUnilateral');
  110. break;
  111. case 'upgrade':
  112. $purchaseStatus = $plugin->get_lang('PendingReasonByUpgrade');
  113. break;
  114. case 'verify':
  115. $purchaseStatus = $plugin->get_lang('PendingReasonByVerify');
  116. break;
  117. case 'other':
  118. default:
  119. $purchaseStatus = $plugin->get_lang('PendingReasonByOther');
  120. break;
  121. }
  122. Display::addFlash(
  123. Display::return_message(
  124. sprintf($plugin->get_lang('PurchaseStatusX'), $purchaseStatus),
  125. 'warning',
  126. false
  127. )
  128. );
  129. break;
  130. default:
  131. Display::addFlash(
  132. Display::return_message($plugin->get_lang('ErrorContactPlatformAdmin'), 'error')
  133. );
  134. break;
  135. }
  136. unset($_SESSION['bc_sale_id']);
  137. header('Location: '.api_get_path(WEB_PLUGIN_PATH).'buycourses/src/course_catalog.php');
  138. exit;
  139. }
  140. $token = isset($_GET['token']) ? $_GET['token'] : null;
  141. if (empty($token)) {
  142. api_not_allowed(true);
  143. }
  144. $shippingDetails = GetShippingDetails($token);
  145. if ($shippingDetails['ACK'] !== 'Success') {
  146. $erroMessage = vsprintf(
  147. $plugin->get_lang('ErrorOccurred'),
  148. [$expressCheckout['L_ERRORCODE0'], $shippingDetails['L_LONGMESSAGE0']]
  149. );
  150. Display::addFlash(
  151. Display::return_message($erroMessage, 'error', false)
  152. );
  153. header('Location: ../index.php');
  154. exit;
  155. }
  156. $interbreadcrumb[] = ["url" => "course_catalog.php", "name" => $plugin->get_lang('CourseListOnSale')];
  157. $templateName = $plugin->get_lang('PaymentMethods');
  158. $tpl = new Template($templateName);
  159. if ($buyingCourse) {
  160. $tpl->assign('course', $course);
  161. } elseif ($buyingSession) {
  162. $tpl->assign('session', $session);
  163. }
  164. $tpl->assign('buying_course', $buyingCourse);
  165. $tpl->assign('buying_session', $buyingSession);
  166. $tpl->assign('title', $sale['product_name']);
  167. $tpl->assign('price', $sale['price']);
  168. $tpl->assign('currency', $sale['currency_id']);
  169. $tpl->assign('user', api_get_user_info($sale['user_id']));
  170. $tpl->assign('form', $form->returnForm());
  171. $content = $tpl->fetch('buycourses/view/success.tpl');
  172. $tpl->assign('content', $content);
  173. $tpl->display_one_col_template();