Notify.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\model\PayOrderModel;
  5. use app\common\model\Wallet;
  6. use app\utils\LogUtil;
  7. use think\Db;
  8. use addons\epay\library\Service;
  9. use think\Request;
  10. use app\admin\model\shopro\Pay as PayModel;
  11. use addons\shopro\service\pay\PayOper;
  12. /**
  13. * 订单支付回调
  14. */
  15. class Notify extends Api
  16. {
  17. // 日志模块名称
  18. const LOG_MODULE = 'Notify';
  19. protected $noNeedLogin = ['*'];
  20. protected $noNeedRight = ['*'];
  21. public function __construct(Request $request = null)
  22. {
  23. parent::__construct($request);
  24. //日志统一写入
  25. register_shutdown_function([new LogUtil, 'close']);
  26. LogUtil::getInstance('Api/'); //设置日志存入通道
  27. }
  28. // 余额充值 支付回调
  29. public function recharge()
  30. {
  31. //验签
  32. $pay_type = input('pay_type', 'wechat');
  33. LogUtil::info("用户充值 {$pay_type}支付回调", self::LOG_MODULE, __FUNCTION__, $this->request->param());
  34. if (!$pay = Service::checkNotify($pay_type)) {
  35. LogUtil::info('签名错误', self::LOG_MODULE, __FUNCTION__);
  36. return $this->error('签名错误 err01');
  37. }
  38. //验证,拿订单号等信息
  39. $data = $pay->verify();
  40. $out_trade_no = $data['out_trade_no'];
  41. [$res, $msg] = PayOrderModel::recharge($out_trade_no);
  42. if (!$res) {
  43. LogUtil::info($msg, self::LOG_MODULE, __FUNCTION__);
  44. return $this->error('签名错误 err02');
  45. }
  46. LogUtil::info('处理成功', self::LOG_MODULE, __FUNCTION__);
  47. return $pay->success();
  48. }
  49. //充值VIP 异步回调对外方法
  50. public function vip_notify_base()
  51. {
  52. //验签
  53. $paytype = input('paytype', 'wechat');
  54. $notify_file = $this->notify_log_start($paytype);
  55. $pay = Service::checkNotify($paytype);
  56. if (!$pay) {
  57. echo '签名错误';
  58. exit;
  59. }
  60. //验证,拿订单号等信息
  61. $data = $pay->verify();
  62. $out_trade_no = $data['out_trade_no'];
  63. //订单查询
  64. $info = Db::name('pay_order')->where('out_trade_no', $out_trade_no)->find();
  65. if (empty($info)) {
  66. echo $pay->success();
  67. exit;
  68. }
  69. if ($info['order_status'] != 0) {
  70. echo $pay->success();
  71. exit;
  72. }
  73. //你可以在此编写订单逻辑
  74. $rs = $this->vip_notify_do($out_trade_no);
  75. if ($rs === false) {
  76. //不论结果都应返回success
  77. echo $pay->success();
  78. exit;
  79. } else {
  80. //不论结果都应返回success
  81. echo $pay->success();
  82. exit;
  83. }
  84. //默认
  85. echo $pay->success();
  86. exit;
  87. }
  88. //充值金币 逻辑
  89. private function vip_notify_do($out_trade_no)
  90. {
  91. Db::startTrans();
  92. $orderInfo = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->lock(true)->find();
  93. if (empty($orderInfo)) {
  94. Db::rollback();
  95. return false;
  96. }
  97. if ($orderInfo['order_status'] != 0) {
  98. Db::rollback();
  99. return false;
  100. }
  101. //逻辑开始
  102. //先充值
  103. $args = json_decode($orderInfo['args'], true);
  104. $user_info = Db::name('user_wallet')->where('user_id', $orderInfo['user_id'])->lock(true)->find();
  105. if ($user_info['vip_endtime'] < time()) {
  106. //过期了
  107. $vip_endtime = time() + (intval($args['days']) * 86400);
  108. $vip_type = 1;
  109. } else {
  110. //追加vip
  111. $vip_endtime = $user_info['vip_endtime'] + (intval($args['days']) * 86400);
  112. $vip_type = 2;
  113. }
  114. $update_data = [
  115. 'vip_endtime' => $vip_endtime,
  116. ];
  117. $result = Db::name('user_wallet')->where('user_id', $orderInfo['user_id'])->update($update_data);
  118. //记录日志
  119. $log_data = [
  120. 'user_id' => $orderInfo['user_id'],
  121. 'before' => $user_info['vip_endtime'],
  122. 'change_value' => intval($args['days']) * 86400,
  123. 'remain' => $vip_endtime,
  124. 'remark' => '购买vip',
  125. 'createtime' => time(),
  126. 'vip_type' => $vip_type,
  127. ];
  128. Db::name('user_vip_log')->insertGetId($log_data);
  129. if ($result === false) {
  130. Db::rollback();
  131. return false;
  132. }
  133. //逻辑结束
  134. //状态
  135. $ros = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->update(['order_status' => 1, 'notifytime' => time()]);
  136. if ($ros === false) {
  137. Db::rollback();
  138. return false;
  139. }
  140. //默认提交
  141. Db::commit();
  142. return true;
  143. }
  144. public function shopro_notify_base_test(){
  145. //验签
  146. $paytype = input('paytype', 'wechat');
  147. //验证,拿订单号等信息
  148. $out_trade_no = input('out_trade_no', '');
  149. //订单查询
  150. $info = Db::name('shopro_pay')->where('pay_sn', $out_trade_no)->find();
  151. if (empty($info)) {
  152. echo 'success';
  153. exit;
  154. }
  155. if ($info['status'] != 'unpaid') {
  156. echo 'success';
  157. exit;
  158. }
  159. //你可以在此编写订单逻辑
  160. $data = [
  161. 'out_trade_no' => $out_trade_no,
  162. 'transaction_id' => 'test',
  163. 'notify_time' => time(),
  164. 'buyer_info' => 'test',
  165. 'pay_fee' => 0,
  166. ];
  167. $rs = $this->shopro_notify_do($out_trade_no,$data,$paytype);
  168. if ($rs === false) {
  169. //不论结果都应返回success
  170. echo 'success';
  171. exit;
  172. } else {
  173. //不论结果都应返回success
  174. echo 'success';
  175. exit;
  176. }
  177. //默认
  178. echo $pay->success();
  179. exit;
  180. }
  181. //充值VIP 异步回调对外方法
  182. public function shopro_notify_base()
  183. {
  184. //验签
  185. $paytype = input('paytype', 'wechat');
  186. $notify_file = $this->notify_log_start($paytype);
  187. $pay = Service::checkNotify($paytype);
  188. if (!$pay) {
  189. echo '签名错误';
  190. exit;
  191. }
  192. //验证,拿订单号等信息
  193. $data = $pay->verify();
  194. $out_trade_no = $data['out_trade_no'];
  195. //订单查询
  196. $info = Db::name('shopro_pay')->where('pay_sn', $out_trade_no)->find();
  197. if (empty($info)) {
  198. echo $pay->success();
  199. exit;
  200. }
  201. if ($info['status'] != 'unpaid') {
  202. echo $pay->success();
  203. exit;
  204. }
  205. //你可以在此编写订单逻辑
  206. $rs = $this->shopro_notify_do($out_trade_no,$data,$paytype);
  207. if ($rs === false) {
  208. //不论结果都应返回success
  209. echo $pay->success();
  210. exit;
  211. } else {
  212. //不论结果都应返回success
  213. echo $pay->success();
  214. exit;
  215. }
  216. //默认
  217. echo $pay->success();
  218. exit;
  219. }
  220. //充值金币 逻辑
  221. private function shopro_notify_do($out_trade_no,$data,$payment)
  222. {
  223. // 查询 pay 交易记录
  224. $payModel = PayModel::where('pay_sn', $out_trade_no)->find();
  225. if (!$payModel || $payModel->status != PayModel::PAY_STATUS_UNPAID) {
  226. // 订单不存在,或者订单已支付
  227. return false;
  228. }
  229. $originData = [];
  230. Db::transaction(function () use ($payModel, $data, $originData, $payment) {
  231. $notify = [
  232. 'pay_sn' => $data['out_trade_no'],
  233. 'transaction_id' => $data['transaction_id'],
  234. 'notify_time' => $data['notify_time'],
  235. 'buyer_info' => $data['buyer_info'],
  236. 'payment_json' => $originData ? json_encode($originData) : json_encode($data),
  237. 'pay_fee' => $data['pay_fee'], // 微信的已经*100处理过了
  238. 'pay_type' => $payment // 支付方式
  239. ];
  240. // pay 实例
  241. $payOper = new PayOper($payModel->user_id);
  242. $payOper->notify($payModel, $notify);
  243. });
  244. return true;
  245. }
  246. //异步日志
  247. private function notify_log_start($paytype = 'wechat')
  248. {
  249. //记录支付回调数据
  250. ignore_user_abort(); // run script in background
  251. set_time_limit(30);
  252. // 日志文件 start
  253. $log_base_dir = '../paylog/' . $paytype . '/';
  254. if (!is_dir($log_base_dir)) {
  255. mkdir($log_base_dir, 0770, true);
  256. @chmod($log_base_dir, 0770);
  257. }
  258. $notify_file = $log_base_dir . 'notify.txt';
  259. if (!file_exists($notify_file)) {
  260. @touch($notify_file);
  261. @chmod($notify_file, 0770);
  262. }
  263. if (filesize($notify_file) > 5242880)//大于5M自动切换
  264. {
  265. rename($notify_file, $log_base_dir . 'notify_' . date('Y_m_d_H_i_s') . '.txt');
  266. }
  267. if (!file_exists($notify_file)) {
  268. @touch($notify_file);
  269. @chmod($notify_file, 0770);
  270. }
  271. // 日志文件 end
  272. //开始写入
  273. $_REQUEST = isset($_REQUEST) ? $_REQUEST : array();
  274. if ($_REQUEST && $paytype == 'alipay') {
  275. file_put_contents($notify_file, "\r\n\r\n" . date('Y-m-d H:i:s') . " [notify][入口接收request]" . json_encode($_REQUEST), FILE_APPEND);
  276. } else {
  277. $xml = file_get_contents("php://input");
  278. file_put_contents($notify_file, "\r\n\r\n" . date('Y-m-d H:i:s') . " [notify][入口接收php://input流原始数据] \n" . $xml, FILE_APPEND);
  279. $xmlObj = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
  280. file_put_contents($notify_file, "\r\n\r\n" . date('Y-m-d H:i:s') . " [notify][入口接收php://input流] " . json_encode($xmlObj), FILE_APPEND);
  281. }
  282. ini_set('display_errors', 'On');
  283. return $notify_file;
  284. }
  285. }