| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- <?php
- namespace addons\HuifuPay\common\service;
- use addons\HuifuPay\common\models\HuifuBillShare;
- use addons\HuifuPay\common\models\HuifuPayOrderModel;
- use addons\Store\common\models\Store;
- use addons\Store\common\models\StoreCashierOrder;
- use common\enums\AddonsEnum;
- use common\enums\RabbitMqEnum;
- use common\enums\StatusEnum;
- use common\helpers\FormatHelper;
- use common\models\mall\MallAddons;
- use common\models\user\User;
- class HuifuBillShareService
- {
- public function page(int $mall_id, array $params, int $store_id = 0)
- {
- $where = [
- ['mall_id' => $mall_id],
- ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]
- ];
- if (!empty($params['member'])) {
- $uids = User::find()->where(['mall_id' => $mall_id])
- ->andWhere(['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])
- ->andWhere(['or', ['like', 'id', $params['member']], ['like', 'nickname', $params['member']], ['like', 'mobile', $params['member']]])
- ->select('id')->column();
- if (empty($uids)) return [];
- $where[] = ['user_id' => $uids];
- }
- $is_install_store = MallAddons::isInstall($mall_id, AddonsEnum::ADDONS_NAME_STORE);
- if (!empty($store_id)) {
- $where[] = ['store_id' => $store_id];
- } else if (!empty($params['store']) && $is_install_store) {
- $sids = Store::find()->where(['mall_id' => $mall_id])
- ->andWhere(['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])
- ->andWhere(['or', ['like', 'id', $params['store']], ['like', 'store_name', $params['store']]])
- ->select('id')->column();
- if (empty($sids)) return [];
- $where[] = ['store_id' => $sids];
- }
- if (!empty($params['out_trade_no'])) {
- $where[] = ['like', 'trade_no', $params['out_trade_no']];
- }
- if (!empty($params['order_no'])) {
- $where[] = ['like', 'order_no', $params['order_no']];
- }
- if (!empty($params['start_time'])) $where[] = ['>=', 'created_at', strtotime($params['start_time'])];
- if (!empty($params['end_time'])) $where[] = ['<=', 'created_at', strtotime($params['end_time'])];
- $ret = HuifuBillShare::listPage([
- 'where' => $where,
- 'order' => 'id desc',
- 'with' => ['order' => function($query) {
- $query->with(['user']);
- }],
- 'limit' => $params['limit'] ?? 10,
- ]);
- if (!empty(HuifuBillShare::getStaticError())) return HuifuBillShare::getStaticError();
- if (!empty($ret['list'])) {
- $stores = [];
- if ($is_install_store) {
- $sids = array_column($ret['list'], 'store_id');
- $stores = Store::lists([
- 'where' => [
- ['id' => $sids]
- ],
- 'index' => 'id'
- ]);
- }
- foreach ($ret['list'] as &$item) {
- $item['store'] = $stores[$item['store_id']] ?? null;
- if (strpos($item['order_no'], 'C') === 0) {
- // 这个属于收银台订单
- $item['order'] = StoreCashierOrder::getOne([['order_no' => $item['order_no']]], true, ['user']);
- }
- }
- }
- return $ret;
- }
- public static function shareRetry(int $mall_id, int $id)
- {
- $share = HuifuBillShare::findOne(['mall_id' => $mall_id, 'id' => $id]);
- if (empty($share)) return '记录不存在';
- if ($share->settle_status != 1) return '当前状态不可进行分佣';
- $ret = self::handleSync(['mall_id' => $mall_id, 'order_no' => $share->order_no]);
- if (!$ret) return '操作失败';
- return true;
- }
- /**
- *
- * @param int $mall_id
- * @param array $params
- * @return HuifuBillShare
- * @throws \Exception
- */
- public static function save(int $mall_id, array $params)
- {
- $model = new HuifuBillShare();
- $model->loadDefaultValues();
- $model->mall_id = $mall_id;
- $model->merchant_no = $params['merchant_no'];
- $model->trade_id = $params['trade_id'] ?? 0;
- $model->order_no = $params['order_no'];
- $model->share_amount = $params['share_amount'];
- $model->fee = $params['fee'];
- $model->store_id = $params['store_id'];
- $model->fee_rate = $params['fee_rate'];
- $model->trade_no = $params['trade_no'];
- $model->order_amount = $params['order_amount'];
- $model->settle_type = $params['settle_type'];
- if (!$model->save()) throw new \Exception($model->getErrorMessage());
- return $model;
- }
- public static function shareCommission(int $mall_id, string $order_no)
- {
- \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_ORDER, RabbitMqEnum::ORDER_QUEUE,
- ['mall_id' => $mall_id, 'order_no' => $order_no], self::class, 'handleSync');
- }
- public static function handleSync($params)
- {
- $config = \Yii::$app->services->setting->addons->get($params['mall_id'], AddonsEnum::ADDONS_NAME_HUIFU_PAY, 'basic');
- $share = HuifuBillShare::findOne(['mall_id' => $params['mall_id'], 'order_no' => $params['order_no']]);
- if ($share->settle_status != 1) return true;
- $pOrder = HuifuPayOrderModel::getOne([['trade_no' => $share->trade_no]], false, [], 'id desc');
- if (empty($pOrder)) {
- \Yii::error("支付订单不存在: {$share->trade_no}");
- return true;
- }
- $t = \Yii::$app->db->beginTransaction();
- try {
- // 请求汇付执行分账
- $huifuService = new HuifuService($share->mall_id);
- // 计算退款比例
- $refund_rate = 1;
- if ($share->refund_amount > 0) $refund_rate = bcsub($refund_rate, bcdiv($share->refund_amount, $share->order_amount, 2), 2);
- $share->pay_fee = bcmul($pOrder->notify['fee_amount'] ?? 1, $refund_rate, 2); // 如果存在退款那么就需要计算退款比例。
- $finally_amount = bcsub($share->order_amount, $share->refund_amount, 2);
- $finally_amount = bcsub($finally_amount, $share->pay_fee, 2);
- $share->finally_share_amount = bcmul($finally_amount, bcmul($share->fee_rate, 0.01, 4), 2); // 商家收到的钱
- $share->finally_fee = bcsub($finally_amount, $share->finally_share_amount, 2); // 平台收到的钱
- if (MallAddons::isInstall($params['mall_id'], AddonsEnum::ADDONS_NAME_STORE)) {
- //门店订单
- $commission = \addons\Store\common\models\StoreCommission::getOne([
- ['mall_id' => $params['mall_id']],
- ['order_no' => $params['order_no']],
- ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]],
- ['settle_type' => 4],
- ], true);
- \Yii::$app->custom->logs(__METHOD__ . "门店提成明细" . json_encode([
- 'params' => $params,
- 'commission' => $commission
- ]));
- if ($commission) {
- $share->finally_fee = $commission['amount_received'];//商家最终
- $share->finally_share_amount = bcsub($finally_amount, $share->finally_fee, 2); //平台最终
- }
- }
- $post = $share->toArray();
- $post['huifu_id'] = $config['huifupay_huifu_id'];
- $post['order_id'] = $share->order_no;
- $post['org_req_date'] = date('Ymd', $pOrder->created_at);
- $post['org_req_seq_id'] = $pOrder->hf_order_no;
- // 原交易为快捷支付必填:QUICK_PAY,
- // 原交易为余额支付必填:ACCT_PAYMENT;
- if ($pOrder->trade_type == 'fast_pay') {
- $post['pay_type'] = 'QUICK_PAY';
- } elseif ($pOrder->trade_type == 'balance_pay') {
- $post['pay_type'] = 'ACCT_PAYMENT';
- }
- $resp = $huifuService->shareCommission($post, [
- [
- 'id' => $share->merchant_no,
- 'num' => $share->finally_fee,
- ],
- [
- 'id' => $config['huifupay_huifu_id'],
- 'num' => $share->finally_share_amount,
- ]
- ]);
- if ($resp === false) {
- $share->response = [HuifuService::getStaticError()];
- $share->finally_fee = 0;
- $share->finally_share_amount = 0;
- } else {
- $share->response = $resp;
- $share->settle_status = 100;
- $share->settle_at = time(); // 结算时间
- }
- if (!$share->save()) throw new \Exception($share->getErrorMessage());
- $t->commit();
- } catch (\Exception $e) {
- var_dump($e->getMessage(), $e->getFile(), $e->getLine());
- \Yii::error(FormatHelper::exception($e, __METHOD__));
- $t->rollBack();
- return false;
- }
- return true;
- }
- public static function findOrder($mall_id, $order_no)
- {
- return HuifuBillShare::findOne(['order_no' => $order_no, 'mall_id' => $mall_id]);
- }
- public static function isEnableShare(int $mall_id)
- {
- $settings = \Yii::$app->services->setting->addons->get($mall_id, AddonsEnum::ADDONS_NAME_HUIFU_PAY, 'basic');
- if (empty($settings['huifupay_status'])) {
- return StatusEnum::DISABLED;
- }
- if (empty($settings['huifupay_ledger_status'])) {
- return StatusEnum::DISABLED;
- }
- return StatusEnum::ENABLED;
- }
- public static function shareProfit()
- {
- }
- }
|