StarPayCashierLogic.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php
  2. namespace App\Http\logic\api;
  3. use App\Constant\Payment;
  4. use App\dao\Estatisticas\RechargeWithDraw;
  5. use App\Inter\CashierInterFace;
  6. use App\Models\PrivateMail;
  7. use App\Models\RecordUserDataStatistics;
  8. use App\Services\PayConfig;
  9. use App\Services\StarPayService;
  10. use App\Services\StoredProcedure;
  11. use App\Util;
  12. use Illuminate\Support\Facades\DB;
  13. use Illuminate\Support\Facades\Log;
  14. use Illuminate\Support\Facades\Redis;
  15. class StarPayCashierLogic implements CashierInterFace
  16. {
  17. /**
  18. * 代付渠道标识,对应 agent.dbo.admin_configs.config_value(type=cash)。
  19. * 使用前请在后台配置同名渠道值。
  20. */
  21. const AGENT = 107;
  22. /**
  23. * 创建 StarPay 代付订单。
  24. */
  25. public function payment($RecordID, $amount, $accountName, $phone, $email, $OrderId, $PixNum, $PixType, $IFSCNumber, $BranchBank, $BankNO)
  26. {
  27. // 查询提现订单
  28. $query = DB::connection('write')
  29. ->table('QPAccountsDB.dbo.OrderWithDraw')
  30. ->where('RecordID', $RecordID)
  31. ->first();
  32. if (!$query) {
  33. Util::WriteLog('StarPay', 'withdraw order not found: '.$RecordID);
  34. return 'fail';
  35. }
  36. $config = (new PayConfig())->getConfig('StarPayOut');
  37. $service = new StarPayService($config);
  38. // 账户号:优先 PixNum,其次 BankNO
  39. $account = $PixNum ?: $BankNO;
  40. if (!$account) {
  41. Util::WriteLog('StarPay_error', 'missing account for withdraw: '.$OrderId);
  42. return 'fail';
  43. }
  44. // 银行编号:优先 BankNO(如果看作 bankId),否则使用配置默认
  45. $bankId = $BranchBank;
  46. if ($bankId === '') {
  47. Util::WriteLog('StarPay_error', 'missing bankId for withdraw: '.$OrderId);
  48. return 'fail';
  49. }
  50. // 提现金额是以分存储,转成两位小数金额
  51. $orderAmount = number_format($amount / 100, 2, '.', '');
  52. $bankList = config('games.mex_bank_list');
  53. try {
  54. $data = $service->cash($OrderId, $orderAmount, $bankId, $bankList[$bankId] ?? '', $account, $accountName);
  55. if ($data === false) {
  56. return 'fail';
  57. }
  58. } catch (\Throwable $e) {
  59. Util::WriteLog('StarPay_error', 'payout request exception: '.$e->getMessage());
  60. return '';
  61. }
  62. // 文档示例:code=200 且 data.transactionStatus = "00" 表示下单成功
  63. if (isset($data['code']) && (string)$data['code'] === '0') {
  64. $transactionStatus = $data['data']['orderStatus'] ?? -99;
  65. if ($transactionStatus >= 0) {
  66. return $data;
  67. }
  68. if ($transactionStatus == -4 || $transactionStatus == -99) {
  69. return $data;
  70. }
  71. }
  72. // 同步下单失败:如果订单在处理中(State=5),退回资金并标记失败
  73. if ((int)$query->State === 5) {
  74. $msg = $data['msg'] ?? 'StarPay payout failed';
  75. $WithDraw = $query->WithDraw + $query->ServiceFee;
  76. $bonus = '30000,'.$WithDraw;
  77. PrivateMail::failMail($query->UserID, $OrderId, $WithDraw, $msg, $bonus);
  78. $withdraw_data = [
  79. 'State' => 6,
  80. 'agent' => self::AGENT,
  81. 'finishDate' => now(),
  82. 'remark' => json_encode($data),
  83. ];
  84. DB::connection('write')
  85. ->table('QPAccountsDB.dbo.OrderWithDraw')
  86. ->where('OrderId', $query->OrderId)
  87. ->update($withdraw_data);
  88. $RecordData = ['after_state' => 6, 'update_at' => now()];
  89. DB::connection('write')
  90. ->table('QPAccountsDB.dbo.AccountsRecord')
  91. ->where('type', 1)
  92. ->where('RecordID', $RecordID)
  93. ->update($RecordData);
  94. }
  95. return 'fail';
  96. }
  97. /**
  98. * StarPay代付回调。
  99. *
  100. * @param array<string,mixed>|string $post
  101. */
  102. public function notify($post)
  103. {
  104. if (!is_array($post)) {
  105. $post = \GuzzleHttp\json_decode($post, true);
  106. }
  107. Util::WriteLog('StarPay', 'payout notify: '.json_encode($post, JSON_UNESCAPED_UNICODE));
  108. $config = (new PayConfig())->getConfig('StarPayOut');
  109. $service = new StarPayService($config);
  110. // 验签
  111. if (!$service->verifySign($post)) {
  112. Util::WriteLog('StarPay_error', 'notify sign invalid');
  113. return 'fail';
  114. }
  115. $res = $service->cashNotify($post);
  116. if (in_array($res->status, [Payment::STATUS_UNKNOWN, Payment::STATUS_IN_PROGRESS])) {
  117. Util::WriteLog('StarPay', 'ignore non-payout notify');
  118. return 'success';
  119. }
  120. $OrderId = $post['merOrderNo'] ?? '';
  121. if ($OrderId === '') {
  122. Util::WriteLog('StarPay_error', 'notify missing merOrderId');
  123. return 'fail';
  124. }
  125. $query = DB::connection('write')
  126. ->table('QPAccountsDB.dbo.OrderWithDraw')
  127. ->where('OrderId', $OrderId)
  128. ->first();
  129. if (!$query) {
  130. Util::WriteLog('StarPay_error', 'withdraw order not found in notify: '.$OrderId);
  131. return 'success';
  132. }
  133. // 只处理 State=5 或 7 的订单,避免重复
  134. if (!in_array((int)$query->State, [5, 7], true)) {
  135. Util::WriteLog('StarPay', 'withdraw already handled: '.$OrderId);
  136. return 'success';
  137. }
  138. $agentID = DB::connection('write')
  139. ->table('agent.dbo.admin_configs')
  140. ->where('config_value', self::AGENT)
  141. ->where('type', 'cash')
  142. ->value('id') ?? '';
  143. $now = now();
  144. $UserID = $query->UserID;
  145. $TakeMoney = $query->WithDraw + $query->ServiceFee;
  146. // Supefina 回调里带有 amount/fee 和 realityAmount/realityFee
  147. // 提醒:这里仍以我们订单金额为准做账,仅将真实金额用于日志,可根据需要扩展到统计侧。
  148. $realityAmount = isset($post['amount']) ? (float)$post['amount'] : null;
  149. $realityFee = isset($post['tradeCharge']) ? (float)$post['tradeCharge'] : null;
  150. Util::WriteLog('StarPay', 'payout reality: amount='.$realityAmount.', fee='.$realityFee.', orderId='.$OrderId);
  151. $withdraw_data = [];
  152. switch ($res->status) {
  153. case Payment::STATUS_SUCCESS: // 提现成功
  154. $withdraw_data = [
  155. 'State' => 2,
  156. 'agent' => $agentID,
  157. 'finishDate' => $now,
  158. ];
  159. // 增加提现记录
  160. $first = DB::connection('write')
  161. ->table('QPAccountsDB.dbo.UserTabData')
  162. ->where('UserID', $UserID)
  163. ->first();
  164. if ($first) {
  165. DB::connection('write')
  166. ->table('QPAccountsDB.dbo.UserTabData')
  167. ->where('UserID', $UserID)
  168. ->increment('TakeMoney', $TakeMoney);
  169. } else {
  170. DB::connection('write')
  171. ->table('QPAccountsDB.dbo.UserTabData')
  172. ->insert(['TakeMoney' => $TakeMoney, 'UserID' => $UserID]);
  173. try {
  174. PrivateMail::praiseSendMail($UserID);
  175. } catch (\Throwable $e) {
  176. // 忽略邮件发送失败
  177. }
  178. }
  179. // 免审记录
  180. $withdrawal_position_log = DB::connection('write')
  181. ->table('agent.dbo.withdrawal_position_log')
  182. ->where('order_sn', $OrderId)
  183. ->first();
  184. if ($withdrawal_position_log) {
  185. DB::connection('write')
  186. ->table('agent.dbo.withdrawal_position_log')
  187. ->where('order_sn', $OrderId)
  188. ->update(['take_effect' => 2, 'update_at' => date('Y-m-d H:i:s')]);
  189. }
  190. try {
  191. StoredProcedure::addPlatformData($UserID, 4, $TakeMoney);
  192. } catch (\Throwable $exception) {
  193. Util::WriteLog('StoredProcedure', $exception->getMessage());
  194. }
  195. $ServiceFee = $query->ServiceFee;
  196. RecordUserDataStatistics::updateOrAdd($UserID, $TakeMoney, 0, $ServiceFee);
  197. $fee = DB::table('QPAccountsDB.dbo.OrderWithDraw')->where('OrderId', $OrderId)
  198. ->value('withdraw_fee');
  199. (new RechargeWithDraw())->withDraw($UserID, $TakeMoney, $fee, $ServiceFee);
  200. $redis = Redis::connection();
  201. $redis->incr('draw_'.date('Ymd').$UserID);
  202. break;
  203. case Payment::STATUS_FAIL: // 提现失败
  204. $msg = $post['msg'] ?? 'Withdraw rejected';
  205. $bonus = '30000,'.$TakeMoney;
  206. PrivateMail::failMail($query->UserID, $OrderId, $TakeMoney, $msg, $bonus);
  207. Util::WriteLog('SupefinaSpeiEmail', [$query->UserID, $OrderId, $TakeMoney, $msg, $bonus]);
  208. $withdraw_data = [
  209. 'State' => 6,
  210. 'agent' => $agentID,
  211. 'remark' => $msg,
  212. ];
  213. break;
  214. case Payment::STATUS_REFUND:
  215. Log::error('代付订单退款', ['data' => $post]);
  216. return 'success';
  217. }
  218. $RecordData = [
  219. 'before_state' => $query->State,
  220. 'after_state' => $withdraw_data['State'] ?? 0,
  221. 'RecordID' => $query->RecordID,
  222. 'update_at' => date('Y-m-d H:i:s'),
  223. ];
  224. DB::connection('write')
  225. ->table('QPAccountsDB.dbo.AccountsRecord')
  226. ->updateOrInsert(
  227. ['RecordID' => $query->RecordID, 'type' => 1],
  228. $RecordData
  229. );
  230. DB::connection('write')
  231. ->table('QPAccountsDB.dbo.OrderWithDraw')
  232. ->where('OrderId', $OrderId)
  233. ->update($withdraw_data);
  234. if (isset($withdraw_data['State']) && (int)$withdraw_data['State'] === 2) {
  235. StoredProcedure::user_label($UserID, 2, $TakeMoney);
  236. }
  237. return 'success';
  238. }
  239. }