AviatrixService.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. namespace App\Game\Services;
  3. use App\Util;
  4. use GuzzleHttp\Client;
  5. use Illuminate\Support\Facades\Crypt;
  6. class AviatrixService
  7. {
  8. protected $client;
  9. protected $baseUrl;
  10. protected $secretKey;
  11. protected $cid;
  12. public function __construct()
  13. {
  14. $this->client = new Client();
  15. $this->cid = '24680com';
  16. $this->baseUrl = 'https://game-preprod.aviatrix.work';
  17. $this->secretKey = '4fb4dbe2-0009-438c-8734-0e23a683588b';
  18. }
  19. protected function generateSignature($data)
  20. {
  21. return base64_encode(hash_hmac('md5', json_encode($data), $this->secretKey, true));
  22. }
  23. public function checkSignature($data,$signature)
  24. {
  25. //Util::WriteLog('24680_aviatri',$data);
  26. return $this->generateSignature($data) == $signature;
  27. }
  28. protected function postRequest($endpoint, $data)
  29. {
  30. $signature = $this->generateSignature($data);
  31. $response = $this->client->post($this->baseUrl . $endpoint, [
  32. 'headers' => [
  33. 'Content-Type' => 'application/json',
  34. 'X-Auth-Signature' => $signature,
  35. ],
  36. 'json' => $data,
  37. ]);
  38. return json_decode($response->getBody(), true);
  39. }
  40. public function getPlayerInfo($data)
  41. {
  42. return $this->postRequest('/playerInfo', $data);
  43. }
  44. public function placeBet($data)
  45. {
  46. return $this->postRequest('/bet', $data);
  47. }
  48. public function processWin($data)
  49. {
  50. return $this->postRequest('/win', $data);
  51. }
  52. public function promoWin($data)
  53. {
  54. return $this->postRequest('/transactions/promoWin', $data);
  55. }
  56. public function healthCheck()
  57. {
  58. return ['healthy' => true];
  59. }
  60. public function closeMatch($data)
  61. {
  62. return $this->postRequest('/closeMatch', $data);
  63. }
  64. /**
  65. * 获取游戏回合信息 (Game API)
  66. * 请求参数:
  67. * @param string $cid , 品牌标识
  68. * @param string $playerId , 玩家标识
  69. * @param string $productId , 游戏标识
  70. * @param string $roundId , 回合标识 (betId和roundId至少提供一个)
  71. * @param string $betId , 下注标识 (betId和roundId至少提供一个)
  72. *
  73. * 调用示例:
  74. * {
  75. * "cid": "somebrand",
  76. * "playerId": "john",
  77. * "productId": "nft-aviatrix",
  78. * "roundId": "2028207"
  79. * }
  80. */
  81. public function getRoundInfo($cid, $playerId, $productId, $roundId = null, $betId = null)
  82. {
  83. $data = compact('cid', 'playerId', 'productId', 'roundId', 'betId');
  84. return $this->postRequest('/game/round', $data);
  85. }
  86. /**
  87. * 启动游戏 (Transfer Wallet API)
  88. * 请求参数:
  89. * @param string $cid , 品牌标识
  90. * @param string $playerId , 玩家标识
  91. * @param string $productId , 游戏标识
  92. * @param string $currency , 货币类型
  93. * @param string $lang , 可选, 语言
  94. * @param string $lobbyUrl , 可选, 大厅URL
  95. * @param boolean $isDemo , 可选, 是否为演示模式
  96. *
  97. * 调用示例:
  98. * {
  99. * "cid": "somebrand",
  100. * "playerId": "john",
  101. * "productId": "nft-aviatrix",
  102. * "currency": "USD",
  103. * "lang": "en",
  104. * "lobbyUrl": "https://example.com/lobby",
  105. * "isDemo": true
  106. * }
  107. */
  108. public function launchGame($playerId, $lang = null, $lobbyUrl = null, $isDemo = null)
  109. {
  110. $cid = $this->cid;
  111. $productId = 'nft-aviatrix';
  112. $sessionToken = Crypt::encryptString($playerId);
  113. $sessionToken = rtrim(strtr($sessionToken, '+/', '-_'), '=');
  114. return $this->baseUrl.'?cid='.$cid.'&productId='.$productId.'&sessionToken='.$sessionToken.'&lang='.$lang;
  115. }
  116. /**
  117. * 检查交易 (Transfer Wallet API)
  118. * 请求参数:
  119. * @param string $cid , 品牌标识
  120. * @param string $txId , 事务标识
  121. *
  122. * 调用示例:
  123. * {
  124. * "cid": "somebrand",
  125. * "txId": "4df40f77-2b38-43f4-b264-1d850f5a6715"
  126. * }
  127. */
  128. public function checkTransaction($cid, $txId)
  129. {
  130. $data = compact('cid', 'txId');
  131. return $this->postRequest('/transferwallet/checkTx', $data);
  132. }
  133. /**
  134. * 获取玩家余额 (Transfer Wallet API)
  135. * 请求参数:
  136. * @param string $cid , 品牌标识
  137. * @param string $playerId , 玩家标识
  138. *
  139. * 调用示例:
  140. * {
  141. * "cid": "somebrand",
  142. * "playerId": "john"
  143. * }
  144. */
  145. public function getBalance($cid, $playerId)
  146. {
  147. $data = compact('cid', 'playerId');
  148. return $this->postRequest('/transferwallet/getBalance', $data);
  149. }
  150. /**
  151. * 获取交易历史 (Transfer Wallet API)
  152. * 请求参数:
  153. * @param string $cid , 品牌标识
  154. * @param string $playerId , 玩家标识
  155. * @param integer $limit , 可选, 返回记录的限制数
  156. * @param string $from , 可选, 起始时间
  157. *
  158. * 调用示例:
  159. * {
  160. * "cid": "somebrand",
  161. * "playerId": "john",
  162. * "limit": 10,
  163. * "from": "2023-01-01"
  164. * }
  165. */
  166. public function getTransactionHistory($cid, $playerId, $limit = null, $from = null)
  167. {
  168. $data = compact('cid', 'playerId', 'limit', 'from');
  169. return $this->postRequest('/transferwallet/getHistory', $data);
  170. }
  171. /**
  172. * 存款 (Transfer Wallet API)
  173. * 请求参数:
  174. * @param string $cid , 品牌标识
  175. * @param string $playerId , 玩家标识
  176. * @param string $txId , 事务标识
  177. * @param string $currency , 货币类型
  178. * @param integer $amount , 存款金额
  179. *
  180. * 调用示例:
  181. * {
  182. * "cid": "somebrand",
  183. * "playerId": "john",
  184. * "txId": "4df40f77-2b38-43f4-b264-1d850f5a6715",
  185. * "currency": "EUR",
  186. * "amount": 1234
  187. * }
  188. */
  189. public function deposit($cid, $playerId, $txId, $currency, $amount)
  190. {
  191. $data = compact('cid', 'playerId', 'txId', 'currency', 'amount');
  192. return $this->postRequest('/transferwallet/deposit', $data);
  193. }
  194. /**
  195. * 取款 (Transfer Wallet API)
  196. * 请求参数:
  197. * @param string $cid , 品牌标识
  198. * @param string $playerId , 玩家标识
  199. * @param string $txId , 事务标识
  200. * @param string $currency , 货币类型
  201. * @param integer $amount , 取款金额
  202. *
  203. * 调用示例:
  204. * {
  205. * "cid": "somebrand",
  206. * "playerId": "john",
  207. * "txId": "4df40f77-2b38-43f4-b264-1d850f5a6715",
  208. * "currency": "EUR",
  209. * "amount": 1234
  210. * }
  211. */
  212. public function withdraw($cid, $playerId, $txId, $currency, $amount)
  213. {
  214. $data = compact('cid', 'playerId', 'txId', 'currency', 'amount');
  215. return $this->postRequest('/transferwallet/withdraw', $data);
  216. }
  217. }