PageRenderTime 33ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/ricaricaweb.php

https://bitbucket.org/xvoip/ricaricaweb-wordpress
PHP | 338 lines | 244 code | 66 blank | 28 comment | 30 complexity | c9dbf5f428f05f2eb0084c13158936bb MD5 | raw file
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) exit;
  3. /*
  4. *
  5. * Telecash - Ricarica Web
  6. * ver. 1.0
  7. *
  8. */
  9. include "messages.php";
  10. include "config.php";
  11. include "database.php";
  12. class telecash_ricaricaweb {
  13. private $_dblink;
  14. private $_conf;
  15. private $messages;
  16. public $currLang = "";
  17. public function __construct() {
  18. $this->loadConf();
  19. }
  20. public function __destruct() {}
  21. public function setup() {}
  22. // create and collect the basic info for a request
  23. public function getRequestBaseData($code="", $ek="") {
  24. $rk = ($ek!="")?$ek:$this->requestKey($code);
  25. $rdata = array();
  26. $page = explode("?", $_SERVER['REQUEST_URI'], 2);
  27. $rdata["tc_url_ok_dinamic"] = "http://".$_SERVER['HTTP_HOST'].$page[0]."?rres=OK";
  28. $rdata["tc_url_ko_dinamic"] = "http://".$_SERVER['HTTP_HOST'].$page[0]."?rres=KO";
  29. $rdata["tc_payment_notification"] = "http://".$_SERVER['HTTP_HOST'].$page[0]."?mng=1";
  30. //$rdata["tc_payment_notification"] = urlencode("http://moneymaster.telecash.it/rpc/operate.php?cmd=updatetrack");
  31. $rdata["tc_custom1"] = $rk;
  32. return $rdata;
  33. }
  34. public function getForm($conf=array()) {
  35. $conf = array_merge($conf, $this->getRequestBaseData($conf["tc_alias"], $_REQUEST["mres"]));
  36. $rows = array();
  37. // $trakingdata = $this->getUrl("http://moneymaster.telecash.it/rpc/operate.php?cmd=starttrack&tcode=".$conf["tc_custom1"]."&tproductid=".$conf["tc_affiliate_merchant"]);
  38. if ($conf["tc_merchant"]=="")
  39. return $rows[] = $this->messages["error_messages"]["ERR_MSG_NO_INSTANCE_FOUND"];
  40. $cssclass = "";
  41. $cssclass .= ($conf["tc_has_paypal"]=="1")?"":"nopaypal";
  42. $action = ($conf["tc_affiliate_merchant"]!="")?"http://panel.moneymaster.it/rpc/send.php":$conf["tc_url"];
  43. $rows[] = "<form action='".$action."' method='POST' id='form_tc_service' class='$cssclass' onsubmit=\"return checkStartPay();\">";
  44. $inopf = array("tc_usetcjs","tc_url","tc_taglio","tc_require_customer_email","tc_has_paypal","tc_template","template","tc_disable_paypal","tc_affiliate_merchant","tc_doublenum","tc_coupon_enable","tc_coupon_enable_default","tc_coupon_default","tc_coupon_default_hidden");
  45. $conf["tc_custom2"] = $conf["tc_affiliate_merchant"];
  46. if ($conf["tc_usetcjs"]=="1") {
  47. $inopf[] = "tc_country";
  48. $usetcjs=true;
  49. }
  50. foreach ($conf as $bfk => $bfv) {
  51. if (!in_array($bfk, $inopf)) {
  52. if ($bfv!="") {
  53. if ($bfk=="tc_set_phone_credit"&&$bfv=="1")
  54. $rows[] = "<input type=hidden id='$bfk' name='".substr($bfk,3)."' value='0' />";
  55. else
  56. $rows[] = "<input type=hidden id='$bfk' name='".substr($bfk,3)."' value='$bfv' />";
  57. }
  58. }
  59. }
  60. if ($conf["tc_coupon_enable"]=="1") {
  61. if ($conf["tc_coupon_enable_default"]=="1")
  62. $rows[] = "<input type=hidden id='tc_coupon' name='coupon' value='".$conf["tc_coupon_default"]."' />";
  63. }
  64. if (!in_array("tc_set_phone_credit", array_keys($conf)))
  65. $rows[] = "<input type=hidden id='tc_set_phone_credit' name='set_phone_credit' value='1' />";
  66. if ($usetcjs) {
  67. $rows[] = "<div class='tc_field'><select id='tc_taglio' name='taglio'></select></div>";
  68. $rows[] = "<div class='tc_field'><select id='tc_country' name='country'></select></div>";
  69. } else {
  70. $stropts = explode(";", $conf["tc_taglio"]);
  71. if (count($stropts)==1) {
  72. $rows[] = "<input type='hidden' name='taglio' id='tc_taglio' value='".$conf["tc_taglio"]."' />";
  73. $wideani = 1;
  74. } else {
  75. $rows[] = "<div class='tc_field'><select id='tc_taglio' name='taglio'>";
  76. foreach ($stropts as $opt) {
  77. $pcs = explode("|", $opt);
  78. $rows[] = "<option value='".$pcs[0]."'>".$pcs[1]."</option>";
  79. }
  80. $rows[] = "</select></div>";
  81. }
  82. }
  83. $rows[] = "<div class='tc_field ".(($wideani)?"wide":"")."'><input type='text' name='ani' id='tc_ani' size=35 maxlength=35 tabindex=1 placeholder='".$this->messages["translations"][$this->currLang]["tc_string_yourphonenumber"]."' /></div>";
  84. if ($conf["tc_doublenum"]=="1")
  85. $rows[] = "<div class='tc_field ".(($wideani)?"wide":"")."'><input type='text' name='anicheck' id='anicheck' size=35 maxlength=35 tabindex=1 placeholder='".$this->messages["translations"][$this->currLang]["tc_string_yourphonenumber2"]."' /></div>";
  86. if ($conf["tc_coupon_enable"]=="1") {
  87. if ($conf["tc_coupon_enable_default"]=="1"&&$conf["tc_coupon_default_hidden"]!="1")
  88. $rows[] = "<div class='tc_field'><span class='coupon'>".$conf["tc_coupon_default"]."</span></div>";
  89. elseif ($conf["tc_coupon_enable_default"]!="1")
  90. $rows[] = "<div class='tc_field'><input type=text id='tc_coupon' name='coupon' value='' placeholder='".$this->messages["translations"][$this->currLang]["tc_string_insertcoupon"]."' /></div>";
  91. }
  92. // payment method
  93. switch ($conf["tc_disable_paypal"]) {
  94. case "0": // paypal and cc
  95. /*
  96. $rows[] = "<div class='tc_field wide'>
  97. <label class='pwselector'><input type='radio' name='disable_paypal' id='tc_disable_paypal' placeholder='' value='0' checked='checked' class='paypal' />&nbsp;<img src='/wp-content/plugins/tc_ricaricaweb/templates/images/paypal.png' border='0' align='absmiddle' /></label>
  98. <label class='pwselector'><input type='radio' name='disable_paypal' id='tc_disable_paypal' placeholder='' value='1' class='creditcard' />&nbsp;<img src='/wp-content/plugins/tc_ricaricaweb/templates/images/credit-card.png' border='0' align='absmiddle' /></label>
  99. </div>";
  100. */
  101. $rows[] = "<div class='tc_field wide'>
  102. <label class='pwselector paypal'><input type='radio' name='disable_paypal' id='tc_disable_paypal' placeholder='' value='0' class='paypal' onclick='switchRadio(\"tc_disable_paypal\", this);' /></label>
  103. <label class='pwselector creditcard checked'><input type='radio' name='disable_paypal' id='tc_disable_paypal' placeholder='' value='1' class='creditcard' checked='checked' onclick='switchRadio(\"tc_disable_paypal\", this);' /></label>
  104. </div>";
  105. $conf["template"] = dirname(__FILE__)."/../templates/tc_ricaricaweb_both.tpl";
  106. break;
  107. case "1": // only paypal
  108. $rows[] = "<input type='hidden' name='disable_paypal' id='tc_disable_paypal' value='0'>";
  109. $conf["template"] = dirname(__FILE__)."/../templates/tc_ricaricaweb_paypal.tpl";
  110. break;
  111. case "2": // only cc
  112. $rows[] = "<input type='hidden' name='disable_paypal' id='tc_disable_paypal' value='1'>";
  113. $conf["template"] = dirname(__FILE__)."/../templates/tc_ricaricaweb_cc.tpl";
  114. break;
  115. }
  116. if ($conf["tc_require_customer_email"]=="1")
  117. $rows[] = "<div class='tc_field'><input type='text' name='customer_email' id='tc_customer_email' size=35 maxlength=100 tabindex=2 placeholder='".$this->messages["translations"][$this->currLang]["tc_string_youremail"]."' /></div>";
  118. $rows[] = "<div class='tc_button wide'><input type='submit' value='".$this->messages["translations"][$this->currLang]["tc_string_pay"]."' name='B1' /></div>";
  119. // $rows[] = "<div class='tc_button'><input type='reset' value='".$this->messages["translations"][$this->currLang]["tc_string_cancel"]."' name='reset' /></div>";
  120. $rows[] = "</form>";
  121. if ($conf["tc_doublenum"]=="1")
  122. $rows[] = "<style>
  123. @media screen
  124. and (min-device-width: 320px)
  125. and (max-device-width: 480px)
  126. and (-webkit-min-device-pixel-ratio: 2)
  127. and (orientation: portrait) {
  128. .tc_div_ricaricaweb.paypal:after, .tc_div_ricaricaweb.cc:after {margin-top:43%;}.tc_div_ricaricaweb:after {margin-top:75%;}
  129. }
  130. </style>";
  131. if ($usetcjs) {
  132. add_action('wp_footer', 'tc_add_javascript');
  133. //$rows[] = "<script src='https://secure.tcserver.it/js/tc.js'></script>";
  134. }
  135. add_action('wp_footer', 'tc_add_javascript_x');
  136. $tpl = file_get_contents($conf["template"]);
  137. $out = str_replace("##fields##", join("", $rows), $tpl);
  138. // return join("", $rows);
  139. return $out;
  140. }
  141. // manage the response from TC server
  142. public function manageResponse($conf=array()) {
  143. $rk = sanitize_text_field($_REQUEST["custom1"]);
  144. $apid = sanitize_text_field($_REQUEST["custom2"]);
  145. $rkc = $this->validRK($rk);
  146. $find = preg_match("/_([A-Za-z\s]+)$/", $rkc[1]->result_descr, $statusmessage);
  147. $trakingdata = json_decode($this->getUrl("http://moneymaster.telecash.it/rpc/operate.php?cmd=gettrack&tcode=$rk&tproductid=$apid"));
  148. if ($rkc[0]) {
  149. $result = sanitize_text_field($_REQUEST["result"]);
  150. $rescode = sanitize_text_field($_REQUEST["result_code"]);
  151. $resmsg = $this->messages["return_codes"][sanitize_text_field($_REQUEST["result_code"])];
  152. if (sanitize_text_field($_REQUEST["rres"])=="OK") {
  153. $result = "<div class='tc_message_confirm'>".$this->messages["translations"][$this->currLang]["tc_string_paymentok"]."</div>";
  154. if ($apid!="")
  155. $result .= '<img src="http://moneymaster.telecash.it/scripts/sale.php?TotalCost='.$trakingdata->tamount.'&OrderID='.$trakingdata->tcode.'&ProductID='.$trakingdata->tproductid.'" width="1" height="1" >';
  156. }
  157. if (sanitize_text_field($_REQUEST["rres"])=="KO") {
  158. $ERRMSG = $this->messages["return_codes"][$_REQUEST["result_code"]];
  159. $result = "<div class='tc_message_error'>".$this->messages["translations"][$this->currLang]["tc_string_paymenterr"]."<br />Errore: ".$ERRMSG."</div>";
  160. }
  161. if (sanitize_text_field($_REQUEST["mng"])=="1") {
  162. $status = (sanitize_text_field($_REQUEST["result"])=="OK")?"validated":"failed";
  163. $q = "update tcrw_tickets set merchant='%s',service_type=%s,status='%s',transaction_id='%s',result='%s',result_code='%s',result_descr='%s',amount='%s',endat='%s',credit_time='%s',service_phone='%s',ps='%s', customer_email='%s' where rk='$rk'";
  164. $q = sprintf($q,
  165. sanitize_text_field($_REQUEST["merchant"]),
  166. sanitize_text_field($_REQUEST["service_type"]),
  167. $status,
  168. sanitize_text_field($_REQUEST["transaction_id"]),
  169. sanitize_text_field($_REQUEST["result"]),
  170. sanitize_text_field($_REQUEST["result_code"]),
  171. sanitize_text_field($_REQUEST["result_descr"]),
  172. sanitize_text_field($_REQUEST["amount"]),
  173. sanitize_text_field($_REQUEST["timestamp"]),
  174. sanitize_text_field($_REQUEST["credit_time"]),
  175. sanitize_text_field($_REQUEST["service_phone"]),
  176. sanitize_text_field($_REQUEST["ps"]),
  177. sanitize_email($_REQUEST["customer_email"])
  178. );
  179. $this->_dblink->query($q);
  180. } else {
  181. $status = (sanitize_text_field($_REQUEST["result"])=="OK")?"validated":"failed";
  182. $q = "update tcrw_tickets set status='%s',transaction_id='%s',result='%s',result_code='%s',result_descr='%s',amount='%s',endat='%s',credit_time='%s',service_phone='%s',ps='%s',customer_email='%s' where rk='$rk'";
  183. $q = sprintf($q,
  184. $status,
  185. sanitize_text_field($_REQUEST["transaction_id"]),
  186. sanitize_text_field($_REQUEST["result"]),
  187. sanitize_text_field($_REQUEST["result_code"]),
  188. sanitize_text_field($_REQUEST["result_descr"]),
  189. sanitize_text_field($_REQUEST["amount"]),
  190. sanitize_text_field($_REQUEST["timestamp"]),
  191. sanitize_text_field($_REQUEST["credit_time"]),
  192. sanitize_text_field($_REQUEST["service_phone"]),
  193. sanitize_text_field($_REQUEST["ps"]),
  194. sanitize_email($_REQUEST["customer_email"])
  195. );
  196. $this->_dblink->query($q);
  197. }
  198. $result .= $this->getForm($conf);
  199. return $result;
  200. } else {
  201. return $this->getForm($conf);
  202. }
  203. }
  204. // create a request key
  205. private function requestKey($code="") {
  206. $rk = md5($this->_conf->merchant.time());
  207. $_SESSION["TcRw_Rk"] = $rk;
  208. $q = "insert into tcrw_tickets (rk,startat,status,service_phone) values ('$rk', now(), 'created', '$code')";
  209. $this->_dblink->query($q);
  210. return $rk;
  211. }
  212. // validate the current request key
  213. private function validRK($tcrk="") {
  214. $r = $this->_dblink->select("select * from tcrw_tickets where rk='$tcrk'");
  215. // return array(($r[0]->status!=""&&$r[0]->status!="failed"&&$r[0]->status!="cancelled"),$r);
  216. return array(($r[0]->id!=""),$r[0]);
  217. }
  218. private function getCurrLang() {
  219. global $wp_version;
  220. $supported = array("it","en","es","fr","de");
  221. // Wordpress
  222. if ($wp_version!="") {
  223. $cl = substr(get_bloginfo( 'language' ), 0, 2);
  224. }
  225. // Joomla
  226. // Prestashop
  227. // Magento
  228. if (in_array($cl,$supported))
  229. return $cl;
  230. else
  231. return "en";
  232. }
  233. // load confiugration from the current environment
  234. private function loadConf() {
  235. $this->_dblink = new tcrw_db();
  236. $confloader = new tcrw_config();
  237. $this->_conf = $confloader->getConf();
  238. $_messages = new telecash_messages();
  239. $_messages->load();
  240. $this->messages = $_messages->items;
  241. $this->currLang = $this->getCurrLang();
  242. foreach ($this->messages["static_strings"] as $k => $v) {
  243. if ($this->messages["translations"][$this->currLang][$k]=="")
  244. $this->messages["translations"][$this->currLang][$k]=$v;
  245. }
  246. }
  247. private function getUrl($url) {
  248. $ch = curl_init();
  249. curl_setopt($ch, CURLOPT_URL, $url);
  250. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  251. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
  252. $res = curl_exec($ch);
  253. curl_close($ch);
  254. return $res;
  255. }
  256. }
  257. function tc_add_javascript() {
  258. wp_register_script('custom_script',
  259. 'https://secure.tcserver.it/js/tc.js',
  260. array('jquery'),
  261. '1.0',
  262. true);
  263. wp_enqueue_script('custom_script');
  264. }
  265. function tc_add_javascript_x() {
  266. wp_register_script('custom_script_x',
  267. plugins_url( 'telecash-ricaricaweb/javascript/frontend.js' ),
  268. array('jquery'),
  269. '1.0',
  270. true);
  271. wp_enqueue_script('custom_script_x');
  272. }