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.

144 lines
6.2 KiB

7 years ago
  1. <?php
  2. //微信支付设置
  3. public function wxconf()
  4. {
  5. //=======【基本信息设置】=====================================
  6. //微信公众号身份的唯一标识。审核通过后,在微信发送的邮件中查看
  7. $wxconfig['APPID'] = 'wx1c7946b5734199d0';
  8. //受理商ID,身份标识
  9. $wxconfig['MCHID'] = '1331184301';
  10. //商户支付密钥Key。审核通过后,在微信发送的邮件中查看
  11. $wxconfig['KEY'] = '93aa64d6552bf09401af7e7e6f9b3be7';
  12. //JSAPI接口中获取openid,审核后在公众平台开启开发模式后可查看
  13. $wxconfig['APPSECRET'] = '93aa64d6552bf09401af7e7e6f9b3be7';
  14. //=======【JSAPI路径设置】===================================
  15. //获取access_token过程中的跳转uri,通过跳转将code传入jsapi支付页面
  16. $wxconfig['JS_API_CALL_URL'] = 'http://'.$_SERVER['HTTP_HOST'].U('Wxpay/index');
  17. //=======【证书路径设置】=====================================
  18. //证书路径,注意应该填写绝对路径
  19. $wxconfig['SSLCERT_PATH'] = './cert/apiclient_cert.pem';
  20. $wxconfig['SSLKEY_PATH'] = './cert/apiclient_key.pem';
  21. //=======【异步通知url设置】===================================
  22. //异步通知url,商户根据实际开发过程设定
  23. $wxconfig['NOTIFY_URL'] = 'http://'.$_SERVER['HTTP_HOST'].U('Wxpay/notify_url');
  24. //=======【curl超时设置】===================================
  25. //本例程通过curl使用HTTP POST方法,此处可修改其超时时间,默认为30秒
  26. $wxconfig['CURL_TIMEOUT'] = 30;
  27. return $wxconfig;
  28. }
  29. //PHP服务端SDK生成APP支付订单信息示例
  30. public function wxt()
  31. {
  32. $body = '商品购买';//订单详情
  33. $out_trade_no = '2017787878';//订单号
  34. $total_fee = floatval(0.01*100);//价格3880.00
  35. $wxconfig=$this->wxconf();
  36. //=========步骤1:网页授权获取用户openid============
  37. //使用jsapi接口
  38. require_once './WxPayPubHelper.class.php';
  39. //import("@.ORG.Wxpay.WxPayPubHelper");
  40. //Vendor('Wxpay.WxPayPubHelper');// 导入微信类
  41. $jsApi = new JsApi_pub($wxconfig);
  42. //通过code获得openid
  43. if (!isset($_GET['code']))
  44. {
  45. //触发微信返回code码
  46. //$url = $jsApi->createOauthUrlForCode($this->wxconfig['JS_API_CALL_URL']);
  47. $reurl='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
  48. $url = $jsApi->createOauthUrlForCode(urlencode($reurl));
  49. Header("Location: $url"); exit;
  50. }
  51. else
  52. {
  53. //获取code码,以获取openid
  54. $code = $_GET['code'];
  55. $jsApi->setCode($code);
  56. $openid = $jsApi->getOpenId();
  57. }
  58. //=========步骤2:使用统一支付接口,获取prepay_id============
  59. //使用统一支付接口
  60. $unifiedOrder = new UnifiedOrder_pub($wxconfig);
  61. $notify_url = $wxconfig['NOTIFY_URL'];//通知地址
  62. //设置统一支付接口参数
  63. //设置必填参数
  64. //appid已填,商户无需重复填写
  65. //mch_id已填,商户无需重复填写
  66. //noncestr已填,商户无需重复填写
  67. //spbill_create_ip已填,商户无需重复填写
  68. //sign已填,商户无需重复填写
  69. $unifiedOrder->setParameter("openid","$openid");//微信用户
  70. $unifiedOrder->setParameter("body","$body");//商品描述
  71. $unifiedOrder->setParameter("out_trade_no","$out_trade_no");//商户订单号
  72. $unifiedOrder->setParameter("total_fee","$total_fee");//总金额
  73. $unifiedOrder->setParameter("notify_url","$notify_url");//通知地址
  74. $unifiedOrder->setParameter("trade_type","JSAPI");//交易类型
  75. //$unifiedOrder->setParameter("attach","test"); //附加数据,选填,在查询API和支付通知中原样返回,可作为自定义参数使用
  76. $prepay_id = $unifiedOrder->getPrepayId();
  77. //=========步骤3:使用jsapi调起支付============
  78. $jsApi->setPrepayId($prepay_id);
  79. $jsApiParameters = $jsApi->getParameters();
  80. $this->assign('jsApiParameters',$jsApiParameters);
  81. $returnUrl='http://'.$_SERVER['HTTP_HOST'].U('User/index');
  82. $this->assign('returnUrl',$returnUrl);
  83. $this->display();
  84. }
  85. //PHP服务端验证异步通知信息参数示例
  86. public function AlipayTradeAppPayNotify()
  87. {
  88. $xml = $GLOBALS['HTTP_RAW_POST_DATA'];
  89. $post_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
  90. $array_data['out_trade_no'] = substr($post_data['out_trade_no'],0,-5);
  91. $array_data['total_fee'] = $post_data['total_fee'];
  92. $array_data['trade_state'] = $post_data['result_code'];
  93. $array_data['transaction_id'] = $post_data['transaction_id'];
  94. $get_arr = explode('&',$post_data['attach']);
  95. foreach($get_arr as $value){
  96. $tmp_arr = explode('=',$value);
  97. $array_data[$tmp_arr[0]] = $tmp_arr[1];
  98. }
  99. $wxorder=serialize($array_data);//保存post数据
  100. $out_trade_no = $array_data['out_trade_no'];
  101. if($array_data['trade_state']=='SUCCESS' )
  102. {
  103. echo "SUCCESS";
  104. }
  105. else
  106. {
  107. echo "FAILE";
  108. }
  109. }
  110. /**
  111. * 微信红包
  112. * @param string $openid 用户openid
  113. */
  114. public function wxhbpay($re_openid,$money,$wishing='恭喜发财,大吉大利!',$act_name='赠红包活动',$remark='赶快领取您的红包!')
  115. {
  116. import("@.ORG.Wxpay.WxPayPubHelper");
  117. $wxHongBaoHelper = new Sendredpack($this->wxconfig);
  118. $wxHongBaoHelper->setParameter("mch_billno", date('YmdHis').rand(1000, 9999));//订单号
  119. $wxHongBaoHelper->setParameter("send_name", '红包');//红包发送者名称
  120. $wxHongBaoHelper->setParameter("re_openid", $re_openid);//接受openid
  121. $wxHongBaoHelper->setParameter("total_amount", floatval($money*100));//付款金额,单位分
  122. $wxHongBaoHelper->setParameter("total_num", 1);//红包収放总人数
  123. $wxHongBaoHelper->setParameter("wishing", $wishing);//红包祝福
  124. $wxHongBaoHelper->setParameter("client_ip", '127.0.0.1');//调用接口的机器 Ip 地址
  125. $wxHongBaoHelper->setParameter("act_name", $act_name);//活劢名称
  126. $wxHongBaoHelper->setParameter("remark", $remark);//备注信息
  127. $responseXml = $wxHongBaoHelper->postXmlSSL();
  128. //用作结果调试输出
  129. //echo htmlentities($responseXml,ENT_COMPAT,'UTF-8');
  130. $responseObj = simplexml_load_string($responseXml, 'SimpleXMLElement', LIBXML_NOCDATA);
  131. return $responseObj->result_code;
  132. }