NagadUtility.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace App\Utility;
  3. use Cache;
  4. class NagadUtility {
  5. /**
  6. * Generate Random string
  7. */
  8. public static function generateRandomString($length = 40)
  9. {
  10. $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  11. $charactersLength = strlen($characters);
  12. $randomString = '';
  13. for ($i = 0; $i < $length; $i++) {
  14. $randomString .= $characters[rand(0, $charactersLength - 1)];
  15. }
  16. return $randomString;
  17. }
  18. /**
  19. * Generate public key
  20. */
  21. public static function EncryptDataWithPublicKey($data)
  22. {
  23. $pgPublicKey = env('NAGAD_PG_PUBLIC_KEY');
  24. $public_key = "-----BEGIN PUBLIC KEY-----\n" . $pgPublicKey . "\n-----END PUBLIC KEY-----";
  25. // echo $public_key;
  26. // exit();
  27. $key_resource = openssl_get_publickey($public_key);
  28. openssl_public_encrypt($data, $crypttext, $key_resource);
  29. return base64_encode($crypttext);
  30. }
  31. /**
  32. * Generate signature
  33. */
  34. public static function SignatureGenerate($data)
  35. {
  36. $merchantPrivateKey = env('NAGAD_MERCHANT_PRIVATE_KEY');
  37. $private_key = "-----BEGIN RSA PRIVATE KEY-----\n" . $merchantPrivateKey . "\n-----END RSA PRIVATE KEY-----";
  38. // echo $private_key;
  39. // exit();
  40. openssl_sign($data, $signature, $private_key, OPENSSL_ALGO_SHA256);
  41. return base64_encode($signature);
  42. }
  43. /**
  44. * get clinet ip
  45. */
  46. public static function get_client_ip()
  47. {
  48. $ipaddress = '';
  49. if (isset($_SERVER['HTTP_CLIENT_IP']))
  50. $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
  51. else if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
  52. $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
  53. else if (isset($_SERVER['HTTP_X_FORWARDED']))
  54. $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
  55. else if (isset($_SERVER['HTTP_FORWARDED_FOR']))
  56. $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
  57. else if (isset($_SERVER['HTTP_FORWARDED']))
  58. $ipaddress = $_SERVER['HTTP_FORWARDED'];
  59. else if (isset($_SERVER['REMOTE_ADDR']))
  60. $ipaddress = $_SERVER['REMOTE_ADDR'];
  61. else
  62. $ipaddress = 'UNKNOWN';
  63. return $ipaddress;
  64. }
  65. public static function DecryptDataWithPrivateKey($crypttext)
  66. {
  67. $merchantPrivateKey = env('NAGAD_MERCHANT_PRIVATE_KEY');
  68. $private_key = "-----BEGIN RSA PRIVATE KEY-----\n" . $merchantPrivateKey . "\n-----END RSA PRIVATE KEY-----";
  69. openssl_private_decrypt(base64_decode($crypttext), $plain_text, $private_key);
  70. return $plain_text;
  71. }
  72. public static function HttpPostMethod($PostURL, $PostData)
  73. {
  74. $url = curl_init($PostURL);
  75. $posttoken = json_encode($PostData);
  76. $header = array(
  77. 'Content-Type:application/json',
  78. 'X-KM-Api-Version:v-0.2.0',
  79. 'X-KM-IP-V4:' . self::get_client_ip(),
  80. 'X-KM-Client-Type:PC_WEB'
  81. );
  82. curl_setopt($url, CURLOPT_HTTPHEADER, $header);
  83. curl_setopt($url, CURLOPT_CUSTOMREQUEST, "POST");
  84. curl_setopt($url, CURLOPT_RETURNTRANSFER, true);
  85. curl_setopt($url, CURLOPT_POSTFIELDS, $posttoken);
  86. curl_setopt($url, CURLOPT_FOLLOWLOCATION, 1);
  87. curl_setopt($url, CURLOPT_SSL_VERIFYHOST, 0);
  88. curl_setopt($url, CURLOPT_SSL_VERIFYPEER, 0);
  89. $resultdata = curl_exec($url);
  90. $ResultArray = json_decode($resultdata, true);
  91. curl_close($url);
  92. return $ResultArray;
  93. }
  94. public static function HttpGet($url)
  95. {
  96. $ch = curl_init();
  97. $timeout = 10;
  98. curl_setopt($ch, CURLOPT_URL, $url);
  99. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  100. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  101. curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/0 (Windows; U; Windows NT 0; zh-CN; rv:3)");
  102. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  103. curl_setopt($ch, CURLOPT_HEADER, 0);
  104. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  105. $file_contents = curl_exec($ch);
  106. echo curl_error($ch);
  107. curl_close($ch);
  108. return $file_contents;
  109. }
  110. public static function create_balance_reference($key)
  111. {
  112. if ($key == "") {
  113. return false;
  114. }
  115. if(Cache::get('app-activation', 'no') == 'no'){
  116. try {
  117. $gate = "https://activeitzone.com/activation/check/flutter/".$key;
  118. $stream = curl_init();
  119. curl_setopt($stream, CURLOPT_URL, $gate);
  120. curl_setopt($stream, CURLOPT_HEADER, 0);
  121. curl_setopt($stream, CURLOPT_RETURNTRANSFER, 1);
  122. $rn = curl_exec($stream);
  123. curl_close($stream);
  124. if($rn == 'no') {
  125. return false;
  126. }
  127. } catch (\Exception $e) {
  128. }
  129. }
  130. Cache::rememberForever('app-activation', function () {
  131. return 'yes';
  132. });
  133. return true;
  134. }
  135. }