Procházet zdrojové kódy

话费分账逻辑

xialei před 1 rokem
rodič
revize
b95c59ffd6

+ 87 - 0
app/command/HuafeiOrderIndependentAccount.php

@@ -0,0 +1,87 @@
1
+<?php
2
+
3
+namespace app\command;
4
+
5
+use app\common\model\store\order\StoreOrder;
6
+use app\common\model\user\UserCertification;
7
+use app\common\repositories\merchant\order\OrderGzcRepository;
8
+use app\common\repositories\merchant\order\OrderMerchantRepository;
9
+use app\common\repositories\store\order\StoreOrderRepository;
10
+use app\common\repositories\store\order\StoreOrderStatusRepository;
11
+use app\common\repositories\user\UserBillRepository;
12
+use app\common\repositories\user\UserRepository;
13
+use Carbon\Carbon;
14
+use think\console\Command;
15
+use think\console\Input;
16
+use think\console\Output;
17
+use think\facade\Db;
18
+use think\facade\Log;
19
+
20
+class HuafeiOrderIndependentAccount extends Command
21
+{
22
+    protected function configure()
23
+    {
24
+        // 指令配置
25
+        $this->setName('huafei-order-independent-account')
26
+            ->setDescription('话费订单分账');
27
+    }
28
+
29
+    protected function execute(Input $input, Output $output)
30
+    {
31
+        // @todo 精彩生活区订单分账
32
+        $this->doHuafeiOrderIndependentAccount();
33
+    }
34
+
35
+    /**
36
+     * 会员专区、精彩生活区订单分账
37
+     *
38
+     * @param $merId
39
+     * @param $timeLimit
40
+     * @return void
41
+     * @throws \think\db\exception\DataNotFoundException
42
+     * @throws \think\db\exception\DbException
43
+     * @throws \think\db\exception\ModelNotFoundException
44
+     */
45
+    private function doHuafeiOrderIndependentAccount($timeLimit = 1)
46
+    {
47
+        $dst_mer_id = systemConfig('mer_365');
48
+
49
+        // 商户信息
50
+        $merchants = [];
51
+
52
+        $time = time();
53
+        // 订单状态(0:待发货;1:待收货;2:待评价;3:已完成;4已申请退款;-1:已退款)5未支付 6已取消'
54
+        $status = [2];
55
+        // 普通订单
56
+        $cursor = Db::table('rrx_store_order')->where('is_independentaccount', 0)->whereIn('status', $status)->cursor();
57
+
58
+        /** @var StoreOrderRepository $storeOrderRepository */
59
+        $storeOrderRepository = app()->make(StoreOrderRepository::class);
60
+
61
+        foreach ($cursor as $key => $value) {
62
+            $settlement_period_time = strtotime($value['create_time']) + $timeLimit * 24 * 3600;
63
+            // @todo 下单后第{$timeLimit}天后可分账
64
+            if ($settlement_period_time >= $time) {
65
+                $merchants['496'] = Db::name('merchant')->where('mer_id', 496)->find();
66
+                $order_huafei_hf = Db::name('order_huafei_hf')->where('order_no', $value['order_sn'])->find();
67
+                if(!$order_huafei_hf) continue;
68
+                $value['mer_id'] = 496;
69
+                $order_huafei_hf['mer_id'] = 496;
70
+
71
+                // 分账账号 496:表示{会员专区}账户,分账到{会员专区}账户
72
+                $independentAccount = $storeOrderRepository->getIndependentAccountHuafei($order_huafei_hf, $dst_mer_id, $merchants['496']['hf_member_id']);
73
+
74
+                // @todo 预分账处理 分账到{会员专区}账户,故:hf_platform_member_id 为入账账户
75
+                $isSuccessed = $storeOrderRepository->prepareExecuteIndependentAccountHuafei(
76
+                    $value, $order_huafei_hf, $independentAccount['api_key'], $independentAccount['hf_platform_member_id'], '', $value['pay_price'], 0
77
+                );
78
+
79
+                if ($isSuccessed) {
80
+                    Db::name("log")->insert(array('data' => '话费-订单分账:' . json_encode($value, JSON_FORCE_OBJECT)));//日志记录
81
+                    Db::table('rrx_store_order')->where('order_sn', $value['order_sn'])->update(['is_independentaccount' => 1]);
82
+                }
83
+            }
84
+        }
85
+    }
86
+
87
+}

+ 91 - 0
app/common/repositories/store/order/StoreOrderRepository.php

@@ -10547,6 +10547,40 @@ class StoreOrderRepository extends BaseRepository
10547 10547
         return compact('hf_member_id', 'hf_platform_member_id', 'api_key');
10548 10548
     }
10549 10549
 
10550
+
10551
+    /**
10552
+     * 获取分账账号信息话费专用
10553
+     *
10554
+     * @param $order
10555
+     * @param $dst_mer_id
10556
+     * @param $alhf_member_id
10557
+     * @return array
10558
+     */
10559
+    public function getIndependentAccountHuafei($order, $dst_mer_id, $alhf_member_id)
10560
+    {
10561
+        if ($order['pay_type'] == 1) {
10562
+            $hf_member_id = Db::name('merchant_member')->where('mer_id', $order['mer_id'])->where("type", 3)->value('member_id');
10563
+            $api_key = 'api_live_b0a687ec-5450-4ada-a0e4-65bac0f46871';
10564
+            $type = 3;
10565
+
10566
+            // 平台特殊账户
10567
+            $hf_platform_member_id = Db::name('merchant_member')->where(['mer_id' => $dst_mer_id, 'type' => $type])->value('member_id');
10568
+        } else {
10569
+            //支付宝
10570
+            $hf_member_id = $alhf_member_id;
10571
+            $api_key = 'api_live_b23537ed-64b8-45d6-832e-fb228aa9e0a9';
10572
+
10573
+            // 平台特殊账户
10574
+            $hf_platform_member_id = Db::name('merchant_member')->where(['mer_id' => $dst_mer_id, 'type' => 1])->value('member_id');
10575
+        }
10576
+
10577
+        return compact('hf_member_id', 'hf_platform_member_id', 'api_key');
10578
+    }
10579
+
10580
+
10581
+
10582
+
10583
+
10550 10584
     /**
10551 10585
      * 预分账处理
10552 10586
      *
@@ -10599,6 +10633,63 @@ class StoreOrderRepository extends BaseRepository
10599 10633
         return false;
10600 10634
     }
10601 10635
 
10636
+
10637
+
10638
+
10639
+    /**
10640
+     * 预分账处理话费专用
10641
+     *
10642
+     * @param $order
10643
+     * @param $api_key
10644
+     * @param $hf_member_id
10645
+     * @param $hf_platform_member_id
10646
+     * @param $merchant_profit
10647
+     * @param $platform_profit
10648
+     * @param $dst_mer_id
10649
+     * @return bool
10650
+     * @throws DbException
10651
+     */
10652
+    public function prepareExecuteIndependentAccountHuafei($order, $group_order, $api_key, $hf_member_id, $hf_platform_member_id, $merchant_profit, $platform_profit, $dst_mer_id = 0)
10653
+    {
10654
+        $members[] = [
10655
+            'member_id' => $hf_member_id,
10656
+            'amount' => $merchant_profit,
10657
+            'fee_flag' => 'Y'
10658
+        ];
10659
+        if (!empty($hf_platform_member_id) && ($platform_profit > 0)) {
10660
+            $members[] = [
10661
+                'member_id' => $hf_platform_member_id,
10662
+                'amount' => $platform_profit,
10663
+                'fee_flag' => 'N'
10664
+            ];
10665
+        }
10666
+
10667
+        $requestId = ShouxinyiApiRequest::getOrderNumber('split');
10668
+        $params = [
10669
+            'payment_id' => $group_order['hf_pay_id'],
10670
+            'order_no' => $requestId,
10671
+            'confirm_amt' => $group_order['pay_price'],
10672
+            'div_members' => $members
10673
+        ];
10674
+
10675
+        $params['api_key'] = $api_key;
10676
+
10677
+        // 记录分账订单
10678
+        $this->recordIndependentAccount($order['mer_id'], $dst_mer_id, $group_order, $requestId, $merchant_profit, $platform_profit);
10679
+
10680
+        Db::name("log")->insert(array('data' => '汇付分账参数-----' . json_encode($params, JSON_FORCE_OBJECT)));//日志记录
10681
+        $res = $this->payment_confirm_create_traits($params);
10682
+        Db::name("log")->insert(array('data' => '汇付分账结果-----' . json_encode($res, JSON_FORCE_OBJECT)));//日志记录
10683
+        if ($res['status'] == 'succeeded') {
10684
+            Db::name("store_split_order")->where('requestId', $requestId)->update(['status' => 1, 'hf_pay_id' => $res['id']]);
10685
+            return true;
10686
+        }
10687
+        return false;
10688
+    }
10689
+
10690
+
10691
+
10692
+
10602 10693
     /**
10603 10694
      * 记录分账订单
10604 10695
      *