wxpay_jspay_pay.php 5.7 KB

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