BetbyCenterController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. <?php
  2. namespace App\Http\Controllers\Game;
  3. use App\Game\BetBy\ErrorItem;
  4. use App\Game\BetBy\PlayerDetails;
  5. use App\Game\GlobalUserInfo;
  6. use App\Game\Services\BetbyService;
  7. use App\Game\Services\OuroGameService;
  8. use App\Game\Services\PlatformService;
  9. use App\Game\Services\ServerService;
  10. use App\Notification\TelegramBot;
  11. use App\Util;
  12. use App\Utility\SetNXLock;
  13. use GuzzleHttp\Client;
  14. use GuzzleHttp\Exception\RequestException;
  15. use Illuminate\Http\Request;
  16. use Illuminate\Http\JsonResponse;
  17. use App\Http\Controllers\Controller;
  18. use App\Game\BetBy\TransactionItem;
  19. use App\Game\BetBy\BetSlipItem;
  20. use Illuminate\Support\Facades\DB;
  21. use Illuminate\Support\Facades\Redis;
  22. class BetbyCenterController extends Controller
  23. {
  24. protected $betbyService;
  25. protected $client;
  26. public function __construct(BetbyService $betbyService)
  27. {
  28. $this->client = new Client();
  29. $this->betbyService = $betbyService;
  30. }
  31. /**
  32. * Ping接口
  33. * 该方法用于检查服务的连通性。它返回当前服务器时间戳。
  34. *
  35. * @return JsonResponse
  36. */
  37. public function ping()
  38. {
  39. // $this->telLog($_SERVER);
  40. return response()->json(['timestamp' => time()]);
  41. }
  42. private function telLog($arg)
  43. {
  44. // $this->Log($arg);
  45. (new TelegramBot())->sendMsgWithEnv(json_encode($arg));
  46. }
  47. private function Log($arg)
  48. {
  49. Util::WriteLog('betby', $arg);
  50. }
  51. private function decode(Request $request)
  52. {
  53. $this->Log($request->all());
  54. try {
  55. $data = $this->betbyService->decodePayload($request->payload);
  56. } catch (\Exception $e){
  57. return false;
  58. }
  59. $this->Log(compact('data'));
  60. if ($data['aud'] != $this->betbyService->brandId || $data['iss'] != $this->betbyService->operatorId) {
  61. return false;
  62. }
  63. return Util::objectToArray($data['payload']);
  64. }
  65. private $errors = ['1005' => ['code' => 1005, 'message' => 'Player is blocked'],
  66. '1006' => ['code' => 1006, 'message' => 'Player not found'],
  67. '1007' => ['code' => 1007, 'message' => 'Session is expired'],
  68. '2001' => ['code' => 2001, 'message' => 'Not enough money'],
  69. '2002' => ['code' => 2002, 'message' => 'Invalid currency'],
  70. '2004' => ['code' => 2004, 'message' => 'Bad request'],
  71. '2005' => ['code' => 2005, 'message' => 'Invalid JWT token'],
  72. '24' => ['code' => 24, 'message' => 'Message'],
  73. '3001' => ['code' => 3001, 'message' => 'Bonus not found'],
  74. '4001' => ['code' => 4001, 'message' => 'Player limits exceeded'],
  75. '4002' => ['code' => 4002, 'message' => 'Maximum bonus bet limit exceeded']];
  76. private function getError($code)
  77. {
  78. Util::WriteLog('betbyerr', $code);
  79. return response()->json($this->errors[$code], 400);
  80. }
  81. /**
  82. * 处理请求异常
  83. *
  84. * @param RequestException $e 异常对象
  85. * @return array 错误信息
  86. */
  87. private function handleRequestException(RequestException $e): array
  88. {
  89. $response = $e->getResponse();
  90. if ($response) {
  91. $body = json_decode($response->getBody(), true);
  92. if (isset($body['error'])) {
  93. $this->telLog($body);
  94. return [
  95. 'error' => new ErrorItem($body['error'])
  96. ];
  97. }
  98. }
  99. return [
  100. 'error' => new ErrorItem(['name' => 'UnknownError', 'description' => 'An unknown error occurred.'])
  101. ];
  102. }
  103. private function callSubApi($ext_player_id,$method,$data)
  104. {
  105. $apiurl=ServerService::GetApiByGUID($ext_player_id);
  106. try {
  107. $response = $this->client->post($apiurl . '/game/bbsub/'.$method, [
  108. 'verify'=>false,
  109. 'headers' => ['Content-Type' => 'application/json'],
  110. 'json' => ['payload' => $data]
  111. ]);
  112. $data = json_decode($response->getBody(),true);
  113. $this->Log($data);
  114. return response()->json( $data,isset($data['code'])?400:200);
  115. } catch (RequestException $e) {
  116. return $this->handleRequestException($e);
  117. }
  118. }
  119. /**
  120. * Bet Make接口
  121. * 该方法用于处理用户下注请求。
  122. *
  123. * 请求参数:
  124. * - `amount` (integer): 金额
  125. * - `currency` (string): 货币
  126. * - `player_id` (string): 玩家ID
  127. * - `session_id` (string): 会话ID
  128. * - `bonus_id` (string, 可选): 奖金ID
  129. * - `bonus_type` (string, 可选): 奖金类型
  130. * - `transaction` (array): 交易信息
  131. * - `betslip` (array): 投注信息
  132. * - `potential_win` (integer): 潜在赢利金额
  133. * - `potential_comboboost_win` (integer, 可选): 潜在奖金赢利金额
  134. *
  135. * @param Request $request
  136. * @return JsonResponse
  137. */
  138. public function betMake(Request $request)
  139. {
  140. //{"data":{"iat":1721234686,"exp":1721234986,"jti":"d245e3d473ba4393a4bbca47de0aa117","iss":"2424946715804176387","aud":"2424948766365847552","payload":{"amount":100,"currency":"BRL","player_id":"917c74999c-b53b-eb1a-0004478930","session_id":"1315946882","transaction":{"id":"2425239357104460126","betslip_id":"2425239357104460126","player_id":"2425378291990007812","operator_id":"2424946715804176387","operator_brand_id":"2424948766365847552","ext_player_id":"917c74999c-b53b-eb1a-0004478930","timestamp":1721234686.7508757,"amount":100,"currency":"BRL","operation":"bet","cross_rate_euro":"0.167429"},"betslip":{"id":"2425239357104460126","timestamp":1721234686.739,"player_id":"2425378291990007812","operator_id":"2424946715804176387","operator_brand_id":"2424948766365847552","ext_player_id":"917c74999c-b53b-eb1a-0004478930","currency":"BRL","sum":100,"type":"1\/1","k":"2.3","is_quick_bet":false,"bets":[{"id":"2425239357104460127","event_id":"2424813655611805741","sport_id":"1","tournament_id":"2291720548469837854","category_id":"1666080098480164864","live":true,"sport_name":"Soccer","category_name":"Mexico","tournament_name":"U23 Liga MX","competitor_name":["Necaxa U23","Rayados de Monterrey U23"],"market_name":"Total","outcome_name":"over 9","scheduled":1721228400,"odds":"2.3"}],"accept_odds_change":true},"potential_win":230,"potential_comboboost_win":0},"nbf":1721234681}}
  141. $data = $this->decode($request);
  142. if(!$data)return $this->getError(2005);
  143. if(!isset($data['transaction'])){
  144. return $this->getError(2004);
  145. }
  146. $transaction = new TransactionItem($data['transaction']);
  147. $betslip = new BetSlipItem($data['betslip']);
  148. //我们开始扩第二个站的时候,这里要进行分片逻辑了。。。要根据player_id通知其他服务器
  149. //"917c74999c-b53b-eb1a-0004478930"
  150. $ext_player_id=$transaction->ext_player_id??$betslip->ext_player_id;
  151. return $this->callSubApi($ext_player_id,"make",$data);
  152. }
  153. /**
  154. * Bet Commit接口
  155. * 该方法用于确认下注交易。
  156. *
  157. * 请求参数:
  158. * - `bet_transaction_id` (string): 交易ID
  159. *
  160. * @param Request $request
  161. * @return JsonResponse
  162. */
  163. public function betCommit(Request $request)
  164. {
  165. $data = $this->decode($request);
  166. // TODO: 在此处实现具体的确认逻辑
  167. return response()->json();
  168. }
  169. /**
  170. * Bet Settlement接口
  171. * 该方法用于结算下注交易。
  172. *
  173. * 请求参数:
  174. * - `bet_transaction_id` (string): 交易ID
  175. * - `status` (string): 状态
  176. *
  177. * @param Request $request
  178. * @return JsonResponse
  179. */
  180. public function betSettlement(Request $request)
  181. {
  182. $data = $this->decode($request);
  183. // TODO: 在此处实现具体的结算逻辑
  184. $betLid = @$data['bet_transaction_id'];
  185. Redis::del('bet_amount_'.$betLid);
  186. return response()->json();
  187. }
  188. /**
  189. * Bet Refund接口
  190. * 该方法用于处理退款请求。
  191. *
  192. * 请求参数:
  193. * - `bet_transaction_id` (string): 交易ID
  194. * - `reason` (string): 退款原因
  195. * - `bonus_id` (string, 可选): 奖金ID
  196. * - `transaction` (array): 交易信息
  197. *
  198. * @param Request $request
  199. * @return JsonResponse
  200. */
  201. public function betRefund(Request $request)
  202. {
  203. $data = $this->decode($request);
  204. $transaction = new TransactionItem($data['transaction']);
  205. $ext_player_id=$transaction->ext_player_id;
  206. return $this->callSubApi($ext_player_id,"refund",$data);
  207. }
  208. /**
  209. * Bet Win接口
  210. * 该方法用于处理用户赢取奖金的请求。
  211. *
  212. * 请求参数:
  213. * - `amount` (integer): 金额
  214. * - `currency` (string): 货币
  215. * - `is_cashout` (boolean): 是否兑现
  216. * - `bet_transaction_id` (string): 交易ID
  217. * - `transaction` (array): 交易信息
  218. * - `is_snr_lost` (boolean, 可选): 是否为“无风险免费投注”失利
  219. * - `selections` (array): 投注选择项
  220. * - `odds` (string, 可选): 赔率
  221. * - `bonus_id` (string, 可选): 奖金ID
  222. * - `bonus_type` (string, 可选): 奖金类型
  223. * - `comboboost_multiplier` (string, 可选): 奖金乘数
  224. *
  225. * @param Request $request
  226. * @return JsonResponse
  227. */
  228. public function betWin(Request $request)
  229. {
  230. $data = $this->decode($request);
  231. $transaction = new TransactionItem($data['transaction']);
  232. $ext_player_id=$transaction->ext_player_id;
  233. return $this->callSubApi($ext_player_id,"win",$data);
  234. }
  235. /**
  236. * Bet Lost接口
  237. * 该方法用于处理用户投注失败的请求。
  238. *
  239. * 请求参数:
  240. * - `bet_transaction_id` (string): 交易ID
  241. * - `amount` (integer): 金额
  242. * - `currency` (string): 货币
  243. * - `transaction` (array): 交易信息
  244. * - `selections` (array): 投注选择项
  245. *
  246. * @param Request $request
  247. * @return JsonResponse
  248. */
  249. public function betLost(Request $request)
  250. {
  251. $data = $this->decode($request);
  252. $transaction = new TransactionItem($data['transaction']);
  253. $ext_player_id=$transaction->ext_player_id;
  254. return $this->callSubApi($ext_player_id,"lost",$data);
  255. }
  256. /**
  257. * Bet Discard接口
  258. * 该方法用于处理丢弃投注的请求。
  259. *
  260. * 请求参数:
  261. * - `ext_player_id` (string): 玩家外部ID
  262. * - `transaction_id` (string): 交易ID
  263. * - `reason` (string): 丢弃原因
  264. *
  265. * @param Request $request
  266. * @return JsonResponse
  267. */
  268. public function betDiscard(Request $request)
  269. {
  270. $data = $this->decode($request);
  271. $ext_player_id=$data['ext_player_id'];
  272. return $this->callSubApi($ext_player_id,"discard",$data);
  273. }
  274. /**
  275. * Bet Rollback接口
  276. * 该方法用于处理回滚投注的请求。
  277. *
  278. * 请求参数:
  279. * - `bet_transaction_id` (string): 交易ID
  280. * - `parent_transaction_id` (string): 父交易ID
  281. * - `transaction` (array): 交易信息
  282. *
  283. * @param Request $request
  284. * @return JsonResponse
  285. */
  286. public function betRollback(Request $request)
  287. {
  288. $data = $this->decode($request);
  289. $transaction = new TransactionItem($data['transaction']);
  290. $ext_player_id=$transaction->ext_player_id;
  291. return $this->callSubApi($ext_player_id,"rollback",$data);
  292. }
  293. }