wxpay_jspay.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. include("../../../config/conn.php");
  3. include("../../../config/function.php");
  4. sesCheck_m();
  5. $sqluser="select * from yjcode_user where uid='".$_SESSION[SHOPUSER]."'";mysql_query("SET NAMES 'GBK'");$resuser=mysql_query($sqluser);
  6. if(!$rowuser=mysql_fetch_array($resuser)){php_toheader("../reg/");}
  7. $sj=date("Y-m-d H:i:s");
  8. if(sqlzhuru($_POST[jvs])=="carpay"){
  9. include("../../../user/buycheck.php");
  10. if($needmoney<=$usermoney){Audit_alert("您的可用余额充足,请用余额直接支付。","../carpay.php?carid=".$carid);}
  11. zwzr();
  12. $bh=time();
  13. $_SESSION[wxddbh]=time()."wx".$rowuser[id]."wx".rnd_num(1000);
  14. $uip=$_SERVER["REMOTE_ADDR"];
  15. $ddbh=time()."|".$rowuser[id];
  16. $money1=sprintf("%.2f",($needmoney-$usermoney));
  17. intotable("yjcode_dingdang","bh,ddbh,userid,sj,uip,money1,ddzt,alipayzt,bz,ifok,wxddbh,carid","'".$bh."','".$ddbh."',".$rowuser[id].",'".$sj."','".$uip."',".$money1.",'等待买家付款','','微信手机支付',0,'".$_SESSION[wxddbh]."','".$caridarr."'");
  18. }
  19. $lib_path = dirname(__FILE__)."/";
  20. require_once $lib_path."WxPay.Config.php";
  21. require_once $lib_path."WxPay.Api.php";
  22. require_once $lib_path."WxPay.Notify.php";
  23. require_once $lib_path."WxPay.JsApiPay.php";
  24. require_once $lib_path."log.php";
  25. require_once "wxconfig.php";
  26. WxPayConfig::set_appid( $payment['wxpay_jspay_appid'] );
  27. WxPayConfig::set_appsecret( $payment['wxpay_jspay_appsecret']);
  28. WxPayConfig::set_mchid( $payment['wxpay_jspay_mchid'] );
  29. WxPayConfig::set_key( $payment['wxpay_jspay_key'] );
  30. if ( empty($_SESSION['wxpay_jspay_openid']) ){
  31. if( empty($payment) ){
  32. return false;
  33. }
  34. $tools = new JsApiPay();
  35. $openid = $tools->GetOpenid();
  36. $_SESSION['wxpay_jspay_openid'] = $openid;
  37. }
  38. $html = '<div style="text-align:center"><button class="btn btn-primary c-btn3" type="button" onclick="javascript:alert(\'请在微信客户端打开链接\')">微信安全支付</button></div>';
  39. // 网页授权获取用户openid
  40. if (! isset($_SESSION['wxpay_jspay_openid']) || empty($_SESSION['wxpay_jspay_openid'])) {
  41. echo $html;
  42. exit();
  43. }
  44. $openId = $_SESSION['wxpay_jspay_openid'];
  45. while1("*","yjcode_dingdang where wxddbh='".$_SESSION[wxddbh]."'");$row1=mysql_fetch_array($res1);$moneyv=$row1['money1']*100;
  46. $root_url = weburl;
  47. //$root_url = str_replace('mobile/', '', $root_url);
  48. $notify_url = $root_url.'m/user/wxpay/wxpay_jspay_notify.php';
  49. $out_trade_no = $_SESSION['wxddbh'];
  50. //var_dump($notify_url);
  51. //iconv("GB2312","UTF-8//IGNORE",webname."商品结算")
  52. //统一下单
  53. $tools = new JsApiPay();
  54. $input = new WxPayUnifiedOrder();
  55. $input->SetBody( $out_trade_no );
  56. $input->SetAttach( $out_trade_no ); //商户支付日志
  57. $input->SetOut_trade_no( $out_trade_no ); //商户订单号
  58. $input->SetTotal_fee( $moneyv ); //总金额
  59. $input->SetTime_start(date("YmdHis"));
  60. //$input->SetTime_expire(date("YmdHis", time() + 600));
  61. //$input->SetGoods_tag("test");
  62. $input->SetNotify_url( $notify_url ); //通知地址
  63. $input->SetTrade_type("JSAPI"); //交易类型
  64. $input->SetProduct_id( $out_trade_no );
  65. $input->SetOpenid($openId);
  66. $wxpay_order = WxPayApi::unifiedOrder($input);
  67. if ( $wxpay_order['return_code'] != 'FALL' ){
  68. $jsApiParameters = $tools->GetJsApiParameters($wxpay_order);
  69. $error = '';
  70. if ( strpos($jsApiParameters, 'error:') === 0 ){
  71. $error = str_replace('error:', '', $jsApiParameters);
  72. $jsApiParameters = '{}';
  73. }
  74. }else{
  75. $error = $wxpay_order['return_msg'];
  76. }
  77. $html = '<div style="text-align:center"><button class="btn btn-primary c-btn3" type="button" onclick="javascript:alert(\'请在微信客户端打开链接\')">微信安全支付</button></div>';
  78. if( empty($error) )
  79. {
  80. $js = '<script type="text/javascript">
  81. function jsApiCall()
  82. {
  83. WeixinJSBridge.invoke(
  84. "getBrandWCPayRequest",
  85. '.$jsApiParameters.',
  86. function(res){
  87. //WeixinJSBridge.log(res.err_msg);
  88. if(res.err_msg == "get_brand_wcpay_request:ok"){
  89. //alert(res.err_code+res.err_desc+res.err_msg);
  90. window.location.href = "'. weburl .'m/user/order.php";
  91. //window.location.replace("'. $root_url .'");
  92. }else{
  93. //返回跳转到订单详情页面
  94. alert(支付失败);
  95. window.location.href = "./index.php";
  96. }
  97. }
  98. );
  99. }
  100. function callpay()
  101. {
  102. if (typeof WeixinJSBridge == "undefined"){
  103. if( document.addEventListener ){
  104. document.addEventListener("WeixinJSBridgeReady", jsApiCall, false);
  105. }else if (document.attachEvent){
  106. document.attachEvent("WeixinJSBridgeReady", jsApiCall);
  107. document.attachEvent("onWeixinJSBridgeReady", jsApiCall);
  108. }
  109. }else{
  110. jsApiCall();
  111. }
  112. }
  113. callpay()
  114. </script>';
  115. $html = '<div style="text-align:center"><button class="btn btn-primary c-btn3" type="button" onclick="callpay()">微信安全支付</button></div>'.$js;
  116. }else{
  117. $html = '<div style="text-align:center"><button class="btn btn-primary c-btn3" type="button" onclick="javascript:alert(\''. $error .'\')">微信安全支付</button></div>';
  118. }
  119. echo $html;
  120. exit;
  121. class PayNotifyCallBack extends WxPayNotify
  122. {
  123. public $data;
  124. //查询订单
  125. public function Queryorder($transaction_id)
  126. {
  127. $input = new WxPayOrderQuery();
  128. $input->SetTransaction_id($transaction_id);
  129. $result = WxPayApi::orderQuery($input);
  130. Log::DEBUG("query:" . json_encode($result));
  131. if(array_key_exists("return_code", $result)
  132. && array_key_exists("result_code", $result)
  133. && $result["return_code"] == "SUCCESS"
  134. && $result["result_code"] == "SUCCESS")
  135. {
  136. return true;
  137. }
  138. return false;
  139. }
  140. //重写回调处理函数
  141. public function NotifyProcess($data, &$msg)
  142. {
  143. Log::DEBUG("call back:" . json_encode($data));
  144. $this->data = $data;
  145. $notfiyOutput = array();
  146. if(!array_key_exists("transaction_id", $data)){
  147. $msg = "输入参数不正确";
  148. return false;
  149. }
  150. //查询订单,判断订单真实性
  151. if(!$this->Queryorder($data["transaction_id"])){
  152. $msg = "订单查询失败";
  153. return false;
  154. }
  155. return true;
  156. }
  157. }
  158. ?>