1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
<?php


namespace app\api\controller;


use addons\epay\library\Service;
use app\common\controller\Api;
use think\Db;
use think\Log;

/**
 * 支付接口
 */
class Pay extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    protected function returnx()
    {
        //支付成功返回
        //你可以在这里定义你的提示信息,但切记不可在此编写逻辑
        $this->success("恭喜你!支付成功!", addon_url("epay/index/index"));

        return;
    }


    /**
     * @ApiTitle    (电子协议)
     * @ApiSummary  (电子协议)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Pay/Xieyi)
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功',
    'data':'电子协议'
    })
     */
    public function Xieyi()
    {
        $this->success('成功', Db::name('xieyi')->where('id', 1)->value('content'));
    }

    /**
     * @ApiTitle    (会员支付)
     * @ApiSummary  (会员支付)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Pay/VipWechat)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="vip_id", type="integer", required=true, description="会员id")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    })
     */
    public function VipWechat()
    {
        $UserId = $this->IsToken();
        $Params = $this->request->param();
        $OrderSn = $this->PayOrder();
        $Price = Db::name('vip')->where('id', $Params['vip_id'])->value('price');
        if ($Price == 0) $this->error('终身会员无法直接购买', 0);
        /*生成支付订单*/
        $Insert = [
            'vip_id' => $Params['vip_id'],
            'OrderSn' => $OrderSn,
            'user_id' => $UserId,
            'price' => $Price,
            'status' => 0,
            'createtime' => time()
        ];
        Db::name('order_vip')->insert($Insert);
        $domain = $this->request->domain();
        $data = [
            'amount' => $Price,
            'orderid' => $OrderSn,
            'type' => "wechat",
            'title' => "会员开通",
            'notifyurl' => $domain . "/api/pay/VipWechatNotify/paytype/wechat",
            'returnurl' => $domain . "/api/pay/returnx",
            'method' => "miniapp",
            'openid' => Db::name('user')->where('id', $UserId)->value('open_id')
        ];
        $return = \addons\epay\library\Service::submitOrder($data);
        echo $return;
    }

    /*
     * 会员支付回调
     */
    public function VipWechatNotify()
    {
        Log::init([
            'path' => LOG_PATH . 'WeChatPayLog',
            'type' => 'file'
        ]);
        Log::write('进入支付回调');
        $paytype = $this->request->param('paytype');
        $pay = Service::checkNotify($paytype);
        if (!$pay) {
            Log::write('签名错误');
            echo '签名错误';
            return;
        }
        $data = $pay->verify();
        try {
            $payamount = $paytype == 'alipay' ? $data['total_amount'] : $data['total_fee'] / 100;
            /*支付单号*/
            $out_trade_no = $data['out_trade_no'];
            Log::write('单号:' . $out_trade_no);
            Log::write('进入回调逻辑');
            $OrderInfo = Db::name('order_vip')->where('OrderSn', $out_trade_no)->find();
            if ($OrderInfo['status'] == 1) return $pay->success('支付成功');
            //你可以在此编写订单逻辑
            /*更改支付订单信息*/
            $UpdateOrder = ['transaction_id' => $data['transaction_id'], 'pay_time' => time(), 'status' => 1];
            Db::name('order_vip')->where('id', $OrderInfo['id'])->update($UpdateOrder);
            /*改变会员到期时间*/
            $UserInfo = Db::name('user')->where('id', $OrderInfo['user_id'])->find();
            $Days = Db::name('vip')->where('id', $OrderInfo['vip_id'])->value('days');
            if ($UserInfo['vip'] == $OrderInfo['vip_id']) $UpdateUser = ['exp_time' => (($UserInfo['exp_time'] == 0) ? time() : $UserInfo['exp_time']) + 86400 * $Days];
            else $UpdateUser = ['vip' => $OrderInfo['vip_id'], 'exp_time' => time() + 86400 * $Days];
            Db::name('user')->where('id', $OrderInfo['user_id'])->update($UpdateUser);
            Log::write('回调逻辑完成');
        } catch (Exception $e) {
        }
        echo $pay->success();
    }


    /**
     * @ApiTitle    (产品支付)
     * @ApiSummary  (产品支付)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Pay/GoodsWechat)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="goods_id", type="integer", required=true, description="产品ID")
     * @ApiParams   (name="hours", type="integer", required=true, description="购买时间(小时)/次品传/全款传0")
     * @ApiParams   (name="name", type="integer", required=true, description="姓名")
     * @ApiParams   (name="sex", type="integer", required=true, description="性别")
     * @ApiParams   (name="mobile", type="integer", required=true, description="手机号")
     * @ApiParams   (name="address", type="integer", required=true, description="地址")
     * @ApiParams   (name="write_image", type="integer", required=true, description="电子签名")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    })
     */
    public function GoodsWechat()
    {
        $UserId = $this->IsToken();
        $Params = $this->request->param();
        $OrderSn = $this->PayOrder();
        $Price = Db::name('goods')->where('id', $Params['goods_id'])->find();
        $UserInfo = Db::name('user')->where('id', $UserId)->find();
        if ($UserInfo['pi_id'] == 0) {
            $Bili = $Price['price'];
        } else {
            $Bili1 = Db::name('bili')->where('goods_id', $Params['goods_id'])->where('pi_id', $UserInfo['pi_id'])->value('bili');
            if (empty($Bili1)) $Bili = $Price['price'];
            else $Bili = $Bili1;
        }
        if ($Price['status'] == 2) if ($UserInfo['vip'] == 0) $this->error('您不是会员不可购买小时数,请成为会员购买小时数', 0);
        /*生成支付订单*/

        $Insert = [
            'goods_id' => $Params['goods_id'],
            'OrderSn' => $OrderSn,
            'user_id' => $UserId,
            'price' => ($Params['hours'] == 0) ? $Bili : ($Bili * $Params['hours']),
//            'price' => ($Price['status'] == 1) ? $Price['price'] : (($Params['hours'] / $Price['hours']) * $Price['price']),
            'status' => 0,
            'name' => $Params['name'],
            'sex' => $Params['sex'],
            'mobile' => $Params['mobile'],
            'address' => $Params['address'],
            'write_image' => $Params['write_image'],
            'minutes' => ($Price['status'] == 1) ? $Price['hours'] * 60 : $Params['hours'] * 60,
            'createtime' => time()
        ];
        Db::name('order_goods')->insert($Insert);
        $domain = $this->request->domain();
        $data = [
            'amount' => ($Params['hours'] == 0) ? $Bili : ($Bili * $Params['hours']),
            'orderid' => $OrderSn,
            'type' => "wechat",
            'title' => "产品购买",
            'notifyurl' => $domain . "/api/pay/GoodsWechatNotify/paytype/wechat",
            'returnurl' => $domain . "/api/pay/returnx",
            'method' => "miniapp",
            'openid' => Db::name('user')->where('id', $UserId)->value('open_id')
        ];
        $return = \addons\epay\library\Service::submitOrder($data);
        echo $return;
    }


    /*
     * 产品支付回调
     */
    public function GoodsWechatNotify()
    {
        $paytype = $this->request->param('paytype');
        $pay = Service::checkNotify($paytype);
        if (!$pay) {
            echo '签名错误';
            return;
        }
        $data = $pay->verify();
        try {
            $payamount = $paytype == 'alipay' ? $data['total_amount'] : $data['total_fee'] / 100;
            /*支付单号*/
            $out_trade_no = $data['out_trade_no'];
            /*订单信息*/
            $OrderInfo = Db::name('order_goods')->where('OrderSn', $out_trade_no)->find();
            /*产品信息*/
            $GoodsStatus = Db::name('goods')->where('id', $OrderInfo['goods_id'])->find();
            /*用户信息*/
            $UserInfo = Db::name('user')->where('id', $OrderInfo['user_id'])->find();
            if ($OrderInfo['status'] == 1) return $pay->success('支付成功');
            //你可以在此编写订单逻辑
            /*更改支付订单信息*/
            $UpdateOrder = ['transaction_id' => $data['transaction_id'], 'pay_time' => time(), 'status' => 1];
            Db::name('order_goods')->where('id', $OrderInfo['id'])->update($UpdateOrder);
            /*如果是全款产品 并且用户会员等级不为终身会员*/
            if ($GoodsStatus['status'] == 1) if ($UserInfo['vip'] != 3) Db::name('user')->where('id', $UserInfo['id'])->update(['exp_time' => 0, 'vip' => 3]);
            /*增加产品时间*/
            /*查询是否购买过*/
            $GoodsAfter = Db::name('goods_after')->where('user_id', $OrderInfo['user_id'])->where('plane_type_id', $GoodsStatus['plane_type_id'])->find();
            if (!empty($GoodsAfter))/*购买过*/ Db::name('goods_after')->where('id', $GoodsAfter['id'])->update(['SupMinutes' => $GoodsAfter['SupMinutes'] + $OrderInfo['minutes'], 'updatetime' => time()]);
            else Db::name('goods_after')->insert(['user_id' => $OrderInfo['user_id'], 'plane_type_id' => $GoodsStatus['plane_type_id'], 'SupMinutes' => $OrderInfo['minutes'], 'AfterMinutes' => 0, 'createtime' => time(), 'updatetime' => time()]);
        } catch (Exception $e) {
        }
        echo $pay->success();
    }


    /**
     * @ApiTitle    (爽约费用支付)
     * @ApiSummary  (爽约费用支付)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Pay/NoPriceWechat)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    })
     */
    public function NoPriceWechat()
    {
        $UserId = $this->IsToken();
        $OrderSn = $this->PayOrder();
        $Map['status'] = ['EQ', 2];
        $Map['type'] = ['EQ', 2];
        $Map['no_price_type'] = ['EQ', 1];
        $Map['user_id'] = ['EQ', $UserId];
        $IsNoPrice = Db::name('yuyue')->where($Map)->select();
        if (empty($IsNoPrice)) $this->error('您没有需要支付的爽约费用', 1);
        foreach ($IsNoPrice as $k => $v) {
            $YuyueIdsInfo[] = $v['id'];
            $Price[] = $v['no_price'];
        }
        /*生成支付订单*/
        $Insert = [
            'OrderSn' => $OrderSn,
            'user_id' => $UserId,
            'price' => array_sum($Price),
            'yuyue_ids' => implode(',', $YuyueIdsInfo),
            'status' => 0,
            'createtime' => time()
        ];
        Db::name('order_no_price')->insert($Insert);
        $domain = $this->request->domain();
        $data = [
            'amount' => array_sum($Price),
            'orderid' => $OrderSn,
            'type' => "wechat",
            'title' => "爽约费用结算",
            'notifyurl' => $domain . "/api/pay/NoPriceWechatNotify/paytype/wechat",
            'returnurl' => $domain . "/api/pay/returnx",
            'method' => "miniapp",
            'openid' => Db::name('user')->where('id', $UserId)->value('open_id')
        ];
        $return = \addons\epay\library\Service::submitOrder($data);
        echo $return;
    }


    /*
    * 爽约费用支付回调
    */
    public function NoPriceWechatNotify()
    {
        $paytype = $this->request->param('paytype');
        $pay = Service::checkNotify($paytype);
        if (!$pay) {
            echo '签名错误';
            return;
        }
        $data = $pay->verify();
        try {
            $payamount = $paytype == 'alipay' ? $data['total_amount'] : $data['total_fee'] / 100;
            /*支付单号*/
            $out_trade_no = $data['out_trade_no'];
            /*订单信息*/
            $OrderInfo = Db::name('order_no_price')->where('OrderSn', $out_trade_no)->find();
            if ($OrderInfo['status'] == 1) return $pay->success('支付成功');
            //你可以在此编写订单逻辑
            /*更改支付订单信息*/
            $UpdateOrder = ['transaction_id' => $data['transaction_id'], 'pay_time' => time(), 'status' => 1];
            Db::name('order_no_price')->where('id', $OrderInfo['id'])->update($UpdateOrder);
            /*更改爽约订单*/
            Db::name('yuyue')->where('id', 'In', $OrderInfo['yuyue_ids'])->where('user_id', $OrderInfo['user_id'])->update(['no_price_type' => 0, 'no_price' => 0]);
        } catch (Exception $e) {
        }
        echo $pay->success();
    }


    /**
     * @ApiTitle    (爽约费用支付)
     * @ApiSummary  (爽约费用支付)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Pay/TeacherWechat)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="id", type="integer", required=true, description="预约单id")
     * @ApiParams   (name="price", type="integer", required=true, description="打赏金额")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    })
     */
    public function TeacherWechat()
    {
        $UserId = $this->IsToken();
        $Id = input('id');
        $Price = input('price');
        $OrderSn = $this->PayOrder();
        $Map['id'] = ['EQ', $Id];
        $Map['user_id'] = ['EQ', $UserId];
        $Info = Db::name('yuyue')->where($Map)->find();
        /*生成支付订单*/
        $Insert = [
            'OrderSn' => $OrderSn,
            'user_id' => $UserId,
            'teacher_id' => $Info['teacher_id'],
            'price' => $Price,
            'yuyue_id' => $Info['id'],
            'status' => 0,
            'type' => 0,
            'createtime' => time()
        ];
        Db::name('order_price')->insert($Insert);
        $domain = $this->request->domain();
        $data = [
            'amount' => $Price,
            'orderid' => $OrderSn,
            'type' => "wechat",
            'title' => "教练打赏",
            'notifyurl' => $domain . "/api/pay/TeacherPriceWechatNotify/paytype/wechat",
            'returnurl' => $domain . "/api/pay/returnx",
            'method' => "miniapp",
            'openid' => Db::name('user')->where('id', $UserId)->value('open_id')
        ];
        $return = \addons\epay\library\Service::submitOrder($data);
        echo $return;
    }


    /*
    * 教练打赏支付回调
    */
    public function TeacherPriceWechatNotify()
    {
        $paytype = $this->request->param('paytype');
        $pay = Service::checkNotify($paytype);
        if (!$pay) {
            echo '签名错误';
            return;
        }
        $data = $pay->verify();
        try {
            $payamount = $paytype == 'alipay' ? $data['total_amount'] : $data['total_fee'] / 100;
            /*支付单号*/
            $out_trade_no = $data['out_trade_no'];
            /*订单信息*/
            $OrderInfo = Db::name('order_price')->where('OrderSn', $out_trade_no)->find();
            if ($OrderInfo['status'] == 1) return $pay->success('支付成功');
            //你可以在此编写订单逻辑
            /*更改支付订单信息*/
            $UpdateOrder = ['transaction_id' => $data['transaction_id'], 'pay_time' => time(), 'status' => 1];
            Db::name('order_price')->where('id', $OrderInfo['id'])->update($UpdateOrder);
        } catch (Exception $e) {
        }
        echo $pay->success();
    }
}