PayTestController.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\dao\Pay\PayController;
  4. use App\Facade\TableName;
  5. use App\Http\helper\CreateOrder;
  6. use App\Http\helper\NumConfig;
  7. use App\Http\logic\api\OrderLogic;
  8. use App\Jobs\Order;
  9. use App\Models\PrivateMail;
  10. use App\Models\RecordUserDataStatistics;
  11. use App\Services\AuPay;
  12. use App\Services\FirstRechargeGifts;
  13. use App\Services\LogDayStatisticalByDayAndChannel;
  14. use App\Services\MonthCard;
  15. use App\Services\OrderService;
  16. use App\Services\OrderServices;
  17. use App\Services\StoredProcedure;
  18. use Illuminate\Http\Request;
  19. use Illuminate\Support\Facades\DB;
  20. use Illuminate\Support\Facades\Log;
  21. class PayTestController
  22. {
  23. // 模拟支付回调
  24. public function pay(Request $request)
  25. {
  26. $bool = env('SURROUNDINGS') ?? false;
  27. if (!$bool) {
  28. return 'Please switch environment';
  29. }
  30. $GameID = $request->userId ?: '';
  31. $GiftsID = $request->GiftsID ?: '';
  32. $payAmt = $request->payAmt ?: '';
  33. // $AdId = $request->AdId ?: 'efa7d051-2f29-429b-b26e-d73b64604673';
  34. $AdId = $request->AdId ?: 'd206cefddda8b6b209428cd5b8129aab';
  35. $eventType = $request->eventType ?: 1;
  36. $GiftsID = empty($GiftsID) ? 0 : $GiftsID;
  37. $payAmt = empty($payAmt) ? 100 : $payAmt;
  38. $userId = DB::table(TableName::QPAccountsDB() . 'AccountsInfo')
  39. ->where('GameID', $GameID)
  40. ->value('UserID');
  41. if (empty($userId)) return '缺少用户ID';
  42. if (empty($payAmt)) return '缺少支付金额';
  43. // 礼包类型验证
  44. $PayVerify = new PayController();
  45. $pay_amount = $PayVerify->verify($userId, $GiftsID, $payAmt);
  46. if ($pay_amount === false) return apiReturnFail($PayVerify->getError());
  47. $orderSn = CreateOrder::order_sn($userId);
  48. $post = [
  49. 'orderid' => $orderSn,
  50. 'prdOrdNo' => '',
  51. 'ordStatus' => 'success',
  52. 'payAmt' => $payAmt * 100,
  53. ];
  54. // 生成订单信息
  55. $logic = new OrderLogic();
  56. $logic->orderCreate($orderSn, $post['payAmt'], 'TestPay', $userId, '', $GiftsID,$AdId,$eventType);
  57. return $this->notify($post);
  58. }
  59. public function notify($post)
  60. {
  61. $order_sn = $post['orderid'];
  62. try {
  63. // 查询订单信息
  64. $order = DB::connection('write')->table('agent.dbo.order')->where('order_sn', $order_sn)->first();
  65. if (!$order) return '订单不存在';
  66. if (!empty($order->pay_at) || !empty($order->finished_at)) return '订单已支付';
  67. $body = [
  68. 'payment_sn' => $post['prdOrdNo'],
  69. 'updated_at' => date('Y-m-d H:i:s'),
  70. ];
  71. $apply_data = [
  72. 'order_id' => $order->id,
  73. 'payment_sn' => $post['prdOrdNo'],
  74. 'payment_code' => 'TestPay',
  75. 'return' => \GuzzleHttp\json_encode($post),
  76. 'is_error' => 0,
  77. 'created_at' => date('Y-m-d H:i:s'),
  78. 'updated_at' => date('Y-m-d H:i:s'),
  79. ];
  80. $ordStatus = $post['ordStatus'];
  81. $GiftsID = $order->GiftsID ?: '';
  82. $userID = $order->user_id ?: '';
  83. $AdId = $order->AdId ?: '';
  84. $eventType = $order->eventType ?: '';
  85. $payAmt = (int)$post['payAmt'] / NumConfig::NUM_VALUE;
  86. // 说明:除了成功,失败和退款状态,都可以认为是处理中
  87. switch ($ordStatus) {
  88. case 'success': // 支付成功
  89. $body['pay_status'] = 1;
  90. $body['pay_at'] = date('Y-m-d H:i:s');
  91. $body['finished_at'] = date('Y-m-d H:i:s');
  92. // 获取金额
  93. $service = new OrderServices();
  94. [$give, $favorable_price, $Recharge, $czReason, $cjReason] = $service->getPayInfo($GiftsID, $userID, $payAmt);
  95. // 增加充值记录
  96. [$Score] = $service->addRecord($userID, $payAmt, $favorable_price, $order_sn, $GiftsID, $Recharge, $czReason, $give, $cjReason,$AdId,$eventType);
  97. // 成功处理回调
  98. Order::dispatch([$userID, $payAmt, $Score, $favorable_price, $GiftsID,$order_sn]);
  99. break;
  100. case 'error': // 支付失败
  101. $body['pay_status'] = 2;
  102. $apply_data['is_error'] = 1;
  103. break;
  104. }
  105. $order_up = DB::connection('write')->table('agent.dbo.order')->where('order_sn', $order_sn)->update($body);
  106. $apply = DB::connection('write')->table('agent.dbo.payment_apply')->insert($apply_data);
  107. if (($order_up && $apply) != true) return '订单更新失败';
  108. exit('OK');
  109. } catch (\Exception $exception) {
  110. return $exception->getMessage();
  111. }
  112. }
  113. // 订单提现状态修改 完成修改为订单失败
  114. public function recovery(Request $request)
  115. {
  116. // +---------------------------------------------------------
  117. // 增加用户提现
  118. // +---------------------------------------------------------
  119. // $custOrderNo = $request->custOrderNo ?? '';
  120. // if (empty($custOrderNo)) {
  121. // return apiReturnFail('订单号为空!');
  122. // }
  123. // $query = DB::connection('write')->table('QPAccountsDB.dbo.OrderWithDraw')->where('OrderId', $custOrderNo)->first();
  124. //
  125. // $withdraw_data = [
  126. // 'State' => 6,
  127. // ];
  128. //
  129. // // 增加提现记录
  130. // $first = DB::connection('write')->table('QPAccountsDB.dbo.UserTabData')->where('UserID', $query->UserID)->first();
  131. // $TakeMoney = $query->WithDraw + $query->ServiceFee;
  132. //
  133. // if ($first) {
  134. // DB::connection('write')->table('QPAccountsDB.dbo.UserTabData')->where('UserID', $query->UserID)->increment('TakeMoney', $TakeMoney);
  135. // }
  136. //
  137. // $RecordData = [
  138. // 'before_state' => 2,
  139. // 'after_state' => 6,
  140. // ];
  141. //
  142. // // 添加用户提现操作记录
  143. // DB::connection('write')->table('QPAccountsDB.dbo.AccountsRecord')->updateOrInsert(['RecordID' => $query->RecordID, 'type' => 1], $RecordData);
  144. //
  145. // //DB::connection('write')->table('QPAccountsDB.dbo.withdraw_notify')->where('order_sn', $custOrderNo)->delete();
  146. // DB::connection('write')->table('QPAccountsDB.dbo.OrderWithDraw')->where('OrderId', $query->OrderId)->update($withdraw_data);
  147. // // 返还用户提现额度
  148. // $user_id = $query->UserID;
  149. // $WithDraw = $query->WithDraw + $query->ServiceFee;
  150. // DB::connection('write')->table('QPAccountsDB.dbo.AccountWithDrawInfo')->where('UserID', $user_id)->increment('HistoryWithDraw', $WithDraw);
  151. // DB::connection('write')->table('QPAccountsDB.dbo.AccountWithDrawInfo')->where('UserID', $user_id)->decrement('Win', $WithDraw);
  152. // return apiReturnSuc();
  153. // +---------------------------------------------------------
  154. // 扣除用户提现
  155. // +---------------------------------------------------------
  156. $custOrderNo = $request->custOrderNo ?? '';
  157. if (empty($custOrderNo)) {
  158. return apiReturnFail('订单号为空!');
  159. }
  160. $query = DB::connection('write')->table('QPAccountsDB.dbo.OrderWithDraw')->where('OrderId', $custOrderNo)->first();
  161. if ($query->State != 2) {
  162. return apiReturnFail('订单不是已完成!或者已经处理过');
  163. }
  164. $withdraw_data = [
  165. 'State' => 6,
  166. ];
  167. // 增加提现记录
  168. $first = DB::connection('write')->table('QPAccountsDB.dbo.UserTabData')->where('UserID', $query->UserID)->first();
  169. $TakeMoney = $query->WithDraw + $query->ServiceFee;
  170. if ($first) {
  171. DB::connection('write')->table('QPAccountsDB.dbo.UserTabData')->where('UserID', $query->UserID)->decrement('TakeMoney', $TakeMoney);
  172. }
  173. // 免审的时候,修改免审状态
  174. $withdrawal_position_log = DB::connection('write')->table('agent.dbo.withdrawal_position_log')->where('order_sn', $custOrderNo)->first();
  175. if ($withdrawal_position_log) {
  176. DB::connection('write')->table('agent.dbo.withdrawal_position_log')->where('order_sn', $custOrderNo)->update(['take_effect' => 1]);
  177. }
  178. $RecordData = [
  179. 'before_state' => 2,
  180. 'after_state' => 6,
  181. ];
  182. // 添加用户提现操作记录
  183. DB::connection('write')->table('QPAccountsDB.dbo.AccountsRecord')->updateOrInsert(['RecordID' => $query->RecordID, 'type' => 1], $RecordData);
  184. DB::connection('write')->table('QPAccountsDB.dbo.withdraw_notify')->where('order_sn', $custOrderNo)->delete();
  185. DB::connection('write')->table('QPAccountsDB.dbo.OrderWithDraw')->where('OrderId', $query->OrderId)->update($withdraw_data);
  186. $msg = 'Liquidation failure'; // 清算失败
  187. $user_id = $query->UserID;
  188. $WithDraw = $query->WithDraw + $query->ServiceFee;
  189. $bonus = '30000,' . $WithDraw;
  190. $TitleString = 'Your withdrawal order application failed.';
  191. $TextString = "1、Your withdrawal order: {$custOrderNo} application failed.
  192. 2、Reasons for order submission failure:{$msg}
  193. 3、This is the withdrawal amount you requested, please collect it as soon as possible.
  194. 4、If you have any questions, please contact customer service.
  195. ";
  196. PrivateMail::sendMail(2, $user_id, $TitleString, $TextString, $bonus, $custOrderNo, $WithDraw, 2);
  197. // 返还用户提现额度
  198. DB::connection('write')->table('QPAccountsDB.dbo.AccountWithDrawInfo')->where('UserID', $user_id)->decrement('HistoryWithDraw', $WithDraw);
  199. DB::connection('write')->table('QPAccountsDB.dbo.AccountWithDrawInfo')->where('UserID', $user_id)->increment('Win', $WithDraw);
  200. return apiReturnSuc();
  201. }
  202. // 用户提现成功
  203. public function cashNotify(Request $request)
  204. {
  205. // +---------------------------------------------------------
  206. // 用户提现成功
  207. // +---------------------------------------------------------
  208. $custOrderNo = $request->custOrderNo ?? '';
  209. $orderStatus = $request->orderStatus ?: 1; # 1.成功 2.失败
  210. if (empty($custOrderNo)) {
  211. return apiReturnFail('订单号为空!');
  212. }
  213. $query = DB::connection('write')->table('QPAccountsDB.dbo.OrderWithDraw')->where('OrderId', $custOrderNo)->first();
  214. if (!$query) {
  215. Log::info('订单不存在');
  216. return 'fail';
  217. }
  218. if ($query->State != 5 && $query->State != 7 && $query->State != 1) { // 不是待支付订单 -- 或者第三方清算中 返回
  219. Log::info('订单状态已完成');
  220. return 'fail';
  221. }
  222. $UserID = $query->UserID;
  223. $TakeMoney = $query->WithDraw + $query->ServiceFee;
  224. $notify_data = [
  225. 'state' => 1,
  226. 'finish_at' => date('Y-m-d H:i:s'),
  227. 'extra' => '',
  228. 'created_at' => date('Y-m-d H:i:s'),
  229. 'updated_at' => date('Y-m-d H:i:s'),
  230. 'order_sn' => $custOrderNo,
  231. 'amount' => $TakeMoney,
  232. ];
  233. Log::info('测试提现结果:' . $orderStatus);
  234. switch ($orderStatus) {
  235. case 1:
  236. Log::info('提现成功');
  237. $withdraw_data = [
  238. 'State' => 2,
  239. 'agent' => 5
  240. ];
  241. // 增加提现记录
  242. $first = DB::connection('write')->table('QPAccountsDB.dbo.UserTabData')->where('UserID', $UserID)->first();
  243. if ($first) {
  244. DB::connection('write')->table('QPAccountsDB.dbo.UserTabData')->where('UserID', $query->UserID)->increment('TakeMoney', $TakeMoney);
  245. } else {
  246. DB::connection('write')->table('QPAccountsDB.dbo.UserTabData')->insert(['TakeMoney' => $TakeMoney, 'UserID' => $UserID]);
  247. }
  248. // 免审的时候,修改免审状态
  249. $withdrawal_position_log = DB::connection('write')->table('agent.dbo.withdrawal_position_log')->where('order_sn', $custOrderNo)->first();
  250. if ($withdrawal_position_log) {
  251. DB::connection('write')->table('agent.dbo.withdrawal_position_log')->where('order_sn', $custOrderNo)->update(['take_effect' => 2, 'update_at' => date('Y-m-d H:i:s')]);
  252. }
  253. $ServiceFee = $query->ServiceFee;
  254. // 增加用户提现值
  255. RecordUserDataStatistics::updateOrAdd($UserID, $TakeMoney, 0, $ServiceFee);
  256. // 给用户发邮件
  257. PrivateMail::successMail($UserID, $custOrderNo, $TakeMoney);
  258. break;
  259. case 0:
  260. $withdraw_data = [
  261. 'State' => 7,
  262. 'agent' => 5
  263. ];
  264. $notify_data = [
  265. 'state' => 2,
  266. ];
  267. break;
  268. case 2: # 提现失败
  269. // 发送邮件给玩家
  270. if (isset($post['failMessage'])) {
  271. $msg = $post['failMessage'];
  272. } else {
  273. $msg = 'Liquidation failure'; // 清算失败
  274. }
  275. $bonus = '30000,' . $TakeMoney;
  276. $TitleString = 'Your withdrawal order application failed.';
  277. $TextString = "1、Your withdrawal order: {$custOrderNo} application failed.
  278. 2、Reasons for order submission failure:{$msg}
  279. 3、This is the withdrawal amount you requested, please collect it as soon as possible.
  280. 4、If you have any questions, please contact customer service.
  281. ";
  282. PrivateMail::sendMail(2, $UserID, $TitleString, $TextString, $bonus, $custOrderNo, $TakeMoney, 2);
  283. // 修改订单状态为失败
  284. $withdraw_data = [
  285. 'State' => 6,
  286. 'agent' => 5
  287. ];
  288. $notify_data = [
  289. 'state' => 2,
  290. ];
  291. break;
  292. }
  293. $RecordData = [
  294. 'before_state' => $query->State,
  295. 'after_state' => $withdraw_data['State'] ?? 0,
  296. 'RecordID' => $query->RecordID,
  297. 'create_at' => date('Y-m-d H:i:s'),
  298. 'update_at' => date('Y-m-d H:i:s')
  299. ];
  300. // 添加用户提现操作记录
  301. DB::connection('write')->table('QPAccountsDB.dbo.AccountsRecord')->updateOrInsert(['RecordID' => $query->RecordID, 'type' => 1], $RecordData);
  302. DB::connection('write')->table('QPAccountsDB.dbo.withdraw_notify')->updateOrInsert(['order_sn' => $custOrderNo], $notify_data);
  303. DB::connection('write')->table('QPAccountsDB.dbo.OrderWithDraw')->where('OrderId', $custOrderNo)->update($withdraw_data);
  304. if (isset($withdraw_data['State']) && $withdraw_data['State'] == 2) {
  305. // 单控标签
  306. StoredProcedure::user_label($UserID, 2, $TakeMoney);
  307. // 渠道后台埋点
  308. (new LogDayStatisticalByDayAndChannel())->updateData($UserID, 2);
  309. }
  310. return apiReturnSuc();
  311. }
  312. public function notify1($post)
  313. {
  314. try {
  315. $custOrderNo = $post['custOrderNo'];
  316. // 查询订单信息
  317. $order = DB::connection('write')->table('agent.dbo.order')->where('order_sn', $custOrderNo)->first();
  318. if (!$order) {
  319. Log::info('订单不存在');
  320. return 'fail';
  321. }
  322. if (!empty($order->pay_at) || !empty($order->finished_at)) {
  323. Log::info('订单已支付');
  324. return 'fail';
  325. }
  326. $body = [
  327. 'payment_sn' => $post['prdOrdNo'],
  328. 'updated_at' => date('Y-m-d H:i:s'),
  329. ];
  330. $apply_data = [
  331. 'order_id' => $order->id,
  332. 'payment_sn' => $post['prdOrdNo'],
  333. 'payment_code' => 'SerPayMent',
  334. 'return' => \GuzzleHttp\json_encode($post),
  335. 'is_error' => 0,
  336. 'created_at' => date('Y-m-d H:i:s'),
  337. 'updated_at' => date('Y-m-d H:i:s'),
  338. ];
  339. $ordStatus = $post['ordStatus'];
  340. $amount = $post['payAmt'];
  341. // 说明:除了成功,失败和退款状态,都可以认为是处理中
  342. switch ($ordStatus) {
  343. case 01: // 支付成功
  344. $body['pay_status'] = 1;
  345. $body['pay_at'] = date('Y-m-d H:i:s');
  346. $body['finished_at'] = date('Y-m-d H:i:s');
  347. $payAmt = $amount / 100;
  348. $GiftsID = $order->GiftsID ?: '';
  349. $userID = $order->user_id ?: '';
  350. (new OrderService())->paySuccess($GiftsID, $userID, $payAmt, $custOrderNo);
  351. break;
  352. case 00: // 未交易
  353. $body['pay_status'] = 4;
  354. $apply_data['is_error'] = 1;
  355. break;
  356. case 02: // 失败
  357. $body['pay_status'] = 2;
  358. $apply_data['is_error'] = 1;
  359. break;
  360. case 03: // 被拒绝
  361. $body['pay_status'] = 3;
  362. $apply_data['is_error'] = 1;
  363. break;
  364. case 05: // 取消支付
  365. $body['pay_status'] = 5;
  366. $apply_data['is_error'] = 1;
  367. break;
  368. case 06: // 未支付
  369. $body['pay_status'] = 6;
  370. $apply_data['is_error'] = 1;
  371. break;
  372. case 07: // 退款
  373. $body['pay_status'] = 7;
  374. $apply_data['is_error'] = 1;
  375. break;
  376. }
  377. $order_up = DB::table('agent.dbo.order')
  378. ->where('order_sn', $custOrderNo)
  379. ->update($body);
  380. $apply = DB::table('agent.dbo.payment_apply')
  381. ->insert($apply_data);
  382. if (($order_up && $apply) != true) {
  383. Log::info('订单更新失败');
  384. return 'fail';
  385. }
  386. Log::info('success');
  387. echo 'SC000000';
  388. return 'SC000000';
  389. } catch (\Exception $exception) {
  390. Log::info('支付失败11 ' . $exception->getMessage());
  391. return apiReturnFail($exception->getMessage());
  392. }
  393. }
  394. }