PaymentService.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: tandunzhao
  5. * Date: 2017/12/2
  6. * Time: 下午7:05
  7. */
  8. namespace App\Modules\Finance\Services;
  9. use App\Libs\PayHelper;
  10. use App\Modules\Finance\Models\FinanceMerchantStat;
  11. use App\Modules\Finance\Models\LiquidatedStat;
  12. use App\Modules\Finance\Models\Payment;
  13. use App\Modules\Finance\Models\WithdrawCash;
  14. use DB;
  15. use Log;
  16. class PaymentService
  17. {
  18. /**
  19. * 创建第三方支付渠道打款
  20. * @param $userId
  21. * @param $withdraw_cash_id
  22. * @param $amount
  23. * @param $remark
  24. * @param $liquidatedStat
  25. * @return bool
  26. */
  27. public static function makeThreeSourcePayment($userId, $withdraw_cash_id, $amount, $remark, LiquidatedStat $liquidatedStat) {
  28. if(empty($liquidatedStat)) {
  29. return false;
  30. }
  31. $withdrawCash = WithdrawCashService::getWithdrawCash($withdraw_cash_id);
  32. if(empty($withdrawCash)) {
  33. return false;
  34. }
  35. $payType = '';
  36. if($liquidatedStat['pay_merchant_source'] == config('common.tonglianpay')) {
  37. $payType = "ALLINPAY";
  38. }
  39. if($liquidatedStat['pay_merchant_source'] == config('common.lianlianpay')) {
  40. $payType = "LIANLIANPAY";
  41. }
  42. if($payType) {
  43. try {
  44. $channelId = $withdrawCash['distribution_channel_id'];
  45. $transactionSerialNumber = date('YmdHis')."_".$channelId;
  46. $cashAccount = CashAccountService::getCashAccountSingle($channelId);
  47. //$bank = BankService::getBankSingle($cashAccount['bank_id']);
  48. $bank = BankService::getBankSingleByNameSource($cashAccount['bank'], $payType);
  49. $bankCode = '';
  50. if(!empty($bank)) {
  51. $bankCode = $bank['code'];
  52. }
  53. $bankName = $cashAccount['account_bank'];
  54. $bankAccountNO = $cashAccount['card_number'];
  55. $bankAccountName = $cashAccount['account_name'];
  56. $payAmount = $amount;
  57. $summaryPay = "summary";
  58. $remarkPay = "remark";
  59. $withdrawCash = WithdrawCashService::getWithdrawCash($withdraw_cash_id);
  60. //当前当条提现是对公还是对私
  61. $isCompany = $withdrawCash['is_company'];
  62. DB::beginTransaction();
  63. try{
  64. $savePayment = PaymentService::addPayment($withdraw_cash_id, $amount, $remark, $liquidatedStat['pay_merchant_source'], $transactionSerialNumber, WithdrawCashService::getWithdrawCashStatusStr(21), "", $isCompany, 0);
  65. if($savePayment) {
  66. //更新单个第三方通道可打款金额
  67. FinanceMerchantStatService::createOrUpdate($channelId, $liquidatedStat['pay_merchant_source'], -$amount);
  68. //更新当前渠道当天当前支付通道可打款余额
  69. LiquidatedStatService::updateAccountBalanceAmountDay($liquidatedStat['pay_merchant_source'], -$amount);
  70. //修改状态打款中
  71. WithdrawCashService::updateWithdrawCashStatus($withdraw_cash_id, $userId, 21, $remark, $transactionSerialNumber);
  72. $resultPayData = PayHelper::pay($payType, $transactionSerialNumber, $bankCode, $bankName, $bankAccountNO, $bankAccountName, $payAmount, $summaryPay, $remarkPay, $channelId, $isCompany);
  73. \Log::info('lianlianpay_ret:'.$transactionSerialNumber.':'.json_encode($resultPayData));
  74. $resultDataMsg = $remark;
  75. if(!empty($resultPayData) && !empty($resultPayData['msg'])) {
  76. $resultDataMsg = $resultDataMsg."_【".$resultPayData['msg']."】";
  77. }
  78. if(!empty($resultPayData) && $resultPayData['code'] == 1) {
  79. $resultDataHttpDataMsg = json_encode($resultPayData['http_result_data']);
  80. PaymentService::updatePaymentStatus($savePayment['id'], '', $resultDataHttpDataMsg, $resultDataMsg);
  81. } else {
  82. $resultDataHttpDataMsg = json_encode($resultPayData['http_result_data']);
  83. WithdrawCashService::updateWithdrawCashStatus($withdraw_cash_id, $userId, 41, $resultDataMsg, $transactionSerialNumber);
  84. PaymentService::updatePaymentStatus($savePayment['id'], WithdrawCashService::getWithdrawCashStatusStr(41), $resultDataHttpDataMsg, $resultDataMsg);
  85. }
  86. }
  87. DB::commit();
  88. } catch (\Exception $e){
  89. Log::error($e->getMessage());
  90. DB::rollback();
  91. //WithdrawCashService::updateWithdrawCashStatus($withdraw_cash_id, $userId, -1, $remark, $transactionSerialNumber);
  92. }
  93. } catch (\Exception $e){
  94. //WithdrawCashService::updateWithdrawCashStatus($withdraw_cash_id, $userId, -1, $e->getMessage(), '');
  95. }
  96. return true;
  97. } else {
  98. return false;
  99. }
  100. }
  101. /**
  102. * 创建人工打款
  103. * @param $userId
  104. * @param $withdrawCashId
  105. * @param $amount
  106. * @param $remark
  107. * @param $tradeNo
  108. * @param int $amount_person
  109. * @return bool
  110. */
  111. public static function makePersonMadePayment($userId, $withdrawCashId, $amount, $remark, $tradeNo, $amount_person = 0) {
  112. $withdrawCash = WithdrawCashService::getWithdrawCash($withdrawCashId);
  113. if(empty($withdrawCash)) {
  114. return false;
  115. }
  116. $channelId = $withdrawCash['distribution_channel_id'];
  117. $isCompany = $withdrawCash['is_company'];
  118. $isCompanyName = "";
  119. if($isCompany == 0) {
  120. $isCompanyName = "对私";
  121. } else if($isCompany == 1) {
  122. $isCompanyName = "对公";
  123. }
  124. $remark = "[".$isCompanyName."]".$remark;
  125. DB::beginTransaction();
  126. try {
  127. //插入人工打款记录
  128. $payment = PaymentService::addPayment($withdrawCashId, $amount, $remark, "人工", $tradeNo, WithdrawCashService::getWithdrawCashStatusStr(32), "", $isCompany, $amount_person);
  129. if(!empty($payment)) {
  130. //更新当然提现记录为 人工打款成功状态
  131. WithdrawCashService::updateWithdrawCashStatus($withdrawCashId, $userId, 32, $remark);
  132. $IgnoreSources = [config('common.officialpay'), config('common.tonglianpay')];
  133. //优先 官方支付 通联支付 --[支付通道自动清算]
  134. foreach ($IgnoreSources as $IgnoreSource) {
  135. if($amount > 0) {
  136. $tempForeach = FinanceMerchantStatService::getFinanceMerchantStatSingle($channelId, $IgnoreSource);
  137. if($tempForeach && isset($tempForeach['distribution_channel_id']) && $tempForeach['distribution_channel_id']) {
  138. if($tempForeach['enable_withdrawal_amount'] >= $amount) {
  139. //当前通道可提现的余额 打款总额,
  140. FinanceMerchantStatService::createOrUpdate($tempForeach['distribution_channel_id'], $tempForeach['pay_merchant_source'], -$amount);
  141. $amount = 0;
  142. } else {
  143. //当前通道可提现的余额变为 0
  144. FinanceMerchantStatService::createOrUpdate($tempForeach['distribution_channel_id'], $tempForeach['pay_merchant_source'], -$tempForeach['enable_withdrawal_amount']);
  145. $amount = $amount - $tempForeach['enable_withdrawal_amount'];
  146. }
  147. }
  148. } else {
  149. break;
  150. }
  151. }
  152. if($amount > 0) {
  153. //更新第三方 支付通道的可提现金额
  154. $financeMerchantStats = FinanceMerchantStatService::getFinanceMerchantStatListIgnoreSourceArray($channelId, $IgnoreSources);
  155. if($financeMerchantStats && !empty($financeMerchantStats) && sizeof($financeMerchantStats) > 0) {
  156. //钱少的支付渠道优先
  157. $payAmount = $amount;//总打款金额
  158. if(is_array($financeMerchantStats) && !emptyempty($financeMerchantStats)){
  159. foreach ($financeMerchantStats as $financeMerchantStat) {
  160. if($financeMerchantStat && isset($financeMerchantStat['distribution_channel_id']) && $financeMerchantStat['distribution_channel_id']) {
  161. $tempAmount = $financeMerchantStat['enable_withdrawal_amount'] - $payAmount;
  162. if($tempAmount < 0) {
  163. //更新 FinanceMerchantStat
  164. FinanceMerchantStatService::createOrUpdate($financeMerchantStat['distribution_channel_id'], $financeMerchantStat['pay_merchant_source'], -$payAmount);
  165. //更新 LiquidatedStat
  166. LiquidatedStatService::createOrUpdate($financeMerchantStat['pay_merchant_source'], $payAmount);
  167. //更新 Liquidation
  168. LiquidationService::addLiquidation($channelId, $financeMerchantStat['pay_merchant_source'], $payAmount, $payment['id'], $remark);
  169. $payAmount = -$tempAmount;
  170. } else {
  171. //更新 FinanceMerchantStat
  172. FinanceMerchantStatService::createOrUpdate($financeMerchantStat['distribution_channel_id'], $financeMerchantStat['pay_merchant_source'], -$payAmount);
  173. //更新 LiquidatedStat
  174. LiquidatedStatService::createOrUpdate($financeMerchantStat['pay_merchant_source'], $payAmount);
  175. //更新 Liquidation
  176. LiquidationService::addLiquidation($channelId, $financeMerchantStat['pay_merchant_source'], $payAmount, $payment['id'], $remark);
  177. break;
  178. }
  179. }
  180. }
  181. }
  182. }
  183. }
  184. }
  185. DB::commit();
  186. } catch (\Exception $e){
  187. Log::error("人工打款失败日志" . $e->getMessage());
  188. DB::rollback();
  189. return false;
  190. }
  191. return true;
  192. }
  193. /**
  194. * 保存打款信息
  195. * @param $withdraw_cash_id
  196. * @param $amount
  197. * @param $remark
  198. * @param $pay_merchant_source 支付渠道
  199. * @param $trade_no 交易流水号
  200. * @param $status 打款状态
  201. * @param $pay_merchant_source_msg 支付通道返回信息
  202. * @param int $is_company 0:私人; 1:公司
  203. * @param int $amount_person 人工打款金额
  204. * @return mixed
  205. */
  206. public static function addPayment($withdraw_cash_id, $amount, $remark, $pay_merchant_source, $trade_no, $status, $pay_merchant_source_msg, $is_company = 0, $amount_person = 0) {
  207. return Payment::addPayment($withdraw_cash_id, $amount, $remark, $pay_merchant_source, $trade_no, $status, $pay_merchant_source_msg, $is_company, $amount_person);
  208. }
  209. /**
  210. * 更新打款状态
  211. * @param $id
  212. * @param String $status
  213. * @param $pay_merchant_source_msg
  214. * @param $pay_merchant_source_result
  215. * @return mixed
  216. */
  217. public static function updatePaymentStatus($id, $status = '', $pay_merchant_source_msg, $pay_merchant_source_result) {
  218. return Payment::updatePaymentStatus($id, $status, $pay_merchant_source_msg, $pay_merchant_source_result);
  219. }
  220. /**
  221. * 获取打款信息列表
  222. * @param string $withdraw_cash_id 可不传
  223. * @param string $start_time 可不传
  224. * @param string $end_time 可不传
  225. * @param string $status 状态 可不传
  226. * @param bool $is_all
  227. * @return mixed
  228. */
  229. public static function getPaymentList($withdraw_cash_id = '', $start_time = '', $end_time = '', $status = '', $is_all=false) {
  230. $payments = Payment::getList($withdraw_cash_id, $start_time, $end_time, $status, $is_all);
  231. return $payments;
  232. }
  233. /**
  234. * 财务对账
  235. * @param array $params[]
  236. * withdraw_cash_id : 提现ID 可选
  237. * channel_id:渠道ID 可选
  238. * channel_name:渠道名称 可选
  239. * account_name: 打款人姓名
  240. * start_date:开始时间 可选
  241. * end_date:结束时间 可选
  242. * search_name: 搜索名称
  243. * is_company: 0:私人; 1:公司
  244. * @param string $statusIn $statusIn 状态 查看接口 WithdrawCashService::getWithdrawCashStatusList 可不传
  245. * @return mixed
  246. */
  247. public static function getPaymentDetailList($params=[], $statusIn = '') {
  248. if(is_numeric($statusIn)) {
  249. $params['status'] = WithdrawCashService::getWithdrawCashStatusStrList($statusIn);
  250. }
  251. $payments = Payment::getListDetail($params);
  252. return $payments;
  253. }
  254. /**
  255. * 更新打款状态
  256. * @param $id id
  257. * @param $status
  258. * @return mixed
  259. */
  260. public static function updateSimplePaymentStatus($id, $status) {
  261. return Payment::updateSimplePaymentStatus($id,$status);
  262. }
  263. }