AgentService.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. namespace addons\YunfastPay\common\services;
  3. use addons\YunfastPay\common\models\AgentOrder;
  4. use addons\YunfastPay\common\models\PaymentTrade;
  5. use addons\YunfastPay\common\models\SplitbillItem;
  6. use addons\YunfastPay\common\models\User;
  7. use addons\YunfastPay\common\models\WithdrawLog;
  8. use addons\YunfastPay\common\services\trans\nocash\AgentTrans;
  9. use yii\data\Pagination;
  10. use yii\helpers\Json;
  11. /**
  12. * 代付订单
  13. * @package addons\YunfastPay\common\services
  14. */
  15. class AgentService extends BaseService
  16. {
  17. /**
  18. * @var YunPayService
  19. */
  20. protected $yunPayService;
  21. /**
  22. * @return YunPayService
  23. */
  24. public function getYunPayService()
  25. {
  26. if (empty($this->yunPayService)) {
  27. return $this->yunPayService = new YunPayService(['mall_id' => $this->mall_id]);
  28. }
  29. return $this->yunPayService;
  30. }
  31. /**
  32. * 获取分页列表
  33. *
  34. * @param array $params
  35. * @return array
  36. */
  37. public function getPageList(array $params = [])
  38. {
  39. $limit = $params['limit'] ?? 20;
  40. $page = $params['page'] ?? 1;
  41. $model = AgentOrder::find();
  42. $model->with(['user' => function ($query) {
  43. $query->select(['id', 'mall_id', 'nickname', 'mobile', 'avatar_url']);
  44. }]);
  45. $model->with(['store' => function ($query) {
  46. $query->select(['id', 'mall_id', 'store_name']);
  47. }]);
  48. $model->andWhere(['mall_id' => $this->mall_id]);
  49. if (!empty($params['user_id'])) {
  50. $model->andWhere(['user_id' => $params['user_id']]);
  51. }
  52. if (!empty($params['user_keyword'])) {
  53. $model->andWhere([
  54. 'user_id' => User::getUserIdsByKeyword($this->mall_id, $params['user_keyword'])
  55. ]);
  56. }
  57. if (!empty($params['acct_keyword'])) {
  58. $model->andWhere([
  59. 'or',
  60. ['=', 'acct_name', $params['acct_keyword']],
  61. ['=', 'acct_number', $params['acct_keyword']],
  62. ]);
  63. }
  64. if (!empty($params['order_no'])) {
  65. $model->andWhere([
  66. 'trade_id' => PaymentTrade::getIdsByNo($this->mall_id, $params['order_no'])
  67. ]);
  68. }
  69. if (!empty($params['time'])) {
  70. $model->andFilterWhere([
  71. 'between',
  72. 'created_at',
  73. strtotime($params['time'][0]),
  74. strtotime($params['time'][1])
  75. ]);
  76. }
  77. $pagination = new Pagination(['totalCount' => $model->count(), 'pageSize' => $limit]);
  78. $model->limit($pagination->limit)->offset($pagination->offset);
  79. $list = $model->orderBy('id desc')
  80. ->asArray()
  81. ->all();
  82. foreach ($list as $k => $v) {
  83. $list[$k]['res_data'] = Json::decode($v['res_data']);
  84. }
  85. $data['list'] = $list;
  86. $data['page_count'] = $pagination->pageCount;
  87. $data['current_page'] = $page + 1;
  88. return $data;
  89. }
  90. /**
  91. * 代付
  92. *
  93. * @param WithdrawLog $log
  94. * @return int
  95. */
  96. public function agentPay(WithdrawLog $log)
  97. {
  98. $service = $this->getYunPayService();
  99. // 提现信息
  100. $info = Json::decode($log->extra);
  101. if (!empty($info['name']) && !empty($info['bank_account'])) {
  102. // 代付类
  103. $trans = new AgentTrans(
  104. $log->order_no . mt_rand(1000, 9999),
  105. $log->actual_num,
  106. $info['bank_account'],
  107. $info['name'],
  108. sprintf('订单%s提现', $log->order_no)
  109. );
  110. // 如果设置账户类型
  111. if (!empty($info['bank_account_type'])) {
  112. $trans->setAcctType($info['bank_account_type']);
  113. }
  114. // 执行代付
  115. $res = $service->sendPayRq($trans);
  116. // 添加代付记录
  117. $reqData = $service->getReqData();
  118. /**
  119. * @var AgentOrder
  120. */
  121. $model = AgentOrder::addLog([
  122. 'mall_id' => $this->mall_id,
  123. 'user_id' => $log->user_id,
  124. 'withdraw_id' => $log->id,
  125. 'trans_amount' => $reqData['transAmt'],
  126. 'order_no' => $reqData['outOrderId'],
  127. 'acct_number' => $reqData['settleAcct'],
  128. 'acct_name' => $reqData['settleAcctNm'],
  129. 'acct_type' => $trans->getAcctType(),
  130. 'req_data' => $reqData,
  131. 'res_data' => $res,
  132. 'status' => $res['transStatus'] ?? '',
  133. ]);
  134. // 申请失败
  135. if ($res['respCode'] != '0000') {
  136. $msg = $res['respMsg'];
  137. $model->agree($log, $msg);
  138. return $this->error($msg);
  139. } else {
  140. // 打款失败
  141. if ($res['transStatus'] == '102') {
  142. $msg = $res['respMsg'];
  143. $model->agree($log, $msg);
  144. return $this->error($msg);
  145. }
  146. // 打款成功
  147. if ($res['transStatus'] == '100') {
  148. $model->success($log);
  149. return $log->id;
  150. }
  151. }
  152. } else {
  153. $msg = '账户信息有误';
  154. WithdrawLog::remit_fail($log, $msg);
  155. return $this->error($msg);
  156. }
  157. return $log->id;
  158. }
  159. /**
  160. * 回调处理
  161. *
  162. * @param array $respData
  163. * @return boolean
  164. */
  165. public function notify(array $respData)
  166. {
  167. /**
  168. * @var AgentOrder
  169. */
  170. $order = AgentOrder::getOrderByNo($this->mall_id, $respData['outOrderId']);
  171. // 订单完成不处理
  172. if ($order->status == '100') return true;
  173. if ($order->type == 1) {
  174. /**
  175. * 提现订单
  176. * @var WithdrawLog
  177. */
  178. $log = $order->withdrawLog;
  179. if ('100' == $respData['transStatus']) {
  180. // 对比金额
  181. if ($log->actual_num == $respData['transAmt']) {
  182. $order->success($log, '100', $respData);
  183. return true;
  184. }
  185. } else if ('102' == $respData['transStatus']) {
  186. // 交易失败
  187. $msg = $respData['respMsg'] ?? $respData['resDesc'];
  188. $order->agree($log, $msg, '102', $respData);
  189. return $this->error($msg);
  190. } else {
  191. $msg = $respData['respMsg'] ?? $respData['resDesc'];
  192. $order->agree($log, $msg, $respData['transStatus'], $respData);
  193. // 未知结果,这种情况不会有。
  194. return $this->error('未知情况');
  195. }
  196. }
  197. if ($order->type == 2) {
  198. /**
  199. * 分账订单
  200. * @var SplitbillItem
  201. */
  202. $log = $order->splitbillLog;
  203. if ('100' == $respData['transStatus']) {
  204. // 对比金额
  205. if ($log->amount == $respData['transAmt']) {
  206. $order->successByStore('100', $respData);
  207. return true;
  208. }
  209. } else if ('102' == $respData['transStatus']) {
  210. // 交易失败
  211. $msg = $respData['respMsg'];
  212. $order->failByStore('102', $respData);
  213. return $this->error($msg);
  214. } else {
  215. // 未知结果,这种情况不会有。
  216. return $this->error('未知情况');
  217. }
  218. }
  219. }
  220. }