dever 5 lat temu
rodzic
commit
11833cae9c
3 zmienionych plików z 273 dodań i 1 usunięć
  1. 143 0
      database/order.php
  2. 19 1
      lib/Info.php
  3. 111 0
      lib/Pay.php

+ 143 - 0
database/order.php

@@ -0,0 +1,143 @@
+<?php
+
+$config = array
+(
+	# 表名
+	'name' => 'order',
+	# 显示给用户看的名称
+	'lang' => '商品订单',
+	'order' => 300,
+	'menu' => false,
+
+	# 数据结构 不同的字段放这里
+	'struct' => array
+	(
+		'id' 		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> 'ID',
+			'default' 	=> '',
+			'desc' 		=> '',
+			'match' 	=> 'is_numeric',
+			//'list'		=> true,
+		),
+
+		'uid'       => array
+        (
+            'type'      => 'int-11',
+            'name'      => '用户名',
+            'default'   => '0',
+            'desc'      => '请选择用户',
+            'match'     => 'is_numeric',
+            'update'    => 'text',
+            //'search'  => 'select',
+            'search'    => array
+            (
+                'api' => 'passport/user-all',
+                'col' => 'username',
+                'result' => 'id',
+            ),
+            'list'      => '{uid} > 0 ? Dever::load("passport/user-one#username", {uid}) : "匿名用户"',
+        ),
+
+		'info_id'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '商品ID',
+			'default' 	=> '',
+			'desc' 		=> '商品ID',
+			'match' 	=> 'is_numeric',
+			'update'	=> 'text',
+			'list'		=> true,
+		),
+
+		'name'		=> array
+		(
+			'type' 		=> 'varchar-100',
+			'name' 		=> '商品名称',
+			'default' 	=> '',
+			'desc' 		=> '商品名称',
+			'match' 	=> 'is_string',
+			'update'	=> 'text',
+			'list'		=> true,
+		),
+
+		'cash'		=> array
+		(
+			'type' 		=> 'varchar-100',
+			'name' 		=> '价格',
+			'default' 	=> '',
+			'desc' 		=> '价格',
+			'match' 	=> 'is_string',
+			'update'	=> 'text',
+			'list'		=> true,
+		),
+
+		'num'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '数量',
+			'default' 	=> '',
+			'desc' 		=> '数量',
+			'match' 	=> 'is_string',
+			'update'	=> 'text',
+			'list'		=> true,
+		),
+
+		'source'		=> array
+		(
+			'type' 		=> 'varchar-100',
+			'name' 		=> '来源',
+			'default' 	=> '',
+			'desc' 		=> '来源',
+			'match' 	=> 'is_string',
+			'update'	=> 'text',
+			//'list'		=> true,
+		),
+
+		'status'		=> array
+		(
+			'type' 		=> 'tinyint-1',
+			'name' 		=> '订单状态',
+			'default' 	=> '1',
+			'desc' 		=> '订单状态',
+			'match' 	=> 'is_numeric',
+		),
+
+		'state'		=> array
+		(
+			'type' 		=> 'tinyint-1',
+			'name' 		=> '数据状态',
+			'default' 	=> '1',
+			'desc' 		=> '请选择状态',
+			'match' 	=> 'is_numeric',
+		),
+
+		'cdate'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '发布时间',
+			'match' 	=> array('is_numeric', time()),
+			'desc' 		=> '',
+			# 只有insert时才生效
+			'insert'	=> true,
+			//'search'	=> 'date',
+			//'list'		=> 'date("Y-m-d H:i:s", {cdate})',
+		),
+	),
+
+	'manage' => array
+    (
+        'insert' => false,
+        'edit' => false,
+        'delete' => false,
+    ),
+
+    # request 请求接口定义
+	'request' => array
+	(
+		
+	),
+);
+
+return $config;

+ 19 - 1
lib/Info.php

@@ -155,6 +155,7 @@ class Info
         return $search;
     }
 
+    # 获取支付信息
     public function getPayState($uid, $id, $category)
     {
         $pay_where['category'] = $category;
@@ -238,7 +239,24 @@ class Info
         return array('id' => $id, 'pay' => $pay, 'cate' => $category, 'top' => $top, 'info' => $info);
     }
 
-    # 获取需求基本信息
+    # 获取支付所需要的信息
+    public function getPayInfo($id, $sku)
+    {
+        $info = Dever::db($this->table_info)->getOne($id);
+
+        if ($info && $sku > 0) {
+            $where['info_id'] = $info['id'];
+            $where['id'] = $sku;
+            $sku = Dever::db('goods/info_sku')->one($where);
+            if ($sku) {
+                $info['price'] = $sku['price'];
+            }
+        }
+
+        return $info;
+    }
+
+    # 获取基本信息
     public function getInfo($id, $attr = true, $order = 'view_reorder')
     {
         $info = Dever::db($this->table_info)->getOne($id);

+ 111 - 0
lib/Pay.php

@@ -0,0 +1,111 @@
+<?php
+
+# 支付
+
+namespace Goods\Lib;
+
+use Dever;
+
+class Pay
+{
+	public function action($uid, $id, $sku, $num, $source)
+    {
+        if (!$uid) {
+            Dever::alert('错误的用户信息');
+        }
+
+        $goods = Dever::load('goods/lib/info')->getPayInfo($id, $sku);
+
+        $user = Dever::db('passport/user')->one($uid);
+        $wechat = Dever::db('passport/wechat')->one(array('uid' => $uid, 'type' => 1, 'system_id' => 1));
+
+        if ($source == 'ios') {
+            $method = 'apple';
+            $account_id = 3;
+            # 使用苹果内购支付
+            $receipt = Dever::input('receipt');
+            if (!$receipt) {
+                Dever::alert('苹果内购支付失败,没有receipt参数');
+            }
+        } elseif ($source == 'android') {
+            $method = 'app';
+            $account_id = 3;
+        } elseif ($source == 'applet') {
+        	# 小程序支付
+            $method = 'applet';
+            $account_id = 2;
+        } else {
+        	# 默认是网页支付
+        	$method = 'page';
+        	$account_id = 1;
+        }
+        
+        $order_data['uid'] = $uid;
+        $order_data['status'] = 1;
+        $order_data['info_id'] = $goods['id'];
+        $order_data['name'] = $goods['name'];
+        $order_data['cash'] = $goods['price'];
+        $order_data['num'] = $num;
+        $order_data['source'] = $source;
+        $order_data['order_id'] = $this->getOrderId();
+        $id = Dever::db('goods/order')->insert($order_data);
+
+        if (!$id) {
+            Dever::alert('支付失败');
+        }
+
+        //$param参数
+        $param = array
+        (
+            'account_id' => $account_id,
+            'project_id' => 1,
+            'uid' => $uid,
+            'username' => $user['username'],
+            'name' => $order_data['name'],
+            'cash' => $order_data['cash'] * $order_data['num'],
+            //'cash' => '0.01',
+            'openid' => isset($wechat['openid']) ? $wechat['openid'] : '',
+            'product_id' => $goods['id'],
+            'order_id' => $order_data['order_id'],
+        );
+
+        if ($method == 'apple') {
+            $param['other'] = $receipt;
+        }
+
+        $result['pay'] = Dever::load('pay/api.' . $method, $param);
+
+        $result['order_id'] = $order_data['order_id'];
+
+        return $result;
+    }
+
+    # 生成订单号
+    public function getOrderId()
+    {
+        $update['order_id'] = $this->createOrderId();
+        $state = Dever::db('goods/order')->one($update);
+        if (!$state) {
+            return $update['order_id'];
+        } else {
+            return $this->getOrderId();
+        }
+    }
+
+    # 生成订单号
+    public function createOrderId()
+    {   
+        if (function_exists('session_create_id')) {
+            return strtoupper(session_create_id());
+        } else {
+            $charid = strtoupper(md5(uniqid(mt_rand(), true)));
+            return substr($charid, 0, 8) . substr($charid, 8, 4) . substr($charid, 12, 4) . substr($charid, 16, 4) . substr($charid, 20, 12);
+        }
+    }
+
+    # 临时订单号 无用
+    public function createTmpOrderId($prefix = '')
+    {
+        return $prefix . (strtotime(date('YmdHis', time()))) . substr(microtime(), 2, 6) . sprintf('%03d', rand(0, 999));
+    }
+}