RefundController.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use function EasyWeChat\Kernel\Support\str_random;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Support\Facades\DB;
  6. use Illuminate\Support\Facades\Auth;
  7. use Illuminate\Support\Facades\Log;
  8. use Illuminate\Support\Facades\Validator;
  9. use EasyWeChat\Factory;
  10. class RefundController extends Controller
  11. {
  12. public function __construct()
  13. {
  14. }
  15. public function info(Request $request){
  16. $input=$request->all();
  17. $data= DB::table("charge")->where('uid',$input['id'])->orderBy('month','desc')->get();
  18. return response()->json([
  19. 'data'=>$data,
  20. ]);
  21. }
  22. public function refund(Request $request)
  23. {
  24. $input=$request->all();
  25. $where['status']=1;
  26. $where['id']=$input['id'];
  27. $aaid=Auth::guard('admin')->user()->id;
  28. $order_pay=DB::table("charge")->where($where)->first();
  29. if(!$order_pay){
  30. return response()->json([
  31. 'error_code'=>410015,
  32. 'msg'=>"订单信息有误"
  33. ]);
  34. }
  35. if($order_pay->out_refund_no){
  36. $refund_no=$order_pay->out_refund_no;
  37. }else{
  38. $refund_no='T'.date("YmdHis").substr(md5(mt_rand(1000,9999)),10,12);
  39. }
  40. if($order_pay->type==2){
  41. $row=DB::table("charge")->where("id",$input['id'])->update([
  42. 'status'=>3,
  43. 'refund_id'=>0,
  44. 'out_refund_no'=>$refund_no,
  45. 'refund_fee'=>$order_pay->fee,
  46. 'refund_at'=>date("Y-m-d H:i:s"),
  47. 'refund_status'=>1
  48. ]);
  49. if($row){
  50. Log::info('管理员'.$aaid.'操作订单id'.$input['id'].'退款成功');
  51. return response()->json([
  52. 'error_code'=>200,
  53. 'msg'=>"退款成功"
  54. ]);
  55. }else{
  56. return response()->json([
  57. 'error_code'=>410015,
  58. 'msg'=>"退款失败"
  59. ]);
  60. }
  61. }
  62. $time=strtotime("-1 years");
  63. $paytime=strtotime($order_pay->pays_at);
  64. if($time>=$paytime){
  65. return response()->json([
  66. 'error_code'=>410015,
  67. 'msg'=>"订单超过一年,不能退款"
  68. ]);
  69. }
  70. $app = Factory::payment(config('wechat.defaults.payment'));
  71. $total_money = $order_pay->fee * 100;
  72. $refund_money = $order_pay->fee * 100;
  73. $out_trade_no=$order_pay->out_trade_no;
  74. $result = $app->refund->byOutTradeNumber(
  75. $out_trade_no,//商户订单号
  76. $refund_no,//商户退款单号
  77. $total_money,//订单金额
  78. $refund_money,//退款金额
  79. [
  80. 'refund_desc' => '退款',
  81. 'notify_url' => 'http://'. $request->server('SERVER_NAME').'/api/refund/refundsuccess',
  82. 'out_refund_no' => $refund_no
  83. ]
  84. );
  85. if($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
  86. DB::table('charge')->where($where)->update([
  87. 'status'=>2,
  88. ]);
  89. Log::info('管理员'.$aaid.'操作订单号'.$out_trade_no.'退款申请请求成功');
  90. return response()->json([
  91. 'error_code'=>200,
  92. 'msg'=>'退款申请请求成功'
  93. ]);
  94. }else{
  95. return response()->json([
  96. 'error_code'=>410015,
  97. 'msg'=>'退款申请请求失败',
  98. // 'data'=>$result
  99. ]);
  100. }
  101. }
  102. // public function req_info_decrypt($str){
  103. // $key = md5(config('wechat.defaults.payment.key'));
  104. // $str = base64_decode($str);
  105. // $str=openssl_decrypt($str, 'aes-256-ecb',$key,1,substr( 0, 16));
  106. // $len = strlen($str);
  107. // $pad = ord($str[$len - 1]);
  108. // $xml = substr($str, 0, strlen($str) - $pad);
  109. // $ss=substr($str,0,35);
  110. // DB::table('charge')->where('id','230')->update([
  111. // 'out_refund_no'=>$ss
  112. // ]);
  113. // libxml_disable_entity_loader(true);
  114. // return json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
  115. // }
  116. public function refundSuccess()
  117. {
  118. $app = Factory::payment(config('wechat.defaults.payment'));
  119. $response = $app->handleRefundedNotify(function ($message,$reqinfo, $fail) {
  120. if($message['appid'] != config('wechat.defaults.payment.app_id')){
  121. //不是商户本身的请求,直接略过
  122. return true;
  123. }
  124. Log::info($message['req_info']);
  125. $order= DB::table("charge")->where('out_trade_no',$reqinfo['out_trade_no'])->first();
  126. if ($order->status == '3') {
  127. return true;
  128. }
  129. // $data = $this->req_info_decrypt($message['req_info']);
  130. if($reqinfo['refund_status'] === 'SUCCESS') {
  131. // Log::info($message['out_trade_no'].'/'.$message['transaction_id'].'/'.$message['result_code']);
  132. $row=DB::table('charge')
  133. ->where('out_trade_no',$reqinfo['out_trade_no'])
  134. ->update([
  135. 'status'=>3,
  136. 'refund_id'=>$reqinfo['refund_id'],
  137. 'out_refund_no'=>$reqinfo['out_refund_no'],
  138. 'refund_fee'=>$reqinfo['settlement_refund_fee']/100,
  139. 'refund_at'=>$reqinfo['success_time'],
  140. 'refund_status'=>1
  141. ]);
  142. if($row){
  143. Log::info('微信退款成功 out_refund_no:'.$reqinfo['out_refund_no']);
  144. return true;
  145. }
  146. }
  147. });
  148. return $response;
  149. }
  150. public function refundListInfo(Request $request){
  151. $where=[];
  152. $adminInfo=Auth::guard('admin')->user();
  153. if($adminInfo){
  154. $atis=$adminInfo->is_team;
  155. if($atis==2){
  156. $where['charge.team_id']=$adminInfo->team_id;
  157. }
  158. }
  159. $search_name = $request->search_name;
  160. $page_size = $request->page_size;
  161. $page_index = $request->page_index;
  162. $num = ($page_index - 1) * $page_size;
  163. // $where['charge.status']=3;
  164. if(!empty($request->month)){
  165. $where['charge.month'] = date("Y-m",strtotime($request->month));
  166. }
  167. if(!empty($request->team_id)){
  168. $where['charge.team_id'] = $request->team_id;
  169. }
  170. $count = DB::table('charge')
  171. ->join('users', 'users.id', '=', 'charge.uid')
  172. ->leftJoin('team','team.id','=','charge.team_id')
  173. ->where(function ($query) use ($search_name) {
  174. $query->where('users.name', 'like', '%' . $search_name . '%')
  175. ->orWhere('users.telphone', 'like', '%' . $search_name . '%')
  176. ->orWhere('users.cre_num', 'like', '%' . $search_name . '%');
  177. })
  178. ->where(function ($query) use ($search_name) {
  179. $query->where('charge.status', '=', '2')
  180. ->orWhere('charge.status', '=', '3');
  181. })
  182. ->where($where)
  183. ->count();
  184. if ($count > 0) {
  185. $data = DB::table('charge')
  186. ->join('users', 'users.id', '=', 'charge.uid')
  187. ->leftJoin('team','team.id','=','charge.team_id')
  188. ->where(function ($query) use ($search_name) {
  189. $query->where('users.name', 'like', '%' . $search_name . '%')
  190. ->orWhere('users.telphone', 'like', '%' . $search_name . '%')
  191. ->orWhere('users.cre_num', 'like', '%' . $search_name . '%');
  192. })
  193. ->where(function ($query) use ($search_name) {
  194. $query->where('charge.status', '=', '2')
  195. ->orWhere('charge.status', '=', '3');
  196. })
  197. ->where($where)
  198. ->select('charge.id','users.name','users.telphone','users.cre_num','team.name as tname','team.id as tid','charge.fee', 'charge.status', 'charge.month', 'charge.pays_at','charge.type','charge.refund_at','charge.refund_status')
  199. ->orderBy('charge.refund_at','desc')
  200. ->skip($num)->take($page_size)->get();
  201. if ($data) {
  202. $data=json_decode($data,true);
  203. foreach ($data as $key => $val) {
  204. $data[$key]['month'] = substr($val['month'], 0, 7);
  205. }
  206. return response()->json([
  207. 'error_code' => 200,
  208. 'msg' => '成功',
  209. 'data' => $data,
  210. 'count' => $count
  211. ]);
  212. } else {
  213. return response()->json([
  214. 'error_code' => 0,
  215. 'msg' => '获取失败'
  216. ]);
  217. }
  218. }else{
  219. return response()->json([
  220. 'error_code' => 200,
  221. 'msg' => '没有缴费信息',
  222. 'list' => [],
  223. 'count' => $count
  224. ]);
  225. }
  226. }
  227. }