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.

131 lines
6.3 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. <?php
  2. namespace App\Http\Controllers\Weixin;
  3. use App\Http\Controllers\Weixin\CommonController;
  4. use Illuminate\Http\Request;
  5. use DB;
  6. use Log;
  7. class WxPayController extends CommonController
  8. {
  9. public function __construct()
  10. {
  11. parent::__construct();
  12. }
  13. /**
  14. * 微信支付回调
  15. */
  16. public function wxpayNotify(Request $request)
  17. {
  18. $res = "SUCCESS"; //支付成功返回SUCCESS,失败返回FAILE
  19. //file_put_contents("1.txt",$GLOBALS['HTTP_RAW_POST_DATA']);
  20. Log::info('微信支付回调数据:'.$GLOBALS['HTTP_RAW_POST_DATA']);
  21. //获取通知的数据
  22. $xml = $GLOBALS['HTTP_RAW_POST_DATA'];
  23. //将XML转为array
  24. //禁止引用外部xml实体
  25. libxml_disable_entity_loader(true);
  26. $post_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
  27. if(isset($post_data['attach']) && !empty($post_data['attach']))
  28. {
  29. $get_arr = explode('&',$post_data['attach']);
  30. foreach($get_arr as $value)
  31. {
  32. $tmp_arr = explode('=',$value);
  33. $post_data[$tmp_arr[0]] = $tmp_arr[1];
  34. }
  35. }
  36. if($post_data['result_code'] == 'SUCCESS')
  37. {
  38. $pay_money = $post_data['total_fee']/100; //支付金额
  39. $pay_time_timestamp = strtotime(date_format(date_create($post_data['time_end']),"Y-m-d H:i:s")); //支付完成时间,时间戳格式
  40. $pay_time_date = date_format(date_create($post_data['time_end']),"Y-m-d H:i:s"); //支付完成时间,date格式Y-m-d H:i:s
  41. //$post_data['out_trade_no'] //商户订单号
  42. //$post_data['transaction_id'] //微信支付订单号
  43. //附加参数pay_type:1充值支付,2订单支付
  44. if($post_data['pay_type'] == 1)
  45. {
  46. //获取充值支付记录
  47. $user_recharge = DB::table('user_recharge')->where(array('recharge_sn'=>$post_data['out_trade_no'],'status'=>0))->first();
  48. if(!$user_recharge){Log::info('充值记录不存在');echo "FAILE";exit;}
  49. if($pay_money < $user_recharge->money){Log::info('充值金额不匹配');echo "FAILE";exit;} //如果支付金额小于要充值的金额
  50. //更新充值支付记录状态
  51. 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));
  52. //增加用户余额
  53. DB::table('user')->where(array('id'=>$user_recharge->user_id))->increment('money', $pay_money);
  54. //添加用户余额记录
  55. 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()));
  56. }
  57. elseif($post_data['pay_type'] == 2)
  58. {
  59. //获取订单记录
  60. $order = DB::table('order')->where(array('order_sn'=>$post_data['out_trade_no'],'order_status'=>0,'pay_status'=>0))->first();
  61. if(!$order){Log::info('订单不存在');echo "FAILE";exit;}
  62. if($pay_money < $order->order_amount){Log::info('订单金额不匹配');exit;} //如果支付金额小于订单金额
  63. //修改订单状态
  64. $order_update_data['pay_status'] = 1;
  65. $order_update_data['pay_money'] = $pay_money; //支付金额
  66. $order_update_data['pay_id'] = 2;
  67. $order_update_data['pay_time'] = $pay_time_timestamp;
  68. $order_update_data['pay_name'] = 'wxpay_jsapi';
  69. $order_update_data['out_trade_no'] = $post_data['transaction_id'];
  70. $order_update_data['updated_at'] = time();
  71. DB::table('order')->where(array('order_sn'=>$post_data['out_trade_no'],'order_status'=>0,'pay_status'=>0))->update($order_update_data);
  72. }
  73. elseif($post_data['pay_type'] == 3)
  74. {
  75. $res = "FAILE";
  76. }
  77. elseif($post_data['pay_type'] == 4)
  78. {
  79. $res = "FAILE";
  80. }
  81. else
  82. {
  83. $res = "FAILE";
  84. }
  85. //file_put_contents("2.txt",$post_data['total_fee'].'--'.$post_data['out_trade_no'].'--'.$post_data['attach'].'--'.$post_data['pay_type']);
  86. }
  87. echo $res;
  88. }
  89. /**
  90. * 发微信红包
  91. * @param string $openid 用户openid
  92. */
  93. public function wxhbpay($re_openid,$money,$wishing='恭喜发财,大吉大利!',$act_name='赠红包活动',$remark='赶快领取您的红包!')
  94. {
  95. //微信支付-start
  96. require_once(resource_path('org/wxpay/WxPayConfig.php')); // 导入微信配置类
  97. require_once(resource_path('org/wxpay/WxPayPubHelper.class.php')); // 导入微信支付类
  98. $wxconfig= \WxPayConfig::wxconfig();
  99. $wxHongBaoHelper = new \Sendredpack($wxconfig);
  100. $wxHongBaoHelper->setParameter("mch_billno", date('YmdHis').rand(1000, 9999));//订单号
  101. $wxHongBaoHelper->setParameter("send_name", '红包');//红包发送者名称
  102. $wxHongBaoHelper->setParameter("re_openid", $re_openid);//接受openid
  103. $wxHongBaoHelper->setParameter("total_amount", floatval($money*100));//付款金额,单位分
  104. $wxHongBaoHelper->setParameter("total_num", 1);//红包収放总人数
  105. $wxHongBaoHelper->setParameter("wishing", $wishing);//红包祝福
  106. $wxHongBaoHelper->setParameter("client_ip", '127.0.0.1');//调用接口的机器 Ip 地址
  107. $wxHongBaoHelper->setParameter("act_name", $act_name);//活劢名称
  108. $wxHongBaoHelper->setParameter("remark", $remark);//备注信息
  109. $responseXml = $wxHongBaoHelper->postXmlSSL();
  110. //用作结果调试输出
  111. //echo htmlentities($responseXml,ENT_COMPAT,'UTF-8');
  112. $responseObj = simplexml_load_string($responseXml, 'SimpleXMLElement', LIBXML_NOCDATA);
  113. return $responseObj->result_code;
  114. }
  115. }