PayPlusCashierLogic.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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\PayPlus;
  9. use App\Services\StoredProcedure;
  10. use App\Util;
  11. use Illuminate\Support\Facades\DB;
  12. use Illuminate\Support\Facades\Redis;
  13. class PayPlusCashierLogic implements CashierInterFace
  14. {
  15. const AGENT = 109;
  16. protected $service;
  17. public function __construct(PayPlus $service = null)
  18. {
  19. $this->service = $service ?: (new PayPlus())->getPayoutService();
  20. }
  21. public function payment(
  22. $RecordID,
  23. $amount,
  24. $accountName,
  25. $phone,
  26. $email,
  27. $OrderId,
  28. $PixNum,
  29. $PixType,
  30. $IFSCNumber,
  31. $BranchBank,
  32. $BankNO
  33. ) {
  34. $query = DB::connection('write')
  35. ->table('QPAccountsDB.dbo.OrderWithDraw')
  36. ->where('RecordID', $RecordID)
  37. ->first();
  38. if (!$query) {
  39. return 'fail';
  40. }
  41. $result = $this->createBeneficiaryAndPayout([
  42. 'user_id' => $query->UserID,
  43. 'amount' => round($amount / NumConfig::NUM_VALUE, 2),
  44. 'currency' => $this->service->getConfig()['currency'] ?? 'USD',
  45. 'order_id' => $OrderId,
  46. 'pix_type' => $PixType,
  47. 'cashapp_account' => $PixNum,
  48. 'email' => $email,
  49. 'phone' => $phone,
  50. 'name' => $accountName,
  51. ]);
  52. if ($result && isset($result['code']) && (int) $result['code'] === 200) {
  53. return $result;
  54. }
  55. return 'fail';
  56. }
  57. public function createBeneficiaryAndPayout(array $input)
  58. {
  59. $beneficiary = $this->resolveBeneficiary($input);
  60. if (!$beneficiary) {
  61. return false;
  62. }
  63. $payload = [
  64. 'amount' => (float) $input['amount'],
  65. 'currency' => strtoupper($input['currency'] ?? ($this->service->getConfig()['currency'] ?? 'USD')),
  66. 'reference_id' => (string) $input['order_id'],
  67. 'beneficiary_id' => (int) $beneficiary['beneficiary_id'],
  68. ];
  69. if ((int) ($input['pix_type'] ?? 1) === 2) {
  70. $payload['paypal_email'] = $input['email'] ?: $beneficiary['email'];
  71. return $this->service->postPayout('/rest/v2/payouts/paypal', $payload);
  72. }
  73. $cashAppAccount = (string) ($input['cashapp_account'] ?? '');
  74. if ($cashAppAccount !== '' && strpos($cashAppAccount, '$') !== 0) {
  75. $cashAppAccount = '$' . $cashAppAccount;
  76. }
  77. $payload['cashapp_account'] = $cashAppAccount ?: '$unknown';
  78. return $this->service->postPayout('/rest/v2/payouts/cashApp', $payload);
  79. }
  80. public function resolveBeneficiary(array $input)
  81. {
  82. $payload = $this->service->buildBeneficiaryPayload($input);
  83. $created = $this->service->postPayout('/rest/v2/beneficiaries/create', $payload);
  84. if ((int) ($created['code'] ?? 0) === 200 && !empty($created['data']['beneficiary_id'])) {
  85. return $created['data'];
  86. }
  87. if ((int) ($created['code'] ?? 0) !== 4002001) {
  88. Util::WriteLog('PayPlus_error', 'beneficiary create failed: ' . json_encode($created));
  89. return false;
  90. }
  91. $query = $this->service->postPayout('/rest/v2/beneficiaries/query', [
  92. 'payee_id' => $payload['payee_id'],
  93. ]);
  94. if ((int) ($query['code'] ?? 0) === 200 && !empty($query['data']['beneficiary_id'])) {
  95. return $query['data'];
  96. }
  97. Util::WriteLog('PayPlus_error', 'beneficiary query failed: ' . json_encode($query));
  98. return false;
  99. }
  100. public function notify($post)
  101. {
  102. if (!is_array($post)) {
  103. $post = json_decode($post, true) ?: [];
  104. }
  105. $orderId = $post['reference_id'] ?? '';
  106. if ($orderId === '') {
  107. return 'success';
  108. }
  109. $query = DB::connection('write')
  110. ->table('QPAccountsDB.dbo.OrderWithDraw')
  111. ->where('OrderId', $orderId)
  112. ->first();
  113. if (!$query || ($query->State != 5 && $query->State != 7)) {
  114. return 'success';
  115. }
  116. $orderStatus = $this->resolvePayoutStatus($post['status'] ?? '');
  117. if (!$orderStatus) {
  118. return 'success';
  119. }
  120. $agent = DB::connection('write')->table('agent.dbo.admin_configs')
  121. ->where('config_value', self::AGENT)
  122. ->where('type', 'cash')
  123. ->select('id')
  124. ->first();
  125. $agentId = $agent->id ?? '';
  126. $now = now();
  127. $userId = $query->UserID;
  128. $takeMoney = $query->WithDraw + $query->ServiceFee;
  129. $withdrawData = [
  130. 'agent' => $agentId,
  131. 'finishDate' => $now,
  132. 'remark' => json_encode($post, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),
  133. ];
  134. if ($orderStatus === 1) {
  135. $withdrawData['State'] = 2;
  136. $this->handleSuccess($query, $takeMoney);
  137. } else {
  138. $withdrawData['State'] = 6;
  139. PrivateMail::failMail($userId, $orderId, $takeMoney, $post['msg'] ?? 'REJECTED', '30000,' . $takeMoney);
  140. }
  141. DB::connection('write')->table('QPAccountsDB.dbo.AccountsRecord')->updateOrInsert(
  142. ['RecordID' => $query->RecordID, 'type' => 1],
  143. [
  144. 'before_state' => $query->State,
  145. 'after_state' => $withdrawData['State'],
  146. 'RecordID' => $query->RecordID,
  147. 'update_at' => date('Y-m-d H:i:s'),
  148. ]
  149. );
  150. DB::connection('write')
  151. ->table('QPAccountsDB.dbo.OrderWithDraw')
  152. ->where('OrderId', $query->OrderId)
  153. ->update($withdrawData);
  154. return 'success';
  155. }
  156. public function resolvePayoutStatus($status)
  157. {
  158. switch (strtoupper((string) $status)) {
  159. case 'PAID':
  160. return 1;
  161. case 'REJECTED':
  162. case 'REFUNDED':
  163. return 2;
  164. default:
  165. return 0;
  166. }
  167. }
  168. protected function handleSuccess($query, $takeMoney)
  169. {
  170. $userId = $query->UserID;
  171. $first = DB::connection('write')
  172. ->table('QPAccountsDB.dbo.UserTabData')
  173. ->where('UserID', $userId)
  174. ->first();
  175. if ($first) {
  176. DB::connection('write')
  177. ->table('QPAccountsDB.dbo.UserTabData')
  178. ->where('UserID', $userId)
  179. ->increment('TakeMoney', $takeMoney);
  180. } else {
  181. DB::connection('write')
  182. ->table('QPAccountsDB.dbo.UserTabData')
  183. ->insert(['TakeMoney' => $takeMoney, 'UserID' => $userId]);
  184. PrivateMail::praiseSendMail($userId);
  185. }
  186. try {
  187. StoredProcedure::addPlatformData($userId, 4, $takeMoney);
  188. } catch (\Exception $exception) {
  189. Util::WriteLog('StoredProcedure', $exception);
  190. }
  191. RecordUserDataStatistics::updateOrAdd($userId, $takeMoney, 0, $query->ServiceFee);
  192. (new RechargeWithDraw())->withDraw($userId, $takeMoney, 0, $query->ServiceFee);
  193. Redis::connection()->incr('draw_' . date('Ymd') . $userId);
  194. PrivateMail::successMail($userId, $query->OrderId, $takeMoney);
  195. StoredProcedure::user_label($userId, 2, $takeMoney);
  196. }
  197. }