| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use blockchain\TronService;
- use blockchain\Web3Service;
- use fast\Random;
- use GuzzleHttp\Exception\RequestException;
- use think\Exception;
- use think\Validate;
- use think\Db;
- use app\common\model\MoneyLog;
- use Tron\Address;
- /**
- * 会员接口
- */
- class Pay extends Api
- {
- protected $noNeedLogin = ['notify', 'notifycz', 'notifydsf'];
- protected $noNeedRight = '*';
- public function _initialize()
- {
- parent::_initialize();
- $this->Usermodel = model('User');
- }
- public function index()
- {
- // // 启动事务
- // Db::startTrans();
- // try {
- if (input('id', 0) > 0) {
- $id = input('id');
- $data = Db::name('task')->where('id', $id)->find();
- if ($data) {
- $number = date('ymdh', time()) . rand(10000, 99999);//订单编号
- Db::name('task')->where('id', $id)->update(['out_trade_no' => $number, 'buytype' => input('buytype')]);
- $data = Db::name('task')->where('id', $id)->find();
- } else {
- $this->error(__('订单数据不对'));
- }
- } else {
- $number = date('ymdh', time()) . rand(10000, 99999);//订单编号
- if (!input('price')) {
- $this->error(__('金额不对'));
- }
- $datapost = input('param.');//订单数据
- $datapost['buzs'] = htmlspecialchars_decode($datapost['buzs']);
- if (input('endtime') == '数量完成自动结束') {
- $endtimesjc = 100000000000;
- } else {
- $endtimesjc = strtotime(input('endtime'));
- }
- $up = [
- 'type' => input('type'),
- 'uid' => input('uid'),
- 'name' => input('name'),
- 'rwurl' => input('rwurl'),
- 'buz' => $datapost['buzs'],
- 'price' => input('price'),
- 'sum' => input('sum'),
- 'sumsy' => input('sum'),
- 'endtime' => input('endtime'),
- 'subtime' => input('subtime'),
- 'shetime' => input('shetime'),
- 'endtimesjc' => $endtimesjc,
- 'buytype' => input('buytype'),
- 'zprice' => input('price') * input('sum'),
- 'out_trade_no' => $number,
- 'createtime' => time(),
- ];
- $id = Db::name('task')->insertGetId($up);
- $data = $up;
- }
- $site = config('site');
- $amount = $data['zprice'];
- if (!$amount) {
- $this->error(__('金额不对'));
- }
- // 异步通知地址
- $notify_url = $site['zfb']['notifyurl'];
- // 订单标题
- $subject = 'wool订单';
- // 订单详情
- $body = 'wool致力于打造最好的移动服务平台';
- // 订单号,示例代码使用时间值作为唯一的订单ID号
- if ($data['buytype'] == '支付宝') {
- $this->allpay($amount, $subject, $body, $number, $notify_url);
- } else if ($data['buytype'] == '余额') {
- $this->yuepay($amount, input('uid'), $id);
- }
- // // 提交事务
- // Db::commit();
- // } catch (\Exception $e) {
- // // 回滚事务
- // var_dump($e);
- // Db::rollback();
- // }
- }
- public function videopay()
- {
- $datavideo_order = Db::name('video_order')->where(['sid' => input('id'), 'uid' => input('uid'), 'lx' => 2])->find();
- $datavideo_order1 = Db::name('video_order')->where(['sid' => input('id'), 'uid' => input('uid'), 'lx' => 1])->find();
- $sad = 1;
- $lx = input('lx');
- if ($datavideo_order1) {
- if ($datavideo_order1['type'] == 2) {
- $sad = 2;
- $this->error(__('已经全集购买'));
- }
- }
- if ($datavideo_order and $lx == 2) {
- if ($datavideo_order['type'] == 2) {
- $this->error(__('已经单集购买'));
- }
- $number = date('ymdh', time()) . rand(10000, 99999);//订单编号
- $price = input('priceq');
- $pricebuy = input('pricebuy');
- if (input('lx') == 2) {
- $price = input('priced');
- $pricebuy = input('pricebuy');
- }
- $up = [
- 'type' => 1,
- 'lx' => $lx,//1 全章购买 2单集购买
- 'uid' => input('uid'),
- 'name' => input('namet'),
- 'img' => input('img'),
- 'price' => $price,
- 'sum' => input('sum'),
- 'sid' => input('pid'),
- 'smid' => input('smid'),
- 'paytype' => input('buytype'),
- 'zprice' => $pricebuy * input('sum'),
- 'out_trade_no' => $number,
- ];
- Db::name('video_order')->where(['id' => $datavideo_order['id']])->update($up);
- $id = $datavideo_order['id'];
- $data = $up;
- } else {
- $number = date('ymdh', time()) . rand(10000, 99999);//订单编号
- if (!input('pricebuy')) {
- $this->error(__('金额不对'));
- }
- $price = input('priceq');
- $pricebuy = input('pricebuy');
- if (input('lx') == 2) {
- $price = input('priced');
- $pricebuy = input('pricebuy');
- }
- $up = [
- 'type' => 1,
- 'lx' => $lx,//1 全章购买 2单集购买
- 'uid' => input('uid'),
- 'name' => input('namet'),
- 'img' => input('img'),
- 'price' => $price,
- 'sum' => input('sum'),
- 'sid' => input('pid'),
- 'smid' => input('smid'),
- 'paytype' => input('buytype'),
- 'zprice' => $pricebuy * input('sum'),
- 'out_trade_no' => $number,
- 'createtime' => time(),
- 'updatetime' => time(),
- ];
- //$this->error($up);
- $id = Db::name('video_order')->insertGetId($up);
- $data = $up;
- }
- $site = config('site');
- $amount = $data['zprice'];
- if (!$amount) {
- $this->error(__('金额不对'));
- }
- // 异步通知地址
- $notify_url = $site['zfb']['notifyurl'];
- // 订单标题
- $subject = 'wool订单';
- // 订单详情
- $body = 'wool致力于打造最好的移动服务平台';
- // 订单号,示例代码使用时间值作为唯一的订单ID号
- //var_dump($data['paytype']);
- if ($data['paytype'] == '支付宝') {
- $this->allpay($amount, $subject, $body, $number, $notify_url);
- } else if ($data['paytype'] == '余额') {
- $this->videoyuepay($amount, input('uid'), $id);
- } else if ($data['paytype'] == '微信') {
- $wxlx = input('wxlx');
- if ($wxlx == 'wxxcx') {
- $openid = input('openid');
- if (!$openid) {
- $this->error(__('openid不对'));
- }
- $this->paywxxcx($id, $number, $amount, $openid);
- } else if ($wxlx == 'wxh5') {
- $buytype = '微信h5';
- $this->paywxh5($id, $number, $amount, $buytype);
- } else if ($wxlx == 'wxgzh') {
- $buytype = '微信公众号';
- $this->paywxh5($id, $number, $amount, $buytype);
- }
- } else if ($data['paytype'] == '汇聚') {
- $paid_price = 0;
- // if ($recharId) {
- // $data = SystemGroupData::getDateValue($recharId);
- // if ($data === false) {
- // return app('json')->fail('您选择的充值方式已下架!');
- // } else {
- // $paid_price = $data['give_money'] ?? 0;
- // }
- // }
- // $rechargeOrder = UserRecharge::addRecharge($request->uid(), $price, 'huifu', $paid_price);
- $recharge = Huifu::wxJsPay(session('openid'), $number, $price, $paid_price, 'video_pay');
- // return app('json')->successful(['type' => $from, 'data' => $recharge]);
- }
- }
- public function paywxh5($oid, $number, $amount, $buytype)
- {
- $site = config('site');
- $user = $this->auth->getUser();
- if (!$user) {
- $this->error(__('user不对'));
- }
- if ($amount <= 0) {
- $this->error(__('金额不对'));
- }
- $up = [
- 'type' => 1,
- 'uid' => $user['id'],
- 'tdnum' => input('tdnum'),//支付通道
- 'amount' => $amount,
- 'out_trade_no' => $number,
- 'createtime' => time(),
- 'buytype' => $buytype,
- 'oid' => $oid,
- ];
- $id = Db::name('paylog')->insertGetId($up);
- $urls = $site['imgurl'] . '/index/paydsf/wxh5?out_trade_no=' . $number;
- if ($buytype == '微信公众号') {
- $urls = $site['imgurl'] . '/index/index/pay?id=' . $id;
- }
- $this->success('订单提交成功 正在跳转支付', $urls);
- }
- public function paywxxcx($oid, $number, $amount, $openid)
- {
- $site = config('site');
- $user = $this->auth->getUser();
- if (!$user) {
- $this->error(__('user不对'));
- }
- if ($amount <= 0) {
- $this->error(__('金额不对'));
- }
- $up = [
- 'type' => 1,
- 'uid' => $user['id'],
- 'amount' => $amount,
- 'out_trade_no' => $number,
- 'createtime' => time(),
- 'buytype' => '微信小程序',
- 'oid' => $oid,
- ];
- $id = Db::name('paylog')->insertGetId($up);
- $modelxcx = new \app\api\controller\Paywx;
- $trs = $modelxcx->wxxcx($number, $amount, $openid);
- $this->success('提交成功', $trs);
- }
- public function videoyuepay($money, $user_id, $id)//余额支付
- {
- $user = $this->Usermodel::get($user_id);
- if ($user && $money != 0) {
- $data = Db::name('video_order')->where('id', $id)->find();
- if ($user->money < $money) {
- $this->error(__('余额不足'));
- }
- $before = $user->money;
- $after = $user->money - $money;
- if ($data['type'] == 1) {
- Db::startTrans();
- try {
- //更新会员信息
- $user->save(['money' => $after]);
- //写入日志
- MoneyLog::create(['user_id' => $user_id, 'money' => $money, 'before' => $before, 'after' => $after, 'memo' => '用户消费']);
- Db::name('video_order')->where('id', $id)->update(['type' => 2, 'paytype' => '余额', 'ftime' => time()]);
- $this->yongjin($id, $user_id, $money, '级消费佣金结算');//佣金结算
- $this->dailiyongjin($id, $data['sid'], $data['smid'], $user_id, $money, '佣金结算');//后台代理佣金结算
- Db::commit();
- $this->success(__('操作成功'));
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- } else if ($data['type'] > 1) {
- $this->error(__('已经支付'));
- }
- } else {
- $this->error(__('金额不对'));
- }
- }
- public function yuepay($money, $user_id, $id)//余额支付
- {
- $user = $this->Usermodel::get($user_id);
- if ($user && $money != 0) {
- $data = Db::name('task')->where('id', $id)->find();
- if ($user->money < $money) {
- $this->error(__('余额不足'));
- }
- $before = $user->money;
- $after = $user->money - $money;
- //更新会员信息
- $user->save(['money' => $after]);
- //写入日志
- MoneyLog::create(['user_id' => $user_id, 'money' => $money, 'before' => $before, 'after' => $after, 'memo' => '用户消费']);
- if ($data['paytype'] == 1) {
- $up = [
- 'paytype' => 2,
- 'buytype' => '余额',
- 'ftime' => time()
- ];
- if (Db::name('task')->where('id', $id)->update($up)) {
- $this->success(__('操作成功'));
- }
- } else if ($data['paytype'] > 1) {
- $this->error(__('已经支付'));
- }
- } else {
- $this->error(__('金额不对'));
- }
- }
- public function czpay()
- {
- $site = config('site');
- $user = $this->auth->getUser();
- if (!$user) {
- $this->error(__('user不对'));
- }
- $number = date('ymdh', time()) . rand(10000, 99999);//订单编
- $amount = input('total', 0);
- $notify_url = $site['zfb']['notifyurlcz'];
- // 订单标题
- $subject = 'wool充值';
- // 订单详情
- $body = 'wool致力于打造最好的移动服务平台';
- if ($amount <= 0) {
- $this->error(__('金额不对'));
- }
- $up = [
- 'type' => 1,
- 'uid' => $user['id'],
- 'amount' => input('total'),
- 'out_trade_no' => $number,
- 'createtime' => time(),
- ];
- $id = Db::name('paylog')->insertGetId($up);
- if (input('type', 'allpay') == 'iap') {
- $this->success('请求成功', $number);
- } else {
- $this->allpay($amount, $subject, $body, $number, $notify_url);
- }
- }
- public function dsfczpay()
- {
- $site = config('site');
- $user = $this->auth->getUser();
- if (!$user) {
- $this->error(__('user不对'));
- }
- $number = date('ymdh', time()) . rand(10000, 99999);//订单编
- $amount = input('total', 0);
- if ($amount <= 0) {
- $this->error(__('金额不对'));
- }
- $up = [
- 'type' => 1,
- 'uid' => $user['id'],
- 'tdnum' => input('tdnum'),
- 'amount' => $amount,
- 'out_trade_no' => $number,
- 'createtime' => time(),
- 'buytype' => isset($site['dsfzf']['zfname']) ? $site['dsfzf']['zfname'] : '',
- ];
- $id = Db::name('paylog')->insertGetId($up);
- $urls = $site['imgurl'] . '/index/paydsf?out_trade_no=' . $number;
- $this->success('订单提交成功 正在跳转支付', $urls);
- }
- public function czpayiaptz()
- {
- $out_trade_no = input('out_trade_no');
- $order = Db::name('paylog')->where('out_trade_no', $out_trade_no)->find();
- //$this->dingding_log($order);
- if ($order['type'] == 1) {
- $up = [
- 'type' => 2,
- 'payment' => null,
- 'trade_no' => null,
- 'buyer' => null,
- 'buytype' => '苹果',
- 'ftime' => time()
- ];
- Db::startTrans();
- try {
- $this->money($order['amount'], $order['uid'], 0, $sxf = 0);
- Db::name('paylog')->where('out_trade_no', $out_trade_no)->update($up);
- Db::commit();
- $this->success('支付成功');
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- } else {
- $this->success('已经支付');
- }
- }
- public function allpay($amount, $subject, $body, $out_trade_no, $notify_url)//支付宝支付
- {
- $total = floatval($amount);
- if (!$total) {
- $this->error(__('金额不对'));
- }
- //require_once('/vendor/alipayrsa2/AopSdk.php');
- //Vendor('AopSdk.alipayrsa2');
- // require_once './vendor/alipayrsa2/aop/AopClient.php';
- // require_once './vendor/alipayrsa2/aop/request/AlipayTradeAppPayRequest.php';
- vendor('alipayrsa2.AopSdk');
- $site = config('site');
- $aop = new \AopClient;
- $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
- $aop->appId = $site['zfb']['appId'];
- //开发者私钥去头去尾去回车,一行字符串
- $aop->rsaPrivateKey = $site['zfb']['rsaPrivateKey'];
- $aop->format = "json";
- $aop->charset = "UTF-8";
- $aop->signType = "RSA2";
- //请填写支付宝公钥,一行字符串
- $aop->alipayrsaPublicKey = $site['zfb']['alipayrsaPublicKey'];
- //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.pay
- $request = new \AlipayTradeAppPayRequest();
- //SDK已经封装掉了公共参数,这里只需要传入业务参数
- $bizcontent = "{\"body\":\"" . $body . "\","
- . "\"subject\": \"" . $subject . "\","
- . "\"out_trade_no\": \"" . $out_trade_no . "\","
- . "\"timeout_express\": \"30m\","
- . "\"total_amount\": \"" . $total . "\","
- . "\"product_code\":\"QUICK_MSECURITY_PAY\""
- . "}";
- $request->setNotifyUrl($notify_url);
- $request->setBizContent($bizcontent);
- //这里和普通的接口调用不同,使用的是sdkExecute
- $response = $aop->sdkExecute($request);
- // 注意:这里不需要使用htmlspecialchars进行转义,直接返回即可
- //$this->success(__('获取成功'),$response);
- echo $response;
- }
- //支付宝回调
- public function notify()
- {
- if ($_POST) {
- //Db::transaction(function () {
- if ($_POST['trade_status'] == 'TRADE_SUCCESS' or $_POST['trade_status'] == 'TRADE_FINISHED') {//处理交易完成或者支付成功的通知 //获取订单号
- $out_trade_no = $_POST['out_trade_no'];//交易号
- $trade_no = $_POST['trade_no'];//订单支付时间
- $gmt_payment = $_POST['gmt_payment'];//转换为时间戳
- $order = Db::name('task')->where('out_trade_no', $out_trade_no)->find();
- if ($order['paytype'] == 1) {
- $up = [
- 'paytype' => 2,
- 'payment' => $gmt_payment,
- 'trade_no' => $trade_no,
- 'buyer' => $_POST['buyer_logon_id'],
- 'buytype' => '支付宝',
- 'ftime' => time()
- ];
- if (Db::name('task')->where('out_trade_no', $out_trade_no)->update($up)) {
- echo 'success';
- }
- } else {
- echo 'success';
- }
- }
- //});
- }
- }
- public function notifycz()
- {
- if ($_POST) {
- //$this->dingding_log($_POST);
- //Db::transaction(function () {
- if ($_POST['trade_status'] == 'TRADE_SUCCESS' or $_POST['trade_status'] == 'TRADE_FINISHED') {//处理交易完成或者支付成功的通知 //获取订单号
- $out_trade_no = $_POST['out_trade_no'];//交易号
- $trade_no = $_POST['trade_no'];//订单支付时间
- $gmt_payment = $_POST['gmt_payment'];//转换为时间戳
- $order = Db::name('paylog')->where('out_trade_no', $out_trade_no)->find();
- //$this->dingding_log($order);
- if ($order['type'] == 1) {
- $up = [
- 'type' => 2,
- 'payment' => $gmt_payment,
- 'trade_no' => $trade_no,
- 'buyer' => $_POST['buyer_logon_id'],
- 'buytype' => '支付宝',
- 'ftime' => time()
- ];
- Db::startTrans();
- try {
- $this->money($order['amount'], $order['uid'], 0, $sxf = 0);
- Db::name('paylog')->where('out_trade_no', $out_trade_no)->update($up);
- Db::commit();
- echo 'success';
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- } else {
- echo 'success';
- }
- }
- //});
- }
- }
- public function notifydsf()//第三方充值回调
- {
- $this->dingding_log($_REQUEST);
- //Db::transaction(function () {
- $returncode = isset($_REQUEST["returncode"]) ? $_REQUEST["returncode"] : 1;
- if ($returncode == "00") {//处理交易完成或者支付成功的通知 //获取订单号
- $out_trade_no = isset($_REQUEST["orderid"]) ? $_REQUEST["orderid"] : '';
- $order = Db::name('paylog')->where('out_trade_no', $out_trade_no)->find();
- $this->dingding_log($order);
- if ($order['type'] == 1) {
- $up = [
- 'type' => 2,
- 'ftime' => time()
- ];
- Db::startTrans();
- try {
- $this->money($order['amount'], $order['uid'], 0, $sxf = 0);
- Db::name('paylog')->where('out_trade_no', $out_trade_no)->update($up);
- Db::commit();
- echo 'OK';
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- } else {
- echo 'OK';
- }
- }
- //});
- }
- public function money($money, $user_id, $id, $sxf = 0)
- {
- $user = $this->Usermodel::get($user_id);
- if ($user && $money != 0) {
- $before = $user->money;
- $after = $user->money + $money;
- //更新会员信息
- $user->save(['money' => $after]);
- //写入日志
- MoneyLog::create(['user_id' => $user_id, 'money' => $money, 'before' => $before, 'after' => $after, 'fid' => $id, 'sxf' => $sxf, 'memo' => '用户充值']);
- } else {
- $this->error(__('金额不对'));
- }
- }
- public function dingding_log($content)
- {
- $r = $_SERVER['DOCUMENT_ROOT'] . '/api_log/' . date('Y-m-d_H-i-s', time()) . '.txt';
- $fp = fopen($r, 'w+');
- fwrite($fp, "执行日期:" . date('Y-m-d H:i:s', time()) . ' ' . var_export($content, true));
- fclose($fp);
- }
- public function FromXml($xml)
- {
- //将XML转为array
- //禁止引用外部xml实体
- libxml_disable_entity_loader(true);
- $values = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
- return $values;
- }
- public function sharepay()
- {
- // $datavideo_order=Db::name('video_share')->where(['vid'=>input('id'),'uid'=>input('uid')])->find();
- // $datavideo_order1=Db::name('video_share')->where(['sid'=>input('id'),'uid'=>input('uid'),'lx'=>1])->find();
- // $lx=input('lx');
- $num = input('num'); //购买的版权数量
- $uid = input('uid');
- $vid = input('vid');
- $user = Db::name('user')->where('id', $uid)->find();
- if (empty($user)){
- $this->error(__('用户不存在'));
- }
- if (empty($num)){
- $this->error(__('请输入数量'));
- }
- // 验证数量
- $user_num = Db::name('video_user')->where('uid', $uid)->where('vid', $vid)->find();
- if ($user_num) {
- $cs_num = $user_num['share'] + $num;
- if ($cs_num > 10 || $num > 10) {
- $this->error(__('单剧集购买版权不能超过10份'));
- }
- }
- $user_share = Db::name('videolist')->where('id', $vid)->value('share');
- if (empty($user_share)){
- $this->error(__('短剧不存在'));
- }
- $user_shares = bcsub($user_share , $num, 0);
- if ($user_shares < 0) {
- $this->error(__('该局总版权只剩下' . $user_share . '份'));
- }
- // if($datavideo_order){
- // if($datavideo_order['type']==2){
- // $this->error(__('该订单已支付'));
- // }
- // $number = date('ymdh', time()) . rand(10000, 99999);//订单编号
- // $price=input('priceq');
- // $pricebuy=input('pricebuy');
- // if(input('lx')==2){
- // $price=input('priced');
- // $pricebuy=input('pricebuy');
- // }
- // $up=[
- // 'type'=>1,
- // 'lx'=>$lx,//1 全章购买 2单集购买
- // 'uid'=>input('uid'),
- // 'name'=>input('namet'),
- // 'img'=>input('img'),
- // 'price'=>$price,
- // 'sum'=>input('sum'),
- // 'sid'=>input('pid'),
- // 'smid'=>input('smid'),
- // 'paytype'=>input('buytype'),
- // 'zprice'=>$pricebuy*input('sum'),
- // 'out_trade_no'=>$number,
- // ];
- // Db::name('video_order')->where(['id'=>$datavideo_order['id']])->update($up);
- // $id=$datavideo_order['id'];
- // $data=$up;
- // }else{
- $number = date('ymdh', time()) . rand(10000, 99999);//订单编号
- // if (!input('pricebuy')) {
- // $this->error(__('金额不对'));
- // }
- // $price = input('price');
- $price = Db::name('videolist')->where('id', input('vid'))->value('sharesprice');;
- // $pricebuy = input('pricebuy');
- // if(input('lx')==2){
- // $price=input('priced');
- // $pricebuy=input('pricebuy');
- // }
- // 获取当前时间的时间戳
- $now = time();
- // 计算三年后的时间戳
- $threeYearsLater = strtotime('+3 years', $now);
- $vname = Db::name('videolist')->where('id', input('vid'))->value('name');
- $total_price = $price * $num;
- $up = [
- 'type' => 1,
- 'uid' => input('uid'),
- 'vid' => input('vid'),
- 'vname' => $vname,
- 'num' => input('num'),
- 'price' => $price,
- 'total_price' => $total_price,
- 'paytype' => input('buytype'),
- 'out_trade_no' => $number,
- 'createtime' => time(),
- 'endtime' => $threeYearsLater,
- 'updatetime' => time(),
- ];
- //$this->error($up);
- $id = Db::name('video_share')->insertGetId($up);
- $data = $up;
- // }
- $site = config('site');
- $amount = $price;
- // $amount = $data['zprice'];
- // if (!$amount) {
- // $this->error(__('金额不对'));
- // }
- // 异步通知地址
- $notify_url = $site['zfb']['notifyurl'];
- // 订单标题
- $subject = 'wool订单';
- // 订单详情
- $body = 'wool致力于打造最好的移动服务平台';
- // 订单号,示例代码使用时间值作为唯一的订单ID号
- //var_dump($data['paytype']);
- if ($data['paytype'] == '支付宝') {
- $this->allpay($amount, $subject, $body, $number, $notify_url);
- } else if ($data['paytype'] == '余额') {
- $this->shareyuepay($amount, input('uid'), $id);
- } else if ($data['paytype'] == '微信') {
- $wxlx = input('wxlx');
- if ($wxlx == 'wxxcx') {
- $openid = input('openid');
- if (!$openid) {
- $this->error(__('openid不对'));
- }
- $this->paywxxcx($id, $number, $amount, $openid);
- } else if ($wxlx == 'wxh5') {
- $buytype = '微信h5';
- $this->paywxh5($id, $number, $amount, $buytype);
- } else if ($wxlx == 'wxgzh') {
- $buytype = '微信公众号';
- $this->paywxh5($id, $number, $amount, $buytype);
- } else if ($data['paytype'] == '汇聚') {
- $paid_price = 0;
- // if ($recharId) {
- // $data = SystemGroupData::getDateValue($recharId);
- // if ($data === false) {
- // return app('json')->fail('您选择的充值方式已下架!');
- // } else {
- // $paid_price = $data['give_money'] ?? 0;
- // }
- // }
- // $rechargeOrder = UserRecharge::addRecharge($request->uid(), $price, 'huifu', $paid_price);
- $recharge = Huifu::wxJsPay(session('openid'), $number, $price, $paid_price, 'share_pay');
- // return app('json')->successful(['type' => $from, 'data' => $recharge]);
- }
- }
- }
- // 判断订单的用户是否有上级以及判断推荐等级
- public function grade_sell($out_trade_no)
- {
- $order = Db::name('video_share')->where('out_trade_no', $out_trade_no)->find();
- $site = config('site');
- if ($order) {
- $user = Db::name('user')->where('id', $order['uid'])->find();
- while ($user['pid'] != 1) { //默认账号pid为1
- $user = Db::name('user')->where('id', $user['pid'])->find(); //上级用户信息
- $grade = $user['grade'];
- $video_user = Db::name('video_user')->where('uid', $user['id'])->where('vid', $order['vid'])->find();
- if (empty($video_user)) { //如果上级没有购买过该视频版权
- $up = [
- 'vid' => $order['vid'],
- 'vname' => $order['vname'],
- 'uid' => $user['id'],
- 'share' => 0,
- 'contribution' => 0,
- 'income' => 0,
- 'createtime' => time(),
- ];
- $id = Db::name('VideoUser')->insertGetId($up); //添加用户版权
- }
- $contribution = 0;
- $check = 0;
- // $sell = $video_user['sell'] + $order['num'];
- $sell = $user['sell'] + $order['num'];
- // switch ($video_user['grade']) {
- switch ($user['grade']) {
- case 0:
- $contribution = $site['gxz']['普通会员'] * $order['num'];
- if ($sell >= 30) {
- $check = 1;
- $sell = $sell - 30;
- }
- break;
- case 1:
- $contribution = $site['gxz']['初级合伙人'] * $order['num'];
- if ($sell >= 300) {
- $check = 1;
- $sell = $sell - 300;
- }
- break;
- case 2:
- $contribution = $site['gxz']['高级合伙人'] * $order['num'];
- if ($sell >= 3000) {
- $check = 1;
- $sell = $sell - 3000;
- }
- break;
- case 3:
- $contribution = $site['gxz']['董事'] * $order['num'];
- default:
- }
- if ($check == 1) {
- // $grade = $video_user['grade'] + 1;
- $grade = $user['grade'] + 1;
- }
- $after = $video_user['contribution'] + $contribution;
- // $res = Db::name('video_user')->where('uid', $user['id'])->where('vid', $order['vid'])->update(['sell' => $sell, 'contribution' => $after, 'grade' => $grade]);
- $res = Db::name('video_user')->where('uid', $user['id'])->where('vid', $order['vid'])->update(['contribution' => $after]);
- $res2 = Db::name('user')->where('id', $user['id'])->update(['grade' => $grade, 'sell' => $sell]);
- $up = [
- 'vid' => $order['vid'],
- 'vname' => $order['vname'],
- 'uid' => $user['id'],
- 'link_id' => $order['id'],
- 'num' => $contribution,
- 'after' => $contribution,
- 'content' => '下级用户购买版权,获得贡献值',
- 'createtime' => time(),
- 'pm' => 0,
- ];
- $id = Db::name('VideoContributionRecord')->insertGetId($up);
- }
- }
- }
- // 添加贡献值记录 订单id
- public function share_record($id)
- {
- $order = Db::name('video_share')->where('id', $id)->find();
- if ($order) {
- $user = Db::name('video_user')->where('uid', $order['uid'])->where('vid', $order['vid'])->find();
- $contribution = $order['num'] + $user['contribution'];
- $up = [
- 'vid' => $order['vid'],
- 'vname' => $order['vname'],
- 'uid' => $user['id'],
- 'link_id' => $order['id'],
- 'num' => $order['num'],
- 'after' => $contribution,
- 'content' => '用户购买版权,获得贡献值',
- 'createtime' => time(),
- 'pm' => 0,
- ];
- $id = Db::name('VideoContributionRecord')->insertGetId($up);
- $share = $user['share'] + $order['num'];
- // 添加版权和贡献值
- $res = Db::name('video_user')->where('uid', $user['uid'])->where('vid', $order['vid'])->update(['contribution' => $contribution, 'share' => $share]);
- }
- }
- public function shareyuepay($money, $user_id, $id)//版权余额支付
- {
- $user = $this->Usermodel::get($user_id);
- if ($user) {
- $data = Db::name('video_share')->where('id', $id)->find();
- if ($user->money < $money) {
- $this->error(__('余额不足'));
- }
- $before = $user->money;
- $after = $user->money - $money;
- if ($data['type'] == 1) {
- Db::startTrans();
- try {
- //更新会员信息
- $user->save(['money' => $after]);
- //写入日志
- MoneyLog::create(['user_id' => $user_id, 'money' => $money, 'before' => $before, 'after' => $after, 'memo' => '用户消费']);
- Db::name('video_share')->where('id', $id)->update(['type' => 2, 'paytype' => '余额', 'pay_time' => time()]);
- $video_user = Db::name('video_user')->where('uid', $user_id)->where('vid', $data['vid'])->find();
- if (empty($video_user)) { //判断用户是否购买过该视频版权
- $up = [
- 'vid' => $data['vid'],
- 'vname' => $data['vname'],
- 'uid' => $data['uid'],
- 'share' => $data['num'],
- 'contribution' => $data['num'],
- 'income' => 0,
- 'createtime' => time(),
- ];
- $id = Db::name('VideoUser')->insertGetId($up);
- }
- $this->share_record($id);//添加贡献值记录
- $this->yongjin($id, $user_id, $money, '级消费佣金结算');//佣金结算
- $this->dailiyongjin($id, $data['vid'], 0, $user_id, $money, '佣金结算');//后台代理佣金结算
- $this->grade_sell($data['out_trade_no']);//判断订单的用户是否有上级以及判断推荐等级)
- // 扣除短剧总版权
- $shares = Db::name('videolist')->where('id', $data['vid'])->value('share');
- $share = $shares - $data['num'];
- Db::name('videolist')->where('id', $data['vid'])->update(['share' => $share]);
- Db::commit();
- $this->success(__('操作成功'));
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- } else if ($data['type'] > 1) {
- $this->error(__('已经支付'));
- }
- } else {
- $this->error(__('用户不存在'));
- }
- }
- // u币充值(展示二维码)
- public function recharge()
- {
- $uid = input('uid');
- $recharge_type = input('type');
- $number = date('ymdh', time()) . rand(10000, 99999);//订单编号
- $user = Db::name('user')->where('id', $uid)->find();
- if (empty($user)) {
- $this->error(__('用户不存在'));
- }
- // $data = [
- // 'order_id'=> $number,
- // 'uid' => $uid,
- // 'paid' => 0,
- // 'add_time' => time()
- // ];
- $return = [];
- $address = Db::name('user_usdt_address')->where('uid', $uid)->find();
- if ($recharge_type == 'trx') {
- // $data['recharge_type']='trx';
- $return['address'] = $address['trx_address'];
- $return['16_address'] = $address['trx_16_address'];
- } elseif ($recharge_type == 'bsc') {
- // $data['recharge_type']='bsc';
- $return['address'] = $address['bsc_address'];
- }
- // Db::name('user_recharge')->insert($data);
- Db::name('user_usdt_address')->where('uid', $uid)->update(['last_recharge' => time()]);
- $this->success($return);
- }
- // 查询u币转账记录
- public function search_recharge()
- {
- $time = strtotime("-1 hours", time());
- $user_address = Db::name('user_usdt_address')->where('last_recharge', '>', $time)->order('last_recharge desc')->select();
- foreach ($user_address as $k => $v) {
- $user_info = Db::name('user')->where('id', $v['uid'])->find();
- // trx部分
- $service = TronService::instance('usdt');
- $time =bcmul($v['last_time'], 1000, 0);
- $info = $service->getTransfer($v['trx_address'], $time);
- @file_put_contents("usdt.txt",json_encode($info). "-trx转账记录\r\n", 8);
- foreach ($info['data'] as $k2 => $v2){
- $transaction_id = $v2['transaction_id'];
- $value =bcdiv($v2['value'], 1000000, 18);
- // $value3 = bcdiv($value, bcpow(10, get_token_info('bsc', 'USDT', 'decimal')), 12);
- // @file_put_contents("usdt.txt",$value3. "-另一种转换方式\r\n", 8);
- $be = DB::name('user_recharge')->where('hash', $transaction_id)->find();
- if ($be) { //hash重复
- continue;
- }
- $number = date('ymdh', time()) . rand(10000, 99999);//订单编号
- $data = [
- 'order_id' => $number,
- 'uid' => $v['uid'],
- 'paid' => 1,
- 'price' => $value,
- 'hash' => $transaction_id,
- 'recharge_type' => 'trx',
- 'pay_time' => time(),
- 'add_time' => time()
- ];
- Db::name('user_recharge')->insert($data);
- Db::name('user_usdt_address')->where('uid', $v['uid'])->update(['last_time' => time()]);
- $money = bcadd($user_info['money'], $value, 18);
- Db::name('user')->where('id', $v['uid'])->update(['money' => $money]);
- MoneyLog::create(['user_id' => $v['uid'], 'money' => $value, 'before' => $user_info['money'], 'after' => $money, 'memo' => '用户trx余额充值']);
- }
- // bsc部分
- $service = Web3Service::instance('bsc', 'usdt', '');
- $info = $service->getLastTransfer([], [$v['bsc_address']], $v['last_time']);
- @file_put_contents("usdt.txt",json_encode($info). "-bsc转账记录\r\n", 8);
- foreach ($info as $k2 => $v2) {
- $value = $v2->params['value']->toString(); //金额
- $value2 = bcdiv($value, bcpow(10, get_token_info('bsc', 'USDT', 'decimal')), 12);
- $transactionHash = $v2->transactionHash;
- $be = DB::name('user_recharge')->where('hash', $transactionHash)->find();
- if ($be) { //hash重复
- continue;
- }
- $number = date('ymdh', time()) . rand(10000, 99999);//订单编号
- $data = [
- 'order_id' => $number,
- 'uid' => $v['uid'],
- 'paid' => 1,
- 'price' => $value2,
- 'hash' => $transactionHash,
- 'recharge_type' => 'bsc',
- 'pay_time' => time(),
- 'add_time' => time()
- ];
- Db::name('user_recharge')->insert($data);
- Db::name('user_usdt_address')->where('uid', $v['uid'])->update(['last_time' => time()]);
- $money = bcadd($user_info['money'], $value2, 18);
- Db::name('user')->where('id', $v['uid'])->update(['money' => $money]);
- MoneyLog::create(['user_id' => $v['uid'], 'money' => $value2, 'before' => $user_info['money'], 'after' => $money, 'memo' => '用户bsc余额充值']);
- }
- }
- $this->success('查询完成');
- }
- public function withdrawal(){
- $uid = input('uid');
- $address = input('address');
- $type = input('type');
- $img = input('img');
- $price = input('price');
- if (empty($address)&&empty($img)){
- $this->error(__('请上传地址或上传收款码'));
- }
- Db::name('txjl')->insert(['uid' => $uid, 'address' => $address, 'type' => $type, 'img' => $img,'money'=> $price,'iscl'=>1]);
- $this->success('提交成功');
- }
- public function test()
- {
- $a= self::base58ToHex('TQ1MKAJqumx3XaFiMtnajGxrE2nFaMhsGu');
- $b = self::base58ToHex('TFRpdAs4SP1uoWcq8Kkx4769QQnJJGyPUX');
- // var_dump($a);
- // var_dump($b);die();
- $service = TronService::instance('usdt');
- $trxBalance = $service->balance('TQ1MKAJqumx3XaFiMtnajGxrE2nFaMhsGu'); //trx主币查询
- var_dump($trxBalance);
- $trcGasAddress = new Address(
- 'TQ1MKAJqumx3XaFiMtnajGxrE2nFaMhsGu',
- '0xed6a4d8cf092bee064b8fdd828980bdaf16700c483c5abfd6affb300c09a5423',
- // '4199f98810123e30a27100af641ccdf03e37bca669'
- $a
- );
- $balance = $service->tokenBalance($trcGasAddress); //trx代币查询
- var_dump($balance);
- $address = new Address(
- 'TFRpdAs4SP1uoWcq8Kkx4769QQnJJGyPUX',
- '0xf322ad257024b72be5a7bf09cbb58aec0bd89ee6de8bdd9858d4bcbdc0c84427',
- // '413be12e20eb06a9110999b6566437f90da137bebb'
- $b
- );
- $balance = $service->tokenBalance($address); //trx代币查询
- var_dump($balance);
- $res2 = $service->tokenTransfer($address, $trcGasAddress, 20); //主币转账
- var_dump($res2);
- // $res = $service->tokenTransfer($trcGasAddress, $address, 0.0002); //代币转账
- // var_dump($res);
- die();
- // bsc
- $service = Web3Service::instance('bsc', 'usdt','ba46d817b91ee27b701e1a02d2f2540ab8641d262650b6aaafe8caf2fa7b6d15');
- $balance = $service->getTokenBalance('0xbfc3b2193653ebf94bfa3fe586627b9c01f26db9'); //bsc代币余额
- var_dump($balance);
- $gas_balance =$service->getBalance('0xbfc3b2193653ebf94bfa3fe586627b9c01f26db9'); //bsc主币余额
- var_dump($gas_balance);
- $res = $service->transferToken('0x83105BDFAc1a15DF3C22D019Ac2F2937AC1Ef098', 2);
- var_dump($res);
- // $site = config('site');
- // var_dump($site['gxz']['普通会员']);
- // var_dump($site['gxz']['初级合伙人']);
- // var_dump($site['gxz']['高级合伙人']);
- // var_dump($site['gxz']['董事']);
- die();
- $service = Web3Service::instance('bsc', 'usdt', '');
- $info = $service->getLastTransfer([], ['0xbfc3b2193653ebf94bfa3fe586627b9c01f26db9'], 1748293200);
- $user_info = Db::name('user')->where('id', 1571)->find();
- foreach ($info as $k2 => $v2) {
- $value = $v2->params['value']->toString(); //金额
- $value2 = bcdiv($value, bcpow(10, get_token_info('bsc', 'TUSD', 'decimal')), 12);
- $transactionHash = $v2->transactionHash;
- $be = DB::name('user_recharge')->where('hash', $transactionHash)->find();
- if ($be) { //hash重复
- continue;
- }
- $number = date('ymdh', time()) . rand(10000, 99999);//订单编号
- $data = [
- 'order_id' => $number,
- 'uid' => $user_info['id'],
- 'paid' => 1,
- 'price' => $value2,
- 'hash' => $transactionHash,
- 'recharge_type' => 'bsc',
- 'pay_time' => time(),
- 'add_time' => time()
- ];
- Db::name('user_recharge')->insert($data);
- Db::name('user_usdt_address')->where('uid', $user_info['id'])->update(['last_time' => time()]);
- $money = bcadd($user_info['money'], $value2, 18);
- Db::name('user')->where('id', $user_info['id'])->update(['money' => $money]);
- MoneyLog::create(['user_id' => $user_info['id'], 'money' => $value2, 'before' => $user_info['money'], 'after' => $money, 'memo' => '用户bsc余额充值']);
- }
- // foreach ($info as $k => $v) {
- // $value = $v->params['value']->toString();
- // var_dump($value);
- // $transactionHash = $v->transactionHash;
- // var_dump($transactionHash);
- // $value = bcdiv($value, bcpow(10, get_token_info('bsc', 'TUSD', 'decimal')), 12); //把后面的0去掉
- // var_dump($value);
- // }
- var_dump($info);
- die();
- // trx TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t
- // $service = TronService::instance('usdt');
- //
- // $info = $service->getTransfer('TFRpdAs4SP1uoWcq8Kkx4769QQnJJGyPUX', 1748293200000);
- // $user_info = Db::name('user')->where('id', 1571)->find();
- // foreach ($info['data'] as $k2 => $v2){
- // $transaction_id = $v2['transaction_id'];
- // $value =bcdiv($v2['value'], 1000000, 0);
- // $be = DB::name('user_recharge')->where('hash', $transaction_id)->find();
- // if ($be) { //hash重复
- // continue;
- // }
- // $number = date('ymdh', time()) . rand(10000, 99999);//订单编号
- // $data = [
- // 'order_id' => $number,
- // 'uid' => 1571,
- // 'paid' => 1,
- // 'price' => $value,
- // 'hash' => $transaction_id,
- // 'type' => 'trx',
- // 'pay_time' => time(),
- // 'add_time' => time()
- // ];
- // Db::name('user_recharge')->insert($data);
- // Db::name('user_usdt_address')->where('uid', 1571)->update(['last_time' => time()]);
- // $money = bcadd($user_info['money'], $value, 18);
- // Db::name('user')->where('id', 1571)->update(['money' => $money]);
- // MoneyLog::create(['user_id' => 1571, 'money' => $value, 'before' => $user_info['money'], 'after' => $money, 'memo' => '用户trx余额充值']);
- //
- // }
- // var_dump($info);die();
- }
- public static function base58ToHex($base58) {
- // TRON 专用 Base58 字符集
- $alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
- // 1. 将 Base58 转换为十进制大数
- $num = '0';
- for ($i = 0; $i < strlen($base58); $i++) {
- $char = $base58[$i];
- $pos = strpos($alphabet, $char);
- if ($pos === false) {
- throw new Exception("无效的 Base58 字符: $char");
- }
- $num = bcmul($num, '58');
- $num = bcadd($num, (string)$pos);
- }
- // 2. 将十进制大数转换为十六进制
- $hex = '';
- while ($num !== '0') {
- $remainder = bcmod($num, '16');
- $hex = dechex((int)$remainder) . $hex;
- $num = bcdiv($num, '16', 0);
- }
- // 3. 添加前导零(每个前导'1'对应一个00)
- $leadingZeros = '';
- for ($i = 0; $i < strlen($base58) && $base58[$i] === '1'; $i++) {
- $leadingZeros .= '00';
- }
- // 4. 组合并确保长度为50字符(25字节)
- $hex = $leadingZeros . $hex;
- $hex = str_pad($hex, 50, '0', STR_PAD_LEFT);
- // 5. 提取地址部分并添加 TRON 前缀
- // 跳过版本字节(0x41)和校验码(最后4字节)
- $addressPart = substr($hex, 2, 40); // 取20字节地址
- return '41' . $addressPart; // TRON 地址以0x41开头
- }
- }
|