BetbyTestController.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. <?php
  2. namespace App\Http\Controllers\Game;
  3. use App\Game\GlobalUserInfo;
  4. use App\Game\Services\BetbyTestService;
  5. use App\Game\Services\OuroGameService;
  6. use App\Game\Services\PlatformService;
  7. use App\Http\helper\NumConfig;
  8. use App\Notification\TelegramBot;
  9. use App\Util;
  10. use App\Utility\SetNXLock;
  11. use Illuminate\Http\Request;
  12. use Illuminate\Http\JsonResponse;
  13. use App\Http\Controllers\Controller;
  14. use App\Game\BetBy\TransactionItem;
  15. use App\Game\BetBy\BetSlipItem;
  16. use Illuminate\Support\Facades\DB;
  17. use Illuminate\Support\Facades\Redis;
  18. class BetbyTestController extends Controller
  19. {
  20. protected $betbyService;
  21. public function __construct(BetbyTestService $betbyService)
  22. {
  23. $this->betbyService = $betbyService;
  24. }
  25. /**
  26. * Ping接口
  27. * 该方法用于检查服务的连通性。它返回当前服务器时间戳。
  28. *
  29. * @return JsonResponse
  30. */
  31. public function ping()
  32. {
  33. return response()->json(['timestamp' => time()]);
  34. }
  35. private function telLog($arg)
  36. {
  37. $this->Log($arg);
  38. // (new TelegramBot())->sendMsgWithEnv(json_encode($arg));
  39. }
  40. private function Log($arg)
  41. {
  42. Util::WriteLog('betby', $arg);
  43. }
  44. private function decode(Request $request)
  45. {
  46. try {
  47. $data = $this->betbyService->decodePayload($request->payload);
  48. } catch (\Exception $e){
  49. return false;
  50. }
  51. if ($data['aud'] != $this->betbyService->brandId || $data['iss'] != $this->betbyService->operatorId) {
  52. return false;
  53. }
  54. return Util::objectToArray($data['payload']);
  55. }
  56. private $errors = ['1005' => ['code' => 1005, 'message' => 'Player is blocked'],
  57. '1006' => ['code' => 1006, 'message' => 'Player not found'],
  58. '1007' => ['code' => 1007, 'message' => 'Session is expired'],
  59. '2001' => ['code' => 2001, 'message' => 'Not enough money'],
  60. '2002' => ['code' => 2002, 'message' => 'Invalid currency'],
  61. '2004' => ['code' => 2004, 'message' => 'Bad request'],
  62. '2005' => ['code' => 2005, 'message' => 'Invalid JWT token'],
  63. '24' => ['code' => 24, 'message' => 'Message'],
  64. '3001' => ['code' => 3001, 'message' => 'Bonus not found'],
  65. '4001' => ['code' => 4001, 'message' => 'Player limits exceeded'],
  66. '4002' => ['code' => 4002, 'message' => 'Maximum bonus bet limit exceeded']];
  67. private function getError($code)
  68. {
  69. Util::WriteLog('betbyerr', $code);
  70. return response()->json($this->errors[$code], 400);
  71. }
  72. /**
  73. * Bet Make接口
  74. * 该方法用于处理用户下注请求。
  75. *
  76. * 请求参数:
  77. * - `amount` (integer): 金额
  78. * - `currency` (string): 货币
  79. * - `player_id` (string): 玩家ID
  80. * - `session_id` (string): 会话ID
  81. * - `bonus_id` (string, 可选): 奖金ID
  82. * - `bonus_type` (string, 可选): 奖金类型
  83. * - `transaction` (array): 交易信息
  84. * - `betslip` (array): 投注信息
  85. * - `potential_win` (integer): 潜在赢利金额
  86. * - `potential_comboboost_win` (integer, 可选): 潜在奖金赢利金额
  87. *
  88. * @param Request $request
  89. * @return JsonResponse
  90. */
  91. public function betMake(Request $request)
  92. {
  93. //{"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}}
  94. $data = $this->decode($request);
  95. if(!$data)return $this->getError(2005);
  96. if(!isset($data['transaction'])){
  97. return $this->getError(2004);
  98. }
  99. $transaction = new TransactionItem($data['transaction']);
  100. $betslip = new BetSlipItem($data['betslip']);
  101. //TODO: 我们开始扩第二个站的时候,这里要进行分片逻辑了。。。要根据player_id通知其他服务器
  102. $user = GlobalUserInfo::getGameUserInfo('GlobalUID', $data['player_id']);
  103. $this->telLog(compact('user', 'data', 'betslip'));
  104. // TODO: 在此处实现具体的下注逻辑
  105. //未找到用户
  106. if(!$user)return $this->getError(1006);
  107. //货币不对
  108. if($data['currency']!='BRL')return $this->getError(2002);
  109. if (Redis::exists('discard_'.$transaction->id)) {
  110. return $this->getError(2004);
  111. }
  112. $res = SetNXLock::getExclusiveLock('betby_bet_'.$transaction->id, 86400);
  113. if (!$res) {
  114. return $this->getError(2004);
  115. }
  116. $score = GlobalUserInfo::getScoreByUserID($user->UserID);
  117. $score = $score*NumConfig::NUM_VALUE;
  118. $userScoreKey = 'userScore-bet-'.$user->UserID;
  119. if(Redis::exists($userScoreKey)){
  120. $score=Redis::get($userScoreKey);
  121. }else{
  122. $userScoreKey = 'userScore-bet-'.$user->UserID;
  123. Redis::set($userScoreKey,$score);
  124. }
  125. //没钱
  126. if ($score < $transaction->amount) return $this->getError(2001);
  127. $userScoreKey = 'userScore-bet-'.$user->UserID;
  128. Redis::set($userScoreKey,$score-$data['amount']);
  129. DB::connection('write')->table('QPTreasureDB.dbo.GameScoreInfo')->where('UserID', $user->UserID)->decrement('Score', $data['amount']);
  130. OuroGameService::notifyWebHall($user->UserID,"","pay_finish",["Golds"=>$score-$data['amount'],"PayNum"=>-$data['amount'],"event"=>"pay"]);
  131. //用户cancel的时候
  132. Redis::set('bet_amount_'.$transaction->id,$data['amount']?:1);
  133. Redis::expire('bet_amount_'.$transaction->id,86400*31);
  134. PlatformService::platformBet('betby','',$data['amount'],$user->UserID);
  135. return response()->json([
  136. 'id' => $transaction->id,//Unique identifier for transaction assigned by Partner 我们要自己生成,可以用数据库id吧
  137. 'ext_transaction_id' => $transaction->id,
  138. 'parent_transaction_id' => null,//make是一切的开始,没有parent
  139. 'user_id' => $data['player_id'],
  140. 'operation' => 'bet',
  141. 'amount' => $data['amount'],
  142. 'currency' => $data['currency'],
  143. 'balance' => $score-$data['amount'], // 示例余额
  144. 'operator_bonus_amount' => 0 // 示例奖金金额
  145. ]);
  146. }
  147. /**
  148. * Bet Commit接口
  149. * 该方法用于确认下注交易。
  150. *
  151. * 请求参数:
  152. * - `bet_transaction_id` (string): 交易ID
  153. *
  154. * @param Request $request
  155. * @return JsonResponse
  156. */
  157. public function betCommit(Request $request)
  158. {
  159. $data = $this->decode($request);
  160. $this->telLog(compact('data'));
  161. // TODO: 在此处实现具体的确认逻辑
  162. return response()->json();
  163. }
  164. /**
  165. * Bet Settlement接口
  166. * 该方法用于结算下注交易。
  167. *
  168. * 请求参数:
  169. * - `bet_transaction_id` (string): 交易ID
  170. * - `status` (string): 状态
  171. *
  172. * @param Request $request
  173. * @return JsonResponse
  174. */
  175. public function betSettlement(Request $request)
  176. {
  177. $data = $this->decode($request);
  178. $this->telLog(compact('data'));
  179. // TODO: 在此处实现具体的结算逻辑
  180. $betLid = @$data['bet_transaction_id'];
  181. Redis::del('bet_amount_'.$betLid);
  182. return response()->json();
  183. }
  184. /**
  185. * Bet Refund接口
  186. * 该方法用于处理退款请求。
  187. *
  188. * 请求参数:
  189. * - `bet_transaction_id` (string): 交易ID
  190. * - `reason` (string): 退款原因
  191. * - `bonus_id` (string, 可选): 奖金ID
  192. * - `transaction` (array): 交易信息
  193. *
  194. * @param Request $request
  195. * @return JsonResponse
  196. */
  197. public function betRefund(Request $request)
  198. {
  199. $data = $this->decode($request);
  200. $transaction = new TransactionItem($data['transaction']);
  201. $this->telLog(compact('data'));
  202. $user = GlobalUserInfo::getGameUserInfo('GlobalUID', $transaction->ext_player_id);
  203. $bet=0;
  204. $key='bet_amount_'.$data['bet_transaction_id'];
  205. $score = GlobalUserInfo::getScoreByUserID($user->UserID);
  206. $score = $score*NumConfig::NUM_VALUE;
  207. $res = SetNXLock::getExclusiveLock('refund_betby_'.$transaction->id, 86400);
  208. if (!$res) {
  209. Util::WriteLog('betbyeee','######has refunded######_'.$transaction->id);
  210. return response()->json([
  211. 'id' => $transaction->id,
  212. 'ext_transaction_id' => $transaction->id,
  213. 'parent_transaction_id' => $transaction->parent_transaction_id,
  214. 'user_id' => $transaction->ext_player_id,
  215. 'operation' => 'refund',
  216. 'amount' => $transaction->amount,
  217. 'currency' => $transaction->currency,
  218. 'balance' => $score+$bet // 示例余额
  219. ]);
  220. // return $this->getError(2004);
  221. }
  222. if(Redis::exists($key)){
  223. $bet=$transaction->amount;
  224. if($bet>0){
  225. DB::connection('write')->table('QPTreasureDB.dbo.GameScoreInfo')->where('UserID', $user->UserID)->increment('Score', $bet);
  226. PlatformService::platformBet('betby','',-$bet,$user->UserID);
  227. $userScoreKey = 'userScore-bet-'.$user->UserID;
  228. Redis::set($userScoreKey,$score+$bet);
  229. OuroGameService::notifyWebHall($user->UserID,"","pay_finish",["Golds"=>$score+$bet,"PayNum"=>$bet,"event"=>"pay"]);
  230. Util::WriteLog('betrefund', ['before_score'=>$score,'after_score'=>$score+$bet ,'data' => $data]);
  231. }
  232. //Redis::del($key);
  233. }
  234. return response()->json([
  235. 'id' => $transaction->id,
  236. 'ext_transaction_id' => $transaction->id,
  237. 'parent_transaction_id' => $transaction->parent_transaction_id,
  238. 'user_id' => $transaction->ext_player_id,
  239. 'operation' => 'refund',
  240. 'amount' => $transaction->amount,
  241. 'currency' => $transaction->currency,
  242. 'balance' => $score+$bet // 示例余额
  243. ]);
  244. }
  245. /**
  246. * Bet Win接口
  247. * 该方法用于处理用户赢取奖金的请求。
  248. *
  249. * 请求参数:
  250. * - `amount` (integer): 金额
  251. * - `currency` (string): 货币
  252. * - `is_cashout` (boolean): 是否兑现
  253. * - `bet_transaction_id` (string): 交易ID
  254. * - `transaction` (array): 交易信息
  255. * - `is_snr_lost` (boolean, 可选): 是否为“无风险免费投注”失利
  256. * - `selections` (array): 投注选择项
  257. * - `odds` (string, 可选): 赔率
  258. * - `bonus_id` (string, 可选): 奖金ID
  259. * - `bonus_type` (string, 可选): 奖金类型
  260. * - `comboboost_multiplier` (string, 可选): 奖金乘数
  261. *
  262. * @param Request $request
  263. * @return JsonResponse
  264. */
  265. public function betWin(Request $request)
  266. {
  267. $data = $this->decode($request);
  268. $transaction = new TransactionItem($data['transaction']);
  269. $this->telLog(compact('data', 'transaction'));
  270. // TODO: 在此处实现具体的赢取奖金逻辑
  271. $user = GlobalUserInfo::getGameUserInfo('GlobalUID', $transaction->ext_player_id);
  272. Util::WriteLog('betbytt',[$transaction->ext_player_id,$user]);
  273. $res = SetNXLock::getExclusiveLock('win_betby_'.$transaction->id, 86400);
  274. $score = GlobalUserInfo::getScoreByUserID($user->UserID);
  275. $score = $score*NumConfig::NUM_VALUE;
  276. $betKey = 'bet_amount_'.$transaction->parent_transaction_id;
  277. $win = intval($data['amount']);
  278. $bet = Redis::get($betKey)?:0;
  279. if (!$res) {
  280. return response()->json([
  281. 'id' => $transaction->id,
  282. 'ext_transaction_id' => $transaction->id,
  283. 'parent_transaction_id' => $transaction->parent_transaction_id,
  284. 'user_id' => $transaction->ext_player_id,
  285. 'operation' => 'win',
  286. 'amount' => $data['amount'],
  287. 'currency' => $data['currency'],
  288. 'balance' => $score // 示例余额
  289. ]);
  290. }
  291. $userScoreKey = 'userScore-bet-'.$user->UserID;
  292. Redis::set($userScoreKey,$score+$win);
  293. if(true){
  294. PlatformService::platformWin($user->UserID,'betby','',$win,$bet,$score+$win);
  295. OuroGameService::notifyWebHall($user->UserID,"","pay_finish",["Golds"=>$score+$win,"PayNum"=>$win,"event"=>"pay"]);
  296. }
  297. return response()->json([
  298. 'id' => $transaction->id,
  299. 'ext_transaction_id' => $transaction->id,
  300. 'parent_transaction_id' => $transaction->parent_transaction_id,
  301. 'user_id' => $transaction->ext_player_id,
  302. 'operation' => 'win',
  303. 'amount' => $data['amount'],
  304. 'currency' => $data['currency'],
  305. 'balance' => $score+$win // 示例余额
  306. ]);
  307. }
  308. /**
  309. * Bet Lost接口
  310. * 该方法用于处理用户投注失败的请求。
  311. *
  312. * 请求参数:
  313. * - `bet_transaction_id` (string): 交易ID
  314. * - `amount` (integer): 金额
  315. * - `currency` (string): 货币
  316. * - `transaction` (array): 交易信息
  317. * - `selections` (array): 投注选择项
  318. *
  319. * @param Request $request
  320. * @return JsonResponse
  321. */
  322. public function betLost(Request $request)
  323. {
  324. $data = $this->decode($request);
  325. $transaction = new TransactionItem($data['transaction']);
  326. $this->telLog(compact('data', 'transaction'));
  327. // TODO: 在此处实现具体的投注失败逻辑
  328. $user = GlobalUserInfo::getGameUserInfo('GlobalUID', $transaction->ext_player_id);
  329. $res = SetNXLock::getExclusiveLock('lost_betby_'.$transaction->id, 86400);
  330. $score = GlobalUserInfo::getScoreByUserID($user->UserID);
  331. $score = $score*NumConfig::NUM_VALUE;
  332. if (!$res) {
  333. //Util::WriteLog('24680_win_error_betby',$data);
  334. //return ['success' => false,'code' => 2401,'message' => 'Session not found or expired.'];
  335. return response()->json([
  336. 'id' => $transaction->id,
  337. 'ext_transaction_id' => $transaction->id,
  338. 'parent_transaction_id' => $transaction->parent_transaction_id,
  339. 'user_id' => $transaction->ext_player_id,
  340. 'operation' => 'lost',
  341. 'balance' => $score // 示例余额
  342. ]);
  343. //return $this->getError(2004);
  344. }
  345. $betKey = 'bet_amount_'.$transaction->parent_transaction_id;
  346. $win = 0;
  347. $bet = Redis::get($betKey)?:0;
  348. if(true){
  349. PlatformService::platformWin($user->UserID,'betby','',$win,$bet,$score+$win);
  350. }
  351. return response()->json([
  352. 'id' => $transaction->id,
  353. 'ext_transaction_id' => $transaction->id,
  354. 'parent_transaction_id' => $transaction->parent_transaction_id,
  355. 'user_id' => $transaction->ext_player_id,
  356. 'operation' => 'lost',
  357. 'balance' => $score // 示例余额
  358. ]);
  359. }
  360. /**
  361. * Bet Discard接口
  362. * 该方法用于处理丢弃投注的请求。
  363. *
  364. * 请求参数:
  365. * - `ext_player_id` (string): 玩家外部ID
  366. * - `transaction_id` (string): 交易ID
  367. * - `reason` (string): 丢弃原因
  368. *
  369. * @param Request $request
  370. * @return JsonResponse
  371. */
  372. public function betDiscard(Request $request)
  373. {
  374. $data = $this->decode($request);
  375. $this->telLog(compact('data'));
  376. $user = GlobalUserInfo::getGameUserInfo('GlobalUID', $data['ext_player_id']);
  377. $bet=0;
  378. $key='bet_amount_'.$data['transaction_id'];
  379. $score = GlobalUserInfo::getScoreByUserID($user->UserID);
  380. $score = $score*NumConfig::NUM_VALUE;
  381. $res = SetNXLock::getExclusiveLock('discard_'.$data['transaction_id'], 86400);
  382. if (!$res) {
  383. return response()->json();
  384. //return ['success' => false,'code' => 2401,'message' => 'Session not found or expired.'];
  385. //return ['success' => true,'balance' => intval($score)];
  386. }
  387. if(Redis::exists($key)){
  388. $bet=Redis::get($key);
  389. if($bet>0){
  390. DB::connection('write')->table('QPTreasureDB.dbo.GameScoreInfo')->where('UserID', $user->UserID)->increment('Score', $bet);
  391. PlatformService::platformBet('betby','',-$bet,$user->UserID);
  392. $userScoreKey = 'userScore-bet-'.$user->UserID;
  393. Redis::set($userScoreKey,$score+$bet);
  394. OuroGameService::notifyWebHall($user->UserID,"","pay_finish",["Golds"=>$score+$bet,"PayNum"=>$bet,"event"=>"pay"]);
  395. Util::WriteLog('betbydiscard', ['before_score'=>$score,'after_score'=>$score+$bet ]);
  396. }
  397. Redis::del($key);
  398. }
  399. return response()->json();
  400. }
  401. /**
  402. * Bet Rollback接口
  403. * 该方法用于处理回滚投注的请求。
  404. *
  405. * 请求参数:
  406. * - `bet_transaction_id` (string): 交易ID
  407. * - `parent_transaction_id` (string): 父交易ID
  408. * - `transaction` (array): 交易信息
  409. *
  410. * @param Request $request
  411. * @return JsonResponse
  412. */
  413. public function betRollback(Request $request)
  414. {
  415. $data = $this->decode($request);
  416. $transaction = new TransactionItem($data['transaction']);
  417. $this->telLog(compact('data', 'transaction'));
  418. // TODO: 在此处实现具体的回滚逻辑
  419. $user = GlobalUserInfo::getGameUserInfo('GlobalUID', $transaction->ext_player_id);
  420. $score = GlobalUserInfo::getScoreByUserID($user->UserID);
  421. $score = $score*NumConfig::NUM_VALUE;
  422. $key='bet_amount_'.$data['bet_transaction_id'];
  423. if(!Redis::exists($key)){
  424. Util::WriteLog('betbyeee','######not find bet key######_'.$data['bet_transaction_id']);
  425. return response()->json([
  426. 'id' => $transaction->id,
  427. 'ext_transaction_id' => $transaction->id,
  428. 'parent_transaction_id' => $transaction->parent_transaction_id,
  429. 'user_id' => $transaction->ext_player_id,
  430. 'operation' => 'rollback',
  431. 'amount' => $transaction->amount,
  432. 'currency' => $transaction->currency,
  433. 'balance' => max($score,0) // 示例余额
  434. ]);
  435. //return $this->getError(2004);
  436. }
  437. $res = SetNXLock::getExclusiveLock('rollbackn_'.$transaction->id, 86400);
  438. if (!$res) {
  439. Util::WriteLog('betbyeee','######repeat rollback######_'.$data['bet_transaction_id']);
  440. return response()->json([
  441. 'id' => $transaction->id,
  442. 'ext_transaction_id' => $transaction->id,
  443. 'parent_transaction_id' => $transaction->parent_transaction_id,
  444. 'user_id' => $transaction->ext_player_id,
  445. 'operation' => 'rollback',
  446. 'amount' => $transaction->amount,
  447. 'currency' => $transaction->currency,
  448. 'balance' => max($score,0) // 示例余额
  449. ]);
  450. //return $this->getError(2004);
  451. //return ['success' => false,'code' => 2401,'message' => 'Session not found or expired.'];
  452. //return ['success' => true,'balance' => intval($score)];
  453. }
  454. $amount = $transaction->amount;
  455. if($amount>$score){
  456. $special = ['user_id' => $user->UserID,'current_score' => $score,'rollback_amount' => $amount];
  457. Util::WriteLog('betby_roollback',compact('data',$special));
  458. $amount = $score;
  459. }
  460. //$bet = Redis::get($key)?:0;
  461. if(true){
  462. DB::connection('write')->table('QPTreasureDB.dbo.GameScoreInfo')->where('UserID', $user->UserID)->decrement('Score', $amount);
  463. PlatformService::platformWin($user->UserID,'betby','',-$amount,0,$score-$amount);
  464. }
  465. $userScoreKey = 'userScore-bet-'.$user->UserID;
  466. Redis::set($userScoreKey,max($score-$amount,0));
  467. return response()->json([
  468. 'id' => $transaction->id,
  469. 'ext_transaction_id' => $transaction->id,
  470. 'parent_transaction_id' => $transaction->parent_transaction_id,
  471. 'user_id' => $transaction->ext_player_id,
  472. 'operation' => 'rollback',
  473. 'amount' => $transaction->amount,
  474. 'currency' => $transaction->currency,
  475. 'balance' => max($score-$amount,0) // 示例余额
  476. ]);
  477. }
  478. }