buy_jsapi.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. include("../../../config/conn.php");
  3. include("../../../config/function.php");
  4. sesCheck_m();
  5. while0("*","yjcode_dingdang where wxddbh='".$_SESSION[wxddbh]."'");$row=mysql_fetch_array($res);$moneyv=$row['money1']*100;
  6. $lib_path = dirname(__FILE__)."/";
  7. require_once $lib_path."WxPay.Config.php";
  8. require_once $lib_path."WxPay.Api.php";
  9. require_once $lib_path."WxPay.Notify.php";
  10. require_once $lib_path."WxPay.JsApiPay.php";
  11. require_once $lib_path."log.php";
  12. require_once "wxconfig.php";
  13. WxPayConfig::set_appid( $payment['wxpay_jspay_appid'] );
  14. WxPayConfig::set_appsecret( $payment['wxpay_jspay_appsecret']);
  15. WxPayConfig::set_mchid( $payment['wxpay_jspay_mchid'] );
  16. WxPayConfig::set_key( $payment['wxpay_jspay_key'] );
  17. if ( empty($_SESSION['wxpay_jspay_openid']) ){
  18. if( empty($payment) ){
  19. return false;
  20. }
  21. $tools = new JsApiPay();
  22. $openid = $tools->GetOpenid();
  23. $_SESSION['wxpay_jspay_openid'] = $openid;
  24. }
  25. $html = '<div style="text-align:center"><button class="btn btn-primary c-btn3" type="button" onclick="javascript:alert(\'请在微信客户端打开链接\')">微信安全支付</button></div>';
  26. // 网页授权获取用户openid
  27. if (! isset($_SESSION['wxpay_jspay_openid']) || empty($_SESSION['wxpay_jspay_openid'])) {
  28. echo $html;
  29. exit();
  30. }
  31. $openId = $_SESSION['wxpay_jspay_openid'];
  32. $root_url = weburl;
  33. //$root_url = str_replace('mobile/', '', $root_url);
  34. $notify_url = $root_url.'m/user/wxpay1/carpay_notify.php';
  35. $out_trade_no = $_SESSION['wxddbh'];
  36. //var_dump($notify_url);
  37. //iconv("GB2312","UTF-8//IGNORE",webname."商品结算")
  38. //统一下单
  39. $tools = new JsApiPay();
  40. $input = new WxPayUnifiedOrder();
  41. $input->SetBody( $out_trade_no );
  42. $input->SetAttach( $out_trade_no ); //商户支付日志
  43. $input->SetOut_trade_no( $out_trade_no ); //商户订单号
  44. $input->SetTotal_fee( $moneyv ); //总金额
  45. $input->SetTime_start(date("YmdHis"));
  46. //$input->SetTime_expire(date("YmdHis", time() + 600));
  47. //$input->SetGoods_tag("test");
  48. $input->SetNotify_url( $notify_url ); //通知地址
  49. $input->SetTrade_type("JSAPI"); //交易类型
  50. $input->SetProduct_id( $out_trade_no );
  51. $input->SetOpenid($openId);
  52. $wxpay_order = WxPayApi::unifiedOrder($input);
  53. if ( $wxpay_order['return_code'] != 'FALL' ){
  54. $jsApiParameters = $tools->GetJsApiParameters($wxpay_order);
  55. $error = '';
  56. if ( strpos($jsApiParameters, 'error:') === 0 ){
  57. $error = str_replace('error:', '', $jsApiParameters);
  58. $jsApiParameters = '{}';
  59. }
  60. }else{
  61. $error = $wxpay_order['return_msg'];
  62. }
  63. echo '<meta name="viewport" content="width=device-width, initial-scale=1"/> ';
  64. $html = '<div style="text-align:center"><button class="btn btn-primary c-btn3" type="button" onclick="javascript:alert(\'请在微信客户端打开链接\')">微信安全支付</button></div>';
  65. if( empty($error) )
  66. {
  67. $js = '<script type="text/javascript">
  68. function jsApiCall()
  69. {
  70. WeixinJSBridge.invoke(
  71. "getBrandWCPayRequest",
  72. '.$jsApiParameters.',
  73. function(res){
  74. //WeixinJSBridge.log(res.err_msg);
  75. if(res.err_msg == "get_brand_wcpay_request:ok"){
  76. //alert(res.err_code+res.err_desc+res.err_msg);
  77. window.location.href = "'. weburl .'m/user/order.php";
  78. //window.location.replace("'. $root_url .'");
  79. }else{
  80. //返回跳转到订单详情页面
  81. alert(支付失败);
  82. window.location.href = "./index.php";
  83. }
  84. }
  85. );
  86. }
  87. function callpay()
  88. {
  89. if (typeof WeixinJSBridge == "undefined"){
  90. if( document.addEventListener ){
  91. document.addEventListener("WeixinJSBridgeReady", jsApiCall, false);
  92. }else if (document.attachEvent){
  93. document.attachEvent("WeixinJSBridgeReady", jsApiCall);
  94. document.attachEvent("onWeixinJSBridgeReady", jsApiCall);
  95. }
  96. }else{
  97. jsApiCall();
  98. }
  99. }
  100. function gouser(){location.href="../";}
  101. callpay()
  102. </script>';
  103. $html = '<div align="center">
  104. <button style="width:210px; height:50px; border-radius: 15px;background-color:#FE6714; border:0px #FE6714 solid; cursor: pointer; color:white; font-size:16px;margin-top:20px;" type="button" onClick="callpay()" >点击支付</button><button style="width:210px; height:50px; border-radius: 15px;background-color:#1E9FFF; border:0px #FE6714 solid; cursor: pointer; color:white; font-size:16px;margin-top:30px;" type="button" onClick="gouser()" >返回会员中心</button>
  105. </div>'.$js;
  106. }else{
  107. $html = '<div style="text-align:center"><button class="btn btn-primary c-btn3" type="button" onclick="javascript:alert(\''. $error .'\')">微信安全支付</button></div>';
  108. }
  109. echo $html;
  110. exit;
  111. ?>