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.

103 lines
4.8 KiB

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