You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
4.7 KiB

7 years ago
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use Log;
  4. use DB;
  5. use Illuminate\Http\Request;
  6. use App\Common\ReturnData;
  7. use App\Common\Helper;
  8. class NotifyController extends CommonController
  9. {
  10. public function __construct()
  11. {
  12. parent::__construct();
  13. }
  14. /**
  15. * 微信支付回调
  16. */
  17. public function wxpayJsapi(Request $request)
  18. {
  19. $res = "SUCCESS"; //支付成功返回SUCCESS,失败返回FAILE
  20. //file_put_contents("1.txt",$GLOBALS['HTTP_RAW_POST_DATA']);
  21. Log::info('微信支付回调数据:'.$GLOBALS['HTTP_RAW_POST_DATA']);
  22. //获取通知的数据
  23. $xml = $GLOBALS['HTTP_RAW_POST_DATA'];
  24. //将XML转为array
  25. //禁止引用外部xml实体
  26. libxml_disable_entity_loader(true);
  27. $post_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
  28. if(isset($post_data['attach']) && !empty($post_data['attach']))
  29. {
  30. $get_arr = explode('&',$post_data['attach']);
  31. foreach($get_arr as $value)
  32. {
  33. $tmp_arr = explode('=',$value);
  34. $post_data[$tmp_arr[0]] = $tmp_arr[1];
  35. }
  36. }
  37. if($post_data['result_code'] == 'SUCCESS')
  38. {
  39. $pay_money = $post_data['total_fee']/100; //支付金额
  40. $pay_time_timestamp = strtotime(date_format(date_create($post_data['time_end']),"Y-m-d H:i:s")); //支付完成时间,时间戳格式
  41. $pay_time_date = date_format(date_create($post_data['time_end']),"Y-m-d H:i:s"); //支付完成时间,date格式Y-m-d H:i:s
  42. //$post_data['out_trade_no'] //商户订单号
  43. //$post_data['transaction_id'] //微信支付订单号
  44. //附加参数pay_type:1充值支付,2订单支付
  45. if($post_data['pay_type'] == 1)
  46. {
  47. //获取充值支付记录
  48. $user_recharge = DB::table('user_recharge')->where(array('recharge_sn'=>$post_data['out_trade_no'],'status'=>0))->first();
  49. if(!$user_recharge){Log::info('充值记录不存在');echo "FAILE";exit;}
  50. if($pay_money < $user_recharge->money){Log::info('充值金额不匹配');echo "FAILE";exit;} //如果支付金额小于要充值的金额
  51. //更新充值支付记录状态
  52. DB::table('user_recharge')->where(array('recharge_sn'=>$post_data['out_trade_no'],'status'=>0))->update(array('pay_time'=>$pay_time_timestamp,'updated_at'=>time(),'pay_type'=>1,'status'=>1,'trade_no'=>$post_data['transaction_id'],'pay_money'=>$pay_money));
  53. //增加用户余额
  54. DB::table('user')->where(array('id'=>$user_recharge->user_id))->increment('money', $pay_money);
  55. //添加用户余额记录
  56. DB::table('user_money')->insert(array('user_id'=>$user_recharge->user_id,'type'=>0,'money'=>$pay_money,'des'=>'充值','user_money'=>DB::table('user')->where(array('id'=>$user_recharge->user_id))->value('money'),'add_time'=>time()));
  57. }
  58. elseif($post_data['pay_type'] == 2)
  59. {
  60. //获取订单记录
  61. $order = DB::table('order')->where(array('order_sn'=>$post_data['out_trade_no'],'order_status'=>0,'pay_status'=>0))->first();
  62. if(!$order){Log::info('订单不存在');echo "FAILE";exit;}
  63. if($pay_money < $order->order_amount){Log::info('订单金额不匹配');exit;} //如果支付金额小于订单金额
  64. //修改订单状态
  65. $order_update_data['pay_status'] = 1;
  66. $order_update_data['pay_money'] = $pay_money; //支付金额
  67. $order_update_data['pay_id'] = 2;
  68. $order_update_data['pay_time'] = $pay_time_timestamp;
  69. $order_update_data['pay_name'] = 'wxpay_jsapi';
  70. $order_update_data['out_trade_no'] = $post_data['transaction_id'];
  71. $order_update_data['updated_at'] = time();
  72. DB::table('order')->where(array('order_sn'=>$post_data['out_trade_no'],'order_status'=>0,'pay_status'=>0))->update($order_update_data);
  73. }
  74. elseif($post_data['pay_type'] == 3)
  75. {
  76. $res = "FAILE";
  77. }
  78. elseif($post_data['pay_type'] == 4)
  79. {
  80. $res = "FAILE";
  81. }
  82. else
  83. {
  84. $res = "FAILE";
  85. }
  86. //file_put_contents("2.txt",$post_data['total_fee'].'--'.$post_data['out_trade_no'].'--'.$post_data['attach'].'--'.$post_data['pay_type']);
  87. }
  88. echo $res;
  89. }
  90. }