<?php

/* ========================================================================
 * $Id: pay.php 10467 2020-07-05 23:05:54Z onez $
 * 
 * Email: www@onez.cn
 * QQ: 6200103
 * HomePage: http://www.onez.cn
 * ========================================================================
 * Copyright 2016-2017 佳蓝科技.
 * 
 * ======================================================================== */

!defined('IN_ONEZ') && exit('Access Denied');
define('IS_ONEZ_DEBUG',1);
# pay
#请求数据
if($MODE=='$pay'){//在线支付
  $nickname=$G['nickname'];
  #代金券
  if($order['code']){
    $T=$G['this']->data()->open('coupons.codes')->one("key1='$order[code]'");
    $item=array();
    $item['index2']=time();
    $item['userid']=$order['userid'];
    if(!$order['userid']){
      $item['key8']=$item['guest']=onez('www.2020')->guest($order['ip']);
      $item['key9']=$item['guestIp']=$order['ip'];
    }
    $G['this']->data()->open('coupons.codes')->update($item,"id='$T[id]'");
    onez('www.2020')->addLog($nickname.'使用了代金券“'.$code.'”','codes');
  }
  #优惠券
  if($order['couponId']){
    $T=$G['this']->data()->open('coupons.users')->one("userid='$order[userid]' and id='$order[couponId]'");
    if($T){
      $c=$G['this']->data()->open('coupons')->one("id='$T[upid]'");
      if($c){
        $item=array();
        $item['time_used']=$item['index2']=time();
        $n=$G['this']->data()->open('coupons.users')->rows("upid='$T[upid]' and index2>0");
        $item['code_used']=$item['key2']=$T['key1'].'-'.($n+1);
        $item['orderno']=$item['key3']=$order['orderno'];
        if(!$order['userid']){
          $item['key8']=$item['guest']=onez('www.2020')->guest($order['ip']);
          $item['key9']=$item['guestIp']=$order['ip'];
        }
        $G['this']->data()->open('coupons.users')->update($item,"id='$T[id]'");
        onez('www.2020')->addLog($nickname.'使用了优惠券“'.$c['name'].'”','codes');
      }
    }
  }
  #商品
  if($order['type']=='code'){
    #每1元增加1成长值
    onez('www.2020')->growup($order['userid'],$order['price']);
    
    
    $T=$G['this']->data()->open('products')->one("id='$order[proId]'");
    if($T){
      #店主获得佣金
      onez('www.2020')->commission($T['userid'],$order['price'],'销售商品佣金'.$T['name']);
      $item=array();
      $item['sellnum']=$item['index1']=$T['index1']+1;
      $G['this']->data()->open('products')->update($item,"id='$T[id]'");
      
      $G['this']->addMessage($order['userid'],'购买了'.$T['name'].'商品','查看订单','/user/orders.php?orderno='.$order['orderno']);
      
      $item=array();
      $item['shopid']=$item['index12']=$T['userid'];#归属店铺
      $G['this']->data()->open('orders')->update($item,"id='$order[id]'");
      
      #测试包返还
      $T2=$G['this']->data()->open('orders')->one("upid='$T[id]' and key1='demo' and userid='$order[userid]'");
      if($T2){
        $item=array();
        $item['isback']=$item['index1']='1';
        $G['this']->data()->open('orders')->update($item,"id='$T2[id]'");
        
        onez('bs.money')->credit()->addIn($order['userid'],$T2['index4'],$T['token'].'商品正式购买DEMO测试包积分退还',array('paytype'=>$order['paytype'],'orderno'=>$order['orderno'],'key6'=>$order['orderno'],'nickname'=>$nickname,'key7'=>$nickname));
      }
      onez('www.2020')->sendMail($order['orderno'],$order['email']);
    }
  }
  #赠送积分
  if($order['totalCredit']){
    onez('bs.money')->credit()->addIn($order['userid'],$order['totalCredit'],$order['subject'],array('paytype'=>$order['paytype'],'orderno'=>$order['orderno'],'key6'=>$order['orderno'],'nickname'=>$nickname,'key7'=>$nickname));
  }
  
  
  if($order['orderType']=='charge'){#充值
    onez('bs.money')->money()->addIn($order['userid'],$order['price'],$order['subject'],array('paytype'=>$order['paytype'],'orderno'=>$order['orderno'],'key6'=>$order['orderno'],'nickname'=>$nickname,'key7'=>$nickname));
    $G['this']->addMessage($order['userid'],'您的账户储值'.$order['price'].'元成功','查看账目','/user/money.php');
  }else{#消费记录
    if($order['orderType']=='vip'){#开通VIP
      $gid=$order['gid'];
      $vip=$G['this']->data()->open('vips')->one("id='$gid'");
      if($vip){
        onez('www.2020')->addLog($G['this']->user($order['userid'],'nickname').'开通了VIP“'.$vip['name'].'”','vip');
        onez('www.2020')->vip_open($order['userid'],$vip);
        
        $G['this']->addMessage($order['userid'],'开通了VIP“'.$vip['name'].'”','查看特权','/user/vip.php');
      }
    }elseif($order['orderType']=='renew'){#续费VIP
      $gid=$order['gid'];
      $vip=$G['this']->data()->open('vips')->one("id='$gid'");
      if($vip){
        onez('www.2020')->addLog($G['this']->user($order['userid'],'nickname').'续费VIP“'.$vip['name'].'”','renew');
        onez('www.2020')->vip_set($order['userid'],array(
          'addday'=>$vip['day'],
        ));
      }
    }
    #if($order['price']>0 && $order['paytype']!='account'){
      onez('bs.money')->money()->addOut($order['userid'],$order['price'],$order['subject'],array('paytype'=>$order['paytype'],'orderno'=>$order['orderno'],'key6'=>$order['orderno'],'nickname'=>$nickname,'key7'=>$nickname));
    #}
  }
  return;
}elseif($MODE=='$post'){
  $action=onez()->gp('action');
  if($action=='data'){
    $orderno=onez()->gp('orderno');
    $order=$G['this']->data()->open('temp.orders')->one("token='$orderno'");
    $data=$order['data'];
    $data['orderno']=$orderno;
    $A['pagedata']=$data;
    $ac=onez()->gp('ac');
    if($ac=='pay'){
      $data['$file']=__FILE__;
      $data['userid']=$G['userid'];
      $data['qq']=onez()->gp('qq');
      $data['email']=onez()->gp('email');
      $data['paytype']=onez()->gp('paytype');
      $data['sitetoken']='www.2020';
      if($data['paytype']=='user'){
        $data['usertype']=onez()->gp('usertype');
        if($data['price']>0){
          return $this->error('代金券金额不足');
        }
      }
      $G['this']->data()->open('temp.orders')->update(array('data'=>$data),"id='$order[id]'");
      $A['data']=$data;
      $A['orderno']=$orderno;
      $pay=onez('bs.pay')->pay($data);
      $pay && $A=array_merge($A,$pay);
      return;
    }
    #$A['pagedata']['is_wxpay']=$G['this']->setting('is_wxpay')?true:false;
    $A['pagedata']['is_alipay']=true;
    #支付方式
    if(onez('bs')->is_mobile()){
      if(onez('bs')->is_weixin()){
        $A['pagedata']['is_alipay']=false;
      }else{
        $A['pagedata']['is_wxpay']=false;
      }
    }
    
    $paytypes=array();
    $A['pagedata']['is_alipay'] && $paytypes[]=array('name'=>'支付宝','token'=>'alipay',);
    $A['pagedata']['is_wxpay'] && $paytypes[]=array('name'=>'微信','token'=>'wxpay',);
    
    
    if($G['userid']){
      if($data['token']!='charge'){
        $paytypes[]=array('name'=>'余额支付','token'=>'account',);
      }
      #$paytypes[]=array('name'=>'人工转账','token'=>'user',);
    }
    #商品列表
    if($data['proId']){
      $T=$G['this']->data()->open('products')->one("id='$data[proId]'");
      $A['pagedata']['products'][]=array(
        'name'=>$T['name'],
        'image'=>$T['image'],
      );
    }
    if($data['carts']){
      foreach($data['carts'] as $v){
        $A['pagedata']['products'][]=array(
          'name'=>$v['title'],
          'image'=>$v['image'],
        );
      }
    }
    $A['pagedata']['paytypes']=$paytypes;
    $A['pagedata']['myuserid']=(int)$G['userid'];
    $A['pagedata']['money']=onez('bs.money')->getMoney($G['userid']);
    
    
  }
  return;
}
?>
<template v-if="$pagedata && $pagedata.paytypes">
<div class="item btns" v-if="pagedata.paytypes.length>0">
  <button type="button" :class="{active:item.token==payType}" v-for="item in pagedata.paytypes" @click="selPayType(item)">{{item.name}}</button>
</div>
<template v-if="payType=='user'&&pagedata.myuserid>0">
<div class="pay-user">
  <div class="layui-row layui-col-space10">
    <div class="layui-col-md4 qr">
      <img :src="pagedata.qrcode_alipay">
      <div class="block" v-if="userType!='alipay'" @click="setUserType('alipay')">
        <span>点击获取支付宝<br />平台收款码</span>
      </div>
    </div>
    <div class="layui-col-md4 qr">
      <img :src="pagedata.qrcode_wxpay">
      <div class="block" v-if="userType!='wxpay'" @click="setUserType('wxpay')">
        <span>点击获取微信<br />平台收款码</span>
      </div>
    </div>
    <div class="layui-col-md4 qr">
      <img :src="pagedata.qrcode_paypay">
      <div class="block" v-if="userType!='paypay'" @click="setUserType('paypay')">
        <span>点击获取PayPay<br />平台收款码</span>
      </div>
    </div>
    <div class="layui-col-md12">
      <div class="block">
        <p>人工转账说明</p>
        <p>1.点击上面可以获取平台收款码，可进行人工转账，注意备注平台账户！</p>
        <p>2.人工转账前与平台客服工作人员联系，正常工作日内一般10分钟到账！</p>
      </div>
    </div>
  </div>
</div>
</template>
<div class="item item-info">
  <onez-pictext :icon="item.image" :name="item.name" v-if="payType!='user' && pagedata.products" v-for="item in pagedata.products" />
  <van-cell title="商品名称" :value="pagedata.subject" />
  <van-cell title="支付方式" v-if="payType=='wxpay'" value="微信" />
  <van-cell title="支付方式" v-if="payType=='alipay'" value="支付宝" />
  <van-cell title="支付方式" v-if="payType=='account'" value="余额支付" />
  <van-cell title="支付方式" v-if="payType=='user'" value="人工转账" />
  <van-cell title="订单编号" :value="pagedata.orderno" />
  <van-cell title="订单金额" :value="pagedata.price" />
  <van-cell title="账户余额" v-if="pagedata.myuserid>0" :value="pagedata.money" />
  <!--<van-cell title="商品小计" v-if="pagedata.totalPrice" :value="pagedata.totalPrice" />
  <van-cell title="折扣优惠" v-if="pagedata.zhekou" :value="pagedata.zhekou" />
  <van-cell title="赠品积分" v-if="pagedata.totalCredit" :value="pagedata.totalCredit" />-->
</div>
<template v-if="pagedata.myuserid==0">
    <div class="item item-form">
      <div class="iconBox">
        <span class="icon iconfont icon-QQ"></span>
      </div>
      <input type="text" ref="qq" placeholder="请输入您的QQ信息"/>
    </div>
    <div class="item item-form">
      <div class="iconBox">
        <span class="layui-icon layui-icon-email"></span>
      </div>
      <input type="text" ref="email" placeholder="请输入您的电子邮箱"/>
    </div>
</template>
<div class="mainBtns">
<van-button type="info" v-if="payType=='user'" @click="pay">提交订单</van-button>
<van-button type="info" v-else @click="pay">确认付款</van-button>
</div>
</template>