Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.66 KB | None | 0 0
  1. <?php
  2.  
  3. use WHMCS\Database\Capsule;
  4.  
  5. define("CLIENTAREA", true);
  6. //define("FORCESSL", true); // Uncomment to force the page to use https://
  7.  
  8. require("init.php");
  9. require_once __DIR__ . '/includes/gatewayfunctions.php';
  10.  
  11. $gatewayParams = getGatewayVariables('nicepaygatewayva');
  12.  
  13. $va = new WHMCS_ClientArea();
  14.  
  15. $va->setPageTitle("Detail Virtual Account untuk Pembayaran Pesanan Anda");
  16.  
  17. $va->addToBreadCrumb('index.php', Lang::trans('globalsystemname'));
  18. $va->addToBreadCrumb('processva.php', 'Virtual Account');
  19.  
  20. class NicepayRequestor {
  21. public $sock = 0;
  22. public $apiUrl;
  23. public $port = 443;
  24. public $status;
  25. public $headers = "";
  26. public $body = "";
  27. public $request;
  28. public $errorcode;
  29. public $errormsg;
  30. public $log;
  31. public $timeout;
  32.  
  33. public function openSocket($apiUrl,$nicepay_timeout_connect) {
  34. $host = parse_url($apiUrl, PHP_URL_HOST);
  35. $tryCount = 0;
  36. // $a = @fsockopen ("ssl://".$host, $this->port, $errno, $errstr, $nicepay_timeout_connect );
  37. if (! $this->sock = @fsockopen ("ssl://".$host, $this->port, $errno, $errstr, $nicepay_timeout_connect )) {
  38. while ($tryCount < 5) {
  39. if ($this->sock = @fsockopen("ssl://".$host, $this->port, $errno, $errstr, $nicepay_timeout_connect )) {
  40. print_r($this->sock); exit;
  41. return true;
  42. }
  43. sleep(2);
  44. $tryCount++;
  45. }
  46. $this->errorcode = $errno;
  47. switch ($errno) {
  48. case - 3 :
  49. $this->errormsg = 'Socket creation failed (-3)';
  50. case - 4 :
  51. $this->errormsg = 'DNS lookup failure (-4)';
  52. case - 5 :
  53. $this->errormsg = 'Connection refused or timed out (-5)';
  54. default :
  55. $this->errormsg = 'Connection failed (' . $errno . ')';
  56. $this->errormsg .= ' ' . $errstr;
  57. }
  58. return false;
  59. }
  60. print_r($this->sock); exit;
  61. return true;
  62. }
  63.  
  64. public function apiRequest($data,$apiUrl,$nicepay_timeout_read) {
  65. $host = parse_url($apiUrl, PHP_URL_HOST);
  66. $uri = parse_url($apiUrl, PHP_URL_PATH);
  67. $this->headers = "";
  68. $this->body = "";
  69. $postdata = $this->buildQueryString ($data);
  70. // print_r($postdata); exit;
  71.  
  72. /* Write */
  73. $request = "POST " . $uri . " HTTP/1.0\r\n";
  74. $request .= "Connection: close\r\n";
  75. $request .= "Host: " . $host . "\r\n";
  76. $request .= "Content-type: application/x-www-form-urlencoded\r\n";
  77. $request .= "Content-length: " . strlen ( $postdata ) . "\r\n";
  78. $request .= "Accept: */*\r\n";
  79. $request .= "\r\n";
  80. $request .= $postdata . "\r\n";
  81. $request .= "\r\n";
  82. // print_r($this->sock); exit;
  83. // print_r($request); exit;
  84. if($this->sock) {
  85. fwrite ( $this->sock, $request );
  86.  
  87. /* Read */
  88. stream_set_blocking ($this->sock, FALSE);
  89.  
  90. $atStart = true;
  91. $IsHeader = true;
  92. $timeout = false;
  93. $start_time = time ();
  94. while ( ! feof ($this->sock ) && ! $timeout) {
  95. $line = fgets ($this->sock, 4096);
  96. $diff = time () - $start_time;
  97. if ($diff >= $nicepay_timeout_read) {
  98. $timeout = true;
  99. }
  100. if ($IsHeader) {
  101. if ($line == "") // for stream_set_blocking
  102. {
  103. continue;
  104. }
  105. if (substr ($line, 0, 2) == "\r\n") // end of header
  106. {
  107. $IsHeader = false;
  108. continue;
  109. }
  110. $this->headers .= $line;
  111. if ($atStart) {
  112. $atStart = false;
  113. if (! preg_match ( '/HTTP\/(\\d\\.\\d)\\s*(\\d+)\\s*(.*)/', $line, $m )) {
  114. $this->errormsg = "Status code line invalid: " . htmlentities ( $line );
  115. fclose ( $this->sock );
  116. return false;
  117. }
  118. $http_version = $m [1];
  119. $this->status = $m [2];
  120. $status_string = $m [3];
  121. continue;
  122. }
  123. } else {
  124. $this->body .= $line;
  125. }
  126. }
  127. fclose ( $this->sock );
  128.  
  129. if ($timeout) {
  130. $this->errorcode = "10200";
  131. $this->errormsg = "Socket Timeout(" . $diff . "SEC)";
  132. return false;
  133. }
  134.  
  135. if(!$this->parseResult($this->body)) {
  136. $this->body = substr($this->body, 4);
  137. return $this->parseResult($this->body);
  138. }
  139. return $this->parseResult($this->body);
  140. } else {
  141. return false;
  142. }
  143. }
  144.  
  145. public function buildQueryString($data) {
  146. $querystring = '';
  147. if (is_array ($data)) {
  148. foreach ($data as $key => $val) {
  149. if (is_array ($val)) {
  150. foreach ($val as $val2) {
  151. if ($key != "key")
  152. $querystring .= urlencode ($key) . '=' . urlencode ( $val2 ) . '&';
  153. }
  154. } else {
  155. if ($key != "key")
  156. $querystring .= urlencode ($key) . '=' . urlencode ($val) . '&';
  157. }
  158. }
  159. $querystring = substr ($querystring, 0, - 1);
  160. } else {
  161. $querystring = $data;
  162. }
  163. return $querystring;
  164. }
  165.  
  166. public function netCancel() {
  167. return true;
  168. }
  169.  
  170. public function getStatus() {
  171. return $this->status;
  172. }
  173.  
  174. public function getBody() {
  175. return $this->body;
  176. }
  177.  
  178. public function getHeaders() {
  179. return $this->headers;
  180. }
  181.  
  182. public function getErrorMsg() {
  183. return $this->errormsg;
  184. }
  185.  
  186. public function getErrorCode() {
  187. return $this->errorcode;
  188. }
  189.  
  190. public function parseResult($result) {
  191. return json_decode($result);
  192. }
  193. }
  194.  
  195. //SET DATA POST
  196. $postfields = array (
  197. 'payMethod' => $_POST['payMethod'],
  198. 'currency' => $_POST['currency'],
  199. 'amt' => $_POST['amt'],
  200. 'referenceNo' => $_POST['referenceNo'],
  201. 'description' => $_POST['description'],
  202. 'bankCd' => $_POST['bankCd'],
  203. 'billingNm' => $_POST['billingNm'],
  204. 'billingPhone' => $_POST['billingPhone'],
  205. 'billingEmail' => $_POST['billingEmail'],
  206. 'billingAddr' => $_POST['billingAddr'],
  207. 'billingCity' => $_POST['billingCity'],
  208. 'billingState' => $_POST['billingState'],
  209. 'billingPostCd' => $_POST['billingPostCd'],
  210. 'billingCountry' => $_POST['billingCountry'],
  211. 'deliveryNm' => $_POST['deliveryNm'],
  212. 'deliveryPhone' => $_POST['deliveryPhone'],
  213. 'deliveryEmail' => $_POST['deliveryEmail'],
  214. 'deliveryAddr' => $_POST['deliveryAddr'],
  215. 'deliveryCity' => $_POST['deliveryCity'],
  216. 'deliveryState' => $_POST['deliveryState'],
  217. 'deliveryPostCd' => $_POST['deliveryPostCd'],
  218. 'deliveryCountry' => $_POST['deliveryCountry'],
  219. 'vacctValidDt' => $_POST['vacctValidDt'],
  220. 'vacctValidTm' => $_POST['vacctValidTm'],
  221. 'iMid' => $_POST['iMid'],
  222. 'merchantToken' => $_POST['merchantToken'],
  223. 'dbProcessUrl' => $_POST['dbProcessUrl'],
  224. 'callBackUrl' => $_POST['callBackUrl'],
  225. 'instmntMon' => $_POST['instmntMon'],
  226. 'instmntType' => $_POST['instmntType'],
  227. 'userIP' => $_POST['userIP'],
  228. 'goodsNm' => $_POST['goodsNm'],
  229. 'vat' => $_POST['vat'],
  230. 'fee' => $_POST['fee'],
  231. 'notaxAmt' => $_POST['notaxAmt'],
  232. 'cartData' => $_POST['cartData'],
  233. );
  234. //print_r($postfields); exit;
  235.  
  236. $apiUrl = "https://www.nicepay.co.id/nicepay/api/onePass.do";
  237. $niceReq = new NicepayRequestor;
  238. $niceReq->openSocket($apiUrl,$gatewayParams['NICEPAY_TIMEOUT_CONNECT']);
  239. $resultData = $niceReq->apiRequest($postfields,$apiUrl,$gatewayParams['NICEPAY_TIMEOUT_READ']);
  240. print_r($resultData); exit;
  241. // unset($postfields);
  242.  
  243.  
  244. //Response from NICEPAY
  245. if(isset($resultData->resultCd) && $resultData->resultCd == "0000"){
  246.  
  247. if($postfields['bankCd'] == "CENA"){
  248. $bankCode = "BCA";
  249. }elseif ($postfields['bankCd'] == "BNIN") {
  250. $bankCode = "BNI";
  251. }elseif ($postfields['bankCd'] == "BMRI") {
  252. $bankCode = "MANDIRI";
  253. }elseif ($postfields['bankCd'] == "HNBN") {
  254. $bankCode = "KEB Hana Bank";
  255. }elseif ($postfields['bankCd'] == "BBBA") {
  256. $bankCode = "PERMATA";
  257. }elseif ($postfields['bankCd'] == "IBBK") {
  258. $bankCode = "BII Maybank";
  259. }else {
  260. $bankCode = "Unknown";
  261. }
  262.  
  263. $va->initPage();
  264.  
  265. $va->assign('resultCd', $resultData->resultCd);
  266. $va->assign('amount', $resultData->amount);
  267. $va->assign('referenceNo', $resultData->referenceNo);
  268. $va->assign('transTm', $postfields['vacctValidTm']);
  269. $va->assign('payMethod', $resultData->payMethod);
  270. $va->assign('tXid', $resultData->tXid);
  271. $va->assign('description', $resultData->description);
  272. $va->assign('callbackUrl', $resultData->callbackUrl);
  273. $va->assign('bankVacctNo', $resultData->bankVacctNo);
  274. $va->assign('transDt', $postfields['vacctValidDt']);
  275. $va->assign('resultMsg', $resultData->resultMsg);
  276.  
  277. $va->assign('variablename', $value);
  278.  
  279. # Check login status
  280. if ($va->isLoggedIn()) {
  281.  
  282. # User is logged in - put any code you like here
  283.  
  284. # Here's an example to get the currently logged in clients first name
  285.  
  286. $clientName = Capsule::table('tblclients')
  287. ->where('id', '=', $va->getUserID())->pluck('firstname');
  288. // 'pluck' was renamed within WHMCS 7.0. Replace it with 'value' instead.
  289. // ->where('id', '=', $ca->getUserID())->value('firstname');
  290.  
  291. $va->assign('clientname', $clientName);
  292.  
  293. } else {
  294.  
  295. # User is not logged in
  296.  
  297. }
  298. //Send Email Invoice Unpaid to Customer
  299. $command = "sendemail";
  300. $adminuser = "admin";
  301. $values["customtype"] = "general";
  302. $values["customsubject"] = "Invoice Payment of ".$gatewayParams['companyName'];
  303. $values["custommessage"] = "
  304. <table>
  305. <tr>
  306. <td colspan='3'>Thank You for Your Order</td>
  307. </tr>
  308. <tr>
  309. <td colspan='3'>Here the Information of your payment :</td>
  310. </tr>
  311. <tr>
  312. <td>Invoice No</td>
  313. <td>:</td>
  314. <td>".$resultData->referenceNo."</td>
  315. </tr>
  316. <tr>
  317. <td>Transaction ID</td>
  318. <td>:</td>
  319. <td>".$resultData->tXid."</td>
  320. </tr>
  321. <tr>
  322. <td>Amount</td>
  323. <td>:</td>
  324. <td>".$resultData->amount."</td>
  325. </tr>
  326. <tr>
  327. <td>Bank</td>
  328. <td>:</td>
  329. <td>".$bankCode."</td>
  330. </tr>
  331. <tr>
  332. <td>Virtual Account Number</td>
  333. <td>:</td>
  334. <td>".$resultData->bankVacctNo."</td>
  335. </tr>
  336. <tr>
  337. <td>Expired Date (Ymd)</td>
  338. <td>:</td>
  339. <td>".$postfields['vacctValidDt']."</td>
  340. </tr>
  341. <tr>
  342. <td>Expired Time (His)</td>
  343. <td>:</td>
  344. <td>".$postfields['vacctValidTm']."</td>
  345. </tr>
  346. </table></nowiki>";
  347. $values["id"] = 1;
  348. $result = localAPI($command,$values,$adminuser);
  349. $va->setTemplate('nicepay_success');
  350.  
  351. $va->output();
  352.  
  353. //header("Location: ".$resultData->data->requestURL."?tXid=".$resultData->tXid);
  354. }elseif (isset($resultData->resultCd)) {
  355. $va->initPage();
  356.  
  357. $va->assign('resultCd', $resultData->resultCd);
  358. $va->assign('resultMsg', $resultData->resultMsg);
  359.  
  360. $va->assign('variablename', $value);
  361.  
  362. # Check login status
  363. if ($va->isLoggedIn()) {
  364.  
  365. # User is logged in - put any code you like here
  366.  
  367. # Here's an example to get the currently logged in clients first name
  368.  
  369. $clientName = Capsule::table('tblclients')
  370. ->where('id', '=', $va->getUserID())->pluck('firstname');
  371. // 'pluck' was renamed within WHMCS 7.0. Replace it with 'value' instead.
  372. // ->where('id', '=', $ca->getUserID())->value('firstname');
  373.  
  374. $va->assign('clientname', $clientName);
  375.  
  376. } else {
  377.  
  378. # User is not logged in
  379.  
  380. }
  381. $va->setTemplate('nicepay_failed');
  382.  
  383. $va->output();
  384. /*// API data not correct, you can redirect back to checkout page or echo error message.
  385. // In this sample, we echo error message
  386. echo "<pre>";
  387. echo "result code :".$resultData->resultCd."\n";
  388. echo "result message :".$resultData->resultMsg."\n";
  389. echo "</pre>";*/
  390. }else {
  391. $va->initPage();
  392.  
  393. $va->assign('variablename', $value);
  394.  
  395. # Check login status
  396. if ($va->isLoggedIn()) {
  397.  
  398. # User is logged in - put any code you like here
  399.  
  400. # Here's an example to get the currently logged in clients first name
  401.  
  402. $clientName = Capsule::table('tblclients')
  403. ->where('id', '=', $va->getUserID())->pluck('firstname');
  404. // 'pluck' was renamed within WHMCS 7.0. Replace it with 'value' instead.
  405. // ->where('id', '=', $ca->getUserID())->value('firstname');
  406.  
  407. $va->assign('clientname', $clientName);
  408.  
  409. } else {
  410.  
  411. # User is not logged in
  412.  
  413. }
  414. $va->setTemplate('nicepay_timeout');
  415.  
  416. $va->output();
  417. // Timeout, you can redirect back to checkout page or echo error message.
  418. // In this sample, we echo error message
  419. //echo "<pre>Connection Timeout. Please Try again.</pre>";
  420. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement