123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630 |
- <?php
- namespace app\api\controller;
- use app\common\library\AliPay;
- use think\cache\driver\Redis;
- use think\Db;
- use think\Exception;
- use EasyWeChat\Factory;
- use think\Session;
- use function Sodium\add;
- class Order extends Base
- {
- public function initialize()
- {
- parent::initialize();
- parent::check_login();
- }
-
- public function createOrder(){
- if (!checkToken($this->uid)) $this->error('别处登录,请重新登录','',0,99);
- $id = input('id');
- $num = input('num',1);
- $pay_type = input('pay_type','wx');
- if (!$id || !$num) $this->error('参数错误');
- $user = getMemberInfoHash($this->uid);
- if ($user['is_auth']==0) $this->error('请实名认证后购买!');
- $coll_info = getCollectionInfoHash($id);
- if ($this->uid!='100000'){
- if ($coll_info['is_deleted']==1 || $coll_info['status']==0) $this->error('藏品已下架');
- }
- $inventory = getCollectionInventory($id);
- if ($inventory<1) $this->error('已售罄');
- if ($user['vip']==2){
- $advance_minutes = getAdvanceMinutes();
- $sell_time = strtotime($coll_info['sell_time'])-($advance_minutes*60);
- }else{
- $sell_time = strtotime($coll_info['sell_time']);
- }
- if ($sell_time>time()) $this->error('商品还未开始售卖!请耐心等待');
-
- $userByCount = getByCount($this->uid,$id);
- file_put_contents("validate_apple_pay_error.txt", '已购买数:'.$userByCount . "\n" . '购买数'.$num . "\n", FILE_APPEND);
- if ($coll_info['buy_count']<($userByCount+$num)) $this->error('每人限购'.$coll_info['buy_count'].'个');
-
- $hashCount = getLenCollection($id);
- if (!$hashCount) $this->error('hash未铸造,无法购买');
-
-
- if (redisSetNx($id)){
-
- loseCollectionInventory($id,$num);
-
- loseMembercard($this->uid);
-
- IncrByCount($this->uid,$id,$num);
- $com = true;
- Db::startTrans();
- try {
- $order_no = get_order_sn();
- $total_fee = bcmul($coll_info['price'],$num,2);
- $order_int =[
- 'mid' => $this->uid,
- 'c_id' => $id,
- 'inventory' => $coll_info['inventory'],
- 'order_no' => $order_no,
- 'num' => $num,
- 'pro_info' => json_encode($coll_info),
- 'pay_type' => $pay_type,
- 'pay_price' => $total_fee
- ];
- Db::name('store_order')->insert($order_int);
- $body = '象寻购买藏品';
- switch ($pay_type){
- case 'wx':
- $config = retrunWxConfig();
- $total_fee = $total_fee * 100;
- $config['notify_url'] = $this->request->root(true).'/api/Pay/WxOrderNotify';
- $app = Factory::payment($config);
- $result = $app->order->unify([
- 'body' => $body,
- 'out_trade_no' => $order_no,
- 'total_fee' => $total_fee,
- 'trade_type' => 'APP',
- ]);
- if ($result['return_code']=='SUCCESS'){
- $jssdk = $app->jssdk;
- $order1 = $jssdk->appConfig($result['prepay_id']);
-
- $retrun_data['order_no'] = $order_no;
- $retrun_data['pay'] = $order1;
-
- Db::name('store_collection')->where('id',$id)->setDec('now_inventory',$num);
-
-
- Db::commit();
- }else{
- $com=false;
- Db::rollback();
- }
- break;
- case 'zfb':
- $zfb = new AliPay();
- $notify_url = $this->request->root(true).'/api/Pay/alipayOrderNotify';
- $order = $zfb->aliPay($body, $total_fee, $order_no, $notify_url);
- $retrun_data['order_no'] = $order_no;
- $retrun_data['pay'] = $order;
-
- Db::name('store_collection')->where('id',$id)->setDec('now_inventory',$num);
-
-
- Db::commit();
- break;
- case 'yue':
- Db::name('store_collection')->where('id',$id)->setDec('now_inventory',$num);
- $retrun_data['order_no'] = $order_no;
- $retrun_data['pay'] = ['order_no'=>$order_no];
- Db::commit();
- break;
- }
- }catch (\Exception $e){
- $com=false;
- Db::rollback();
- }
- if ($com){
- DelRedisSetNx($id);
- $this->success('成功',$retrun_data);
- }else{
-
- addCollectionInventory($id,$num);
-
- addMembercard($this->uid);
-
- DecrByCount($this->uid,$id,$num);
- DelRedisSetNx($id);
- $this->error('服务器繁忙,请稍后重试');
- }
- }else{
- $this->error('服务器繁忙');
- }
- }
-
- public function getMyOrderList(){
- $status = input('status',0);
- $where = [
- 'is_deleted'=>0,
- 'status'=>$status,
- 'mid'=>$this->uid
- ];
- $count = Db::name('store_order')->where($where)->count();
- $list = Db::name('store_order')
- ->where($where)
- ->field('id,c_id,order_no,num,pro_info,status,create_at,pay_price,pay_type')
- ->order('id desc')
- ->limit($this->off_set,$this->page_num)
- ->select();
-
- $cancel_time = getCancelTime();
- foreach ($list as &$v){
- $v['pro_info'] = json_decode($v['pro_info'],true);
- if ($v['status']==0){
- $v['cancel_time'] = date('Y-m-d H:i:s',strtotime($v['create_at'])+($cancel_time*60));
- }
- }
- $this->success('成功',compact('count','list'));
- }
-
- public function MyOrderListDetail(){
- $order_id = input('order_id');
- if (!$order_id) $this->error('参数错误');
- $order = Db::name('store_order')
- ->where('id',$order_id)
- ->where('mid',$this->uid)
- ->where('is_deleted',0)
- ->find();
- if (!$order) $this->error('订单不存在');
-
- $cancel_time = getCancelTime();
- $order['pro_info'] = json_decode($order['pro_info'],true);
- if ($order['status']==0){
- $order['cancel_time'] = date('Y-m-d H:i:s',strtotime($order['create_at'])+($cancel_time*60));
- }
- $this->success('成功',$order);
- }
-
- public function walletPayOrder(){
- $order_no = input('order_no');
- $pay_pass = input('pay_pass');
- if (!$order_no) $this->error('参数错误');
- if (!$pay_pass) $this->error('请输入二级密码');
- $pay_way = Db::name('SystemPayWay')->where('code','yue')->field('status')->find();
- if(empty($pay_way)) $this->error('余额支付不存在');
- if($pay_way['status'] != 1) $this->error('余额支付已关闭');
- $order = Db::name('store_order')->where('order_no',$order_no)->where('is_deleted',0)->find();
- if (!$order) $this->error('订单不存在');
- if ($order['status']!=0) $this->error('订单已支付或已取消');
- if ($order['pay_type']!='yue') $this->error('该订单无法使用余额支付');
- $member = Db::name('store_member')
- ->where('id', $order['mid'])
- ->where('is_deleted',0)
- ->field('second_password,money')
- ->find();
- if(empty($member)) $this->error('用户信息不正确');
- if (empty($member['second_password'])) $this->error('未设置支付密码');
- if ($member['second_password']!=md5($pay_pass)) $this->error('密码错误');
- $money = $member['money'];
- $total_fee = $order['pay_price'];
- if($money < $total_fee) $this->error('账户余额不足');
- $result['out_trade_no'] = $order_no;
- $return = $this->dealData($result);
- if ($return){
- $this->success('支付成功');
- }else{
- $this->error('支付失败');
- }
- }
-
- function dealData($result){
- Db::startTrans();
- try {
- $order = Db::name('store_order')->where('order_no',$result['out_trade_no'])->find();
- Db::name('store_order')
- ->where('order_no',$result['out_trade_no'])
- ->update(['status'=>1,'pay_at'=>date('Y-m-d H:i:s'),'return_success_info'=>json_encode($result,true)]);
- $this->reduceMoney($order['mid'],$order['pay_price']);
- $info = Db::name('store_order_info')->where('order_id',$order['id'])->count();
- if ($info>=$order['num']){
- Db::commit();
- return true;
- }
- $array = [];
- for ($i=0;$i<$order['num'];$i++){
-
- $rank = getRanking($order['c_id'])+1;
- $tag = getTag($order['c_id'],$rank,$order['inventory']);
- saveRanking($order['c_id']);
- $company = '象寻数字科技(上海)有限公司';
- $company_hash = Db::name('hash2')->where('goods_id',$order['c_id'])->where('success',1)->field('hash,ddcid')->find();
- $company_hash_time = date('Y-m-d H:i:s');
- $collectors_hash = '';
- $date = [
- 'order_id'=>$order['id'],
- 'order_no'=>get_order_sn(),
- 'tag'=>$tag,
- 'mid'=>$order['mid'],
- 'c_id'=>$order['c_id'],
- 'pro_info'=>$order['pro_info'],
- 'company'=>$company,
- 'company_hash'=>$company_hash['hash'],
- 'company_hash_time'=>$company_hash_time,
- 'ddcid'=>$company_hash['ddcid'],
- 'collectors_hash'=>$collectors_hash,
- 'collectors_hash_time'=>'',
- 'type'=>2
- ];
- $array[] = $date;
- }
- Db::name('store_order_info')->insertAll($array);
-
- $by_collection_integral = getConfigValue('by_collection_integral');
- if ($by_collection_integral){
- $by_collection_integral = bcmul($by_collection_integral,$order['num']);
- memberMoneyChange($by_collection_integral,1,$order['mid'],'购买藏品',1,$order['id']);
- }
- Db::commit();
- return true;
- } catch (\Exception $e){
- Db::rollback();
- return false;
- }
- }
- function reduceMoney($mem_id,$money){
- if($money <= 0) return ['code'=>true,'message'=>'成功'];
- $old_money = Db::name('StoreMember') -> where('id',$mem_id)->field('id,money')->find();
- Db::name('StoreMember') -> where('id',$mem_id)->setDec('money',$money);
- $sre_data['mem_id']=$mem_id;
- $sre_data['change']=$money;
- $sre_data['pm']=0;
- $sre_data['old_money']=$old_money['money'];
- $sre_data['reason']='购买藏品';
- $sre_data['time']=time();
- Db::name('SystemMoneyRecord')->insert($sre_data);
- }
-
- public function payOrder(){
- $order_id = input('order_id');
- if (!$order_id) $this->error('参数错误');
- $order = Db::name('store_order')->where('id',$order_id)->where('is_deleted',0)->find();
- if (!$order) $this->error('订单不存在');
- if ($order['status']!=0) $this->error('订单已支付或已取消');
- $order_no = $order['order_no'];
- $total_fee = $order['pay_price'];
- $pay_type = $order['pay_type'];
- $body = '象寻购买藏品';
- switch ($pay_type){
- case 'wx':
- $config = retrunWxConfig();
- $total_fee = $total_fee * 100;
- $config['notify_url'] = $this->request->root(true).'/api/Pay/WxOrderNotify';
- $app = Factory::payment($config);
- $result = $app->order->unify([
- 'body' => $body,
- 'out_trade_no' => $order_no,
- 'total_fee' => $total_fee,
- 'trade_type' => 'APP',
- ]);
- if ($result['return_code']=='SUCCESS') {
- $jssdk = $app->jssdk;
- $order1 = $jssdk->appConfig($result['prepay_id']);
- Db::name('store_order')->where('order_no', $order_no)->update(['wx_order' => json_encode($order1, true)]);
- $retrun_data['order_no'] = $order_no;
- $retrun_data['pay'] = $order1;
- $this->success('成功',$retrun_data);
- }else{
- $this->error('调起支付失败,请稍后重试');
- }
- break;
- case 'zfb':
- $zfb = new AliPay();
- $notify_url = $this->request->root(true).'/api/Pay/alipayOrderNotify';
- $order1 = $zfb->aliPay($body, $total_fee, $order_no, $notify_url);
- $retrun_data['order_no'] = $order_no;
- $retrun_data['pay'] = $order1;
- $this->success('成功',$retrun_data);
- break;
- }
- }
-
- public function cancelOrder(){
- $order_id = input('order_id');
- if (!$order_id) $this->error('参数错误');
- $order = Db::name('store_order')->where('id',$order_id)->where('is_deleted',0)->find();
- if (!$order) $this->error('订单不存在');
- if ($order['status']!=0) $this->error('订单已支付或已取消');
- $com = true;
- Db::startTrans();
- try {
-
- Db::name('store_order')->where('id',$order_id)->update(['status'=>2,'cancel_at'=>date('Y-m-d H:i:s'),'cancel_state'=>2]);
-
- Db::name('store_collection')->where('id',$order['c_id'])->setInc('now_inventory',$order['num']);
-
- Db::name('store_member')->where('id',$order['mid'])->setInc('snap_card');
- Db::commit();
- }catch (\Exception $e){
- $com=false;
- Db::rollback();
- }
- if ($com){
-
- addCollectionInventory($order['c_id'],$order['num']);
-
- addMembercard($this->uid);
-
- DecrByCount($this->uid,$order['c_id'],$order['num']);
- $this->success('取消成功');
- }else{
- $this->error('取消失败,请稍后重试');
- }
- }
-
- public function getMySendList(){
- $where = [
- 'mid'=>$this->uid,
- ];
- $count = Db::name('store_order_info')->where($where)->whereIn('status','2,3')->count();
- $list = Db::name('store_order_info')
- ->where($where)
- ->whereIn('status','2,3')
- ->field('id,order_no,pro_info,over_time,create_at,status,to_mid,collectors_hash')
- ->order('id desc')
- ->limit($this->off_set,$this->page_num)
- ->select();
- foreach ($list as &$v){
- $v['pro_info'] = json_decode($v['pro_info'],true);
- $v['send_name'] = Db::name('store_member')->where('id',$v['to_mid'])->value('name');
- }
- $this->success('成功',compact('count','list'));
- }
-
- public function getMySendListDetail(){
- $order_id = input('order_id');
- if (!$order_id) $this->error('参数错误');
- $order = Db::name('store_order_info')
- ->where('id',$order_id)
- ->where('mid',$this->uid)
- ->find();
- if (!$order) $this->error('订单不存在');
- $order['pro_info'] = json_decode($order['pro_info'],true);
- $order['send_name'] = Db::name('store_member')->where('id',$order['to_mid'])->value('name');
- $this->success('成功',$order);
- }
- function savetest($order_no){
- $result['out_trade_no'] = $order_no;
- $order = Db::name('store_order')->where('order_no',$result['out_trade_no'])->find();
- if ($order['status']==0){
- Db::startTrans();
- try {
- Db::name('store_order')
- ->where('order_no',$result['out_trade_no'])
- ->update(['status'=>1,'pay_at'=>date('Y-m-d H:i:s'),'return_success_info'=>json_encode($result,true)]);
- $array = [];
- for ($i=0;$i<$order['num'];$i++){
-
- $rank = getRanking($order['c_id'])+1;
- $tag = getTag($order['c_id'],$rank,$order['inventory']);
- saveRanking($order['c_id']);
- $company = '象寻数字科技(上海)有限公司';
- $company_hash = '';
- $company_hash_time = '';
- $collectors_hash = '';
- $date = [
- 'order_id'=>$order['id'],
- 'order_no'=>get_order_sn(),
- 'tag'=>$tag,
- 'mid'=>$order['mid'],
- 'pro_info'=>$order['pro_info'],
- 'company'=>$company,
- 'company_hash'=>$company_hash,
- 'company_hash_time'=>$company_hash_time,
- 'collectors_hash'=>$collectors_hash,
- 'collectors_hash_time'=>date('Y-m-d H:i:s')
- ];
- $array[] = $date;
- }
- Db::name('store_order_info')->insertAll($array);
- Db::commit();
-
- } catch (\Exception $e){
- Db::rollback();
-
- }
- }
- }
- }
|