1 | <?php |
||
2 | /* For licensing terms, see /license.txt */ |
||
3 | |||
4 | /** |
||
5 | * Responses to AJAX calls. |
||
6 | * |
||
7 | * @package chamilo.plugin.buycourses |
||
8 | */ |
||
9 | $cidReset = true; |
||
10 | |||
11 | require_once __DIR__.'/../../../main/inc/global.inc.php'; |
||
12 | |||
13 | api_protect_admin_script(true); |
||
14 | |||
15 | $plugin = BuyCoursesPlugin::create(); |
||
16 | |||
17 | $paypalEnable = $plugin->get('paypal_enable'); |
||
18 | $commissionsEnable = $plugin->get('commissions_enable'); |
||
19 | |||
20 | $action = isset($_GET['a']) ? $_GET['a'] : null; |
||
21 | |||
22 | switch ($action) { |
||
23 | case 'saleInfo': |
||
24 | //$saleId is only used in getSale() and is always filtered there |
||
25 | $saleId = isset($_POST['id']) ? $_POST['id'] : ''; |
||
26 | $sale = $plugin->getSale($saleId); |
||
27 | $productType = ($sale['product_type'] == 1) ? get_lang('Course') : get_lang('Session'); |
||
28 | $paymentType = ($sale['payment_type'] == 1) ? 'Paypal' : $plugin->get_lang('BankTransfer'); |
||
29 | $productInfo = ($sale['product_type'] == 1) |
||
30 | ? api_get_course_info_by_id($sale['product_id']) |
||
31 | : api_get_session_info($sale['product_id']); |
||
32 | $currency = $plugin->getSelectedCurrency(); |
||
33 | if ($sale['product_type'] == 1) { |
||
34 | $productImage = $productInfo['course_image_large']; |
||
35 | } else { |
||
36 | $productImage = ($productInfo['image']) |
||
37 | ? $productInfo['image'] |
||
38 | : Template::get_icon_path('session_default.png'); |
||
39 | } |
||
40 | |||
41 | $userInfo = api_get_user_info($sale['user_id']); |
||
42 | |||
43 | $html = '<h2>'.$sale['product_name'].'</h2>'; |
||
44 | $html .= '<div class="row">'; |
||
45 | $html .= '<div class="col-sm-6 col-md-6">'; |
||
46 | $html .= '<ul>'; |
||
47 | $html .= '<li><b>'.$plugin->get_lang('OrderPrice').':</b> '.$sale['price'].'</li>'; |
||
48 | $html .= '<li><b>'.$plugin->get_lang('CurrencyType').':</b> '.$currency['iso_code'].'</li>'; |
||
49 | $html .= '<li><b>'.$plugin->get_lang('ProductType').':</b> '.$productType.'</li>'; |
||
50 | $html .= '<li><b>'.$plugin->get_lang('OrderDate').':</b> ' |
||
51 | .api_format_date($sale['date'], DATE_TIME_FORMAT_LONG_24H).'</li>'; |
||
52 | $html .= '<li><b>'.$plugin->get_lang('Buyer').':</b> '.$userInfo['complete_name'].'</li>'; |
||
53 | $html .= '<li><b>'.$plugin->get_lang('PaymentMethods').':</b> '.$paymentType.'</li>'; |
||
54 | $html .= '</ul>'; |
||
55 | $html .= '</div>'; |
||
56 | $html .= '<div class="col-sm-6 col-md-6">'; |
||
57 | $html .= '<img class="thumbnail" src="'.$productImage.'" >'; |
||
58 | $html .= '</div>'; |
||
59 | $html .= '</div>'; |
||
60 | echo $html; |
||
61 | break; |
||
62 | case 'stats': |
||
63 | $stats = []; |
||
64 | $stats['completed_count'] = 0; |
||
65 | $stats['completed_total_amount'] = 0; |
||
66 | $stats['pending_count'] = 0; |
||
67 | $stats['pending_total_amount'] = 0; |
||
68 | $stats['canceled_count'] = 0; |
||
69 | $stats['canceled_total_amount'] = 0; |
||
70 | |||
71 | $completedPayouts = $plugin->getPayouts(BuyCoursesPlugin::PAYOUT_STATUS_COMPLETED); |
||
72 | $pendingPayouts = $plugin->getPayouts(BuyCoursesPlugin::PAYOUT_STATUS_PENDING); |
||
73 | $canceledPayouts = $plugin->getPayouts(BuyCoursesPlugin::PAYOUT_STATUS_CANCELED); |
||
74 | $currency = $plugin->getSelectedCurrency(); |
||
75 | |||
76 | foreach ($completedPayouts as $completed) { |
||
77 | $stats['completed_count'] = count($completedPayouts); |
||
78 | $stats['completed_total_amount'] += $completed['commission']; |
||
79 | $stats['completed_total_amount'] = number_format($stats['completed_total_amount'], 2); |
||
80 | } |
||
81 | |||
82 | foreach ($pendingPayouts as $pending) { |
||
83 | $stats['pending_count'] = count($pendingPayouts); |
||
84 | $stats['pending_total_amount'] += $pending['commission']; |
||
85 | $stats['pending_total_amount'] = number_format($stats['pending_total_amount'], 2); |
||
86 | } |
||
87 | |||
88 | foreach ($canceledPayouts as $canceled) { |
||
89 | $stats['canceled_count'] = count($canceledPayouts); |
||
90 | $stats['canceled_total_amount'] += $canceled['commission']; |
||
91 | $stats['canceled_total_amount'] = number_format($stats['canceled_total_amount'], 2); |
||
92 | } |
||
93 | |||
94 | $html = ' |
||
95 | <div class="row"> |
||
96 | <p> |
||
97 | <ul> |
||
98 | <li> |
||
99 | '.get_plugin_lang("PayoutsTotalCompleted", "BuyCoursesPlugin").' |
||
100 | <b>'.$stats['completed_count'].'</b> - '.get_plugin_lang("TotalAmount", "BuyCoursesPlugin").' |
||
101 | <b>'.$stats['completed_total_amount'].' '.$currency['iso_code'].'</b> |
||
102 | </li> |
||
103 | <li>'.get_plugin_lang("PayoutsTotalPending", "BuyCoursesPlugin").' |
||
104 | <b>'.$stats['pending_count'].'</b> - '.get_plugin_lang("TotalAmount", "BuyCoursesPlugin").' |
||
105 | <b>'.$stats['pending_total_amount'].' '.$currency['iso_code'].'</b> |
||
106 | </li> |
||
107 | <li>'.get_plugin_lang("PayoutsTotalCanceled", "BuyCoursesPlugin").' |
||
108 | <b>'.$stats['canceled_count'].'</b> - '.get_plugin_lang("TotalAmount", "BuyCoursesPlugin").' |
||
109 | <b>'.$stats['canceled_total_amount'].' '.$currency['iso_code'].'</b> |
||
110 | </li> |
||
111 | </ul> |
||
112 | </p> |
||
113 | </div> |
||
114 | '; |
||
115 | echo $html; |
||
116 | break; |
||
117 | case 'processPayout': |
||
118 | if (api_is_anonymous()) { |
||
119 | break; |
||
120 | } |
||
121 | |||
122 | $html = ''; |
||
123 | $allPays = []; |
||
124 | $totalAccounts = 0; |
||
125 | $totalPayout = 0; |
||
126 | $payouts = isset($_POST['payouts']) ? $_POST['payouts'] : ''; |
||
127 | |||
128 | if (!$payouts) { |
||
129 | echo Display::return_message(get_plugin_lang("SelectOptionToProceed", "BuyCoursesPlugin"), 'error', false); |
||
130 | |||
131 | break; |
||
132 | } |
||
133 | |||
134 | foreach ($payouts as $index => $id) { |
||
135 | $allPays[] = $plugin->getPayouts(BuyCoursesPlugin::PAYOUT_STATUS_PENDING, $id); |
||
136 | } |
||
137 | |||
138 | foreach ($allPays as $payout) { |
||
139 | $totalPayout += number_format($payout['commission'], 2); |
||
140 | $totalAccounts++; |
||
141 | } |
||
142 | |||
143 | $currentCurrency = $plugin->getSelectedCurrency(); |
||
144 | |||
145 | $isoCode = $currentCurrency['iso_code']; |
||
146 | |||
147 | $html .= '<p>'.get_plugin_lang("VerifyTotalAmountToProceedPayout", "BuyCoursesPlugin").'</p>'; |
||
148 | $html .= ' |
||
149 | <p> |
||
150 | <ul> |
||
151 | <li>'.get_plugin_lang("TotalAcounts", "BuyCoursesPlugin").' <b>'.$totalAccounts.'</b></li> |
||
152 | <li>'.get_plugin_lang("TotalPayout", "BuyCoursesPlugin").' <b>'.$isoCode.' '.$totalPayout.'</b></li> |
||
153 | </ul> |
||
154 | </p> |
||
155 | <p>'.get_plugin_lang("CautionThisProcessCantBeCanceled", "BuyCoursesPlugin").'</p> |
||
156 | <br /><br /> |
||
157 | <div id="spinner" class="text-center"></div> |
||
158 | '; |
||
159 | |||
160 | echo $html; |
||
161 | break; |
||
162 | |||
163 | case 'proceedPayout': |
||
164 | if (api_is_anonymous()) { |
||
165 | break; |
||
166 | } |
||
167 | |||
168 | $paypalParams = $plugin->getPaypalParams(); |
||
169 | $pruebas = $paypalParams['sandbox'] == 1; |
||
170 | $paypalUsername = $paypalParams['username']; |
||
171 | $paypalPassword = $paypalParams['password']; |
||
172 | $paypalSignature = $paypalParams['signature']; |
||
173 | require_once "paypalfunctions.php"; |
||
174 | $allPayouts = []; |
||
175 | $totalAccounts = 0; |
||
176 | $totalPayout = 0; |
||
177 | |||
178 | $payouts = isset($_POST['payouts']) ? $_POST['payouts'] : ''; |
||
179 | |||
180 | if (!$payouts) { |
||
181 | echo Display::return_message(get_plugin_lang("SelectOptionToProceed", "BuyCoursesPlugin"), 'error', false); |
||
182 | |||
183 | break; |
||
184 | } |
||
185 | |||
186 | foreach ($payouts as $index => $id) { |
||
187 | $allPayouts[] = $plugin->getPayouts(BuyCoursesPlugin::PAYOUT_STATUS_PENDING, $id); |
||
188 | } |
||
189 | |||
190 | $currentCurrency = $plugin->getSelectedCurrency(); |
||
191 | |||
192 | $isoCode = $currentCurrency['iso_code']; |
||
193 | |||
194 | $result = MassPayment($allPayouts, $isoCode); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
195 | |||
196 | if ($result['ACK'] === 'Success') { |
||
197 | foreach ($allPayouts as $payout) { |
||
198 | $plugin->setStatusPayouts($payout['id'], BuyCoursesPlugin::PAYOUT_STATUS_COMPLETED); |
||
199 | } |
||
200 | echo Display::return_message(get_plugin_lang("PayoutSuccess", "BuyCoursesPlugin"), 'success', false); |
||
201 | } else { |
||
202 | echo Display::return_message( |
||
203 | '<b>'.$result['L_SEVERITYCODE0'].' '.$result['L_ERRORCODE0'].'</b> - ' |
||
204 | .$result['L_SHORTMESSAGE0'].'<br /><ul><li>'.$result['L_LONGMESSAGE0'].'</li></ul>', |
||
205 | 'error', |
||
206 | false |
||
207 | ); |
||
208 | } |
||
209 | break; |
||
210 | |||
211 | case 'cancelPayout': |
||
212 | if (api_is_anonymous()) { |
||
213 | break; |
||
214 | } |
||
215 | |||
216 | $payoutId = isset($_POST['id']) ? $_POST['id'] : ''; |
||
217 | $plugin->setStatusPayouts($payoutId, BuyCoursesPlugin::PAYOUT_STATUS_CANCELED); |
||
218 | echo ''; |
||
219 | break; |
||
220 | } |
||
221 | exit; |
||
222 |