Notify.php 7.6 KB

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