| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- <?php
- namespace addons\YunfastPay\common\services;
- use addons\YunfastPay\common\models\AgentOrder;
- use addons\YunfastPay\common\models\PaymentTrade;
- use addons\YunfastPay\common\models\SplitbillItem;
- use addons\YunfastPay\common\models\User;
- use addons\YunfastPay\common\models\WithdrawLog;
- use addons\YunfastPay\common\services\trans\nocash\AgentTrans;
- use yii\data\Pagination;
- use yii\helpers\Json;
- /**
- * 代付订单
- * @package addons\YunfastPay\common\services
- */
- class AgentService extends BaseService
- {
- /**
- * @var YunPayService
- */
- protected $yunPayService;
- /**
- * @return YunPayService
- */
- public function getYunPayService()
- {
- if (empty($this->yunPayService)) {
- return $this->yunPayService = new YunPayService(['mall_id' => $this->mall_id]);
- }
- return $this->yunPayService;
- }
- /**
- * 获取分页列表
- *
- * @param array $params
- * @return array
- */
- public function getPageList(array $params = [])
- {
- $limit = $params['limit'] ?? 20;
- $page = $params['page'] ?? 1;
- $model = AgentOrder::find();
- $model->with(['user' => function ($query) {
- $query->select(['id', 'mall_id', 'nickname', 'mobile', 'avatar_url']);
- }]);
- $model->with(['store' => function ($query) {
- $query->select(['id', 'mall_id', 'store_name']);
- }]);
- $model->andWhere(['mall_id' => $this->mall_id]);
- if (!empty($params['user_id'])) {
- $model->andWhere(['user_id' => $params['user_id']]);
- }
- if (!empty($params['user_keyword'])) {
- $model->andWhere([
- 'user_id' => User::getUserIdsByKeyword($this->mall_id, $params['user_keyword'])
- ]);
- }
- if (!empty($params['acct_keyword'])) {
- $model->andWhere([
- 'or',
- ['=', 'acct_name', $params['acct_keyword']],
- ['=', 'acct_number', $params['acct_keyword']],
- ]);
- }
- if (!empty($params['order_no'])) {
- $model->andWhere([
- 'trade_id' => PaymentTrade::getIdsByNo($this->mall_id, $params['order_no'])
- ]);
- }
- if (!empty($params['time'])) {
- $model->andFilterWhere([
- 'between',
- 'created_at',
- strtotime($params['time'][0]),
- strtotime($params['time'][1])
- ]);
- }
- $pagination = new Pagination(['totalCount' => $model->count(), 'pageSize' => $limit]);
- $model->limit($pagination->limit)->offset($pagination->offset);
- $list = $model->orderBy('id desc')
- ->asArray()
- ->all();
- foreach ($list as $k => $v) {
- $list[$k]['res_data'] = Json::decode($v['res_data']);
- }
- $data['list'] = $list;
- $data['page_count'] = $pagination->pageCount;
- $data['current_page'] = $page + 1;
- return $data;
- }
- /**
- * 代付
- *
- * @param WithdrawLog $log
- * @return int
- */
- public function agentPay(WithdrawLog $log)
- {
- $service = $this->getYunPayService();
- // 提现信息
- $info = Json::decode($log->extra);
- if (!empty($info['name']) && !empty($info['bank_account'])) {
- // 代付类
- $trans = new AgentTrans(
- $log->order_no . mt_rand(1000, 9999),
- $log->actual_num,
- $info['bank_account'],
- $info['name'],
- sprintf('订单%s提现', $log->order_no)
- );
- // 如果设置账户类型
- if (!empty($info['bank_account_type'])) {
- $trans->setAcctType($info['bank_account_type']);
- }
- // 执行代付
- $res = $service->sendPayRq($trans);
- // 添加代付记录
- $reqData = $service->getReqData();
- /**
- * @var AgentOrder
- */
- $model = AgentOrder::addLog([
- 'mall_id' => $this->mall_id,
- 'user_id' => $log->user_id,
- 'withdraw_id' => $log->id,
- 'trans_amount' => $reqData['transAmt'],
- 'order_no' => $reqData['outOrderId'],
- 'acct_number' => $reqData['settleAcct'],
- 'acct_name' => $reqData['settleAcctNm'],
- 'acct_type' => $trans->getAcctType(),
- 'req_data' => $reqData,
- 'res_data' => $res,
- 'status' => $res['transStatus'] ?? '',
- ]);
- // 申请失败
- if ($res['respCode'] != '0000') {
- $msg = $res['respMsg'];
- $model->agree($log, $msg);
- return $this->error($msg);
- } else {
- // 打款失败
- if ($res['transStatus'] == '102') {
- $msg = $res['respMsg'];
- $model->agree($log, $msg);
- return $this->error($msg);
- }
- // 打款成功
- if ($res['transStatus'] == '100') {
- $model->success($log);
- return $log->id;
- }
- }
- } else {
- $msg = '账户信息有误';
- WithdrawLog::remit_fail($log, $msg);
- return $this->error($msg);
- }
- return $log->id;
- }
- /**
- * 回调处理
- *
- * @param array $respData
- * @return boolean
- */
- public function notify(array $respData)
- {
- /**
- * @var AgentOrder
- */
- $order = AgentOrder::getOrderByNo($this->mall_id, $respData['outOrderId']);
- // 订单完成不处理
- if ($order->status == '100') return true;
- if ($order->type == 1) {
- /**
- * 提现订单
- * @var WithdrawLog
- */
- $log = $order->withdrawLog;
- if ('100' == $respData['transStatus']) {
- // 对比金额
- if ($log->actual_num == $respData['transAmt']) {
- $order->success($log, '100', $respData);
- return true;
- }
- } else if ('102' == $respData['transStatus']) {
- // 交易失败
- $msg = $respData['respMsg'] ?? $respData['resDesc'];
- $order->agree($log, $msg, '102', $respData);
- return $this->error($msg);
- } else {
- $msg = $respData['respMsg'] ?? $respData['resDesc'];
- $order->agree($log, $msg, $respData['transStatus'], $respData);
- // 未知结果,这种情况不会有。
- return $this->error('未知情况');
- }
- }
- if ($order->type == 2) {
- /**
- * 分账订单
- * @var SplitbillItem
- */
- $log = $order->splitbillLog;
- if ('100' == $respData['transStatus']) {
- // 对比金额
- if ($log->amount == $respData['transAmt']) {
- $order->successByStore('100', $respData);
- return true;
- }
- } else if ('102' == $respData['transStatus']) {
- // 交易失败
- $msg = $respData['respMsg'];
- $order->failByStore('102', $respData);
- return $this->error($msg);
- } else {
- // 未知结果,这种情况不会有。
- return $this->error('未知情况');
- }
- }
- }
- }
|