Notify.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 recharge_notify_base(){
  15. //验签
  16. $paytype = input('paytype','hitpay');
  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->recharge_notify_do($out_trade_no);
  39. if($rs === false){
  40. //不论结果都应返回success
  41. echo $pay->success();
  42. exit;
  43. }else{
  44. //不论结果都应返回success
  45. echo $pay->success();
  46. exit;
  47. }
  48. //默认
  49. echo $pay->success();
  50. exit;
  51. }
  52. //充值金币 逻辑
  53. private function recharge_notify_do($out_trade_no){
  54. Db::startTrans();
  55. $orderInfo = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->lock(true)->find();
  56. if (empty($orderInfo)) {
  57. Db::rollback();
  58. return false;
  59. }
  60. if($orderInfo['order_status'] != 0){
  61. Db::rollback();
  62. return false;
  63. }
  64. //逻辑开始
  65. $args = json_decode($orderInfo['args'],true);
  66. $result = model('Wallet')->lockChangeAccountRemain($orderInfo['user_id'],'gold',$args['gold'],10, '金币充值','pay_order',$orderInfo['id']);
  67. if($result['status']===false)
  68. {
  69. Db::rollback();
  70. return false;
  71. }
  72. //逻辑结束
  73. //状态
  74. $ros = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->update(['order_status'=>1,'notifytime'=>time()]);
  75. if($ros === false) {
  76. Db::rollback();
  77. return false;
  78. }
  79. //默认提交
  80. Db::commit();
  81. return true;
  82. }
  83. //异步日志
  84. private function notify_log_start($paytype = 'wechat'){
  85. //记录支付回调数据
  86. ignore_user_abort(); // run script in background
  87. set_time_limit(30);
  88. // 日志文件 start
  89. $log_base_dir = '../paylog/'.$paytype.'/';
  90. if (!is_dir($log_base_dir))
  91. {
  92. mkdir($log_base_dir, 0770, true);
  93. @chmod($log_base_dir, 0770);
  94. }
  95. $notify_file = $log_base_dir.'notify.txt';
  96. if(!file_exists($notify_file)) {
  97. @touch($notify_file);
  98. @chmod($notify_file, 0770);
  99. }
  100. if(filesize($notify_file)>5242880)//大于5M自动切换
  101. {
  102. rename($notify_file, $log_base_dir.'notify_'.date('Y_m_d_H_i_s').'.txt');
  103. }
  104. if(!file_exists($notify_file)) {
  105. @touch($notify_file);
  106. @chmod($notify_file, 0770);
  107. }
  108. // 日志文件 end
  109. //开始写入
  110. $xml = file_get_contents("php://input");
  111. file_put_contents($notify_file, "\r\n\r\n".date('Y-m-d H:i:s')." [notify][入口接收php://input流原始数据] \n".$xml, FILE_APPEND);
  112. ini_set('display_errors','On');
  113. return $notify_file;
  114. }
  115. }