SfPayCashierLogic.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. <?php
  2. namespace App\Http\logic\api;
  3. use App\dao\Estatisticas\RechargeWithDraw;
  4. use App\Http\helper\NumConfig;
  5. use App\Inter\CashierInterFace;
  6. use App\Models\PrivateMail;
  7. use App\Models\RecordUserDataStatistics;
  8. use App\Services\SfPay;
  9. use App\Services\PayConfig;
  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 SfPayCashierLogic implements CashierInterFace
  16. {
  17. const AGENT = 108; // SfPay代付渠道值(对应 admin_configs config_value)
  18. protected $agent = 108;
  19. /**
  20. * 支付方式位掩码 → SfPay payProduct 映射(代付)
  21. * 1=cashapp, 2=paypal
  22. */
  23. protected $payoutProductMap = [
  24. 1 => 'USA202', // cashapp
  25. 2 => 'USA203', // paypal
  26. ];
  27. /**
  28. * PixType → 代付方式映射
  29. */
  30. protected $bankTypeMap = [
  31. 1 => 'USA202', // cashapp
  32. 2 => 'USA203', // paypal
  33. ];
  34. /**
  35. * 提交代付申请
  36. */
  37. public function payment($RecordID, $amount, $accountName, $phone, $email, $OrderId, $PixNum, $PixType, $IFSCNumber, $BranchBank, $BankNO)
  38. {
  39. // 查询订单
  40. $query = DB::connection('write')->table('QPAccountsDB.dbo.OrderWithDraw')->where('RecordID', $RecordID)->first();
  41. if (!$query) return 'fail';
  42. $payConfigService = new PayConfig();
  43. $config = $payConfigService->getConfig('SfPay');
  44. $service = new SfPay();
  45. // 确定代付产品
  46. $payProduct = $this->payoutProductMap[$PixType] ?? 'USA202';
  47. // 构建收款账号
  48. if ($PixType == 1) {
  49. // CashApp: cashtag,需 $ 前缀
  50. $account = ($PixNum && strpos($PixNum, '$') !== 0) ? '$' . $PixNum : $PixNum;
  51. } elseif ($PixType == 2) {
  52. // PayPal: 邮箱
  53. $account = $email ?: $PixNum;
  54. } else {
  55. $account = $PixNum ?: $email;
  56. }
  57. // 构建代付请求参数
  58. $params = [
  59. 'merchantOrderNo' => $OrderId,
  60. 'amount' => (float)number_format($amount / NumConfig::NUM_VALUE, 2, '.', ''),
  61. 'beneficiaryAccount' => $account,
  62. 'beneficiaryName' => $accountName ?: 'user',
  63. 'countryId' => 'USA',
  64. 'payProduct' => $payProduct,
  65. 'notifyUrl' => $config['cash_notify_url'] ?? '',
  66. 'beneficiaryEmail' => $email ?: '',
  67. 'beneficiaryPhoneNo' => $phone ?: '',
  68. ];
  69. Log::info('SfPay 提现参数:', $params);
  70. $url = $service->apiUrl . '/gateway/payout/init';
  71. try {
  72. $result = $service->curlPost($url, $params);
  73. } catch (\Exception $exception) {
  74. Log::info('SfPay 提现请求异常:', [$exception->getMessage()]);
  75. Util::WriteLog('SfPay_error', 'SfPay 提现请求异常:' . $exception->getMessage());
  76. return 'fail';
  77. }
  78. Log::info('SfPay 提现结果:', [$result ?? 'no result']);
  79. try {
  80. $data = \GuzzleHttp\json_decode($result, true);
  81. } catch (\Exception $e) {
  82. Util::WriteLog("SfPay_error", [$result, $e->getMessage()]);
  83. return 'fail';
  84. }
  85. // SfPay 代付成功响应: code=0
  86. if (isset($data['code']) && $data['code'] === 0) {
  87. return $data;
  88. }
  89. // 同步失败处理:回复玩家金币,标记订单失败
  90. if ($query->State == 5) {
  91. $msg = 'Liquidation failure';
  92. $WithDraw = $query->WithDraw + $query->ServiceFee;
  93. $bonus = '30000,' . $WithDraw;
  94. PrivateMail::failMail($query->UserID, $OrderId, $WithDraw, $msg, $bonus);
  95. $withdraw_data = [
  96. 'State' => 6,
  97. 'agent' => 1080,
  98. 'finishDate' => now(),
  99. 'remark' => json_encode($data)
  100. ];
  101. DB::connection('write')->table('QPAccountsDB.dbo.OrderWithDraw')
  102. ->where('OrderId', $query->OrderId)
  103. ->update($withdraw_data);
  104. $RecordData = ['after_state' => 6, 'update_at' => now()];
  105. DB::connection('write')->table('QPAccountsDB.dbo.AccountsRecord')
  106. ->where('type', 1)
  107. ->where('RecordID', $RecordID)
  108. ->update($RecordData);
  109. }
  110. return 'fail';
  111. }
  112. /**
  113. * 代付异步回调处理
  114. *
  115. * SfPay代付回调格式(POST JSON,不加密):
  116. * {
  117. * "data": {
  118. * "amount": "30.00",
  119. * "orderNo": "...",
  120. * "message": "...",
  121. * "type": "payout",
  122. * "merchantOrderNo": "...",
  123. * "processedTime": "...",
  124. * "processAmount": "0.00",
  125. * "status": "SUCCESS"
  126. * },
  127. * "signature_n": "..."
  128. * }
  129. */
  130. public function notify($post)
  131. {
  132. if (!is_array($post)) {
  133. $post = \GuzzleHttp\json_decode($post, true);
  134. }
  135. Util::WriteLog('SfPay', 'SfPay 提现回调:' . json_encode($post));
  136. try {
  137. $callbackData = $post['data'] ?? [];
  138. $signatureN = $post['signature_n'] ?? '';
  139. if (empty($callbackData) || empty($signatureN)) {
  140. Util::WriteLog('SfPay', '提现回调数据不完整');
  141. return 'SUCCESS';
  142. }
  143. // 验签
  144. $service = new SfPay();
  145. if (!$service->verifySign($callbackData, $signatureN)) {
  146. Util::WriteLog('SfPay', '提现回调签名验证失败');
  147. return 'SUCCESS';
  148. }
  149. // 判断订单是否存在
  150. $OrderId = $callbackData['merchantOrderNo'] ?? '';
  151. $query = DB::connection('write')->table('QPAccountsDB.dbo.OrderWithDraw')
  152. ->where('OrderId', $OrderId)
  153. ->first();
  154. if (!$query) {
  155. Util::WriteLog('SfPay', '提现订单不存在: ' . $OrderId);
  156. return 'SUCCESS';
  157. }
  158. // 订单已完成处理
  159. if ($query->State != 5 && $query->State != 7) {
  160. Util::WriteLog('SfPay', $OrderId . '_订单状态已完成');
  161. return 'SUCCESS';
  162. }
  163. $agentID = DB::connection('write')->table('agent.dbo.admin_configs')
  164. ->where('config_value', self::AGENT)
  165. ->where('type', 'cash')
  166. ->select('id')
  167. ->first()->id ?? '';
  168. $now = now();
  169. $notify_data = [
  170. 'state' => 1,
  171. 'finish_at' => $now,
  172. 'casOrdNo' => $callbackData['orderNo'] ?? '',
  173. 'extra' => \GuzzleHttp\json_encode($post),
  174. 'created_at' => $now,
  175. 'updated_at' => $now,
  176. 'order_sn' => $OrderId,
  177. 'amount' => $query->WithDraw,
  178. ];
  179. // 判断回调结果: SUCCESS=成功, FAILURE=失败
  180. $status = $callbackData['status'] ?? '';
  181. $orderStatus = 0;
  182. if ($status === 'SUCCESS') {
  183. $orderStatus = 1; // 成功
  184. } elseif ($status === 'FAILURE' || $status === 'REVERSED') {
  185. $orderStatus = 2; // 失败
  186. }
  187. if (!$orderStatus) {
  188. Util::WriteLog('SfPay', 'SfPay 提现处理中:' . $OrderId);
  189. return 'SUCCESS';
  190. }
  191. Util::WriteLog('SfPay', 'SfPay 提现结果:' . $OrderId . '_' . $orderStatus);
  192. $UserID = $query->UserID;
  193. $TakeMoney = $query->WithDraw + $query->ServiceFee;
  194. $withdraw_data = [];
  195. switch ($orderStatus) {
  196. case 1: // 提现成功
  197. Util::WriteLog('SfPay', 'SfPay提现成功');
  198. $withdraw_data = [
  199. 'State' => 2,
  200. 'agent' => $agentID,
  201. 'finishDate' => $now
  202. ];
  203. // 增加提现记录
  204. $first = DB::connection('write')->table('QPAccountsDB.dbo.UserTabData')
  205. ->where('UserID', $UserID)->first();
  206. if ($first) {
  207. DB::connection('write')->table('QPAccountsDB.dbo.UserTabData')
  208. ->where('UserID', $UserID)
  209. ->increment('TakeMoney', $TakeMoney);
  210. } else {
  211. DB::connection('write')->table('QPAccountsDB.dbo.UserTabData')
  212. ->insert(['TakeMoney' => $TakeMoney, 'UserID' => $UserID]);
  213. try {
  214. PrivateMail::praiseSendMail($UserID);
  215. } catch (\Exception $e) {
  216. // 忽略邮件发送失败
  217. }
  218. }
  219. // 免审的时候,修改免审状态
  220. $withdrawal_position_log = DB::connection('write')
  221. ->table('agent.dbo.withdrawal_position_log')
  222. ->where('order_sn', $OrderId)
  223. ->first();
  224. if ($withdrawal_position_log) {
  225. DB::connection('write')->table('agent.dbo.withdrawal_position_log')
  226. ->where('order_sn', $OrderId)
  227. ->update(['take_effect' => 2, 'update_at' => date('Y-m-d H:i:s')]);
  228. }
  229. try {
  230. StoredProcedure::addPlatformData($UserID, 4, $TakeMoney);
  231. } catch (\Exception $exception) {
  232. Util::WriteLog('StoredProcedure', $exception);
  233. }
  234. $ServiceFee = $query->ServiceFee;
  235. // 增加用户提现值
  236. RecordUserDataStatistics::updateOrAdd($UserID, $TakeMoney, 0, $ServiceFee);
  237. // 数据统计后台 -- 提现记录添加
  238. (new RechargeWithDraw())->withDraw($UserID, $TakeMoney, 0, $ServiceFee);
  239. $redis = Redis::connection();
  240. $redis->incr('draw_' . date('Ymd') . $UserID);
  241. PrivateMail::successMail($UserID, $OrderId, $TakeMoney);
  242. break;
  243. case 2: // 提现失败
  244. $msg = 'Encomenda rejeitada pelo banco';
  245. $bonus = '30000,' . $TakeMoney;
  246. PrivateMail::failMail($query->UserID, $OrderId, $TakeMoney, $msg, $bonus);
  247. Util::WriteLog('SfPayEmail', [$query->UserID, $OrderId, $TakeMoney, $msg, $bonus]);
  248. $withdraw_data = [
  249. 'State' => 6,
  250. 'agent' => $agentID,
  251. 'remark' => $callbackData['message'] ?? ''
  252. ];
  253. $notify_data['state'] = 2;
  254. break;
  255. }
  256. $RecordData = [
  257. 'before_state' => $query->State,
  258. 'after_state' => $withdraw_data['State'] ?? 0,
  259. 'RecordID' => $query->RecordID,
  260. 'update_at' => date('Y-m-d H:i:s')
  261. ];
  262. // 添加用户提现操作记录
  263. DB::connection('write')->table('QPAccountsDB.dbo.AccountsRecord')
  264. ->updateOrInsert(['RecordID' => $query->RecordID, 'type' => 1], $RecordData);
  265. DB::connection('write')->table('QPAccountsDB.dbo.OrderWithDraw')
  266. ->where('OrderId', $query->OrderId)
  267. ->update($withdraw_data);
  268. if (isset($withdraw_data['State']) && $withdraw_data['State'] == 2) {
  269. // 单控标签
  270. StoredProcedure::user_label($UserID, 2, $TakeMoney);
  271. }
  272. return 'SUCCESS';
  273. } catch (\Exception $exception) {
  274. Util::WriteLog('SfPay', 'SfPay异步业务逻辑处理失败:' . $exception->getMessage());
  275. return 'SUCCESS';
  276. }
  277. }
  278. }