WiwiPayCashierLogic.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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\WiwiPay;
  9. use App\Services\PayConfig;
  10. use App\Services\PayUtils;
  11. use App\Services\StoredProcedure;
  12. use App\Util;
  13. use Illuminate\Support\Facades\DB;
  14. use Illuminate\Support\Facades\Log;
  15. use Illuminate\Support\Facades\Redis;
  16. class WiwiPayCashierLogic implements CashierInterFace
  17. {
  18. const AGENT = 99; // 需要根据实际情况修改
  19. protected $agent = 99;
  20. public function payment($RecordID, $amount, $accountName, $phone, $email, $OrderId, $PixNum, $PixType, $IFSCNumber, $BranchBank, $BankNO)
  21. {
  22. // 查询订单号
  23. $query = DB::connection('write')->table('QPAccountsDB.dbo.OrderWithDraw')->where('RecordID', $RecordID)->first();
  24. if (!$query) return 'fail'; // 订单不存在
  25. $payConfigService = new PayConfig();
  26. $config = $payConfigService->getConfig('WiwiPayOut');
  27. $wayCode = [
  28. 1 => 'ecashapp',
  29. 2 => 'paypal'
  30. ];
  31. $wayParam = [];
  32. if($PixType == 1){
  33. $wayParam = ["cashtag" => '$'.$PixNum];
  34. }
  35. if($PixType == 2){
  36. $wayParam = ["email" => $email];
  37. }
  38. // 构建提现请求参数
  39. $params = [
  40. "mchNo" => $config['mchNo'] ?? '',
  41. "mchOrderNo" => $OrderId,
  42. "amount" => intval($amount),
  43. "currency" => $config['currency'] ?? "usd",
  44. "wayCode" => $wayCode[$PixType],
  45. "notifyUrl" => $config['cashNotify'],
  46. "wayParam" => $wayParam,
  47. "timestamp" => round(microtime(true) * 1000),
  48. "signType" => $config['signType'] ?? "MD5"
  49. ];
  50. // 直接使用 PayUtils 签名(使用 WiwiPayOut 的 key)
  51. $apiKey = $config['key'];
  52. $signedParams = PayUtils::sign($params, $apiKey);
  53. $url = $config['apiUrl'];
  54. Log::info('WiwiPay 提现参数:', $signedParams);
  55. try {
  56. // 使用独立的 curlPost 方法发送请求
  57. $result = $this->curlPost($url, $signedParams);
  58. } catch (\Exception $exception) {
  59. Log::info('WiwiPay 提现请求异常:', [$exception->getMessage()]);
  60. return 'fail';
  61. }
  62. Log::info('WiwiPay 提现结果:', [$result ?? "no result"]);
  63. try {
  64. $data = \GuzzleHttp\json_decode($result, true);
  65. } catch (\Exception $e) {
  66. Util::WriteLog("WiwiPay_error", [$result, $e->getMessage(), $e->getTraceAsString()]);
  67. return 'fail';
  68. }
  69. if (isset($data['code']) && $data['code'] == 0) {
  70. return $data;
  71. } else {
  72. if ($query->State == 5) {
  73. // 同步失败,发送邮件给玩家,退还金币
  74. $msg = 'Liquidation failure';
  75. $WithDraw = $query->WithDraw + $query->ServiceFee;
  76. $bonus = '30000,' . $WithDraw;
  77. PrivateMail::failMail($query->UserID, $OrderId, $WithDraw, $msg, $bonus);
  78. $withdraw_data = ['State' => 6, 'agent' => $this->agent, 'finishDate' => now(),'remark' => json_encode($data)];
  79. DB::connection('write')->table('QPAccountsDB.dbo.OrderWithDraw')->where('OrderId', $query->OrderId)->update($withdraw_data);
  80. $RecordData = ['after_state' => 6, 'update_at' => now()];
  81. DB::connection('write')->table('QPAccountsDB.dbo.AccountsRecord')->where('type', 1)->where('RecordID', $RecordID)->update($RecordData);
  82. }
  83. return 'fail';
  84. }
  85. }
  86. public function notify($post)
  87. {
  88. if (!is_array($post)) $post = \GuzzleHttp\json_decode($post, true);
  89. Util::WriteLog('WiwiPay', 'WiwiPay 提现回调:' . json_encode($post));
  90. try {
  91. // 判断订单是否存在
  92. $OrderId = $post['mchOrderNo'] ?? '';
  93. $query = DB::connection('write')->table('QPAccountsDB.dbo.OrderWithDraw')->where('OrderId', $OrderId)->first();
  94. if (!$query) {
  95. Util::WriteLog('WiwiPay','订单不存在');
  96. return '{"success":true,"message":"Accepted"}';
  97. }
  98. if ($query->State != 5 && $query->State != 7) {
  99. Util::WriteLog('WiwiPay',$OrderId.'_订单状态已完成');
  100. return 'SUCCESS';
  101. }
  102. $agentID = DB::connection('write')->table('agent.dbo.admin_configs')
  103. ->where('config_value', self::AGENT)
  104. ->where('type', 'cash')
  105. ->select('id')
  106. ->first()->id ?? '';
  107. $now = now();
  108. $notify_data = [
  109. 'state' => 1,
  110. 'finish_at' => $now,
  111. 'casOrdNo' => $post['mchOrderNo'] ?? '',
  112. 'extra' => \GuzzleHttp\json_encode($post),
  113. 'created_at' => $now,
  114. 'updated_at' => $now,
  115. 'order_sn' => $OrderId,
  116. 'amount' => $query->WithDraw,
  117. ];
  118. //state
  119. //0-订单生成
  120. //1-支付中
  121. //2-支付成功
  122. //3-支付失败
  123. //4-已撤销
  124. //5-已退款
  125. //6-订单关闭
  126. // 判断订单状态
  127. $orderStatus = 0;
  128. if (isset($post['state'])) {
  129. // state: 2=成功, 3=失败
  130. if ($post['state'] == 2) {
  131. $orderStatus = 1; // 成功
  132. } elseif ($post['state'] == 3) {
  133. $orderStatus = 2; // 失败
  134. }
  135. }
  136. if (!$orderStatus) {
  137. Util::WriteLog('WiwiPay', 'WiwiPay 提现处理中:' . $OrderId);
  138. return 'success';
  139. }
  140. Util::WriteLog('WiwiPay', 'WiwiPay 提现结果:' . $OrderId . '_' . $orderStatus);
  141. $UserID = $query->UserID;
  142. $TakeMoney = $query->WithDraw + $query->ServiceFee;
  143. switch ($orderStatus) {
  144. case 1: // 提现成功
  145. Util::WriteLog('WiwiPay', 'WiwiPay提现成功');
  146. $withdraw_data = [
  147. 'State' => 2,
  148. 'agent' => $agentID,
  149. 'finishDate' => $now
  150. ];
  151. // 增加提现记录
  152. $first = DB::connection('write')->table('QPAccountsDB.dbo.UserTabData')->where('UserID', $UserID)->first();
  153. if ($first) {
  154. DB::connection('write')->table('QPAccountsDB.dbo.UserTabData')->where('UserID', $UserID)->increment('TakeMoney', $TakeMoney);
  155. } else {
  156. DB::connection('write')->table('QPAccountsDB.dbo.UserTabData')->insert(['TakeMoney' => $TakeMoney, 'UserID' => $UserID]);
  157. try {
  158. PrivateMail::praiseSendMail($UserID);
  159. } catch (\Exception $e) {
  160. // 忽略邮件发送失败
  161. }
  162. }
  163. // 免审的时候,修改免审状态
  164. $withdrawal_position_log = DB::connection('write')->table('agent.dbo.withdrawal_position_log')->where('order_sn', $OrderId)->first();
  165. if ($withdrawal_position_log) {
  166. DB::connection('write')->table('agent.dbo.withdrawal_position_log')->where('order_sn', $OrderId)->update(['take_effect' => 2, 'update_at' => date('Y-m-d H:i:s')]);
  167. }
  168. try {
  169. StoredProcedure::addPlatformData($UserID, 4, $TakeMoney);
  170. } catch (\Exception $exception) {
  171. Util::WriteLog('StoredProcedure', $exception);
  172. }
  173. $ServiceFee = $query->ServiceFee;
  174. // 增加用户提现值
  175. RecordUserDataStatistics::updateOrAdd($UserID, $TakeMoney, 0, $ServiceFee);
  176. // 数据统计后台 -- 提现记录添加
  177. (new RechargeWithDraw())->withDraw($UserID, $TakeMoney);
  178. $redis = Redis::connection();
  179. $redis->incr('draw_' . date('Ymd') . $UserID);
  180. break;
  181. case 2: // 提现失败
  182. $msg = 'Encomenda rejeitada pelo banco';
  183. $bonus = '30000,' . $TakeMoney;
  184. PrivateMail::failMail($query->UserID, $OrderId, $TakeMoney, $msg, $bonus);
  185. Util::WriteLog('WiwiPayEmail', [$query->UserID, $OrderId, $TakeMoney, $msg, $bonus]);
  186. $withdraw_data = ['State' => 6, 'agent' => $agentID, 'remark' => @$post['errMsg'] ?: ''];
  187. $notify_data = ['state' => 2];
  188. break;
  189. }
  190. $RecordData = [
  191. 'before_state' => $query->State,
  192. 'after_state' => $withdraw_data['State'] ?? 0,
  193. 'RecordID' => $query->RecordID,
  194. 'update_at' => date('Y-m-d H:i:s')
  195. ];
  196. // 添加用户提现操作记录
  197. DB::connection('write')->table('QPAccountsDB.dbo.AccountsRecord')->updateOrInsert(['RecordID' => $query->RecordID, 'type' => 1], $RecordData);
  198. // DB::connection('write')->table('QPAccountsDB.dbo.withdraw_notify')->updateOrInsert(['order_sn' => $OrderId], $notify_data);
  199. DB::connection('write')->table('QPAccountsDB.dbo.OrderWithDraw')->where('OrderId', $query->OrderId)->update($withdraw_data);
  200. if (isset($withdraw_data['State']) && $withdraw_data['State'] == 2) {
  201. // 单控标签
  202. StoredProcedure::user_label($UserID, 2, $TakeMoney);
  203. }
  204. return 'success';
  205. } catch (\Exception $exception) {
  206. Util::WriteLog('WiwiPay', 'WiwiPay异步业务逻辑处理失败:' . $exception->getMessage());
  207. return '{"success":false,"message":"商户自定义出错信息"}';
  208. }
  209. }
  210. /**
  211. * POST请求方法(复用 WiwiPay 的实现)
  212. */
  213. private function curlPost($url, $payload)
  214. {
  215. $timeout = 20;
  216. $data = json_encode($payload, JSON_UNESCAPED_UNICODE);
  217. $headers = [
  218. 'Content-Type: application/json',
  219. ];
  220. $ch = curl_init();
  221. curl_setopt($ch, CURLOPT_URL, $url);
  222. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  223. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  224. curl_setopt($ch, CURLOPT_POST, 1);
  225. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  226. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  227. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  228. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  229. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  230. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  231. $result = curl_exec($ch);
  232. if (curl_errno($ch)) {
  233. $error = curl_error($ch);
  234. Util::WriteLog('WiwiPay_error', 'CURL Error: ' . $error);
  235. curl_close($ch);
  236. return false;
  237. }
  238. if (strstr($result, 'code') || $httpCode != 200) {
  239. // 可选:记录错误日志
  240. }
  241. curl_close($ch);
  242. return $result;
  243. }
  244. }