1 | <?php |
||
2 | /* For license terms, see /license.txt */ |
||
3 | |||
4 | use ChamiloSession as Session; |
||
5 | |||
6 | /** |
||
7 | * Process purchase confirmation script for the Buy Courses plugin. |
||
8 | * |
||
9 | * @package chamilo.plugin.buycourses |
||
10 | */ |
||
11 | require_once '../config.php'; |
||
12 | |||
13 | $plugin = BuyCoursesPlugin::create(); |
||
14 | $serviceSaleId = Session::read('bc_service_sale_id'); |
||
15 | $couponId = Session::read('bc_coupon_id'); |
||
16 | |||
17 | if (empty($serviceSaleId)) { |
||
18 | api_not_allowed(true); |
||
19 | } |
||
20 | |||
21 | $serviceSale = $plugin->getServiceSale($serviceSaleId, $coupon); |
||
22 | $userInfo = api_get_user_info($serviceSale['buyer']['id']); |
||
23 | |||
24 | if (!empty($couponId)) { |
||
25 | $coupon = $plugin->getCouponService($couponId, $serviceSale['service_id']); |
||
26 | $serviceSale['item'] = $plugin->getService($serviceSale['service_id'], $coupon); |
||
27 | } |
||
28 | |||
29 | if (empty($serviceSale)) { |
||
30 | api_not_allowed(true); |
||
31 | } |
||
32 | |||
33 | $currency = $plugin->getCurrency($serviceSale['currency_id']); |
||
34 | $globalParameters = $plugin->getGlobalParameters(); |
||
35 | |||
36 | switch ($serviceSale['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 | // This var $itemPrice may be "0" if the transaction does not include a one-time purchase such as when you set up |
||
46 | // a billing agreement for a recurring payment that is not immediately charged. When the field is set to 0, |
||
47 | // purchase-specific fields are ignored. This little condition handle this fact. |
||
48 | $itemPrice = $serviceSale['price']; |
||
49 | |||
50 | $returnUrl = api_get_path(WEB_PLUGIN_PATH).'buycourses/src/service_success.php'; |
||
51 | $cancelUrl = api_get_path(WEB_PLUGIN_PATH).'buycourses/src/service_error.php'; |
||
52 | |||
53 | // The extra params for handle the hard job, this var is VERY IMPORTANT !! |
||
54 | $extra = ''; |
||
55 | require_once 'paypalfunctions.php'; |
||
56 | |||
57 | $extra .= "&L_PAYMENTREQUEST_0_NAME0={$serviceSale['service']['name']}"; |
||
58 | $extra .= "&L_PAYMENTREQUEST_0_QTY0=1"; |
||
59 | $extra .= "&L_PAYMENTREQUEST_0_AMT0=$itemPrice"; |
||
60 | |||
61 | // Full Checkout express |
||
62 | $expressCheckout = CallShortcutExpressCheckout( |
||
63 | $itemPrice, |
||
64 | $currency['iso_code'], |
||
65 | 'paypal', |
||
66 | $returnUrl, |
||
67 | $cancelUrl, |
||
68 | $extra |
||
69 | ); |
||
70 | |||
71 | if ($expressCheckout['ACK'] !== 'Success') { |
||
72 | $erroMessage = vsprintf( |
||
73 | $plugin->get_lang('ErrorOccurred'), |
||
74 | [$expressCheckout['L_ERRORCODE0'], $expressCheckout['L_LONGMESSAGE0']] |
||
75 | ); |
||
76 | Display::addFlash( |
||
77 | Display::return_message($erroMessage, 'error', false) |
||
78 | ); |
||
79 | |||
80 | $plugin->cancelServiceSale($serviceSale['id']); |
||
81 | header('Location: '.api_get_path(WEB_PLUGIN_PATH).'buycourses/src/service_catalog.php'); |
||
82 | exit; |
||
83 | } |
||
84 | |||
85 | if (!empty($globalParameters['sale_email'])) { |
||
86 | $messageConfirmTemplate = new Template(); |
||
87 | $messageConfirmTemplate->assign('user', $userInfo); |
||
88 | $messageConfirmTemplate->assign( |
||
89 | 'sale', |
||
90 | [ |
||
91 | 'date' => $serviceSale['buy_date'], |
||
92 | 'product' => $serviceSale['service']['name'], |
||
93 | 'currency' => $currency['iso_code'], |
||
94 | 'price' => $serviceSale['price'], |
||
95 | 'reference' => $serviceSale['reference'], |
||
96 | ] |
||
97 | ); |
||
98 | |||
99 | api_mail_html( |
||
100 | '', |
||
101 | $globalParameters['sale_email'], |
||
102 | $plugin->get_lang('bc_subject'), |
||
103 | $messageConfirmTemplate->fetch('buycourses/view/message_confirm.tpl') |
||
104 | ); |
||
105 | } |
||
106 | |||
107 | RedirectToPayPal($expressCheckout['TOKEN']); |
||
108 | break; |
||
109 | case BuyCoursesPlugin::PAYMENT_TYPE_TRANSFER: |
||
110 | $transferAccounts = $plugin->getTransferAccounts(); |
||
111 | |||
112 | $form = new FormValidator( |
||
113 | 'success', |
||
114 | 'POST', |
||
115 | api_get_self(), |
||
116 | null, |
||
117 | null, |
||
118 | FormValidator::LAYOUT_INLINE |
||
119 | ); |
||
120 | |||
121 | if ($form->validate()) { |
||
122 | $formValues = $form->getSubmitValues(); |
||
123 | |||
124 | if (isset($formValues['cancel'])) { |
||
125 | $plugin->cancelServiceSale($serviceSale['id']); |
||
126 | |||
127 | unset($_SESSION['bc_service_sale_id']); |
||
128 | unset($_SESSION['bc_coupon_id']); |
||
129 | Display::addFlash( |
||
130 | Display::return_message($plugin->get_lang('OrderCancelled'), 'error', false) |
||
131 | ); |
||
132 | header('Location: '.api_get_path(WEB_PLUGIN_PATH).'buycourses/src/service_catalog.php'); |
||
133 | exit; |
||
134 | } |
||
135 | |||
136 | $messageTemplate = new Template(); |
||
137 | $messageTemplate->assign( |
||
138 | 'service_sale', |
||
139 | [ |
||
140 | 'name' => $serviceSale['service']['name'], |
||
141 | 'buyer' => $serviceSale['buyer']['name'], |
||
142 | 'buy_date' => $serviceSale['buy_date'], |
||
143 | 'start_date' => $serviceSale['start_date'], |
||
144 | 'end_date' => $serviceSale['end_date'], |
||
145 | 'currency' => $currency['iso_code'], |
||
146 | 'price' => $serviceSale['price'], |
||
147 | 'reference' => $serviceSale['reference'], |
||
148 | ] |
||
149 | ); |
||
150 | $messageTemplate->assign('transfer_accounts', $transferAccounts); |
||
151 | $buyer = api_get_user_info($serviceSale['buyer']['id']); |
||
152 | |||
153 | MessageManager::send_message_simple( |
||
154 | $buyer['user_id'], |
||
155 | $plugin->get_lang('bc_subject'), |
||
156 | $messageTemplate->fetch('buycourses/view/service_message_transfer.tpl') |
||
157 | ); |
||
158 | |||
159 | if (!empty($globalParameters['sale_email'])) { |
||
160 | $messageConfirmTemplate = new Template(); |
||
161 | $messageConfirmTemplate->assign('user', $userInfo); |
||
162 | $messageConfirmTemplate->assign( |
||
163 | 'sale', |
||
164 | [ |
||
165 | 'date' => $serviceSale['buy_date'], |
||
166 | 'product' => $serviceSale['service']['name'], |
||
167 | 'currency' => $currency['iso_code'], |
||
168 | 'price' => $serviceSale['price'], |
||
169 | 'reference' => $serviceSale['reference'], |
||
170 | ] |
||
171 | ); |
||
172 | |||
173 | api_mail_html( |
||
174 | '', |
||
175 | $globalParameters['sale_email'], |
||
176 | $plugin->get_lang('bc_subject'), |
||
177 | $messageConfirmTemplate->fetch('buycourses/view/message_confirm.tpl') |
||
178 | ); |
||
179 | } |
||
180 | |||
181 | Display::addFlash( |
||
182 | Display::return_message( |
||
183 | sprintf( |
||
184 | $plugin->get_lang('PurchaseStatusX'), |
||
185 | $plugin->get_lang('PendingReasonByTransfer') |
||
186 | ), |
||
187 | 'success', |
||
188 | false |
||
189 | ) |
||
190 | ); |
||
191 | |||
192 | unset($_SESSION['bc_service_sale_id']); |
||
193 | unset($_SESSION['bc_coupon_id']); |
||
194 | header('Location: '.api_get_path(WEB_PLUGIN_PATH).'buycourses/src/service_catalog.php'); |
||
195 | exit; |
||
196 | } |
||
197 | |||
198 | $form->addButton( |
||
199 | 'confirm', |
||
200 | $plugin->get_lang('ConfirmOrder'), |
||
201 | 'check', |
||
202 | 'success', |
||
203 | 'default', |
||
204 | null, |
||
205 | ['id' => 'confirm'] |
||
206 | ); |
||
207 | $form->addButton( |
||
208 | 'cancel', |
||
209 | $plugin->get_lang('CancelOrder'), |
||
210 | 'times', |
||
211 | 'danger', |
||
212 | 'default', |
||
213 | null, |
||
214 | ['id' => 'cancel'] |
||
215 | ); |
||
216 | |||
217 | $template = new Template(); |
||
218 | $template->assign('terms', $globalParameters['terms_and_conditions']); |
||
219 | $template->assign('title', $serviceSale['service']['name']); |
||
220 | $template->assign('price', $serviceSale['price']); |
||
221 | $template->assign('currency', $serviceSale['currency_id']); |
||
222 | $template->assign('buying_service', $serviceSale); |
||
223 | $template->assign('user', $userInfo); |
||
224 | $template->assign('service', $serviceSale['service']); |
||
225 | $template->assign('service_item', $serviceSale['item']); |
||
226 | $template->assign('transfer_accounts', $transferAccounts); |
||
227 | $template->assign('form', $form->returnForm()); |
||
228 | |||
229 | $content = $template->fetch('buycourses/view/process_confirm.tpl'); |
||
230 | |||
231 | $template->assign('content', $content); |
||
232 | $template->display_one_col_template(); |
||
233 | break; |
||
234 | case BuyCoursesPlugin::PAYMENT_TYPE_CULQI: |
||
235 | // We need to include the main online script, acording to the Culqi documentation the JS needs to be loeaded |
||
236 | // directly from the main url "https://integ-pago.culqi.com" because a local copy of this JS is not supported |
||
237 | $htmlHeadXtra[] = '<script src="//integ-pago.culqi.com/js/v1"></script>'; |
||
238 | |||
239 | $form = new FormValidator( |
||
240 | 'success', |
||
241 | 'POST', |
||
242 | api_get_self(), |
||
243 | null, |
||
244 | null, |
||
245 | FormValidator::LAYOUT_INLINE |
||
246 | ); |
||
247 | |||
248 | if ($form->validate()) { |
||
249 | $formValues = $form->getSubmitValues(); |
||
250 | if (isset($formValues['cancel'])) { |
||
251 | $plugin->cancelServiceSale($serviceSale['id']); |
||
252 | |||
253 | unset($_SESSION['bc_service_sale_id']); |
||
254 | unset($_SESSION['bc_coupon_id']); |
||
255 | |||
256 | Display::addFlash( |
||
257 | Display::return_message( |
||
258 | $plugin->get_lang('OrderCanceled'), |
||
259 | 'warning', |
||
260 | false |
||
261 | ) |
||
262 | ); |
||
263 | |||
264 | header('Location: '.api_get_path(WEB_PLUGIN_PATH).'buycourses/index.php'); |
||
265 | exit; |
||
266 | } |
||
267 | } |
||
268 | $form->addButton( |
||
269 | 'confirm', |
||
270 | $plugin->get_lang('ConfirmOrder'), |
||
271 | 'check', |
||
272 | 'success', |
||
273 | 'default', |
||
274 | null, |
||
275 | ['id' => 'confirm'] |
||
276 | ); |
||
277 | $form->addButton( |
||
278 | 'cancel', |
||
279 | $plugin->get_lang('CancelOrder'), |
||
280 | 'times', |
||
281 | 'danger', |
||
282 | 'default', |
||
283 | null, |
||
284 | ['id' => 'cancel'] |
||
285 | ); |
||
286 | |||
287 | $template = new Template(); |
||
288 | $template->assign('terms', $globalParameters['terms_and_conditions']); |
||
289 | $template->assign('title', $serviceSale['service']['name']); |
||
290 | $template->assign('price', floatval($serviceSale['price'])); |
||
291 | $template->assign('currency', $plugin->getSelectedCurrency()); |
||
292 | $template->assign('buying_service', $serviceSale); |
||
293 | $template->assign('user', $userInfo); |
||
294 | $template->assign('service', $serviceSale['service']); |
||
295 | $template->assign('service_item', $serviceSale['item']); |
||
296 | $template->assign('form', $form->returnForm()); |
||
297 | $template->assign('is_culqi_payment', true); |
||
298 | $template->assign('culqi_params', $culqiParams = $plugin->getCulqiParams()); |
||
299 | $content = $template->fetch('buycourses/view/process_confirm.tpl'); |
||
300 | $template->assign('content', $content); |
||
301 | $template->display_one_col_template(); |
||
302 | break; |
||
303 | |||
304 | case BuyCoursesPlugin::PAYMENT_TYPE_TPV_CECABANK: |
||
305 | $cecabankParams = $plugin->getcecabankParams(); |
||
306 | $currency = $plugin->getCurrency($sale['currency_id']); |
||
307 | |||
308 | $form = new FormValidator( |
||
309 | 'success', |
||
310 | 'POST', |
||
311 | api_get_self(), |
||
312 | null, |
||
313 | null, |
||
314 | FormValidator::LAYOUT_INLINE |
||
315 | ); |
||
316 | |||
317 | if ($form->validate()) { |
||
318 | $formValues = $form->getSubmitValues(); |
||
319 | |||
320 | if (isset($formValues['cancel'])) { |
||
321 | $plugin->cancelServiceSale($sale['id']); |
||
322 | |||
323 | unset($_SESSION['bc_sale_id']); |
||
324 | unset($_SESSION['bc_coupon_id']); |
||
325 | |||
326 | header('Location: '.api_get_path(WEB_PLUGIN_PATH).'buycourses/index.php'); |
||
327 | exit; |
||
328 | } |
||
329 | |||
330 | $urlTpv = $cecabankParams['merchart_id']; |
||
331 | $currency = $plugin->getCurrency($sale['currency_id']); |
||
332 | $signature = $plugin->getCecabankSignature($sale['reference'], $sale['price']); |
||
333 | |||
334 | echo '<form name="tpv_chamilo" action="'.$urlTpv.'" method="POST">'; |
||
335 | echo '<input type="hidden" name="MerchantID" value="'.$cecabankParams['merchant_id'].'" />'; |
||
336 | echo '<input type="hidden" name="AcquirerBIN" value="'.$cecabankParams['acquirer_bin'].'" />'; |
||
337 | echo '<input type="hidden" name="TerminalID" value="'.$cecabankParams['terminal_id'].'" />'; |
||
338 | echo '<input type="hidden" name="URL_OK" value="'.api_get_path(WEB_PLUGIN_PATH).'buycourses/src/cecabank_success.php'.'" />'; |
||
339 | echo '<input type="hidden" name="URL_NOK" value="'.api_get_path(WEB_PLUGIN_PATH).'buycourses/src/cecabank_cancel.php'.'" />'; |
||
340 | echo '<input type="hidden" name="Firma" value="'.$signature.'" />'; |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
341 | echo '<input type="hidden" name="Cifrado" value="'.$cecabankParams['cypher'].'" />'; |
||
342 | echo '<input type="hidden" name="Num_operacion" value="'.$sale['reference'].'" />'; |
||
343 | echo '<input type="hidden" name="Importe" value="'.($sale['price'] * 100).'" />'; |
||
344 | echo '<input type="hidden" name="TipoMoneda" value="'.$cecabankParams['currency'].'" />'; |
||
345 | echo '<input type="hidden" name="Exponente" value="'.$cecabankParams['exponent'].'" />'; |
||
346 | echo '<input type="hidden" name="Pago_soportado" value="'.$cecabankParams['supported_payment'].'" />'; |
||
347 | echo '</form>'; |
||
348 | |||
349 | echo '<SCRIPT language=javascript>'; |
||
350 | echo 'document.tpv_chamilo.submit();'; |
||
351 | echo '</script>'; |
||
352 | |||
353 | exit; |
||
354 | } |
||
355 | |||
356 | $form->addButton( |
||
357 | 'confirm', |
||
358 | $plugin->get_lang('ConfirmOrder'), |
||
359 | 'check', |
||
360 | 'success', |
||
361 | 'default', |
||
362 | null, |
||
363 | ['id' => 'confirm'] |
||
364 | ); |
||
365 | $form->addButtonCancel($plugin->get_lang('CancelOrder'), 'cancel'); |
||
366 | |||
367 | $template = new Template(); |
||
368 | $template->assign('terms', $globalParameters['terms_and_conditions']); |
||
369 | $template->assign('title', $serviceSale['service']['name']); |
||
370 | $template->assign('price', $serviceSale['price']); |
||
371 | $template->assign('currency', $serviceSale['currency_id']); |
||
372 | $template->assign('buying_service', $serviceSale); |
||
373 | $template->assign('user', $userInfo); |
||
374 | $template->assign('service', $serviceSale['service']); |
||
375 | $template->assign('service_item', $serviceSale['item']); |
||
376 | $template->assign('transfer_accounts', $transferAccounts); |
||
377 | $template->assign('form', $form->returnForm()); |
||
378 | |||
379 | $content = $template->fetch('buycourses/view/process_confirm.tpl'); |
||
380 | |||
381 | $template->assign('content', $content); |
||
382 | $template->display_one_col_template(); |
||
383 | |||
384 | break; |
||
385 | } |
||
386 |