AiPayCashierLogic.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <?php
  2. namespace App\Http\logic\api;
  3. use App\dao\Estatisticas\RechargeWithDraw;
  4. use App\dao\Pay\AccountPayInfo;
  5. use App\Http\helper\NumConfig;
  6. use App\Inter\CashierInterFace;
  7. use App\Models\PrivateMail;
  8. use App\Models\RecordUserDataStatistics;
  9. use App\Services\AiPay;
  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 AiPayCashierLogic implements CashierInterFace
  16. {
  17. const AGENT = 101;
  18. protected $agent = 101;
  19. public function payment($RecordID, $amount, $accountName, $phone, $email, $OrderId, $PixNum, $PixType, $IFSCNumber, $BranchBank, $BankNO)
  20. {
  21. $query = DB::connection('write')->table('QPAccountsDB.dbo.OrderWithDraw')->where('RecordID', $RecordID)->first();
  22. if (!$query) {
  23. return 'fail';
  24. }
  25. $service = new AiPay('AiPayOut');
  26. $config = $service->getConfig();
  27. if(!$accountName){
  28. $dao = new AccountPayInfo();
  29. $$accountName = $dao->randUserName($accountName);
  30. }
  31. $paymentMethod = $this->resolvePaymentMethod($PixType);
  32. $accountData = $this->buildAccountData($paymentMethod, $accountName, $PixNum, $email, $phone, $IFSCNumber, $BranchBank, $BankNO);
  33. if ($paymentMethod === null || empty($accountData)) {
  34. Util::WriteLog('AiPay', 'unsupported PixType for withdraw: ' . $PixType.'_'.$accountName);
  35. return 'fail';
  36. }
  37. $amountDecimal = number_format($amount / NumConfig::NUM_VALUE, 2, '.', '');
  38. $params = [
  39. 'mchNo' => $config['mchNo'] ?? '',
  40. 'mchOrderNo' => $OrderId,
  41. 'paymentMethod' => $paymentMethod,
  42. 'amount' => $amountDecimal,
  43. 'notifyUrl' => $config['cashNotify'] ?? '',
  44. 'timestamp' => (string)round(microtime(true) * 1000),
  45. 'accountData' => $accountData,
  46. ];
  47. $signedParams = $service->sign($params);
  48. $result = $service->post('/api/transfer', $signedParams);
  49. Log::info('AiPay withdraw request', $signedParams);
  50. Log::info('AiPay withdraw response', [$result]);
  51. try {
  52. $data = \GuzzleHttp\json_decode($result, true);
  53. } catch (\Throwable $e) {
  54. Util::WriteLog('AiPay_error', $e->getMessage());
  55. return 'fail';
  56. }
  57. if (isset($data['code']) && (int)$data['code'] === 0) {
  58. return $data;
  59. }
  60. return 'fail';
  61. }
  62. public function notify($post)
  63. {
  64. if (!is_array($post)) {
  65. $post = \GuzzleHttp\json_decode($post, true);
  66. }
  67. Util::WriteLog('AiPay', 'cash callback: ' . json_encode($post, JSON_UNESCAPED_UNICODE));
  68. $OrderId = $post['mchOrderNo'] ?? '';
  69. $query = DB::connection('write')->table('QPAccountsDB.dbo.OrderWithDraw')->where('OrderId', $OrderId)->first();
  70. if (!$query) {
  71. Util::WriteLog('AiPay', 'withdraw order not found: ' . $OrderId);
  72. return '{"success":true,"message":"order not found"}';
  73. }
  74. if (!in_array($query->State, [5, 7])) {
  75. Util::WriteLog('AiPay', $OrderId . ' already handled');
  76. return 'success';
  77. }
  78. $agentID = DB::connection('write')->table('agent.dbo.admin_configs')
  79. ->where('config_value', self::AGENT)
  80. ->where('type', 'cash')
  81. ->value('id') ?? '';
  82. $status = (int)($post['status'] ?? 0);
  83. $now = now();
  84. $withdraw_data = [];
  85. $notifyState = 0;
  86. $msg = $post['msg'] ?? '';
  87. $TakeMoney = $query->WithDraw + $query->ServiceFee;
  88. $UserID = $query->UserID;
  89. switch ($status) {
  90. case 3: // success
  91. $withdraw_data = [
  92. 'State' => 2,
  93. 'agent' => $agentID,
  94. 'finishDate' => $now,
  95. ];
  96. $this->handleSuccess($UserID, $TakeMoney, $OrderId, $query->ServiceFee);
  97. $notifyState = 1;
  98. break;
  99. case 4: // fail
  100. case 5: // reversal
  101. $msg = $msg ?: 'Withdraw rejected';
  102. $bonus = '30000,' . $TakeMoney;
  103. PrivateMail::failMail($UserID, $OrderId, $TakeMoney, $msg, $bonus);
  104. Util::WriteLog('AiPayEmail', [$UserID, $OrderId, $TakeMoney, $msg, $bonus]);
  105. $withdraw_data = ['State' => 6, 'agent' => $agentID, 'remark' => $msg];
  106. $notifyState = 2;
  107. break;
  108. default:
  109. Util::WriteLog('AiPay', 'cash notify pending: ' . $OrderId);
  110. return 'success';
  111. }
  112. $recordData = [
  113. 'before_state' => $query->State,
  114. 'after_state' => $withdraw_data['State'] ?? $query->State,
  115. 'RecordID' => $query->RecordID,
  116. 'update_at' => date('Y-m-d H:i:s'),
  117. ];
  118. DB::connection('write')->table('QPAccountsDB.dbo.AccountsRecord')
  119. ->updateOrInsert(['RecordID' => $query->RecordID, 'type' => 1], $recordData);
  120. DB::connection('write')->table('QPAccountsDB.dbo.OrderWithDraw')
  121. ->where('OrderId', $OrderId)
  122. ->update($withdraw_data);
  123. return 'success';
  124. }
  125. protected function resolvePaymentMethod($PixType): ?string
  126. {
  127. $map = [
  128. 1 => '1201', // CashApp
  129. 2 => '1202', // PayPal
  130. 3 => '1203', // Venmo
  131. 4 => '1204', // CARD
  132. ];
  133. return $map[$PixType] ?? null;
  134. }
  135. protected function buildAccountData(?string $paymentMethod, $accountName, $PixNum, $email, $phone, $IFSCNumber, $BranchBank, $BankNO): array
  136. {
  137. $splitName = $this->splitName($accountName);
  138. switch ($paymentMethod) {
  139. case '1201':
  140. $account = $PixNum ?: $phone;
  141. if ($account && strpos($account, '$') !== 0) {
  142. $account = '$' . $account;
  143. }
  144. return ['account' => $account];
  145. case '1202':
  146. case '1203':
  147. return [
  148. 'account' => $email ?: $PixNum,
  149. 'firstName' => $splitName['first'],
  150. 'lastName' => $splitName['last'],
  151. ];
  152. case '1204':
  153. return [
  154. 'cardNo' => $PixNum ?: $BankNO,
  155. 'cardExpireMonth' => $this->extractMonth($BranchBank),
  156. 'cardExpireYear' => $this->extractYear($BranchBank),
  157. 'cardSecurityCode' => substr($IFSCNumber ?? '', 0, 4),
  158. 'cardBrand' => 'visa',
  159. 'firstName' => $splitName['first'],
  160. 'lastName' => $splitName['last'],
  161. ];
  162. }
  163. return [];
  164. }
  165. // protected function splitName($name): array
  166. // {
  167. // $name = trim($name ?: 'User');
  168. // $parts = preg_split('/\s+/', $name, 2);
  169. // return [
  170. // 'first' => $parts[0] ?? 'User',
  171. // 'last' => $parts[1] ?? 'Edward',
  172. // ];
  173. // }
  174. protected function splitName(string $name = null): array
  175. {
  176. $name = trim($name ?? '');
  177. if ($name === '') $name = 'Willam Edson';
  178. // // 处理 "Last, First"
  179. // if (str_contains($name, ',')) {
  180. // $tmp = array_map('trim', explode(',', $name, 2));
  181. // $name = ($tmp[1] ?? '') . ' ' . ($tmp[0] ?? '');
  182. // $name = trim($name);
  183. // }
  184. // 清洗:保留字母、空格、-、'
  185. $name = preg_replace("/[^A-Za-z\\s\\-']/u", ' ', $name);
  186. $name = trim(preg_replace('/\s+/', ' ', $name));
  187. $parts = explode(' ', $name, 2);
  188. return [
  189. 'first' => $parts[0] ?: 'Willam',
  190. 'last' => $parts[1] ?? 'Edward', // 或 'Edward' / 'User'
  191. ];
  192. }
  193. protected function extractMonth($value): string
  194. {
  195. if (preg_match('/(\d{2})/', (string)$value, $match)) {
  196. return $match[1];
  197. }
  198. return '01';
  199. }
  200. protected function extractYear($value): string
  201. {
  202. if (preg_match('/(\d{2,4})/', (string)$value, $match)) {
  203. return substr($match[1], -2);
  204. }
  205. return '25';
  206. }
  207. protected function handleSuccess($UserID, $TakeMoney, $OrderId, $serviceFee): void
  208. {
  209. $first = DB::connection('write')->table('QPAccountsDB.dbo.UserTabData')->where('UserID', $UserID)->first();
  210. if ($first) {
  211. DB::connection('write')->table('QPAccountsDB.dbo.UserTabData')->where('UserID', $UserID)->increment('TakeMoney', $TakeMoney);
  212. } else {
  213. DB::connection('write')->table('QPAccountsDB.dbo.UserTabData')->insert(['TakeMoney' => $TakeMoney, 'UserID' => $UserID]);
  214. try {
  215. PrivateMail::praiseSendMail($UserID);
  216. } catch (\Throwable $e) {
  217. // ignore mail failure
  218. }
  219. }
  220. try {
  221. StoredProcedure::addPlatformData($UserID, 4, $TakeMoney);
  222. } catch (\Throwable $exception) {
  223. Util::WriteLog('StoredProcedure', $exception->getMessage());
  224. }
  225. $withdrawal_position_log = DB::connection('write')->table('agent.dbo.withdrawal_position_log')->where('order_sn', $OrderId)->first();
  226. if ($withdrawal_position_log) {
  227. DB::connection('write')->table('agent.dbo.withdrawal_position_log')
  228. ->where('order_sn', $OrderId)
  229. ->update(['take_effect' => 2, 'update_at' => date('Y-m-d H:i:s')]);
  230. }
  231. RecordUserDataStatistics::updateOrAdd($UserID, $TakeMoney, 0, $serviceFee);
  232. (new RechargeWithDraw())->withDraw($UserID, $TakeMoney);
  233. $redis = Redis::connection();
  234. $redis->incr('draw_' . date('Ymd') . $UserID);
  235. StoredProcedure::user_label($UserID, 2, $TakeMoney);
  236. }
  237. }