Notify.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. //充值金币 逻辑
  54. private function recharge_notify_do($out_trade_no){
  55. Db::startTrans();
  56. $orderInfo = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->lock(true)->find();
  57. if (empty($orderInfo)) {
  58. Db::rollback();
  59. return false;
  60. }
  61. if($orderInfo['order_status'] != 0){
  62. Db::rollback();
  63. return false;
  64. }
  65. //逻辑开始
  66. //$args = json_decode($orderInfo['args'],true);
  67. $result = model('Wallet')->lockChangeAccountRemain($orderInfo['user_id'],'money',$orderInfo['order_amount'],10, '余额充值','pay_order',$orderInfo['id']);
  68. if($result['status']===false)
  69. {
  70. Db::rollback();
  71. return false;
  72. }
  73. //逻辑结束
  74. //状态
  75. $ros = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->update(['order_status'=>1,'notifytime'=>time()]);
  76. if($ros === false) {
  77. Db::rollback();
  78. return false;
  79. }
  80. //默认提交
  81. Db::commit();
  82. return true;
  83. }
  84. ////////////////////////////////////////////////////////////////////////
  85. //充值VIP 异步回调对外方法
  86. public function vip_notify_base(){
  87. //验签
  88. $paytype = input('paytype','wechat');
  89. $notify_file = $this->notify_log_start($paytype);
  90. $pay = Service::checkNotify($paytype);
  91. if (!$pay) {
  92. echo '签名错误';
  93. exit;
  94. }
  95. //验证,拿订单号等信息
  96. $data = $pay->verify();
  97. $out_trade_no = $data['out_trade_no'];
  98. //订单查询
  99. $info = Db::name('pay_order')->where('out_trade_no',$out_trade_no)->find();
  100. if(empty($info)){
  101. return $pay->success()->send();
  102. exit;
  103. }
  104. if($info['order_status'] != 0)
  105. {
  106. return $pay->success()->send();
  107. exit;
  108. }
  109. //你可以在此编写订单逻辑
  110. $rs = $this->vip_notify_do($out_trade_no);
  111. if($rs === false){
  112. //不论结果都应返回success
  113. return $pay->success()->send();
  114. exit;
  115. }else{
  116. //不论结果都应返回success
  117. return $pay->success()->send();
  118. exit;
  119. }
  120. //默认
  121. return $pay->success()->send();
  122. exit;
  123. }
  124. //充值金币 逻辑
  125. private function vip_notify_do($out_trade_no){
  126. Db::startTrans();
  127. $orderInfo = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->lock(true)->find();
  128. if (empty($orderInfo)) {
  129. Db::rollback();
  130. return false;
  131. }
  132. if($orderInfo['order_status'] != 0){
  133. Db::rollback();
  134. return false;
  135. }
  136. //逻辑开始
  137. //先充值
  138. $args = json_decode($orderInfo['args'],true);
  139. $user_info = Db::name('user_wallet')->where('user_id',$orderInfo['user_id'])->lock(true)->find();
  140. if($user_info['vip_endtime'] < time()){
  141. //过期了
  142. $vip_endtime = time() + (intval($args['days']) * 86400);
  143. $vip_type = 1;
  144. }else{
  145. //追加vip
  146. $vip_endtime = $user_info['vip_endtime'] + (intval($args['days']) * 86400);
  147. $vip_type = 2;
  148. }
  149. $update_data = [
  150. 'vip_endtime'=>$vip_endtime,
  151. ];
  152. $result = Db::name('user_wallet')->where('user_id',$orderInfo['user_id'])->update($update_data);
  153. //记录日志
  154. $log_data = [
  155. 'user_id' => $orderInfo['user_id'],
  156. 'before' => $user_info['vip_endtime'],
  157. 'change_value' => intval($args['days']) * 86400,
  158. 'remain' => $vip_endtime,
  159. 'remark' => '安卓购买vip',
  160. 'createtime' => time(),
  161. 'vip_type' => $vip_type,
  162. ];
  163. Db::name('user_vip_log')->insertGetId($log_data);
  164. if($result === false)
  165. {
  166. Db::rollback();
  167. return false;
  168. }
  169. //逻辑结束
  170. //状态
  171. $ros = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->update(['order_status'=>1,'notifytime'=>time()]);
  172. if($ros === false) {
  173. Db::rollback();
  174. return false;
  175. }
  176. //默认提交
  177. Db::commit();
  178. return true;
  179. }
  180. //异步日志
  181. private function notify_log_start($paytype = 'wechat'){
  182. //记录支付回调数据
  183. ignore_user_abort(); // run script in background
  184. set_time_limit(30);
  185. // 日志文件 start
  186. $log_base_dir = '../epaylog/'.$paytype.'/';
  187. if (!is_dir($log_base_dir))
  188. {
  189. mkdir($log_base_dir, 0770, true);
  190. @chmod($log_base_dir, 0770);
  191. }
  192. $notify_file = $log_base_dir.'notify.txt';
  193. if(!file_exists($notify_file)) {
  194. @touch($notify_file);
  195. @chmod($notify_file, 0770);
  196. }
  197. if(filesize($notify_file)>5242880)//大于5M自动切换
  198. {
  199. rename($notify_file, $log_base_dir.'notify_'.date('Y_m_d_H_i_s').'.txt');
  200. }
  201. if(!file_exists($notify_file)) {
  202. @touch($notify_file);
  203. @chmod($notify_file, 0770);
  204. }
  205. // 日志文件 end
  206. //开始写入
  207. $_REQUEST = isset($_REQUEST) ? $_REQUEST : array();
  208. if($_REQUEST && $paytype == 'alipay') {
  209. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收request]".json_encode($_REQUEST), FILE_APPEND);
  210. } else {
  211. $xml = file_get_contents("php://input");
  212. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收php://input流原始数据] \n".$xml, FILE_APPEND);
  213. $xmlObj = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
  214. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收php://input流] ".json_encode($xmlObj), FILE_APPEND);
  215. }
  216. ini_set('display_errors','On');
  217. return $notify_file;
  218. }
  219. }