Zory 1 miesiąc temu
rodzic
commit
58fc40e7e6

+ 1 - 1
app/controller/api/Goods.php

@@ -56,7 +56,7 @@ class Goods extends Base
             $priceData = empty($sku['price']) ? $goods['price'] * 100 : $sku['price'] * 100;
             $param['line_price'] = empty($sku['line_price']) ? $goods['price'] * 100 : $sku['line_price'] * 100;
             $param['price'] = $priceData * $param['number'];
-            $param['order_sn'] = CodeExtend::uniqidDate(18);
+            $param['order_sn'] = "AL".CodeExtend::uniqidDate(18).rand(100,999);
             $param['openid'] = $request->user['openid'];
             $param['sku_name'] = $param['name'];
             $param['poi_id'] = $goods['poi_id'];

+ 48 - 6
app/controller/api/Order.php

@@ -3,14 +3,17 @@
 namespace app\controller\api;
 
 use app\extra\basic\Base;
+use app\extra\dyMini\Pay;
 use app\middleware\AuthMiddleware;
 use app\model\saas\SaasOrder;
+use app\service\saas\OrderService;
 use DI\Attribute\Inject;
 use LinFly\Annotation\Attributes\Route\Controller;
 use LinFly\Annotation\Attributes\Route\GetMapping;
 use LinFly\Annotation\Attributes\Route\Middleware;
 use LinFly\Annotation\Attributes\Route\PostMapping;
 use support\Request;
+use support\Response;
 
 
 #[Controller("/dy/order"),Middleware(AuthMiddleware::class)]
@@ -20,9 +23,27 @@ class Order extends Base
     #[Inject]
     protected SaasOrder $model;
 
+    #[Inject]
+    protected OrderService $service;
+
+    #[GetMapping('list')]
+    public function getDataList(Request $request): Response
+    {
+        try {
+            $param = $request->all();
+            if (!empty($param['size'])) {
+                $param['pageSize'] = $param['size'];
+            }
+            $param['openid'] = $request->user['openid'];
+            $data = $this->service->setModel()->getList($param);
+            return successTrans("success.data",pageFormat($data));
+        } catch (\Throwable $th) {
+            return error($th->getMessage());
+        }
+    }
 
     #[GetMapping("confirm")]
-    public function confirmOrder(Request $request)
+    public function confirmOrder(Request $request): Response
     {
         try {
             $param = $this->_valid([
@@ -39,16 +60,37 @@ class Order extends Base
     }
 
 
-
-    #[PostMapping("data")]
-    public function confirmOrderSubmit(Request $request)
+    /**
+     * 发起支付
+     * @param Request $request
+     * @return Response
+     */
+    #[PostMapping("toPay")]
+    public function orderPay(Request $request): Response
     {
         try {
             $param = $this->_valid([
-                "order.require"     => trans("empty.require")
+                "order.require"     => trans("empty.require"),
+                "mobile.default"    => ""
             ],$request->method());
             if (!is_array($param)) return error($param);
-
+            $order = $this->model->where("order_sn",$param['order'])->with(['product','poi'])->findOrEmpty();
+            $payParam = [
+                "order_sn"      => $order['order_sn'],
+                "total"         => $order['price'],
+                "name"          => $order['product']['product_name'],
+                "notify_url"    => ""
+            ];
+            $byteAuthorization = (new Pay)->config([
+                "appid"     => sConf("wechat.mini_appid"),
+                "secret"    => sConf("wechat.mini_secret"),
+                "salt"      => sConf("wechat.mch_salt"),
+            ])->createOrder($payParam);
+            if (!empty($param['mobile'])) {
+                $order->mobile = $param['mobile'];
+                $order->save();
+            }
+            return success("ok",['pay' => $byteAuthorization]);
         } catch (\Throwable $throwable) {
             return error($throwable->getMessage());
         }

+ 94 - 0
app/extra/dyMini/Pay.php

@@ -0,0 +1,94 @@
+<?php
+
+namespace app\extra\dyMini;
+
+use app\extra\dyLife\BasicLife;
+use yzh52521\EasyHttp\Http;
+
+class Pay extends BasicLife
+{
+
+    public function createOrder(array $data = [])
+    {
+        $url = "https://developer.toutiao.com/api/apps/ecpay/v1/create_order";
+        $param = [
+            "out_order_no"  => $data['order_sn'],
+            "total_amount"  => (int) $data['total'],
+            "subject"       => $data['name'],
+            "body"          => $data['name'],
+            "valid_time"    => 1800,
+            "notify_url"    => $data['notify_url'],
+            "cp_extra"      => $data['order_sn']
+        ];
+        $param['sign'] = $this->requestSign($param);
+        $param['app_id'] = $this->config['appid'];
+        $resp = Http::asJson()->post($url,$param)->array();
+        if ($resp['err_no'] == 0) {
+            return $resp['data'];
+        }
+        return [];
+    }
+
+    /**
+     * 生成签名
+     */
+    private function requestSign($params): string
+    {
+        $rList = [];
+        foreach($params as $k =>$v) {
+            if ($k == "other_settle_params" || $k == "app_id" || $k == "sign" || $k == "thirdparty_id")
+                continue;
+
+            $value = trim(strval($v));
+            if (is_array($v)) {
+                $value = arrayToStr($v);
+            }
+
+            $len = strlen($value);
+            if ($len > 1 && substr($value, 0,1)=="\"" && substr($value, $len-1)=="\"")
+                $value = substr($value,1, $len-1);
+            $value = trim($value);
+            if ($value == "" || $value == "null")
+                continue;
+            $rList[] = $value;
+        }
+        $rList[] = $this->config['salt'];
+        sort($rList, SORT_STRING);
+        return md5(implode('&', $rList));
+    }
+
+    /**
+     * @param $privateKeyStr
+     * @param $data
+     * @param $appId
+     * @param $nonceStr
+     * @param $timestamp
+     * @param $keyVersion
+     * @return string|null
+     * @throws \Exception
+     */
+    public function getByteAuthorization(string $privateKeyStr, string $data, string $appId, string $nonceStr,string $timestamp, string $keyVersion) {
+        $byteAuthorization = '';
+        // 读取私钥
+        $privateKey = openssl_pkey_get_private($privateKeyStr);
+        if (!$privateKey) {
+            throw new \Exception("Invalid private key");
+        }
+        // 生成签名
+        $signature = $this->getSignature("POST", "/requestOrder", $timestamp, $nonceStr, $data, $privateKeyStr);
+        if ($signature === false) {
+            return null;
+        }
+        // 构造 byteAuthorization
+        $byteAuthorization = sprintf("SHA256-RSA2048 appid=%s,nonce_str=%s,timestamp=%s,key_version=%s,signature=%s", $appId, $nonceStr, $timestamp, $keyVersion, $signature);
+        return $byteAuthorization;
+    }
+
+    public function getSignature(string $method, string $url, string $timestamp, string $nonce, $data, $privateKey) {
+//        printf("method:%s\n url:%s\n timestamp:%s\n nonce:%s\n data:%s", $method, $url, $timestamp, $nonce, $data);
+        $targetStr = $method. "\n" . $url. "\n" . $timestamp. "\n" . $nonce. "\n" . $data. "\n";
+        openssl_sign($targetStr, $sign, $privateKey, OPENSSL_ALGO_SHA256);
+        return base64_encode($sign);
+    }
+
+}

+ 36 - 0
app/service/saas/OrderService.php

@@ -0,0 +1,36 @@
+<?php
+
+namespace app\service\saas;
+
+use app\extra\basic\Service;
+use app\model\saas\SaasOrder;
+
+class OrderService extends Service
+{
+    /**
+     *
+     * @return $this
+     */
+    public function setModel()
+    {
+        $this->mode = (new SaasOrder);
+        return $this;
+    }
+
+
+    /**
+     *
+     * @param array $param
+     * @return array
+     */
+    public function searchFilter(array $param = []): array
+    {
+        $filter = [];
+        !empty($param['openid']) && $filter[] = ["openid", '=', $param['openid']];
+        !empty($param['status']) && $filter[] = ["status", '=', $param['status']];
+        !empty($param['type']) && $filter[] = ["type", '=', $param['type']];
+        !empty($param['name']) && $filter[] = ["poi_name", 'like', "%{$param['name']}%"];
+        return $filter;
+    }
+
+}