2
0

PagYeepPayCashierLogic.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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\PagYeepPay;
  10. use App\Services\PayConfig;
  11. use App\Services\PayUtils;
  12. use App\Services\StoredProcedure;
  13. use App\Util;
  14. use Illuminate\Support\Facades\DB;
  15. use Illuminate\Support\Facades\Log;
  16. use Illuminate\Support\Facades\Redis;
  17. class PagYeepPayCashierLogic implements CashierInterFace
  18. {
  19. const AGENT = 103; // PagYeepPay代付渠道值
  20. protected $agent = 103;
  21. public function payment($RecordID, $amount, $accountName, $phone, $email, $OrderId, $PixNum, $PixType, $IFSCNumber, $BranchBank, $BankNO)
  22. {
  23. // 查询订单号
  24. $query = DB::connection('write')->table('QPAccountsDB.dbo.OrderWithDraw')->where('RecordID', $RecordID)->first();
  25. if (!$query) return 'fail'; // 订单不存在
  26. $dao = new AccountPayInfo();
  27. [$userPhone, $userName, $userEmail] = $dao->payInfo($query->UserID);
  28. // 使用PagYeepPay服务类进行签名(PagYeepPayOut配置)
  29. $service = new PagYeepPay();
  30. $payConfigService = new PayConfig();
  31. $config = $payConfigService->getConfig('PagYeepPayOut');
  32. $service->config = $config;
  33. $service->key = $config['key'] ?? '';
  34. $service->orgNo = $config['orgNo'] ?? '';
  35. $service->custId = $config['custId'] ?? '';
  36. // PagYeepPay支付方式映射(根据文档:CASH / CASH_APP / APPLE_PAY)
  37. $payOutTypes = [
  38. 1 => 'ecashapp', // CashApp
  39. 2 => 'paypal' // PayPal
  40. ];
  41. // 根据PixType选择账号和代付方式
  42. // PixType: 1=CashApp, 2=PayPal
  43. $account = '';
  44. $payOutType = $payOutTypes[$PixType] ?? 'ecashapp';
  45. if ($PixType == 1) {
  46. if ($PixNum && strpos($PixNum, '$') !== 0) {
  47. $PixNum = '$' . $PixNum;
  48. }
  49. $account = $PixNum; // CashApp标签,如: $abcd1234
  50. } elseif ($PixType == 2) {
  51. $account = $email; // PayPal邮箱,如: 1111@gmail.com
  52. }
  53. // 构建提现请求参数(根据文档)
  54. $params = [
  55. "version" => $config['version'] ?? "2.1", // 版本号
  56. "orgNo" => $service->orgNo, // 机构编号
  57. "custId" => $service->custId, // 商户编号
  58. "custOrdNo" => $OrderId, // 商户代付订单号
  59. "country" => "US", // 国家
  60. "currency" => "USD", // 货币
  61. "casAmt" => (int) $amount, // 代付金额(分)
  62. "callBackUrl" => $config['callBackUrl'],
  63. "payoutType" => 'EPS', // 代付方式
  64. "accountName" => $accountName ?: 'U'.$query->UserID, // 收款人姓名
  65. "cardType" => $payOutType,
  66. "cardNo" => $account,
  67. "phone" => $userPhone,
  68. "email" => $userEmail,
  69. ];
  70. // 如果有回调地址配置
  71. if (!empty($config['callBackUrl'])) {
  72. $params["callBackUrl"] = $config['callBackUrl'];
  73. }
  74. // 使用PagYeepPay的签名方法
  75. $signedParams = $service->sign($params);
  76. $url = $config['apiUrl'].'/establish/consumption.ac';
  77. Util::WriteLog('PagYeepPay_payout', 'PagYeepPay提现请求: ' . $url . ' | 参数: ' . json_encode($signedParams));
  78. try {
  79. // 使用独立的 curlPost 方法发送请求
  80. $result = $service->curlPost($url, $signedParams);
  81. } catch (\Exception $exception) {
  82. Util::WriteLog('PagYeepPay_payout_error', '提现请求异常:' . $exception->getMessage());
  83. return 'fail';
  84. }
  85. Util::WriteLog('PagYeepPay_payout', 'PagYeepPay提现结果: ' . ($result ?? "no result"));
  86. try {
  87. // 解析返回结果(可能是form表单格式或JSON格式)
  88. parse_str($result, $data);
  89. if (empty($data)) {
  90. $data = \GuzzleHttp\json_decode($result, true);
  91. }
  92. } catch (\Exception $e) {
  93. Util::WriteLog("PagYeepPay_payout_error", ['解析失败', $result, $e->getMessage()]);
  94. return 'fail';
  95. }
  96. // 根据文档,返回码000000表示成功
  97. if (isset($data['code']) && $data['code'] == '000000') {
  98. Util::WriteLog('PagYeepPay_payout', "提现订单创建成功: {$OrderId}");
  99. return $data;
  100. } else {
  101. // 提现失败处理
  102. Util::WriteLog('PagYeepPay_payout_error', "提现失败: {$OrderId} | " . json_encode($data));
  103. if ($query->State == 5) {
  104. // 同步失败,发送邮件给玩家,退还金币
  105. $msg = $data['msg'] ?? 'Transfer failed';
  106. $WithDraw = $query->WithDraw + $query->ServiceFee;
  107. $bonus = '30000,' . $WithDraw;
  108. PrivateMail::failMail($query->UserID, $OrderId, $WithDraw, $msg, $bonus);
  109. $withdraw_data = [
  110. 'State' => 6,
  111. 'agent' => $this->agent,
  112. 'finishDate' => now(),
  113. 'remark' => json_encode($data)
  114. ];
  115. DB::connection('write')->table('QPAccountsDB.dbo.OrderWithDraw')
  116. ->where('OrderId', $query->OrderId)
  117. ->update($withdraw_data);
  118. $RecordData = ['after_state' => 6, 'update_at' => now()];
  119. DB::connection('write')->table('QPAccountsDB.dbo.AccountsRecord')
  120. ->where('type', 1)
  121. ->where('RecordID', $RecordID)
  122. ->update($RecordData);
  123. }
  124. return 'fail';
  125. }
  126. }
  127. public function notify($post)
  128. {
  129. if (!is_array($post)) {
  130. // 可能是form表单格式
  131. parse_str($post, $post);
  132. if (empty($post)) {
  133. $post = \GuzzleHttp\json_decode($post, true);
  134. }
  135. }
  136. Util::WriteLog('PagYeepPay', 'PagYeepPay 提现回调:' . json_encode($post));
  137. try {
  138. // 根据文档,回调参数:custOrderNo, prdOrdNo, payAmt, ordStatus, casDesc, utr, sign
  139. $OrderId = $post['custOrderNo'] ?? '';
  140. $query = DB::connection('write')->table('QPAccountsDB.dbo.OrderWithDraw')->where('OrderId', $OrderId)->first();
  141. if (!$query) {
  142. Util::WriteLog('PagYeepPay_payout','提现订单不存在: ' . $OrderId);
  143. return 'SC000000';
  144. }
  145. if ($query->State != 5 && $query->State != 7) {
  146. Util::WriteLog('PagYeepPay_payout', $OrderId . '_提现订单状态已完成');
  147. return 'SC000000';
  148. }
  149. $agentID = DB::connection('write')->table('agent.dbo.admin_configs')
  150. ->where('config_value', self::AGENT)
  151. ->where('type', 'cash')
  152. ->select('id')
  153. ->first()->id ?? '';
  154. $now = now();
  155. $notify_data = [
  156. 'state' => 1,
  157. 'finish_at' => $now,
  158. 'casOrdNo' => $post['prdOrdNo'] ?? '', // 平台代付订单号
  159. 'extra' => \GuzzleHttp\json_encode($post),
  160. 'created_at' => $now,
  161. 'updated_at' => $now,
  162. 'order_sn' => $OrderId,
  163. 'amount' => $query->WithDraw,
  164. ];
  165. // 根据文档,代付状态:07=清算完成(成功), 08=清算失败
  166. $orderStatus = 0;
  167. $ordStatus = $post['ordStatus'] ?? '';
  168. if ($ordStatus == '07') {
  169. $orderStatus = 1; // 提现成功
  170. } elseif ($ordStatus == '08') {
  171. $orderStatus = 2; // 提现失败
  172. }
  173. if (!$orderStatus) {
  174. Util::WriteLog('PagYeepPay_payout', 'PagYeepPay提现处理中:' . $OrderId . ', 状态: ' . $ordStatus);
  175. return 'SC000000';
  176. }
  177. Util::WriteLog('PagYeepPay_payout', 'PagYeepPay提现回调:' . $OrderId . ', 状态: ' . $ordStatus . ', 处理结果: ' . $orderStatus);
  178. $UserID = $query->UserID;
  179. $TakeMoney = $query->WithDraw + $query->ServiceFee;
  180. switch ($orderStatus) {
  181. case 1: // 提现成功
  182. Util::WriteLog('PagYeepPay', 'PagYeepPay提现成功');
  183. $withdraw_data = [
  184. 'State' => 2,
  185. 'agent' => $agentID,
  186. 'finishDate' => $now
  187. ];
  188. // 增加提现记录
  189. $first = DB::connection('write')->table('QPAccountsDB.dbo.UserTabData')->where('UserID', $UserID)->first();
  190. if ($first) {
  191. DB::connection('write')->table('QPAccountsDB.dbo.UserTabData')->where('UserID', $UserID)->increment('TakeMoney', $TakeMoney);
  192. } else {
  193. DB::connection('write')->table('QPAccountsDB.dbo.UserTabData')->insert(['TakeMoney' => $TakeMoney, 'UserID' => $UserID]);
  194. try {
  195. PrivateMail::praiseSendMail($UserID);
  196. } catch (\Exception $e) {
  197. // 忽略邮件发送失败
  198. }
  199. }
  200. // 免审的时候,修改免审状态
  201. $withdrawal_position_log = DB::connection('write')->table('agent.dbo.withdrawal_position_log')->where('order_sn', $OrderId)->first();
  202. if ($withdrawal_position_log) {
  203. 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')]);
  204. }
  205. try {
  206. StoredProcedure::addPlatformData($UserID, 4, $TakeMoney);
  207. } catch (\Exception $exception) {
  208. Util::WriteLog('StoredProcedure', $exception);
  209. }
  210. $ServiceFee = $query->ServiceFee;
  211. // 增加用户提现值
  212. RecordUserDataStatistics::updateOrAdd($UserID, $TakeMoney, 0, $ServiceFee);
  213. // 数据统计后台 -- 提现记录添加
  214. (new RechargeWithDraw())->withDraw($UserID, $TakeMoney);
  215. $redis = Redis::connection();
  216. $redis->incr('draw_' . date('Ymd') . $UserID);
  217. break;
  218. case 2: // 提现失败
  219. $msg = $post['casDesc'] ?? 'Encomenda rejeitada pelo banco';
  220. $bonus = '30000,' . $TakeMoney;
  221. PrivateMail::failMail($query->UserID, $OrderId, $TakeMoney, $msg, $bonus);
  222. Util::WriteLog('PagYeepPayEmail', [$query->UserID, $OrderId, $TakeMoney, $msg, $bonus]);
  223. $withdraw_data = ['State' => 6, 'agent' => $agentID, 'remark' => $msg];
  224. $notify_data = ['state' => 2];
  225. break;
  226. }
  227. $RecordData = [
  228. 'before_state' => $query->State,
  229. 'after_state' => $withdraw_data['State'] ?? 0,
  230. 'RecordID' => $query->RecordID,
  231. 'update_at' => date('Y-m-d H:i:s')
  232. ];
  233. // 添加用户提现操作记录
  234. DB::connection('write')->table('QPAccountsDB.dbo.AccountsRecord')->updateOrInsert(['RecordID' => $query->RecordID, 'type' => 1], $RecordData);
  235. DB::connection('write')->table('QPAccountsDB.dbo.OrderWithDraw')->where('OrderId', $query->OrderId)->update($withdraw_data);
  236. if (isset($withdraw_data['State']) && $withdraw_data['State'] == 2) {
  237. // 单控标签
  238. StoredProcedure::user_label($UserID, 2, $TakeMoney);
  239. }
  240. Util::WriteLog('PagYeepPay_payout', "提现回调处理完成: {$OrderId}");
  241. return 'SC000000';
  242. } catch (\Exception $exception) {
  243. Util::WriteLog('PagYeepPay_payout_error', 'PagYeepPay提现回调处理失败:' . $exception->getMessage() . "\n" . $exception->getTraceAsString());
  244. return 'SC000000';
  245. }
  246. }
  247. }