Notify.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. <?php
  2. namespace app\api\controller;
  3. use addons\epay\library\Service;
  4. use app\admin\model\Fishery;
  5. use app\admin\model\FisherySuppliers;
  6. use app\admin\model\method\Time;
  7. use app\admin\model\Smslog;
  8. use app\admin\model\Smsorder;
  9. use app\admin\model\Tzmoneylog;
  10. use app\admin\model\Tzmoneyrecharge;
  11. use app\admin\model\UserMoneyLog;
  12. use app\admin\model\UserMoneyRecharge;
  13. use app\api\controller\shop\Order;
  14. use app\api\model\order\OrderItem;
  15. use app\common\controller\Api;
  16. use app\common\library\Log;
  17. use app\common\service\OrderCallback;
  18. use think\Db;
  19. use think\Exception;
  20. /**
  21. * @ApiInternal
  22. * Class 回调
  23. * @package app\api\controller
  24. */
  25. class Notify extends Api
  26. {
  27. protected $noNeedLogin = ['*'];
  28. protected $noNeedRight = ['*'];
  29. //钓鱼订单支付回调
  30. public function method_notify(){
  31. $paytype = $this->request->param('type');
  32. file_put_contents("example.txt",$paytype);
  33. \think\Log::write('【钓鱼订单支付】回调,type:'.$paytype);
  34. $pay = Service::checkNotify($paytype);
  35. if (!$pay) {
  36. file_put_contents("exampl.txt",'出错了');
  37. \think\Log::write('【钓鱼订单支付】回调,签名错了');
  38. echo '签名错误';
  39. return;
  40. }
  41. $data = $pay->verify();
  42. file_put_contents("exampl.txt",json_encode($data));
  43. try {
  44. $out_trade_no = $data['out_trade_no'];
  45. \think\Log::write('【钓鱼订单支付】回调,out_trade_no:'.$out_trade_no);
  46. $order_model = new \app\admin\model\Order();
  47. $order_item_model = new \app\admin\model\Orderitem();
  48. $user_model = new \app\admin\model\User();
  49. $time_model = new Time();
  50. $score = new Pay();
  51. $number = $order_model->where('order_no',$out_trade_no)->field('item_money,wucan_money,user_id,id')->find();
  52. $time_number = $order_item_model->where('order_id',$number['id'])->where('type',1)->field('number,time_id')->find();
  53. $number_sta = $time_model->where('id',$time_number['time_id'])->value('sale_num');
  54. $user_number = $user_model->where('id',$number['user_id'])->value('score');
  55. $zong = ceil($number['item_money']+$user_number+$number['wucan_money']);
  56. $score->score($number['user_id'],$zong,'下单返利积分');
  57. Db::name('user')->where('id',$number['user_id'])->update(['score'=>$zong]);
  58. $order_model->isUpdate('true',['order_no'=>$out_trade_no])->save(['status'=>2,'pay_time'=>time()]);
  59. $time_model->isUpdate('true',['id'=>$time_number['time_id']])->save(['sale_num'=>$number_sta-$time_number['number']]);
  60. //你可以在此编写订单逻辑
  61. } catch (Exception $e) {
  62. }
  63. //下面这句必须要执行,且在此之前不能有任何输出
  64. return $pay->success()->send();
  65. }
  66. //短信购买订单支付回调
  67. public function sms_notify(){
  68. $paytype = $this->request->param('type');
  69. $pay = Service::checkNotify($paytype);
  70. if (!$pay) {
  71. echo '签名错误';
  72. return;
  73. }
  74. $data = $pay->verify();
  75. file_put_contents('w_pay.txt',json_encode($data));
  76. try {
  77. $out_trade_no = $data['out_trade_no'];
  78. $smslog_model= new Smsorder();
  79. $user_model = new \app\admin\model\User();
  80. $number = $smslog_model->where('order_no',$out_trade_no)->field('number,user_id')->find();
  81. $user_number = $user_model->where('id',$number['user_id'])->value('sms_number');
  82. $num = $number['number']+$user_number;
  83. $user_model->isUpdate('true',['id'=>$number['user_id']])->save(['sms_number'=>$num]);
  84. $smslog_model->isUpdate('true',['order_no'=>$out_trade_no])->save(['status'=>1]);
  85. //你可以在此编写订单逻辑
  86. } catch (Exception $e) {
  87. }
  88. //下面这句必须要执行,且在此之前不能有任何输出
  89. return $pay->success()->send();
  90. }
  91. public function release_notify(){
  92. $paytype = $this->request->param('type');
  93. $pay = Service::checkNotify($paytype);
  94. if (!$pay) {
  95. \think\Log::write('【发布鱼塘】回调签名错误,paytype:',$paytype);
  96. echo '签名错误';
  97. return;
  98. }
  99. $data = $pay->verify();
  100. try {
  101. $out_trade_no = $data['out_trade_no'];
  102. $yutang_model = new \app\admin\model\Yutang();
  103. $yutang_model->isUpdate('true',['order_no'=>$out_trade_no])->save(['status'=>2]);
  104. //你可以在此编写订单逻辑
  105. } catch (Exception $e) {
  106. }
  107. //下面这句必须要执行,且在此之前不能有任何输出
  108. return $pay->success()->send();
  109. }
  110. //充值订单支付回调
  111. public function balance_notify(){
  112. $paytype = $this->request->param('type');
  113. $pay = Service::checkNotify($paytype);
  114. if (!$pay) {
  115. echo '签名错误';
  116. return;
  117. }
  118. $data = $pay->verify();
  119. try {
  120. $out_trade_no = $data['out_trade_no'];
  121. $smslog_model= new Tzmoneyrecharge();
  122. $user_model = new \app\admin\model\User();
  123. $log_model = new Tzmoneylog();
  124. $number = $smslog_model->where('order_no',$out_trade_no)->field('money,user_id')->find();
  125. $add_data =[
  126. 'user_id'=>$number['user_id'],
  127. 'type'=>2,
  128. 'form'=>'余额充值',
  129. 'amount'=>$number['money'],
  130. 'create_time'=>time(),
  131. ];
  132. $user_number = $user_model->where('id',$number['user_id'])->field('t_money,t_cmoney')->find();
  133. $user_model->isUpdate('true',['id'=>$number['user_id']])->save(['t_money'=>$number['t_cmoney']+$user_number['t_cmoney'],'t_cmoney'=>$number['money']+$user_number['t_cmoney']]);
  134. $smslog_model->isUpdate('true',['order_no'=>$out_trade_no])->save(['status'=>2]);
  135. $log_model->insert($add_data);
  136. //你可以在此编写订单逻辑
  137. } catch (Exception $e) {
  138. }
  139. //下面这句必须要执行,且在此之前不能有任何输出
  140. return $pay->success()->send();
  141. }
  142. /**
  143. * 用户-钱包充值订单支付回调
  144. * 1、用户余额
  145. * 2、用户余额记录
  146. * 3、修改充值订单
  147. */
  148. public function user_recharge_notify(){
  149. $paytype = $this->request->param('type');
  150. $pay = Service::checkNotify($paytype);
  151. if (!$pay) {
  152. echo '签名错误';
  153. return;
  154. }
  155. $data = $pay->verify();
  156. try {
  157. $payamount = $paytype == 'alipay' ? $data['total_amount'] : $data['total_fee'] / 100;
  158. $out_trade_no = $data['out_trade_no'];
  159. //你可以在此编写订单逻辑
  160. // $payamount = 100 / 100;
  161. // $out_trade_no = 'CZ202302061655249720003';
  162. // $data = [];
  163. // 查询当前充值订单
  164. $recharge_model = new UserMoneyRecharge();
  165. $recharge_info = $recharge_model->where('order_sn',$out_trade_no)->find();
  166. if(!$recharge_info){
  167. \think\Log::write('支付回调错误【用户-钱包充值订单】未查询到用户充值订单,返回信息:'.json_encode($data));
  168. return ; // todo
  169. // return $pay->success()->send();
  170. }
  171. Db::startTrans();
  172. try {
  173. $user_id = $recharge_info->user_id;
  174. $user = \app\api\model\User::find($user_id);
  175. $money_log = [
  176. 'user_id' => $user_id,
  177. 'type' => 2,
  178. 'memo' => '余额充值',
  179. 'money' => $payamount,
  180. 'before' => $user->money,
  181. 'after' => $user->money + $payamount,
  182. 'createtime' => time(),
  183. ];
  184. $money_log_model = new UserMoneyLog();
  185. $money_log_model->insert($money_log);
  186. // 用户几个
  187. $user->money += $payamount;
  188. $user->save();
  189. $recharge_data = [
  190. 'pay_time' => time(),
  191. 'status' => 2
  192. ];
  193. $recharge_info->save($recharge_data);
  194. Db::commit();
  195. } catch (Exception $e) {
  196. \think\Log::write('支付回调错误【用户-钱包充值订单】错误信息:'.$e.'。保存错误,返回信息:'.json_encode($data));
  197. }
  198. } catch (Exception $e) {
  199. }
  200. //下面这句必须要执行,且在此之前不能有任何输出
  201. return $pay->success()->send();
  202. }
  203. /**
  204. * 塘主支付入驻资金 回调
  205. * 1、用户--改为已缴费
  206. * 2、入驻信息修改
  207. */
  208. public function fishery_notify(){
  209. $paytype = $this->request->param('type');
  210. \think\Log::write('【塘主支付入驻资金】回调 starting,paytype:'.$paytype);
  211. $pay = Service::checkNotify($paytype);
  212. if (!$pay) {
  213. echo '签名错误';
  214. \think\Log::write('【塘主支付入驻资金】回调签名错误,paytype:'.$paytype);
  215. return;
  216. }
  217. $data = $pay->verify();
  218. \think\Log::write('【塘主支付入驻资金】支付返回信息:'.json_encode($data));
  219. try {
  220. $payamount = $paytype == 'alipay' ? $data['total_amount'] : $data['total_fee'] / 100;
  221. $out_trade_no = $data['out_trade_no'];
  222. //你可以在此编写订单逻辑
  223. // $payamount = 0.01;
  224. // $out_trade_no = 'RZ202302161602024840016';
  225. // $data = [];
  226. // 查询申请入驻记录
  227. $fishery_model = new Fishery();
  228. $fishery_info = $fishery_model->where('pay_no',$out_trade_no)->where('status',5)->find();
  229. if(!$fishery_info){
  230. \think\Log::write('支付回调错误【塘主支付入驻资金】未查询到关联订单号,返回信息:'.json_encode($data));
  231. }else{
  232. Db::startTrans();
  233. try {
  234. $user_id = $fishery_info->user_id;
  235. $user = \app\api\model\User::find($user_id);
  236. // 用户--改为已缴费
  237. $user->is_pond = 5;
  238. $user->save();
  239. // 入驻信息修改
  240. $fishery_data = [
  241. 'pay_time' => time(),
  242. 'status' => 4
  243. ];
  244. $fishery_info->save($fishery_data);
  245. Db::commit();
  246. } catch (Exception $e) {
  247. Db::rollback();
  248. \think\Log::write('支付回调错误【塘主支付入驻资金】错误信息:'.$e.'。保存错误,返回信息:'.json_encode($data));
  249. }
  250. }
  251. } catch (Exception $e) {
  252. }
  253. \think\Log::write('【塘主支付入驻资金】回调 end');
  254. //下面这句必须要执行,且在此之前不能有任何输出
  255. return $pay->success()->send();
  256. }
  257. /**
  258. * 渔场主供货信息 回调
  259. * 1、修改供货信息支付状态 和 发布状态
  260. */
  261. public function suppliers_notify(){
  262. $paytype = $this->request->param('type');
  263. $pay = Service::checkNotify($paytype);
  264. if (!$pay) {
  265. echo '签名错误';
  266. return;
  267. }
  268. $data = $pay->verify();
  269. try {
  270. \think\Log::write('支付回调【渔场主供货信息】,返回信息:'.json_encode($data));
  271. $payamount = $paytype == 'alipay' ? $data['total_amount'] : $data['total_fee'] / 100;
  272. $out_trade_no = $data['out_trade_no'];
  273. //你可以在此编写订单逻辑
  274. // $payamount = 1000 / 100;
  275. // $out_trade_no = 'RZ202302071540128800003';
  276. // $data = [];
  277. $suppliers_model = new FisherySuppliers();
  278. $suppliers_info = $suppliers_model
  279. //->where('user_id', $this->auth->id)
  280. ->where('pay_no', $out_trade_no)
  281. ->where('status', 2)
  282. ->find();
  283. if(!$suppliers_info){
  284. \think\Log::write('支付回调错误【渔场主供货信息】未查询到关联订单号,返回信息:'.json_encode($data));
  285. return ; // todo
  286. // return $pay->success()->send();
  287. }
  288. // 回调处理
  289. OrderCallback::Suppliers($suppliers_info);
  290. } catch (Exception $e) {
  291. }
  292. //下面这句必须要执行,且在此之前不能有任何输出
  293. return $pay->success()->send();
  294. }
  295. }