Notify.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use addons\epay\library\Service;
  6. use app\common\library\Tenim;
  7. /**
  8. * 订单支付回调
  9. */
  10. class Notify extends Api
  11. {
  12. protected $noNeedLogin = ['*'];
  13. protected $noNeedRight = ['*'];
  14. //充值金币 异步回调对外方法
  15. public function recharge_notify_base(){
  16. //验签
  17. $paytype = input('paytype','wechat');
  18. $notify_file = $this->notify_log_start($paytype);
  19. $pay = Service::checkNotify($paytype);
  20. if (!$pay) {
  21. echo '签名错误';
  22. exit;
  23. }
  24. //验证,拿订单号等信息
  25. $data = $pay->verify();
  26. $out_trade_no = $data['out_trade_no'];
  27. //订单查询
  28. $info = Db::name('pay_order')->where('out_trade_no',$out_trade_no)->find();
  29. if(empty($info)){
  30. return $pay->success()->send();
  31. exit;
  32. }
  33. if($info['order_status'] != 0)
  34. {
  35. return $pay->success()->send();
  36. exit;
  37. }
  38. //你可以在此编写订单逻辑
  39. $rs = $this->recharge_notify_do($out_trade_no);
  40. if($rs === false){
  41. //不论结果都应返回success
  42. return $pay->success()->send();
  43. exit;
  44. }else{
  45. //不论结果都应返回success
  46. return $pay->success()->send();
  47. exit;
  48. }
  49. //默认
  50. return $pay->success()->send();
  51. exit;
  52. }
  53. public function recharge_notify_test(){
  54. $out_trade_no = input('out_trade_no');
  55. $rs = $this->recharge_notify_do($out_trade_no);
  56. dump($rs);
  57. }
  58. //充值金币 逻辑
  59. public function recharge_notify_do($out_trade_no,$log_type = 10,$remark = '余额充值'){
  60. Db::startTrans();
  61. $orderInfo = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->lock(true)->find();
  62. if (empty($orderInfo)) {
  63. Db::rollback();
  64. return false;
  65. }
  66. if($orderInfo['order_status'] != 0){
  67. Db::rollback();
  68. return false;
  69. }
  70. $user_id = $orderInfo['user_id'];
  71. //逻辑开始
  72. $args = json_decode($orderInfo['args'],true);
  73. $giftmoney = isset($args['giftmoney']) ? $args['giftmoney'] : 0;
  74. $rechargemoney = bcadd($orderInfo['order_amount'],$giftmoney,2);
  75. $result = model('Wallet')->lockChangeAccountRemain($user_id,'money',$rechargemoney,$log_type, $remark,'pay_order',$orderInfo['id']);
  76. if($result['status']===false)
  77. {
  78. Db::rollback();
  79. return false;
  80. }
  81. //逻辑结束
  82. //状态
  83. $ros = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->update(['order_status'=>1,'notifytime'=>time()]);
  84. if($ros === false) {
  85. Db::rollback();
  86. return false;
  87. }
  88. //vip价格
  89. $user_info = Db::name('user')->field('is_vipprice')->where('id',$user_id)->find();
  90. if($user_info['is_vipprice'] == 0){
  91. $recharge_sum = Db::name('user_money_log')->where('user_id',$user_id)->where('log_type','IN',[10,15])->sum('change_value');
  92. if($recharge_sum >= config('site.vipprice_recharge_sum')){
  93. Db::name('user')->where('id',$user_id)->update(['is_vipprice' => 1]);
  94. }
  95. }
  96. //我的
  97. $user_wallet = Db::name('user_wallet')->field('intro_level,money,intro_uid')->where('user_id',$user_id)->find();
  98. //上级
  99. $intro_uid = $user_wallet['intro_uid'];
  100. $up_wallet = Db::name('user_wallet')->field('intro_level,money,intro_uid')->where('user_id',$intro_uid)->find();
  101. //星级配置
  102. $intro_level_config = Db::name('intro_level_config')->order('id asc')->select();
  103. // $intro_level_column = array_column($intro_level_config,'intro_bili','id'); // [id => intro_bili]
  104. //入金升级
  105. if($orderInfo['order_amount'] >= config('site.intro_rujin_money')){
  106. //自己入金
  107. if($user_wallet['intro_level'] == '-1'){
  108. //看看直推了几个,就给升到对应的星级
  109. $intro_num = Db::name('user_wallet')->where('intro_uid',$user_id)->where('intro_level','neq','-1')->count();//我直推的个数(入金的)
  110. $level = $this->get_intro_level($intro_level_config,$intro_num);
  111. Db::name('user_wallet')->where('user_id',$user_id)->update(['intro_level'=>$level]); //我升级了,最低入金,最高顶级
  112. //上级升级
  113. if($up_wallet && $up_wallet['intro_level'] != 5){
  114. //看看直推了几个,就给升到对应的星级
  115. $intro_num = Db::name('user_wallet')->where('intro_uid',$intro_uid)->where('intro_level','neq','-1')->count();//直推的个数(入金的)
  116. $level = $this->get_intro_level($intro_level_config,$intro_num);
  117. Db::name('user_wallet')->where('user_id',$intro_uid)->update(['intro_level'=>$level]); //升级了
  118. }
  119. }
  120. }
  121. //提成
  122. $chuju = config('site.intro_chuju_min_money');
  123. if($user_wallet['money'] >= $chuju && $user_wallet['intro_level'] != '-1'){
  124. if($up_wallet && $up_wallet['money'] >= $chuju && $up_wallet['intro_level'] != '-1' && $up_wallet['intro_level'] >= $user_wallet['intro_level']){
  125. //上级提成
  126. $intro_intro_recharge_bili = config('site.intro_intro_recharge_bili');//比例
  127. $intro_money = bcdiv(bcmul($orderInfo['order_amount'],$intro_intro_recharge_bili,2),100,2);
  128. if($intro_money > 0){
  129. $result = model('Wallet')->lockChangeAccountRemain($intro_uid,'intromoney',$intro_money,101, '直推充值'.$orderInfo['order_amount'],'pay_order',$orderInfo['id']);
  130. if($result['status']===false)
  131. {
  132. Db::rollback();
  133. return false;
  134. }
  135. }
  136. //上上级提成
  137. $top_uid = $up_wallet['intro_uid'];
  138. $top_wallet = Db::name('user_wallet')->field('intro_level,money')->where('user_id',$top_uid)->find();
  139. if($top_wallet && $top_wallet['money'] >= $chuju && $top_wallet['intro_level'] != '-1' && $top_wallet['intro_level'] >= $up_wallet['intro_level']){
  140. $intro_top_recharge_bili = config('site.intro_top_recharge_bili'); //比例
  141. $top_money = bcdiv(bcmul($orderInfo['order_amount'],$intro_top_recharge_bili,2),100,2);
  142. if($top_money > 0){
  143. $result = model('Wallet')->lockChangeAccountRemain($top_uid,'intromoney',$top_money,102, '间推充值'.$orderInfo['order_amount'],'pay_order',$orderInfo['id']);
  144. if($result['status']===false)
  145. {
  146. Db::rollback();
  147. return false;
  148. }
  149. }
  150. }
  151. }
  152. }
  153. //默认提交
  154. Db::commit();
  155. return true;
  156. }
  157. //根据直推人数,得出应升的等级
  158. private function get_intro_level($intro_level_config,$intro_num){
  159. $rs = 0;
  160. foreach($intro_level_config as $key => $level){
  161. if($intro_num >= $level['intro_count']){
  162. $rs = $level['id'];
  163. }
  164. }
  165. return $rs;
  166. }
  167. ////////////////////////////////////////////////////////////////////////
  168. //异步日志
  169. private function notify_log_start($paytype = 'wechat'){
  170. //记录支付回调数据
  171. ignore_user_abort(); // run script in background
  172. set_time_limit(30);
  173. // 日志文件 start
  174. $log_base_dir = '../epaylog/'.$paytype.'/';
  175. if (!is_dir($log_base_dir))
  176. {
  177. mkdir($log_base_dir, 0770, true);
  178. @chmod($log_base_dir, 0770);
  179. }
  180. $notify_file = $log_base_dir.'notify.txt';
  181. if(!file_exists($notify_file)) {
  182. @touch($notify_file);
  183. @chmod($notify_file, 0770);
  184. }
  185. if(filesize($notify_file)>5242880)//大于5M自动切换
  186. {
  187. rename($notify_file, $log_base_dir.'notify_'.date('Y_m_d_H_i_s').'.txt');
  188. }
  189. if(!file_exists($notify_file)) {
  190. @touch($notify_file);
  191. @chmod($notify_file, 0770);
  192. }
  193. // 日志文件 end
  194. //开始写入
  195. $_REQUEST = isset($_REQUEST) ? $_REQUEST : array();
  196. if($_REQUEST && $paytype == 'alipay') {
  197. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收request]".json_encode($_REQUEST), FILE_APPEND);
  198. } else {
  199. $xml = file_get_contents("php://input");
  200. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收php://input流原始数据] \n".$xml, FILE_APPEND);
  201. $xmlObj = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
  202. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收php://input流] ".json_encode($xmlObj), FILE_APPEND);
  203. }
  204. ini_set('display_errors','On');
  205. return $notify_file;
  206. }
  207. }