ChannelRecharge.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  1. <?php
  2. /**
  3. * 渠道充值接口控制器
  4. *
  5. */
  6. namespace app\mcpsapi\controller;
  7. use app\mcpsapi\controller\Guild;
  8. use app\common\model\ChannelRecharge as ChannelRechargeModel;
  9. use app\common\library\WeixinPay;
  10. use think\Db;
  11. use think\Config;
  12. use app\common\model\Setting;
  13. use think\Exception;
  14. use app\common\library\MakeReport;
  15. use think\Loader;
  16. class ChannelRecharge extends Guild {
  17. protected $nowTime;
  18. protected $payRequestLimit = 5; //重复下单的限制时间
  19. protected $redis; //redis的句柄对象
  20. /**
  21. * 不进行父类的登录验证,所以增加构造方法重写了父类的初始化方法
  22. */
  23. public function _initialize()
  24. {
  25. parent::_initialize();
  26. $this->nowTime = NOW_TIMESTAMP;
  27. $this->redis = \think\Cache::store('default')->handler();
  28. }
  29. /**
  30. * 我的充值列表
  31. * @return [type] [description]
  32. */
  33. public function index()
  34. {
  35. //判断当前账号是否有此功能
  36. if(!in_array($this->_channelLevel,[1,2])){
  37. $this->jsonResult('', 0, '您无权限使用该功能');
  38. }
  39. $list_rows = $this->input('list_rows',10);
  40. $page = $this->input('page',1);
  41. $type = $this->input('type','0','intval'); //1(充值收入),2(充值支出),3(直充),4(结算充值收入)
  42. $account = $this->input('account','','trim'); //收入账号
  43. $apply_begin_time = $this->input('apply_begin_time','','trim');
  44. $apply_end_time = $this->input('apply_end_time','','trim');
  45. $finish_begin_time = $this->input('finish_begin_time','','trim');
  46. $finish_end_time = $this->input('finish_end_time','','trim');
  47. $download = $this->input('download',0,'intval');
  48. //判断是否选择交易类型
  49. if(!in_array($type,[1,2,3,4])){
  50. $this->jsonResult('', 0, '请选择交易类型');
  51. }
  52. $condition = [];
  53. if($account){
  54. $condition['channel_name'] = $account;
  55. }
  56. if($type==1){
  57. $condition['channel_id'] = $this->_channelId;
  58. $condition['recharge_type'] = 1;
  59. }
  60. else if($type==2){
  61. $condition['send_channel_id'] = $this->_channelId;
  62. $condition['recharge_type'] = 1;
  63. }
  64. else if($type==3){
  65. $condition['channel_id'] = $this->_channelId;
  66. $condition['recharge_type'] = 2;
  67. }
  68. else if($type==4){
  69. $condition['channel_id'] = $this->_channelId;
  70. $condition['recharge_type'] = 3;
  71. }
  72. //申请开始时间和结束时间不为空时
  73. if ($apply_begin_time != '' && $apply_end_time != '') {
  74. $condition['create_time'] = [
  75. ['>=', strtotime($apply_begin_time)],
  76. ['<=', strtotime($apply_end_time . ' 23:59:59')],
  77. ];
  78. } //开始时间不为空时
  79. elseif ($apply_begin_time != '') {
  80. $condition['create_time'] = ['>=', strtotime($apply_begin_time)];
  81. } //结束时间不为空时
  82. elseif ($apply_end_time != '') {
  83. $condition['create_time'] = ['<=', strtotime($apply_end_time . ' 23:59:59')];
  84. }
  85. //到账开始时间和结束时间不为空时
  86. if ($finish_begin_time != '' && $finish_end_time != '') {
  87. $condition['finish_time'] = [
  88. ['>=', strtotime($finish_begin_time)],
  89. ['<=', strtotime($finish_end_time . ' 23:59:59')],
  90. ];
  91. } //开始时间不为空时
  92. elseif ($finish_begin_time != '') {
  93. $condition['finish_time'] = ['>=', strtotime($finish_begin_time)];
  94. } //结束时间不为空时
  95. elseif ($finish_end_time != '') {
  96. $condition['finish_time'] = ['<=', strtotime($finish_end_time . ' 23:59:59')];
  97. }
  98. // var_dump($condition);
  99. if($download){
  100. $sql = model('ChannelRecharge')
  101. ->where($condition)
  102. ->whereRaw('channel_id='.$this->_channelId.' or send_channel_id='.$this->_channelId)
  103. ->order("id desc")
  104. ->fetchSql(true)
  105. ->select();
  106. // echo $sql;
  107. if ((new MakeReport())->addTask('guild.ChannelRechargeIndex', $sql, 'cps'.session('guild_info')['id'])){
  108. $this->jsonResult('', 20000, '报表生成的任务已经提交, 报表生成完成后,会及时通知您,请耐心稍等');
  109. }
  110. else{
  111. $this->jsonResult('', 20013, '报表生成任务不可重复提交,如遇到无法导出情况,建议修改查询条件解除当前状态,提交重新生成报表任务!');
  112. }
  113. }
  114. $rechargeList = model('ChannelRecharge')
  115. ->where($condition)
  116. ->whereRaw('channel_id='.$this->_channelId.' or send_channel_id='.$this->_channelId)
  117. ->order("id desc")
  118. ->paginate(['list_rows'=>$list_rows,'page'=>$page])
  119. ->toArray();
  120. // echo model('ChannelRecharge')->getLastSql()."-----lastsql------<br>";
  121. $this->jsonResult($rechargeList, 20000, '获取列表成功');
  122. }
  123. /**
  124. * 充值审核列表
  125. * @return [type] [description]
  126. */
  127. public function checklist()
  128. {
  129. //判断当前账号是否有此功能
  130. if(!in_array($this->_channelLevel,[1])){
  131. $this->jsonResult('', 0, '您无权限使用该功能');
  132. }
  133. $list_rows = input('list_rows',10);
  134. $page = input('page',1);
  135. $account = $this->input('account','','trim');
  136. $orderid = $this->input('orderid','','trim');
  137. $apply_begin_time = $this->input('apply_begin_time','','trim');
  138. $apply_end_time = $this->input('apply_end_time','','trim');
  139. $download = $this->input('download',0,'intval');
  140. $condition = [];
  141. $condition['send_channel_id'] = $this->_channelId;
  142. if($orderid){
  143. $condition['orderid'] = $orderid;
  144. }
  145. if($account){
  146. $condition['channel_name'] = $account;
  147. }
  148. //申请开始时间和结束时间不为空时
  149. if ($apply_begin_time != '' && $apply_end_time != '') {
  150. $condition['create_time'] = [
  151. ['>=', strtotime($apply_begin_time)],
  152. ['<=', strtotime($apply_end_time . ' 23:59:59')],
  153. ];
  154. } //开始时间不为空时
  155. elseif ($apply_begin_time != '') {
  156. $condition['create_time'] = ['>=', strtotime($apply_begin_time)];
  157. } //结束时间不为空时
  158. elseif ($apply_end_time != '') {
  159. $condition['create_time'] = ['<=', strtotime($apply_end_time . ' 23:59:59')];
  160. }
  161. if($download){
  162. $sql = model('ChannelRecharge')
  163. ->where($condition)
  164. ->order("id desc")
  165. ->fetchSql(true)
  166. ->select();
  167. // echo $sql;
  168. if ((new MakeReport())->addTask('guild.ChannelRechargeChecklist', $sql, 'cps'.session('guild_info')['id'])){
  169. $this->jsonResult('', 20000, '报表生成的任务已经提交, 报表生成完成后,会及时通知您,请耐心稍等');
  170. }
  171. else{
  172. $this->jsonResult('', 20013, '报表生成任务不可重复提交,如遇到无法导出情况,建议修改查询条件解除当前状态,提交重新生成报表任务!');
  173. }
  174. }
  175. $rechargeList = model('ChannelRecharge')
  176. ->where($condition)
  177. ->order("id desc")
  178. ->paginate(['list_rows'=>$list_rows,'page'=>$page])
  179. ->toArray();
  180. $this->jsonResult($rechargeList, 20000, '获取列表成功');
  181. }
  182. /**
  183. * 充值审核(查看)
  184. */
  185. public function audit(){
  186. if ($this->request->isPost()) {
  187. $id = intval($this->input('id')); //渠道ID
  188. if(!in_array($this->_channelLevel,[1])){
  189. $this->jsonResult('', 0, '您不能进行该操作');
  190. }
  191. if(!$id){
  192. $this->jsonResult('', 0, '请选择要审核的记录');
  193. }
  194. $adminInfo = model("ChannelAdmin")->field("id,username,channel_id,pay_password,secondary_password")->where(['id'=>session('guild_info')['id'],'channel_id'=>$this->_channelId])->find();
  195. if(!$adminInfo){
  196. $this->jsonResult('', 0, '账号异常,请重新登录后再试');
  197. }
  198. else if(!$adminInfo['pay_password'] || !$adminInfo['secondary_password']){
  199. $this->jsonResult('', 0, '请设置交易密码和二级密码后才能进行审核');
  200. }
  201. $rechargeInfo = model('ChannelRecharge')->where(['id'=>$id,'send_channel_id'=>$this->_channelId])->find();
  202. if(empty($rechargeInfo)){
  203. $this->jsonResult('', 0, '记录不存在或您无权限操作');
  204. }
  205. $this->jsonResult($rechargeInfo, 20000, '获取记录成功');
  206. exit;
  207. }
  208. else{
  209. $this->jsonResult('', 0, '非法请求');
  210. }
  211. }
  212. //充值审核(操作)
  213. function doAudit(){
  214. if ($this->request->isPost()) {
  215. $id = intval($this->input('id')); //渠道ID
  216. $status = intval($this->input('status')); //审核状态:1(审核通过),2(审核不通过)
  217. $remark = $this->input('remark','','trim'); //审核备注
  218. $pay_password = $this->input('pay_password','','trim'); //交易密码
  219. if(!in_array($this->_channelLevel,[1])){
  220. $this->jsonResult('', 0, '您不能进行该操作');
  221. }
  222. if(!$id){
  223. $this->jsonResult('', 0, '请选择要审核的记录');
  224. }
  225. if($status<>1 && $status<>2){
  226. $this->jsonResult('', 0, '非法审核状态');
  227. }
  228. if(!$pay_password){
  229. $this->jsonResult('', 0, '请输入交易密码');
  230. }
  231. $adminInfo = model("ChannelAdmin")->field("id,username,channel_id,pay_password,secondary_password")->where(['id'=>session('guild_info')['id'],'channel_id'=>$this->_channelId])->find();
  232. if(!$adminInfo){
  233. $this->jsonResult('', 0, '账号异常,请重新登录后再试');
  234. }
  235. else if(!$adminInfo['pay_password'] || !$adminInfo['secondary_password']){
  236. $this->jsonResult('', 0, '请设置交易密码和二级密码后才能进行审核');
  237. }
  238. else if($adminInfo['pay_password'] != mg_password($pay_password)){
  239. $this->jsonResult('', 0, '交易密码错误!');
  240. }
  241. $rechargeInfo = model('ChannelRecharge')->where(['id'=>$id,'send_channel_id'=>$this->_channelId])->find();
  242. if(empty($rechargeInfo)){
  243. $this->jsonResult('', 0, '记录不存在或您无权限操作');
  244. }
  245. else if($rechargeInfo['status']<>0){
  246. $this->jsonResult('', 0, '该记录已审核过');
  247. }
  248. $channelInfo = model("Channel")->where(['id'=>$this->_channelId])->find();
  249. if($channelInfo){
  250. if($status==1){
  251. if($channelInfo['amount'] < $rechargeInfo['amount']){
  252. $this->jsonResult('', 0, '您当前余额不足,不能进行审核发币');
  253. }
  254. }
  255. }
  256. else{
  257. $this->jsonResult('', 0, '您的账号有异常,请联系管理员');
  258. }
  259. $auditData = array();
  260. $auditData['id'] = $id;
  261. $auditData['status'] = $status;
  262. $auditData['finish_time'] = NOW_TIMESTAMP;
  263. $auditData['check_remark'] = $remark;
  264. $auditData['check_admin_type'] = 2;
  265. $auditData['check_admin_name'] = $this->_channelName;
  266. Db::startTrans();
  267. try {
  268. //审核通过
  269. if($status==1){
  270. $detData = array();
  271. $detData['channel_id'] = $this->_channelId;
  272. $detData['channel_name'] = $this->_channelName;
  273. $detData['change_amount'] = -$rechargeInfo['amount'];
  274. $detData['account_type'] = 1; //通用账户
  275. $detData['type'] = 5; //充值支出
  276. $detData['out_orderid'] = $rechargeInfo['orderid'];
  277. $detData['create_time'] = NOW_TIMESTAMP;
  278. $insertDetId = model('ChannelAccountDet')->insertGetId($detData);
  279. if ( !$insertDetId) {
  280. throw new Exception("添加账户变动明细失败");
  281. }
  282. $updData = array();
  283. $updData['amount'] = Db::raw("amount-".$rechargeInfo['amount']);
  284. $updData['update_time'] = time();
  285. $updFromResult = model('Channel')->where(['id'=>$this->_channelId,'amount'=>array('egt',$rechargeInfo['amount'])])->update($updData);
  286. if (!$updFromResult) {
  287. throw new Exception("账户金额变动失败");
  288. }
  289. $detData = array();
  290. $detData['channel_id'] = $rechargeInfo['channel_id'];
  291. $detData['channel_name'] = $rechargeInfo['channel_name'];
  292. $detData['change_amount'] = $rechargeInfo['amount'];
  293. $detData['account_type'] = 1; //通用账户
  294. $detData['type'] = 4; //充值收入
  295. $detData['out_orderid'] = $rechargeInfo['orderid'];
  296. $detData['create_time'] = NOW_TIMESTAMP;
  297. $insertDetId = model('ChannelAccountDet')->insertGetId($detData);
  298. if ( !$insertDetId) {
  299. throw new Exception("添加账户变动明细失败");
  300. }
  301. $updData = array();
  302. $updData['amount'] = Db::raw("amount+".$rechargeInfo['amount']);
  303. $updData['update_time'] = time();
  304. $updToResult = model('Channel')->where(['id'=>$rechargeInfo['channel_id']])->update($updData);
  305. if (!$updToResult) {
  306. throw new Exception("账户金额变动失败");
  307. }
  308. $auditData['finish_status'] = 1;
  309. $result = model('ChannelRecharge')->where(['id'=>$id,'status'=>array('in',[0])])->update($auditData);
  310. if (!$result) {
  311. throw new Exception("充值审核失败");
  312. }
  313. }
  314. else if($status==2){ //审核不通过
  315. $result = model('ChannelRecharge')->where(['id'=>$id,'status'=>array('in',[0])])->update($auditData);
  316. if (!$result) {
  317. throw new Exception("充值审核失败");
  318. }
  319. }
  320. Db::commit();
  321. } catch (\Exception $e) {
  322. Db::rollback();
  323. $this->jsonResult('', 0, '添加失败: ' . $e->getMessage());
  324. }
  325. $this->jsonResult('', 20000, '审核成功');
  326. exit;
  327. }
  328. else{
  329. $this->jsonResult('', 0, '非法请求');
  330. }
  331. }
  332. /**
  333. * 充值页面
  334. */
  335. public function recharge()
  336. {
  337. if ($this->request->isPost()) {
  338. //判断当前账号是否有充值权限
  339. if(!in_array($this->_channelLevel,[1,2])){
  340. $this->jsonResult('', 0, '您无权限进行充值相关操作');
  341. }
  342. else{
  343. $channelInfo = model("Channel")->field("id,name,level,parent_id,amount,js_amount")->where(['id'=>$this->_channelId])->find();
  344. if(!$channelInfo){
  345. $this->jsonResult('', 0, '账号异常,请重新登录后再试');
  346. }
  347. $adminInfo = model("ChannelAdmin")->field("id,username,channel_id,pay_password,secondary_password")->where(['id'=>session('guild_info')['id'],'channel_id'=>$this->_channelId])->find();
  348. if(!$adminInfo){
  349. $this->jsonResult('', 0, '账号异常,请重新登录后再试');
  350. }
  351. else if(!$adminInfo['secondary_password']){
  352. $this->jsonResult('', 0, '请先设置二级密码后才能进行操作');
  353. }
  354. //判断支付金额
  355. $condis = array();
  356. $condis['channel_id'] = $this->_channelId;
  357. $condis['begin_time'] = array('elt',$this->nowTime);
  358. $condis['end_time'] = array('egt',$this->nowTime);
  359. $rechargeRatio = model("ChannelRechargeRatio")->where($condis)->find();
  360. if(!empty($rechargeRatio) && $rechargeRatio['ratio']){
  361. $rechargeRatio = $rechargeRatio['ratio'];
  362. }
  363. else{
  364. $rechargeRatio = 100;
  365. }
  366. $channelInfo['recharge_ratio'] = $rechargeRatio;
  367. $this->jsonResult($channelInfo, 20000, '获取充值相关信息成功');
  368. }
  369. exit;
  370. }
  371. else{
  372. $this->jsonResult('', 0, '非法请求');
  373. exit;
  374. }
  375. }
  376. /**
  377. * 充值操作处理
  378. */
  379. public function doRecharge()
  380. {
  381. if ($this->request->isPost()) {
  382. $amount = $this->input('amount'); //充值金额
  383. $real_amount = $this->input('real_amount'); //支付金额
  384. $recharge_type = $this->input('recharge_type'); //充值方式:1(申请),2(直充),3(结算币充值)
  385. $paytype = $this->input('paytype'); //支付类型:zfb(支付宝),wxpay(微信)
  386. $out_order_no = $this->input('out_order_no'); //交易凭证
  387. $remark = $this->input('remark'); //备注
  388. $orderid = 'R'.makeOrderid();
  389. $checkResult = $this->validate($this->input,'ChannelRecharge.add');
  390. if (true !== $checkResult) {
  391. $this->jsonResult('', 0, $checkResult);
  392. }
  393. //判断当前账号是否有充值权限
  394. if(!in_array($this->_channelLevel,[1,2])){
  395. $this->jsonResult('', 0, '您不能进行充值');
  396. }
  397. if(!in_array($recharge_type,[1,2,3])){
  398. $this->jsonResult('', 0, '非正确的充值方式');
  399. }
  400. else{
  401. if($recharge_type==2 && !in_array($paytype,['zfb','wxpay'])){
  402. $this->jsonResult('', 0, '非正确的支付类型');
  403. }
  404. }
  405. if($this->_channelLevel==2 && $recharge_type<>1){
  406. $this->jsonResult('', 0, '您只能使用申请方式充值');
  407. }
  408. if(abs(intval($real_amount*100)/100-$real_amount) > 0.01){
  409. $this->jsonResult('', 0, '请正确填写支付金额!');
  410. }
  411. else{
  412. //判断支付金额
  413. $condis = array();
  414. $condis['channel_id'] = $this->_channelId;
  415. $condis['begin_time'] = array('elt',$this->nowTime);
  416. $condis['end_time'] = array('egt',$this->nowTime);
  417. $rechargeRatio = model("ChannelRechargeRatio")->where($condis)->find();
  418. if(!empty($rechargeRatio) && $rechargeRatio['ratio']){
  419. $rechargeRatio = $rechargeRatio['ratio'];
  420. }
  421. else{
  422. $rechargeRatio = 100;
  423. }
  424. $discountAmt = floatval($amount*$rechargeRatio/100);
  425. if($discountAmt <> $real_amount){
  426. $this->jsonResult('', 0, '充值金额异常,请刷新页面后重新充值');
  427. }
  428. }
  429. $channelInfo = model("Channel")->where(['id'=>$this->_channelId])->find();
  430. if($channelInfo && $channelInfo['parent_id']){
  431. if($recharge_type==3 && $channelInfo['js_amount']<$amount){
  432. $this->jsonResult('', 0, '您的结算币余额不足');
  433. }
  434. }
  435. else{
  436. $this->jsonResult('', 0, '您的账号有异常,请联系管理员');
  437. }
  438. //指定时间内,禁止重复下单
  439. if(!requestDuplicateCheck('recharge_pay_duplicate_'.$this->_channelId,$this->payRequestLimit)){
  440. $this->jsonResult('', 0, '充值请求过多,请于'.$this->payRequestLimit.'s以后,再次进行充值操作');
  441. }
  442. $rechargeData = array();
  443. $rechargeData['orderid'] = $orderid;
  444. $rechargeData['channel_id'] = $this->_channelId;
  445. $rechargeData['channel_name'] = $this->_channelName;
  446. if($this->_channelLevel==1){
  447. $rechargeData['send_channel_id'] = '0';
  448. $rechargeData['send_channel_name'] = '平台';
  449. }
  450. else if($this->_channelLevel==2){
  451. $parentChannelInfo = model("Channel")->where(['id'=>$channelInfo['parent_id']])->find();
  452. $rechargeData['send_channel_id'] = $parentChannelInfo['id'];
  453. $rechargeData['send_channel_name'] = $parentChannelInfo['name'];
  454. }
  455. $rechargeData['amount'] = $amount;
  456. $rechargeData['real_amount'] = $real_amount;
  457. $rechargeData['recharge_type'] = $recharge_type;
  458. $rechargeData['paytype'] = $paytype;
  459. $rechargeData['out_order_no'] = $out_order_no;
  460. $rechargeData['remark'] = $remark;
  461. $rechargeData['recharge_ratio'] = $rechargeRatio;
  462. $rechargeData['status'] = 0;
  463. $rechargeData['create_time'] = time();
  464. // 启动事务
  465. Db::startTrans();
  466. try{
  467. $result = ''; //返回值
  468. //结算币充值
  469. if($recharge_type==3){
  470. $rechargeData['status'] = 1;
  471. $rechargeData['finish_status'] = 1;
  472. $rechargeData['finish_time'] = NOW_TIMESTAMP;
  473. $insertRechargeId = model("ChannelRecharge")->insert($rechargeData);
  474. if(!$insertRechargeId){
  475. throw new Exception("创建订单失败! ");
  476. }
  477. $detData = array();
  478. $detData['channel_id'] = $this->_channelId;
  479. $detData['channel_name'] = $this->_channelName;
  480. $detData['change_amount'] = -$amount;
  481. $detData['account_type'] = 2; //结算账户
  482. $detData['type'] = 7; //结算充值
  483. $detData['out_orderid'] = $orderid;
  484. $detData['create_time'] = NOW_TIMESTAMP;
  485. $insertDetId = model('ChannelAccountDet')->insertGetId($detData);
  486. if ( !$insertDetId) {
  487. throw new Exception("添加账户变动明细失败");
  488. }
  489. $updData = array();
  490. $updData['js_amount'] = Db::raw("js_amount-".$amount);
  491. $updData['update_time'] = time();
  492. $updResult = model('Channel')->where(['id'=>$this->_channelId,'js_amount'=>array('egt',$amount)])->update($updData);
  493. if (!$updResult) {
  494. throw new Exception("账户金额变动失败");
  495. }
  496. $detData = array();
  497. $detData['channel_id'] = $this->_channelId;
  498. $detData['channel_name'] = $this->_channelName;
  499. $detData['change_amount'] = $amount;
  500. $detData['account_type'] = 1; //通用账户
  501. $detData['type'] = 4; //充值收入
  502. $detData['out_orderid'] = $orderid;
  503. $detData['create_time'] = NOW_TIMESTAMP;
  504. $insertDetId = model('ChannelAccountDet')->insertGetId($detData);
  505. if ( !$insertDetId) {
  506. throw new Exception("添加账户变动明细失败");
  507. }
  508. $updData = array();
  509. $updData['amount'] = Db::raw("amount+".$amount);
  510. $updData['update_time'] = time();
  511. $updResult = model('Channel')->where(['id'=>$this->_channelId])->update($updData);
  512. if (!$updResult) {
  513. throw new Exception("账户金额变动失败");
  514. }
  515. }
  516. else{
  517. $insertRechargeId = model("ChannelRecharge")->insert($rechargeData);
  518. if(!$insertRechargeId){
  519. throw new Exception("创建订单失败! ");
  520. }
  521. }
  522. // 提交事务
  523. Db::commit();
  524. $this->redis->del('recharge_pay_duplicate_'.$this->_channelId);
  525. if($recharge_type==2){
  526. //第三方支付用的商品简单描述 (去除&+等特殊字符)
  527. $payBody = "用户充值";
  528. //支付方式是支付宝或者支付宝混合支付时
  529. if($paytype=='zfb'){
  530. //支付宝支付参数
  531. $alipayParam = $this->getAlipayAopParam($orderid,$rechargeData['real_amount'],$payBody);
  532. $result['alipay_param'] = $alipayParam;
  533. }
  534. //支付方式是微信官方h5支付或者微信官方h5混合支付时
  535. elseif($paytype=='wxpay')
  536. {
  537. $wxpayh5_param = $this->getKjWxpayScanParam($orderid,$rechargeData['real_amount'],$payBody);
  538. $result = ['wxpay_param'=>$wxpayh5_param];
  539. }
  540. }
  541. } catch (\Exception $e) {
  542. // 回滚事务
  543. Db::rollback();
  544. $this->jsonResult('', 0, '订单生成失败'.$e->getMessage());
  545. }
  546. $result['orderid'] = $orderid;
  547. $this->jsonResult($result, 20000, '订单生成成功');
  548. exit;
  549. }
  550. else{
  551. $this->jsonResult('', 0, '非法请求');
  552. exit;
  553. }
  554. }
  555. public function queryRechargeOrder(){
  556. if ($this->request->isPost()) {
  557. //判断当前账号是否有充值权限
  558. if(!in_array($this->_channelLevel,[1])){
  559. $this->jsonResult('', 0, '您无权限进行充值结果查询相关操作');
  560. }
  561. else{
  562. $orderid = $this->input('orderid'); //充值订单号
  563. $out_trade_no = $this->input('out_trade_no'); //第三方订单号
  564. if(!$orderid){
  565. $this->jsonResult('', 0, '请输入您要查询的充值订单号');
  566. }
  567. if(!$out_trade_no){
  568. $this->jsonResult('', 0, '请输入您要查询的第三方订单号');
  569. }
  570. $channelInfo = model("Channel")->field("id,name,level,parent_id,amount,js_amount")->where(['id'=>$this->_channelId])->find();
  571. if(!$channelInfo){
  572. $this->jsonResult('', 0, '账号异常,请重新登录后再试');
  573. }
  574. $map = array();
  575. $map['orderid'] = $orderid;
  576. $map['channel_id'] = $this->_channelId;
  577. $rechargeInfo = model('ChannelRecharge')->field('id,status,amount,orderid,recharge_type,paytype')->where($map)->find();
  578. if(!$rechargeInfo){
  579. $this->jsonResult('', 0, '充值订单不存在或非您所属充值订单');
  580. }
  581. else if($rechargeInfo['recharge_type']<>2 || $rechargeInfo['paytype']<>'wxpay'){
  582. $this->jsonResult('', 0, '非微信直充订单,该接口不支持查询');
  583. }
  584. else{
  585. $rechargeStatus = 0; //充值结果
  586. if($rechargeInfo['status']==1){
  587. $rechargeStatus = 1;
  588. }
  589. else{
  590. vendor('kjpaySdk.Kjpay','.class.php');
  591. $kjpay = new \Kjpay();
  592. $is_pay = json_decode($kjpay->queryOrder($out_trade_no), true);
  593. if($is_pay['data']['status']==1){
  594. $rechargeStatus = 1;
  595. // $this->updateRehcargeOrder($orderid,$is_pay['data']['amount'],$is_pay['data']['trade_no']);
  596. }
  597. }
  598. $retData = array();
  599. $retData['recharge_status'] = $rechargeStatus;
  600. $this->jsonResult($retData, 20000, '查询充值结果成功');
  601. }
  602. }
  603. }
  604. else{
  605. $this->jsonResult('', 0, '非法请求');
  606. exit;
  607. }
  608. }
  609. /**
  610. * 获取支付宝支付参数
  611. *
  612. * @param string $orderid 订单号
  613. * @param float $real_amount 支付金额
  614. * @param string $productname 商品名称
  615. * @param string $productdesc 描述
  616. *
  617. * @return string 支付参数字符串
  618. */
  619. private function getAlipayAopParam($orderid,$real_amount,$productname,$productdesc='')
  620. {
  621. //Alipay Aop Sdk支付发
  622. Loader::import('alipay.pagepay.service.AlipayTradeService');
  623. Loader::import('alipay.pagepay.buildermodel.AlipayTradePagePayContentBuilder');
  624. $payRequestBuilder = new \AlipayTradePagePayContentBuilder();
  625. $payRequestBuilder->setBody(preg_replace("/[\&\+]+/", '', $productdesc));
  626. $payRequestBuilder->setSubject(preg_replace("/[\&\+]+/", '', $productname));
  627. $payRequestBuilder->setTotalAmount($real_amount);
  628. $payRequestBuilder->setOutTradeNo($orderid);
  629. $aop = new \AlipayTradeService(config('alipayAop'));
  630. $result = $aop->pagePay($payRequestBuilder,config('alipayAop')['return_url'],config('alipayAop')['notify_url']);
  631. return $result;
  632. // exit;
  633. //Alipay Aop Sdk支付发起
  634. // vendor('alipayAop.AopSdk');
  635. // $aop = new \AopClient();
  636. // $aop->gatewayUrl = 'https://openapi.alipaydev.com/gateway.do';
  637. // $aop->appId = Config::get('alipayAop')["app_id"];
  638. // $aop->rsaPrivateKey = Config::get('alipayAop')['merchant_private_key'];
  639. // $aop->alipayrsaPublicKey = Config::get('alipayAop')['alipay_public_key'];
  640. // $aop->apiVersion = '1.0';
  641. // $aop->signType = Config::get('alipayAop')['sign_type'];
  642. // $aop->postCharset = 'utf-8';
  643. // $aop->format = 'json';
  644. // $request = new \AlipayTradeAppPayRequest();
  645. // $bizcontent = array();
  646. // $bizcontent['subject'] = preg_replace("/[\&\+]+/", '', $productname); // 主题(过滤特殊字符)
  647. // $bizcontent['body'] = preg_replace("/[\&\+]+/", '', $productdesc); // 商品描述信息(过滤特殊字符)
  648. // $bizcontent['out_trade_no'] = $orderid; // 订单号
  649. // $bizcontent['timeout_express'] = '30m'; // 超时时间
  650. // $bizcontent['total_amount'] = $real_amount; // 支付金额
  651. // $bizcontent['product_code'] = 'QUICK_MSECURITY_PAY'; // 支付方式
  652. // $request->setBizContent(json_encode($bizcontent));
  653. // $request->setNotifyUrl(Config::get('alipayAop')['notify_url']);
  654. // $result = $aop->pageExecute($request);
  655. // echo htmlspecialchars($result);
  656. // return $result;
  657. }
  658. /**
  659. * 快接微信h5支付
  660. *
  661. * @param string $orderid 订单号
  662. * @param float $real_amount 支付金额
  663. * @param string $body 商品名称
  664. *
  665. * @return array 支付参数数组
  666. */
  667. private function getKjWxpayScanParam($orderid,$real_amount,$body)
  668. {
  669. $pay_return = false;
  670. vendor('kjpaySdk.Kjpay','.class.php');
  671. $kjpay = new \Kjpay();
  672. $return_url = Config::get('wxpay-h5-kj')['return_url']."?orderid=".$orderid;
  673. $notify_url = Config::get('wxpay-h5-kj')['notify_url'];
  674. $response = $kjpay->scanpay($body, $orderid, $real_amount,$return_url,$notify_url);
  675. // var_dump($response);
  676. if (!$response) {
  677. return false;
  678. }
  679. $result = $response;
  680. if( array_key_exists("status", $result) && $result["status"] == "1")
  681. {
  682. $pay_return['image'] = $result['data']['image'];
  683. $pay_return['trade_no'] = $result['data']['trade_no'];
  684. $pay_return['referer'] = Config::get('wxpay-h5-kj')['referer'];
  685. }
  686. else{
  687. throw new \Exception(' 错误代码:'.$result["code"].' 错误代码描述:'.$result["msg"]);
  688. }
  689. return $pay_return;
  690. }
  691. /**
  692. * 更新订单信息
  693. *
  694. * @param string $orderid:订单编号
  695. * @param string $real_amount:第三方支付的金额(现金部分)
  696. *
  697. * @return boolean
  698. */
  699. private function updateRehcargeOrder($orderid,$real_amount='',$trans_code='')
  700. {
  701. $result = false;
  702. //消费订单信息
  703. $rechargeInfo = model('ChannelRecharge')->field('id,orderid,channel_id,channel_name,send_channel_id,status,amount,real_amount,recharge_type,paytype')->where(['orderid'=>$orderid])->find();
  704. if($real_amount!='' && $rechargeInfo['real_amount']!=$real_amount)
  705. {
  706. $result = false;
  707. //写入日志
  708. log_message('充值订单编号:'.$orderid.'支付金额不一致,订单状态更新失败','error',LOG_PATH . '../paylog/');
  709. }
  710. //待支付状态时
  711. elseif($rechargeInfo['status']==0){
  712. $id = $rechargeInfo['id'];
  713. // 启动事务
  714. Db::startTrans();
  715. try{
  716. $rechargeData = array();
  717. $rechargeData['id'] = $id;
  718. $rechargeData['status'] = 1;
  719. $rechargeData['finish_time'] = NOW_TIMESTAMP;
  720. $rechargeData['check_remark'] = '充值查询处理';
  721. $rechargeData['check_admin_type'] = 1;
  722. $rechargeData['check_admin_name'] = '系统自动';
  723. $rechargeData['finish_status'] = 1;
  724. $rechargeData['out_order_no'] = $trans_code;
  725. $result = model('ChannelRecharge')->where(['id'=>$id,'status'=>array('in',[0])])->update($rechargeData);
  726. if (!$result) {
  727. throw new Exception("充值订单查询处理失败");
  728. }
  729. $detData = array();
  730. $detData['channel_id'] = $rechargeInfo['channel_id'];
  731. $detData['channel_name'] = $rechargeInfo['channel_name'];
  732. $detData['change_amount'] = $rechargeInfo['amount'];
  733. $detData['account_type'] = 1; //通用账户
  734. $detData['type'] = 4; //充值收入
  735. $detData['out_orderid'] = $rechargeInfo['orderid'];
  736. $detData['create_time'] = NOW_TIMESTAMP;
  737. $insertDetId = model('ChannelAccountDet')->insertGetId($detData);
  738. if ( !$insertDetId) {
  739. throw new Exception("添加账户变动明细失败");
  740. }
  741. $updData = array();
  742. $updData['amount'] = Db::raw("amount+".$rechargeInfo['amount']);
  743. $updData['update_time'] = time();
  744. $updToResult = model('Channel')->where(['id'=>$rechargeInfo['channel_id']])->update($updData);
  745. if (!$updToResult) {
  746. throw new Exception("账户金额变动失败");
  747. }
  748. $result = true;
  749. // 提交事务
  750. Db::commit();
  751. } catch (\Exception $e) {
  752. // 回滚事务
  753. Db::rollback();
  754. $result = false;
  755. //写入日志
  756. log_message('订单编号:'.$orderid.'充值订单查询处理状态更新失败'.$e->getMessage(),'error',LOG_PATH . '../paylog/');
  757. }
  758. log_message('订单编号:'.$orderid.'充值订单查询处理状态更新成功','error',LOG_PATH . '../paylog/');
  759. }
  760. else{
  761. $result = false;
  762. log_message('订单编号:'.$orderid.'充值订单查询处理状态更新失败:该充值订单已处理过','error',LOG_PATH . '../paylog/');
  763. }
  764. }
  765. }