stanley-king 4 months ago
parent
commit
7c5cb5728a
2 changed files with 144 additions and 1 deletions
  1. 143 1
      helper/vendor/yifutongtax.php
  2. 1 0
      test/TestSSH2.php

+ 143 - 1
helper/vendor/yifutongtax.php

@@ -11,6 +11,7 @@
 namespace vendor;
 
 use Log;
+use refill;
 
 class yifutongtax
 {
@@ -20,6 +21,7 @@ class yifutongtax
         $local_files = $this->down_files();
         foreach ($local_files as $file) {
             Log::record("file=$file",Log::DEBUG);
+            $this->parese_file($file);
         }
     }
 
@@ -114,8 +116,148 @@ class yifutongtax
 
     public function parese_file($file)
     {
+        $matcher = function ($input)
+        {
+            $data = explode('|', $input);
+            $count = count($data);
+            if ($count === 8) {
+                return [
+                    'type' => 'order',
+                    'bank_card' => trim($data[0]),
+                    'mch_order' => trim($data[1]),
+                    'status' => 'SUCCESS',
+                    'amount' => 1,
+                    'quantity' => intval(trim($data[3])),
+                    'official_sn' => trim($data[4]),
+                    'card_no' => trim($data[5]),
+                    'order_time'  => strtotime(trim($data[6])),
+                    'finish_time' => strtotime(trim($data[7]))
+                ];
+            }
+            elseif($count === 2) {
+                return [
+                    'type' => 'check',
+                    'count' => intval(trim($data[0])),
+                    'total' => floatval(trim($data[1]))
+                ];
+            }
+            else {
+                return false;
+            }
+        };
 
-    }
+        $reader = function ($file)
+        {
+            yield [0,''];
+            $handle = fopen($file, "r");
+
+            while (($line = fgets($handle)) !== false) {
+                yield [1,$line];
+            }
+
+            fclose($handle);
+            yield [2,''];
+        };
 
+        $checker = function ($file) use ($reader, $matcher)
+        {
+            $stat = ['count' => 0, 'total' => 0.00];
+            $total = ['count' => 0, 'total' => 0.00];
+
+            foreach ($reader($file) as $value)
+            {
+                $flag = $value[0];
+                $text = $value[1];
 
+                if ($flag === 1) {
+                    $val = $matcher($text);
+                    if ($val === false) {
+                        continue;
+                    } elseif ($val['type'] == 'order') {
+                        $stat['count'] += 1;
+                        $stat['total'] += $val['amount'] * $val['quantity'];
+                    } elseif ($val['type'] == 'check') {
+                        $total = $val;
+                    }
+                }
+            }
+
+            return $stat['count'] == $total['count'] and $stat['total'] == $total['total'];
+        };
+
+        $mchinfo_getter = function ($mchid)
+        {
+            $mchinfo = Model('merchant')->getMerchantInfo(['mchid' => $mchid]);
+            if (empty($mchinfo)) {
+                return false;
+            }
+
+            return ['mchid' => $mchid, 'buyer_id' => intval($mchinfo['admin_id'])];
+        };
+
+        $mchid_getter = function ($bank_card)
+        {
+            $card_mchids = [
+                '9629980601009991' => 10532, //同心
+                '9629980601009926' => 10530, //怡科
+                '9629980601009918' => 10530
+            ];
+
+            return $card_mchids[$bank_card] ?? false;
+        };
+
+        $order_adder = function ($file) use ($reader, $matcher, $mchid_getter, $mchinfo_getter)
+        {
+            foreach ($reader($file) as $value)
+            {
+                $flag = $value[0];
+                $text = $value[1];
+
+                if ($flag === 1) {
+                    $val = $matcher($text);
+                    if ($val === false) {
+                        continue;
+                    }
+                    elseif ($val['type'] == 'order')
+                    {
+                        $mchid = $mchid_getter($val['bank_card']);
+                        if($mchid === false) {
+                            Log::record("Cannot find merchant by bank_card={$val['bank_card']}", Log::DEBUG);
+                        }
+                        else
+                        {
+                            $minfo = $mchinfo_getter($mchid);
+                            $params = [
+                                'mchid' => $mchid,
+                                'buyer_id' => $minfo['buyer_id'],
+                                'mch_order' => $val['mch_order'],
+                                'status' => $val['status'],
+                                'amount' => $val['amount'],
+                                'quantity' => $val['quantity'],
+                                'official_sn' => $val['official_sn'],
+                                'card_no' => $val['card_no'],
+                                'order_time' => time(),
+                                'notify_url' => ''
+                            ];
+
+                            $mch_order = $params['mch_order'];
+                            $val['mch_id'] = $mchid;
+
+                            refill\util::write_yifutong_order($mch_order,$val);
+                            refill\util::push_add($params);
+                        }
+                    }
+                }
+            }
+        };
+
+        $is_ok = $checker($file);
+        Log::record("$file is_ok = $is_ok", Log::DEBUG);
+
+        if ($is_ok) {
+            $order_adder($file);
+        } else {
+            Log::record("Check is not pass.", Log::DEBUG);
+        }
+    }
 }

+ 1 - 0
test/TestSSH2.php

@@ -82,6 +82,7 @@ class TestSSH2 extends TestCase
     {
         $yft = new vendor\yifutongtax();
         $file = BASE_ROOT_PATH . '/test/ssh/20241209_000110089970029.txt';
+        $file = BASE_ROOT_PATH . '/test/ssh/20241211_000110089970029.txt';
         $yft->parese_file($file);
     }