Notify.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. //异步日志
  86. private function notify_log_start($paytype = 'wechat'){
  87. //记录支付回调数据
  88. ignore_user_abort(); // run script in background
  89. set_time_limit(30);
  90. // 日志文件 start
  91. $log_base_dir = '../epaylog/'.$paytype.'/';
  92. if (!is_dir($log_base_dir))
  93. {
  94. mkdir($log_base_dir, 0770, true);
  95. @chmod($log_base_dir, 0770);
  96. }
  97. $notify_file = $log_base_dir.'notify.txt';
  98. if(!file_exists($notify_file)) {
  99. @touch($notify_file);
  100. @chmod($notify_file, 0770);
  101. }
  102. if(filesize($notify_file)>5242880)//大于5M自动切换
  103. {
  104. rename($notify_file, $log_base_dir.'notify_'.date('Y_m_d_H_i_s').'.txt');
  105. }
  106. if(!file_exists($notify_file)) {
  107. @touch($notify_file);
  108. @chmod($notify_file, 0770);
  109. }
  110. // 日志文件 end
  111. //开始写入
  112. $_REQUEST = isset($_REQUEST) ? $_REQUEST : array();
  113. if($_REQUEST && $paytype == 'alipay') {
  114. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收request]".json_encode($_REQUEST), FILE_APPEND);
  115. } else {
  116. $xml = file_get_contents("php://input");
  117. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收php://input流原始数据] \n".$xml, FILE_APPEND);
  118. $xmlObj = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
  119. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收php://input流] ".json_encode($xmlObj), FILE_APPEND);
  120. }
  121. ini_set('display_errors','On');
  122. return $notify_file;
  123. }
  124. }