00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00032 include_once './payment/classes/class.ilPurchasePaypal.php';
00033 include_once './payment/classes/class.ilPaymentShoppingCart.php';
00034 include_once './payment/classes/class.ilPaymentBaseGUI.php';
00035 include_once './payment/classes/class.ilPaypalSettings.php';
00036
00037 class ilPaymentShoppingCartGUI extends ilPaymentBaseGUI
00038 {
00039 var $ctrl;
00040
00041 var $lng;
00042 var $user_obj;
00043
00044
00045
00046
00047 var $psc_obj = null;
00048
00049
00050
00051
00052 var $paypal_obj = null;
00053
00054 var $paypalConfig;
00055
00056 function ilPaymentShoppingCartGUI(&$user_obj)
00057 {
00058 global $ilCtrl;
00059
00060 $this->ctrl =& $ilCtrl;
00061
00062 $this->ilPaymentBaseGUI();
00063
00064 $this->user_obj =& $user_obj;
00065
00066 $ppSet = new ilPaypalSettings();
00067 $this->paypalConfig = $ppSet->getAll();
00068 }
00072 function &executeCommand()
00073 {
00074 global $tree;
00075
00076 $cmd = $this->ctrl->getCmd();
00077 switch ($this->ctrl->getNextClass($this))
00078 {
00079
00080 default:
00081 if(!$cmd = $this->ctrl->getCmd())
00082 {
00083 $cmd = 'showItems';
00084 }
00085 $this->$cmd();
00086 break;
00087 }
00088 }
00089
00090 function finishPaypal()
00091 {
00092 $this->__initPaypalObject();
00093
00094 if (!($fp = $this->paypal_obj->openSocket()))
00095 {
00096 sendInfo($this->lng->txt('pay_paypal_failed')."<br />".$this->lng->txt('pay_paypal_unreachable')."<br />".$this->lng->txt('pay_paypal_error_info'));
00097 $this->showItems();
00098 }
00099 else
00100 {
00101 $res = $this->paypal_obj->checkData($fp);
00102 if ($res == SUCCESS)
00103 {
00104 sendInfo($this->lng->txt('pay_paypal_success'), true);
00105 $this->ctrl->redirectByClass('ilpaymentbuyedobjectsgui');
00106 }
00107 else
00108 {
00109 switch ($res)
00110 {
00111 case ERROR_WRONG_CUSTOMER : sendInfo($this->lng->txt('pay_paypal_failed')."<br />".$this->lng->txt('pay_paypal_error_wrong_customer')."<br />".$this->lng->txt('pay_paypal_error_info'));
00112 break;
00113 case ERROR_NOT_COMPLETED : sendInfo($this->lng->txt('pay_paypal_failed')."<br />".$this->lng->txt('pay_paypal_error_not_completed')."<br />".$this->lng->txt('pay_paypal_error_info'));
00114 break;
00115 case ERROR_PREV_TRANS_ID : sendInfo($this->lng->txt('pay_paypal_failed')."<br />".$this->lng->txt('pay_paypal_error_prev_trans_id')."<br />".$this->lng->txt('pay_paypal_error_info'));
00116 break;
00117 case ERROR_WRONG_VENDOR : sendInfo($this->lng->txt('pay_paypal_failed')."<br />".$this->lng->txt('pay_paypal_error_wrong_vendor')."<br />".$this->lng->txt('pay_paypal_error_info'));
00118 break;
00119 case ERROR_WRONG_ITEMS : sendInfo($this->lng->txt('pay_paypal_failed')."<br />".$this->lng->txt('pay_paypal_error_wrong_items')."<br />".$this->lng->txt('pay_paypal_error_info'));
00120 break;
00121 case ERROR_FAIL : sendInfo($this->lng->txt('pay_paypal_failed')."<br />".$this->lng->txt('pay_paypal_error_fails')."<br />".$this->lng->txt('pay_paypal_error_info'));
00122 break;
00123 }
00124 $this->showItems();
00125 }
00126 fclose($fp);
00127 }
00128 }
00129
00130 function cancelPaypal()
00131 {
00132 sendInfo($this->lng->txt('pay_paypal_canceled'));
00133 $this->showItems();
00134 }
00135
00136 function showItems()
00137 {
00138 global $ilObjDataCache, $ilUser;
00139
00140 include_once './payment/classes/class.ilPaymentPrices.php';
00141
00142 $this->tpl->addBlockfile('ADM_CONTENT','adm_content','tpl.pay_shopping_cart.html','payment');
00143
00144 $this->__initShoppingCartObject();
00145
00146 include_once './payment/classes/class.ilGeneralSettings.php';
00147
00148 $genSet = new ilGeneralSettings();
00149
00150 include_once './payment/classes/class.ilPayMethods.php';
00151
00152 if (ilPayMethods::_enabled('pm_bmf')) $pay_methods[] = PAY_METHOD_BMF;
00153 if (ilPayMethods::_enabled('pm_paypal')) $pay_methods[] = PAY_METHOD_PAYPAL;
00154
00155 $num_items = 0;
00156 if (is_array($pay_methods))
00157 {
00158 for ($p = 0; $p < count($pay_methods); $p++)
00159 {
00160
00161 if ($pay_methods[$p] == PAY_METHOD_BMF)
00162 $tpl =& new ilTemplate("./payment/templates/default/tpl.pay_shopping_cart_bmf.html",true,true);
00163 else if ($pay_methods[$p] == PAY_METHOD_PAYPAL)
00164 $tpl =& new ilTemplate("./payment/templates/default/tpl.pay_shopping_cart_paypal.html",true,true);
00165
00166 if(count($items = $this->psc_obj->getEntries($pay_methods[$p])))
00167 {
00168 $counter = 0;
00169 foreach($items as $item)
00170 {
00171 $tmp_pobject =& new ilPaymentObject($this->user_obj,$item['pobject_id']);
00172
00173 $obj_id = $ilObjDataCache->lookupObjId($tmp_pobject->getRefId());
00174 $obj_type = $ilObjDataCache->lookupType($obj_id);
00175 $obj_title = $ilObjDataCache->lookupTitle($obj_id);
00176
00177 $f_result[$counter][] = ilUtil::formCheckBox(0,'item[]',$item['psc_id']);
00178 $f_result[$counter][] = "<a href=\"goto.php?target=".$obj_type."_".$tmp_pobject->getRefId() . "\">".$obj_title."</a>";
00179
00180 $price_arr = ilPaymentPrices::_getPrice($item['price_id']);
00181 $f_result[$counter][] = $price_arr['duration'].' '.$this->lng->txt('paya_months');
00182
00183 $f_result[$counter][] = ilPaymentPrices::_getPriceString($item['price_id']);
00184
00185 if ($pay_methods[$p] == PAY_METHOD_PAYPAL)
00186 {
00187 $tpl->setCurrentBlock("loop_items");
00188 $tpl->setVariable("LOOP_ITEMS_NO", ($counter+1));
00189 $tpl->setVariable("LOOP_ITEMS_NAME", "[".$obj_id."]: ".$obj_title);
00190 $tpl->setVariable("LOOP_ITEMS_AMOUNT", $price_arr['unit_value'].".".$price_arr['sub_unit_value']);
00191 $tpl->parseCurrentBlock("loop_items");
00192
00193 # $buttonParams["item_name_".($counter+1)] = $obj_title;
00194 # $buttonParams["amount_".($counter+1)] = $price_arr['unit_value'].".".$price_arr['sub_unit_value'];
00195 }
00196
00197 unset($tmp_obj);
00198 unset($tmp_pobject);
00199
00200 ++$counter;
00201 }
00202
00203 $tpl->setCurrentBlock("buy_link");
00204 switch($pay_methods[$p])
00205 {
00206 case PAY_METHOD_BMF:
00207 $tpl->setVariable("SCRIPT_LINK", './payment/start_bmf.php');
00208 break;
00209
00210 case PAY_METHOD_PAYPAL:
00211 $tpl->setVariable("SCRIPT_LINK", "https://".$this->paypalConfig["server_host"].$this->paypalConfig["server_path"]);
00212 $tpl->setVariable("POPUP_BLOCKER", $this->lng->txt('popup_blocker'));
00213 $tpl->setVariable("VENDOR", $this->paypalConfig["vendor"]);
00214 $tpl->setVariable("RETURN", ILIAS_HTTP_PATH . "/" . $this->ctrl->getLinkTarget($this, "finishPaypal"));
00215 $tpl->setVariable("CANCEL_RETURN", ILIAS_HTTP_PATH . "/" . $this->ctrl->getLinkTarget($this, "cancelPaypal"));
00216 $tpl->setVariable("CUSTOM", $ilUser->getId());
00217 $tpl->setVariable("CURRENCY", $genSet->get("currency_unit"));
00218 $tpl->setVariable("PAGE_STYLE", $this->paypalConfig["page_style"]);
00219
00220 # $buttonParams["upload"] = 1;
00221 # $buttonParams["charset"] = "utf-8";
00222 # $buttonParams["business"] = $this->paypalConfig["vendor"];
00223 # $buttonParams["currency_code"] = "EUR";
00224 # $buttonParams["return"] = "http://www.databay.de/user/jens/paypal.php";
00225 # $buttonParams["rm"] = 2;
00226 # $buttonParams["cancel_return"] = "http://www.databay.de/user/jens/paypal.php";
00227 # $buttonParams["custom"] = "HALLO";
00228 # $buttonParams["invoice"] = "0987654321";
00229 # if ($enc_data = $this->__encryptButton($buttonParams))
00230 # {
00231 # $tpl->setVariable("ENCDATA", $enc_data);
00232 # }
00233
00234 break;
00235 }
00236 $tpl->setVariable("PAYPAL_HINT", $this->lng->txt('pay_hint_paypal'));
00237 $tpl->setVariable("PAYPAL_INFO", $this->lng->txt('pay_info_paypal'));
00238 $tpl->setVariable("TXT_BUY", $this->lng->txt('pay_click_to_buy'));
00239 $tpl->parseCurrentBlock("buy_link");
00240
00241 $tpl->setCurrentBlock("loop");
00242
00243 $this->__showItemsTable($tpl, $f_result, $pay_methods[$p]);
00244 unset($f_result);
00245
00246 $tpl->parseCurrentBlock("loop");
00247
00248 if ($pay_methods[$p] == PAY_METHOD_BMF)
00249 $this->tpl->setVariable("BMF", $tpl->get());
00250 else if ($pay_methods[$p] == PAY_METHOD_PAYPAL)
00251 $this->tpl->setVariable("PAYPAL", $tpl->get());
00252
00253 $num_items += $counter;
00254 }
00255
00256 }
00257 }
00258
00259 if ($num_items == 0)
00260 {
00261 sendInfo($this->lng->txt('pay_shopping_cart_empty'));
00262
00263 return false;
00264 }
00265 else
00266 {
00267 return true;
00268 }
00269
00270 }
00271
00272 function __showItemsTable(&$a_tpl, $a_result_set, $a_pay_method = 0)
00273 {
00274 include_once './payment/classes/class.ilGeneralSettings.php';
00275
00276 $genSet = new ilGeneralSettings();
00277
00278 $tbl =& $this->initTableGUI();
00279 $tpl =& $tbl->getTemplateObject();
00280
00281
00282 $tpl->setCurrentBlock("tbl_form_header");
00283
00284 $tpl->setVariable("FORMACTION",$this->ctrl->getFormAction($this));
00285 $tpl->parseCurrentBlock();
00286
00287 $tpl->setCurrentBlock("tbl_action_row");
00288 $tpl->setCurrentBlock("plain_buttons");
00289 $tpl->parseCurrentBlock();
00290
00291 $tpl->setVariable("COLUMN_COUNTS",4);
00292 $tpl->setVariable("IMG_ARROW", ilUtil::getImagePath("arrow_downright.gif"));
00293
00294 $tpl->setCurrentBlock("tbl_action_button");
00295 $tpl->setVariable("BTN_NAME","deleteItem");
00296 $tpl->setVariable("BTN_VALUE",$this->lng->txt("delete"));
00297 $tpl->parseCurrentBlock();
00298 $tpl->setCurrentBlock("tbl_action_row");
00299 $tpl->setVariable("TPLPATH",$this->tpl->tplPath);
00300 $tpl->parseCurrentBlock();
00301
00302 $title = $this->lng->txt("paya_shopping_cart");
00303 switch($a_pay_method)
00304 {
00305 case PAY_METHOD_BMF:
00306 $title .= " (" . $this->lng->txt("payment_system") . ": " . $this->lng->txt("pays_bmf") . ")";
00307 break;
00308
00309 case PAY_METHOD_PAYPAL:
00310 $title .= " (" . $this->lng->txt("payment_system") . ": " . $this->lng->txt("pays_paypal") . ")";
00311 break;
00312 }
00313 $tbl->setTitle($title,"icon_pays_cart.gif",$this->lng->txt("paya_shopping_cart"));
00314 $tbl->setHeaderNames(array($this->lng->txt(""),
00315 $this->lng->txt("title"),
00316 $this->lng->txt("duration"),
00317 $this->lng->txt("price_a")));
00318
00319 $tbl->setHeaderVars(array("",
00320 "table".$a_pay_method."_title",
00321 "table".$a_pay_method."_duration",
00322 "table".$a_pay_method."_price"),
00323 array("cmd" => "",
00324 "cmdClass" => "ilpaymentshoppingcartgui",
00325 "baseClass" => "ilPersonalDesktopGUI",
00326 "cmdNode" => $_GET["cmdNode"]));
00327
00328 $offset = $_GET["table".$a_pay_method."_offset"];
00329 $order = $_GET["table".$a_pay_method."_sort_by"];
00330 $direction = $_GET["table".$a_pay_method."_sort_order"] ? $_GET['table'.$a_pay_method.'_sort_order'] : 'desc';
00331
00332 $tbl->setPrefix("table".$a_pay_method."_");
00333 $tbl->setOrderColumn($order,'table'.$a_pay_method.'_title');
00334 $tbl->setOrderDirection($direction);
00335 $tbl->setOffset($offset);
00336 $tbl->setLimit($_GET["limit"]);
00337 $tbl->setMaxCount(count($a_result_set));
00338 $tbl->setFooter("tblfooter",$this->lng->txt("previous"),$this->lng->txt("next"));
00339 $tbl->setData($a_result_set);
00340
00341
00342 $sc_obj =& new ilPaymentShoppingCart($this->user_obj);
00343
00344 $totalAmount = $sc_obj->getTotalAmount();
00345 $vat = $sc_obj->getVat($totalAmount[$a_pay_method]);
00346
00347 $tpl->setCurrentBlock("tbl_footer_linkbar");
00348 $amount = "<b>" . $this->lng->txt("pay_bmf_total_amount") . ": " . number_format($totalAmount[$a_pay_method], 2, ',', '.') . " " . $genSet->get("currency_unit") . "</b>";
00349 if ($vat > 0)
00350 {
00351 $amount .= "<br>\n" . $genSet->get("vat_rate") . "% " . $this->lng->txt("pay_bmf_vat_included") . ": " . number_format($vat, 2, ',', '.') . " " . $genSet->get("currency_unit");
00352 }
00353
00354 $tpl->setVariable("LINKBAR", $amount);
00355 $tpl->parseCurrentBlock("tbl_footer_linkbar");
00356 $tpl->setCurrentBlock('tbl_footer');
00357 $tpl->setVariable('COLUMN_COUNT',4);
00358 $tpl->parseCurrentBlock();
00359
00360 $tbl->render();
00361
00362 $a_tpl->setVariable("ITEMS_TABLE",$tbl->tpl->get());
00363
00364 return true;
00365 }
00366
00367 function deleteItem()
00368 {
00369 if(!count($_POST['item']))
00370 {
00371 sendInfo($this->lng->txt('pay_select_one_item'));
00372
00373 $this->showItems();
00374 return true;
00375 }
00376 $this->__initShoppingCartObject();
00377
00378 foreach($_POST['item'] as $id)
00379 {
00380 $this->psc_obj->delete($id);
00381 }
00382 sendInfo($this->lng->txt('pay_deleted_items'));
00383 $this->showItems();
00384
00385 return true;
00386 }
00387
00388
00389 function __initShoppingCartObject()
00390 {
00391 $this->psc_obj =& new ilPaymentShoppingCart($this->user_obj);
00392 }
00393
00394 function __initPaypalObject()
00395 {
00396 $this->paypal_obj =& new ilPurchasePaypal($this->user_obj);
00397 }
00398
00405 function __encryptButton($buttonParams)
00406 {
00407 $merchant_cert = $this->paypalConfig["vendor_cert"];
00408 $merchant_key = $this->paypalConfig["vendor_key"];
00409 $end_cert = $this->paypalConfig["enc_cert"];
00410
00411 $tmpin_file = tempnam('/tmp', 'paypal_');
00412 $tmpout_file = tempnam('/tmp', 'paypal_');
00413 $tmpfinal_file = tempnam('/tmp', 'paypal_');
00414
00415 $rawdata = array();
00416 $buttonParams['cert_id'] = $this->paypalConfig["cert_id"];
00417 foreach ($buttonParams as $name => $value) {
00418 $rawdata[] = "$name=$value";
00419 }
00420 $rawdata = implode("\n", $rawdata);
00421
00422 $fp = fopen($tmpin_file, 'w');
00423 if (!$fp) {
00424 echo "Could not open temporary file '$tmpin_file')";
00425 return false;
00426 # return PayPal::raiseError("Could not open temporary file '$tmpin_file')");
00427 }
00428 fwrite($fp, $rawdata);
00429 fclose($fp);
00430
00431 if (!@openssl_pkcs7_sign($tmpin_file, $tmpout_file, $merchant_cert,
00432 array($merchant_key, $this->paypalConfig["private_key_password"]),
00433 array(), PKCS7_BINARY)) {
00434 echo "Could not sign encrypted data: " . openssl_error_string();
00435 return false;
00436 # return PayPal::raiseError("Could not sign encrypted data: " . openssl_error_string());
00437 }
00438
00439 $data = file_get_contents($tmpout_file);
00440 $data = explode("\n\n", $data);
00441 $data = $data[1];
00442 $data = base64_decode($data);
00443 $fp = fopen($tmpout_file, 'w');
00444 if (!$fp) {
00445 echo "Could not open temporary file '$tmpin_file')";
00446 return false;
00447 # return PayPal::raiseError("Could not open temporary file '$tmpin_file')");
00448 }
00449 fwrite($fp, $data);
00450 fclose($fp);
00451
00452 if (!@openssl_pkcs7_encrypt($tmpout_file, $tmpfinal_file, $enc_cert, array(), PKCS7_BINARY)) {
00453 echo "Could not encrypt data:" . openssl_error_string();
00454 return false;
00455 # return PayPal::raiseError("Could not encrypt data:" . openssl_error_string());
00456 }
00457
00458 $encdata = @file_get_contents($tmpfinal_file, false);
00459 if (!$encdata) {
00460 echo "Encryption and signature of data failed.";
00461 return false;
00462 # return PayPal::raiseError("Encryption and signature of data failed.");
00463 }
00464
00465 $encdata = explode("\n\n", $encdata);
00466 $encdata = trim(str_replace("\n", '', $encdata[1]));
00467 $encdata = "-----BEGIN PKCS7-----$encdata-----END PKCS7-----";
00468
00469 @unlink($tmpfinal_file);
00470 @unlink($tmpin_file);
00471 @unlink($tmpout_file);
00472
00473 return $encData;
00474 }
00475
00476 }
00477 ?>