WxpayController.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2018/5/19
  6. * Time: 16:37
  7. */
  8. namespace app\web\controller;
  9. use think\Db;
  10. header('Access-Control-Allow-Origin:https://open.weixin.qq.com');
  11. class WxpayController extends MainController{
  12. public function __construct()
  13. {
  14. parent::__construct();
  15. require_once CMF_ROOT.'vendor/wxpay/lib/WxPayApi.php';
  16. require_once CMF_ROOT.'vendor/wxpay/WxPayJsApiPay.php';
  17. require_once CMF_ROOT.'vendor/wxpay/log.php';
  18. require_once CMF_ROOT.'vendor/wxpay/lib/WxPay.Config.php';
  19. }
  20. //统一下单
  21. public function pay_order()
  22. {
  23. $id=$this->request->param('id');
  24. if(!isset($id)||empty($id)){
  25. $this->api_return_json(array('msg'=>'该订单id不存在')) ;
  26. }
  27. $order=Db::name('orders')
  28. ->field('o.order_sn,o.goods_id,o.hospital_id,o.appointment_time,o.pay_price,p.title,u.wx_openid')
  29. ->alias('o')
  30. ->join('goods p', 'p.id=o.goods_id', 'left')
  31. ->join('wx_user u', 'u.user_id=o.user_id', 'left')
  32. ->where(array('o.id'=>$id,'o.is_delete'=>0))
  33. ->find();
  34. if(empty($order))
  35. $this->api_return_json(array('msg'=>'订单不存在'));
  36. //检测库存
  37. //总库存
  38. $res=db('hospital_good')->where(['good_id'=>$order['goods_id'],'hospital_id'=>$order['hospital_id'],'deleted_at'=>null])->find();
  39. if(empty($res))
  40. $this->api_return_json(array('msg'=>'医院疫苗信息不存在'));
  41. //已预约库存
  42. $has=db('orders')->where(['goods_id'=>$order['goods_id'],'hospital_id'=>$order['hospital_id'],'appointment_time'=>$order['appointment_time'],'pay_status'=>array(1,2)])->count();
  43. if((int)$res['stock']<=$has)
  44. $this->api_return_json(array('msg'=>'您预约时间的疫苗库存不足,请重新换个时间预约'));
  45. $order_money=$order['pay_price'];
  46. /*------------------------支付流程----------------------------*/
  47. $tools = new \JsApiPay();
  48. //②、统一下单
  49. $input = new \WxPayUnifiedOrder();
  50. $input->SetBody($order['title']);
  51. $input->SetOut_trade_no($order['order_sn']);
  52. if ($this->is_product) {
  53. $input->SetTotal_fee(((double)($order_money) * 100));
  54. } else {
  55. $input->SetTotal_fee(((double)(0.01) * 100));
  56. }
  57. //
  58. $input->SetTime_start(date("YmdHis"));
  59. $input->SetTime_expire(date("YmdHis", time() + 600));
  60. $input->SetGoods_tag('预约疫苗');
  61. $input->SetNotify_url(url('web/Notify/orderNotify','','html',true));
  62. $input->SetTrade_type("JSAPI");
  63. $input->SetSpbill_create_ip(get_client_ip());
  64. $input->SetOpenid($order['wx_openid']);
  65. $orders = \WxPayApi::unifiedOrder($input);
  66. $jsApiParameters = json_decode($tools->GetJsApiParameters($orders));
  67. $this->api_return_json(array('pay_key' => $jsApiParameters), 1);
  68. }
  69. //统一下单会员
  70. public function pay_member_order()
  71. {
  72. $id=$this->request->param('id');
  73. if(!isset($id)||empty($id)){
  74. $this->api_return_json(array('msg'=>'该订单id不存在')) ;
  75. }
  76. $order=Db::name('orders')
  77. ->field('o.order_sn,o.goods_id,o.hospital_id,o.appointment_time,o.pay_price,p.title,u.wx_openid')
  78. ->alias('o')
  79. ->join('goods p', 'p.id=o.goods_id', 'left')
  80. ->join('wx_user u', 'u.user_id=o.user_id', 'left')
  81. ->where(array('o.id'=>$id,'o.is_delete'=>0))
  82. ->find();
  83. if(empty($order))
  84. $this->api_return_json(array('msg'=>'订单不存在'));
  85. //检测库存
  86. //总库存
  87. $order_money=$order['pay_price'];
  88. /*------------------------支付流程----------------------------*/
  89. $tools = new \JsApiPay();
  90. //②、统一下单
  91. $input = new \WxPayUnifiedOrder();
  92. $input->SetBody('会员服务');
  93. $input->SetOut_trade_no($order['order_sn']);
  94. if ($this->is_product) {
  95. $input->SetTotal_fee(((double)($order_money) * 100));
  96. } else {
  97. $input->SetTotal_fee(((double)(0.01) * 100));
  98. }
  99. //
  100. $input->SetTime_start(date("YmdHis"));
  101. $input->SetTime_expire(date("YmdHis", time() + 600));
  102. $input->SetGoods_tag('会员');
  103. $input->SetNotify_url(url('web/Notify/memberOrderNotify','','html',true));
  104. $input->SetTrade_type("JSAPI");
  105. $input->SetSpbill_create_ip(get_client_ip());
  106. $input->SetOpenid($order['wx_openid']);
  107. $orders = \WxPayApi::unifiedOrder($input);
  108. $jsApiParameters = json_decode($tools->GetJsApiParameters($orders));
  109. $this->api_return_json(array('pay_key' => $jsApiParameters), 1);
  110. }
  111. //申请退款
  112. public function apply_refund($out_trade_no='t_2018060218250996720000000001',$total_fee=1,$refund_fee=1){
  113. /*------------------------退款流程----------------------------*/
  114. file_put_contents(CMF_ROOT . "data/refund_log/refund_log.txt", date('Y-m-d H:i:s')."\n进入退款,订单号:".$out_trade_no."订单金额:".$total_fee."退款金额:".$refund_fee."\n", FILE_APPEND);
  115. $input = new \WxPayRefund();
  116. $input->SetOut_trade_no($out_trade_no);
  117. $input->SetTotal_fee($total_fee);
  118. $input->SetRefund_fee($refund_fee);
  119. $input->SetOut_refund_no('1529522091'.date("YmdHis"));
  120. $input->SetOp_user_id('1529522091');
  121. $result=\WxPayApi::refund($input);
  122. file_put_contents(CMF_ROOT . "data/refund_log/refund_log.txt", date('Y-m-d H:i:s')."\n退款的结果:" . json_encode($result).'退款单号:'.$out_trade_no.'退款金额:'.$refund_fee.'付款金额:'.$total_fee."\n", FILE_APPEND);
  123. if($result['return_code']=='SUCCESS' && $result['result_code']=='SUCCESS'){
  124. return array('return_code'=>$result['return_code'],'result_code'=>$result['result_code']);
  125. }else{
  126. return array('return_code'=>$result['return_code'],'result_code'=>$result['result_code'],'err_code_des'=>$result['err_code_des'],'return_msg'=>$result['return_msg']);
  127. }
  128. exit();
  129. }
  130. //发红包
  131. public function sendRedpackage($money,$user_id){
  132. header("content-type:text/html;charset=utf-8");
  133. $url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack';
  134. $user=Db::name('wx_user')->field('wx_openid2 as wx_openid')->where(array('user_id'=>$user_id))->find();
  135. $params = array();
  136. $account_key = \WxPayConfig::KEY;
  137. $params['nonce_str'] = md5(time() . rand(1, 9999));
  138. $params['wxappid'] = config('mp_wxconfig')['appid'];//wxd6053c3091d8aaaf
  139. $params['mch_id'] = \WxPayConfig::MCHID;
  140. $params['mch_billno'] = 'red'.date("YmdHis",time()).mt_rand(10,99);
  141. $params['send_name'] = '云货舱';
  142. $params['scene_id']='PRODUCT_5';
  143. $params['re_openid'] =$user['wx_openid'];//
  144. $params['total_amount'] =(int)$money*100;//分
  145. $params['total_num'] = 1;
  146. $params['wishing'] = '恭喜您提现成功';
  147. $params['client_ip'] = $this->real_ip();
  148. $params['act_name'] = '提现';
  149. $params['remark'] = '由于商户号3个月交易记录才能开放提现,故先采取发红包方式';
  150. ksort($params);
  151. $param_str = '';
  152. foreach ($params as $key => $param) {
  153. $param_str .= $key . '=' . $param . '&';
  154. }
  155. $sign = strtoupper(md5($param_str . 'key=' . $account_key));
  156. $params['sign'] = $sign;
  157. ksort($params);
  158. $xml = $this->ToXml($params);
  159. //$result = $this->curl_post_xml($url, $xml);
  160. // echo BASEPATH.'../wx_pay/cert/apiclient_key.pem';exit;
  161. $result = \WxPayApi::postXmlCurl($xml, $url, true);
  162. $result = $this->FromXml($result);
  163. try{
  164. if($result['result_code']=='SUCCESS')
  165. return true;
  166. else
  167. return false;
  168. }catch (\Exception $e){
  169. return false;
  170. }
  171. }
  172. /**
  173. * 获得用户的真实IP地址
  174. *
  175. * @access public
  176. * @return string
  177. */
  178. function real_ip()
  179. {
  180. static $realip = NULL;
  181. if ($realip !== NULL)
  182. {
  183. return $realip;
  184. }
  185. if (isset($_SERVER))
  186. {
  187. if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
  188. {
  189. $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
  190. /* 取X-Forwarded-For中第一个非unknown的有效IP字符串 */
  191. foreach ($arr AS $ip)
  192. {
  193. $ip = trim($ip);
  194. if ($ip != 'unknown')
  195. {
  196. $realip = $ip;
  197. break;
  198. }
  199. }
  200. }
  201. elseif (isset($_SERVER['HTTP_CLIENT_IP']))
  202. {
  203. $realip = $_SERVER['HTTP_CLIENT_IP'];
  204. }
  205. else
  206. {
  207. if (isset($_SERVER['REMOTE_ADDR']))
  208. {
  209. $realip = $_SERVER['REMOTE_ADDR'];
  210. }
  211. else
  212. {
  213. $realip = '0.0.0.0';
  214. }
  215. }
  216. }
  217. else
  218. {
  219. if (getenv('HTTP_X_FORWARDED_FOR'))
  220. {
  221. $realip = getenv('HTTP_X_FORWARDED_FOR');
  222. }
  223. elseif (getenv('HTTP_CLIENT_IP'))
  224. {
  225. $realip = getenv('HTTP_CLIENT_IP');
  226. }
  227. else
  228. {
  229. $realip = getenv('REMOTE_ADDR');
  230. }
  231. }
  232. preg_match("/[\d\.]{7,15}/", $realip, $onlineip);
  233. $realip = !empty($onlineip[0]) ? $onlineip[0] : '0.0.0.0';
  234. return $realip;
  235. }
  236. }