service_process_confirm.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php
  2. /* For license terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. /**
  5. * Process purchase confirmation script for the Buy Courses plugin.
  6. *
  7. * @package chamilo.plugin.buycourses
  8. */
  9. require_once '../config.php';
  10. $plugin = BuyCoursesPlugin::create();
  11. $serviceSaleId = Session::read('bc_service_sale_id');
  12. if (empty($serviceSaleId)) {
  13. api_not_allowed(true);
  14. }
  15. $serviceSale = $plugin->getServiceSale($serviceSaleId);
  16. $userInfo = api_get_user_info($serviceSale['buyer']['id']);
  17. if (empty($serviceSale)) {
  18. api_not_allowed(true);
  19. }
  20. $currency = $plugin->getCurrency($serviceSale['currency_id']);
  21. $globalParameters = $plugin->getGlobalParameters();
  22. switch ($serviceSale['payment_type']) {
  23. case BuyCoursesPlugin::PAYMENT_TYPE_PAYPAL:
  24. $paypalParams = $plugin->getPaypalParams();
  25. $pruebas = $paypalParams['sandbox'] == 1;
  26. $paypalUsername = $paypalParams['username'];
  27. $paypalPassword = $paypalParams['password'];
  28. $paypalSignature = $paypalParams['signature'];
  29. // This var $itemPrice may be "0" if the transaction does not include a one-time purchase such as when you set up
  30. // a billing agreement for a recurring payment that is not immediately charged. When the field is set to 0,
  31. // purchase-specific fields are ignored. This little condition handle this fact.
  32. $itemPrice = $serviceSale['price'];
  33. $returnUrl = api_get_path(WEB_PLUGIN_PATH).'buycourses/src/service_success.php';
  34. $cancelUrl = api_get_path(WEB_PLUGIN_PATH).'buycourses/src/service_error.php';
  35. // The extra params for handle the hard job, this var is VERY IMPORTANT !!
  36. $extra = '';
  37. require_once 'paypalfunctions.php';
  38. $extra .= "&L_PAYMENTREQUEST_0_NAME0={$serviceSale['service']['name']}";
  39. $extra .= "&L_PAYMENTREQUEST_0_QTY0=1";
  40. $extra .= "&L_PAYMENTREQUEST_0_AMT0=$itemPrice";
  41. // Full Checkout express
  42. $expressCheckout = CallShortcutExpressCheckout(
  43. $itemPrice,
  44. $currency['iso_code'],
  45. 'paypal',
  46. $returnUrl,
  47. $cancelUrl,
  48. $extra
  49. );
  50. if ($expressCheckout['ACK'] !== 'Success') {
  51. $erroMessage = vsprintf(
  52. $plugin->get_lang('ErrorOccurred'),
  53. [$expressCheckout['L_ERRORCODE0'], $expressCheckout['L_LONGMESSAGE0']]
  54. );
  55. Display::addFlash(
  56. Display::return_message($erroMessage, 'error', false)
  57. );
  58. $plugin->cancelServiceSale($serviceSale['id']);
  59. header('Location: '.api_get_path(WEB_PLUGIN_PATH).'buycourses/src/service_catalog.php');
  60. exit;
  61. }
  62. if (!empty($globalParameters['sale_email'])) {
  63. $messageConfirmTemplate = new Template();
  64. $messageConfirmTemplate->assign('user', $userInfo);
  65. $messageConfirmTemplate->assign(
  66. 'sale',
  67. [
  68. 'date' => $serviceSale['buy_date'],
  69. 'product' => $serviceSale['service']['name'],
  70. 'currency' => $currency['iso_code'],
  71. 'price' => $serviceSale['price'],
  72. 'reference' => $serviceSale['reference'],
  73. ]
  74. );
  75. api_mail_html(
  76. '',
  77. $globalParameters['sale_email'],
  78. $plugin->get_lang('bc_subject'),
  79. $messageConfirmTemplate->fetch('buycourses/view/message_confirm.tpl')
  80. );
  81. }
  82. RedirectToPayPal($expressCheckout['TOKEN']);
  83. break;
  84. case BuyCoursesPlugin::PAYMENT_TYPE_TRANSFER:
  85. $transferAccounts = $plugin->getTransferAccounts();
  86. $form = new FormValidator(
  87. 'success',
  88. 'POST',
  89. api_get_self(),
  90. null,
  91. null,
  92. FormValidator::LAYOUT_INLINE
  93. );
  94. if ($form->validate()) {
  95. $formValues = $form->getSubmitValues();
  96. if (isset($formValues['cancel'])) {
  97. $plugin->cancelServiceSale($serviceSale['id']);
  98. unset($_SESSION['bc_service_sale_id']);
  99. Display::addFlash(
  100. Display::return_message($plugin->get_lang('OrderCancelled'), 'error', false)
  101. );
  102. header('Location: '.api_get_path(WEB_PLUGIN_PATH).'buycourses/src/service_catalog.php');
  103. exit;
  104. }
  105. $messageTemplate = new Template();
  106. $messageTemplate->assign(
  107. 'service_sale',
  108. [
  109. 'name' => $serviceSale['service']['name'],
  110. 'buyer' => $serviceSale['buyer']['name'],
  111. 'buy_date' => $serviceSale['buy_date'],
  112. 'start_date' => $serviceSale['start_date'],
  113. 'end_date' => $serviceSale['end_date'],
  114. 'currency' => $currency['iso_code'],
  115. 'price' => $serviceSale['price'],
  116. 'reference' => $serviceSale['reference'],
  117. ]
  118. );
  119. $messageTemplate->assign('transfer_accounts', $transferAccounts);
  120. $buyer = api_get_user_info($serviceSale['buyer']['id']);
  121. api_mail_html(
  122. $buyer['complete_name'],
  123. $buyer['email'],
  124. $plugin->get_lang('bc_subject'),
  125. $messageTemplate->fetch('buycourses/view/service_message_transfer.tpl')
  126. );
  127. if (!empty($globalParameters['sale_email'])) {
  128. $messageConfirmTemplate = new Template();
  129. $messageConfirmTemplate->assign('user', $userInfo);
  130. $messageConfirmTemplate->assign(
  131. 'sale',
  132. [
  133. 'date' => $serviceSale['buy_date'],
  134. 'product' => $serviceSale['service']['name'],
  135. 'currency' => $currency['iso_code'],
  136. 'price' => $serviceSale['price'],
  137. 'reference' => $serviceSale['reference'],
  138. ]
  139. );
  140. api_mail_html(
  141. '',
  142. $globalParameters['sale_email'],
  143. $plugin->get_lang('bc_subject'),
  144. $messageConfirmTemplate->fetch('buycourses/view/message_confirm.tpl')
  145. );
  146. }
  147. Display::addFlash(
  148. Display::return_message(
  149. sprintf(
  150. $plugin->get_lang('PurchaseStatusX'),
  151. $plugin->get_lang('PendingReasonByTransfer')
  152. ),
  153. 'success',
  154. false
  155. )
  156. );
  157. unset($_SESSION['bc_service_sale_id']);
  158. header('Location: '.api_get_path(WEB_PLUGIN_PATH).'buycourses/src/service_catalog.php');
  159. exit;
  160. }
  161. $form->addButton(
  162. 'confirm',
  163. $plugin->get_lang('ConfirmOrder'),
  164. 'check',
  165. 'success',
  166. 'default',
  167. null,
  168. ['id' => 'confirm']
  169. );
  170. $form->addButton(
  171. 'cancel',
  172. $plugin->get_lang('CancelOrder'),
  173. 'times',
  174. 'danger',
  175. 'default',
  176. null,
  177. ['id' => 'cancel']
  178. );
  179. $template = new Template();
  180. $template->assign('terms', $globalParameters['terms_and_conditions']);
  181. $template->assign('title', $serviceSale['service']['name']);
  182. $template->assign('price', $serviceSale['price']);
  183. $template->assign('currency', $serviceSale['currency_id']);
  184. $template->assign('buying_service', $serviceSale);
  185. $template->assign('user', $userInfo);
  186. $template->assign('service', $serviceSale['service']);
  187. $template->assign('service_item', $serviceSale['item']);
  188. $template->assign('transfer_accounts', $transferAccounts);
  189. $template->assign('form', $form->returnForm());
  190. $content = $template->fetch('buycourses/view/process_confirm.tpl');
  191. $template->assign('content', $content);
  192. $template->display_one_col_template();
  193. break;
  194. case BuyCoursesPlugin::PAYMENT_TYPE_CULQI:
  195. // We need to include the main online script, acording to the Culqi documentation the JS needs to be loeaded
  196. // directly from the main url "https://integ-pago.culqi.com" because a local copy of this JS is not supported
  197. $htmlHeadXtra[] = '<script src="//integ-pago.culqi.com/js/v1"></script>';
  198. $form = new FormValidator(
  199. 'success',
  200. 'POST',
  201. api_get_self(),
  202. null,
  203. null,
  204. FormValidator::LAYOUT_INLINE
  205. );
  206. if ($form->validate()) {
  207. $formValues = $form->getSubmitValues();
  208. if (isset($formValues['cancel'])) {
  209. $plugin->cancelServiceSale($serviceSale['id']);
  210. unset($_SESSION['bc_service_sale_id']);
  211. Display::addFlash(
  212. Display::return_message(
  213. $plugin->get_lang('OrderCanceled'),
  214. 'warning',
  215. false
  216. )
  217. );
  218. header('Location: '.api_get_path(WEB_PLUGIN_PATH).'buycourses/index.php');
  219. exit;
  220. }
  221. }
  222. $form->addButton(
  223. 'confirm',
  224. $plugin->get_lang('ConfirmOrder'),
  225. 'check',
  226. 'success',
  227. 'default',
  228. null,
  229. ['id' => 'confirm']
  230. );
  231. $form->addButton(
  232. 'cancel',
  233. $plugin->get_lang('CancelOrder'),
  234. 'times',
  235. 'danger',
  236. 'default',
  237. null,
  238. ['id' => 'cancel']
  239. );
  240. $template = new Template();
  241. $template->assign('terms', $globalParameters['terms_and_conditions']);
  242. $template->assign('title', $serviceSale['service']['name']);
  243. $template->assign('price', floatval($serviceSale['price']));
  244. $template->assign('currency', $plugin->getSelectedCurrency());
  245. $template->assign('buying_service', $serviceSale);
  246. $template->assign('user', $userInfo);
  247. $template->assign('service', $serviceSale['service']);
  248. $template->assign('service_item', $serviceSale['item']);
  249. $template->assign('form', $form->returnForm());
  250. $template->assign('is_culqi_payment', true);
  251. $template->assign('culqi_params', $culqiParams = $plugin->getCulqiParams());
  252. $content = $template->fetch('buycourses/view/process_confirm.tpl');
  253. $template->assign('content', $content);
  254. $template->display_one_col_template();
  255. break;
  256. }