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.
 
 
 
 
 

133 lines
4.1 KiB

  1. <?php
  2. ini_set('date.timezone','Asia/Shanghai');
  3. //error_reporting(E_ERROR);
  4. require_once "../lib/WxPay.Api.php";
  5. require_once "WxPay.JsApiPay.php";
  6. require_once 'log.php';
  7. require_once '../../Common/Function.php';
  8. //初始化日志
  9. $logHandler= new CLogFileHandler("../logs/".date('Y-m-d').'.log');
  10. $log = Log::Init($logHandler, 15);
  11. //打印输出数组信息
  12. function printf_info($data)
  13. {
  14. foreach($data as $key=>$value){
  15. echo "<font color='#00ff55;'>$key</font> : $value <br/>";
  16. }
  17. }
  18. //①、获取用户openid
  19. $tools = new JsApiPay();
  20. $openId = $tools->GetOpenid();
  21. //var_dump($_GET['orderid']);exit();
  22. if (!empty($_GET['orderid']) && $_GET['orderid'] !=='false'){
  23. $orderno=$_GET['orderid'];
  24. }else{
  25. echo "<script>alert('订单号有误')</script>";
  26. exit;
  27. }
  28. $url="http://xmwxc.zhizhuchuxing.cn/core/order_detail.asp?orderno=".$orderno;
  29. $result=http_request($url);
  30. wlog($openId."--".$orderno."--".$url."--".$result);
  31. $orderDetail=json_decode($result,true);
  32. //$money=$orderDetail['price']*100;
  33. $money=1;
  34. $name=$orderDetail['prod_name'];
  35. $order_status=$orderDetail['order_status'];
  36. if ($order_status !=='待支付'){
  37. //echo "<script>alert('不是待支付订单')</script>";
  38. //exit;
  39. }
  40. //$openId='oCVcbv1udkL4iZm-t1h8YMrMDSyU';
  41. //②、统一下单
  42. $input = new WxPayUnifiedOrder();
  43. $input->SetBody($name.": ".$orderno);
  44. //$input->SetOut_trade_no(WxPayConfig::MCHID.date("YmdHis"));
  45. $input->SetOut_trade_no($orderno);
  46. $input->SetTotal_fee($money);
  47. $input->SetTime_start(date("YmdHis"));
  48. $input->SetTime_expire(date("YmdHis", time() + 600));
  49. $input->SetGoods_tag("代金券");
  50. $reurl=dirname("http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'])."/success_notify.php";
  51. wlog($reurl);
  52. $input->SetNotify_url($reurl);
  53. $input->SetTrade_type("JSAPI");
  54. $input->SetOpenid($openId);
  55. $order = WxPayApi::unifiedOrder($input);
  56. //echo '<font color="#f00"><b>统一下单支付单信息qwwww</b></font><br/>';
  57. //printf_info($order);
  58. $jsApiParameters = $tools->GetJsApiParameters($order);
  59. //print_r($jsApiParameters);
  60. //获取共享收货地址js函数参数
  61. $editAddress = $tools->GetEditAddressParameters();
  62. //③、在支持成功回调通知中处理成功之后的事宜,见 notify.php
  63. /**
  64. * 注意:
  65. * 1、当你的回调地址不可访问的时候,回调通知会失败,可以通过查询订单来确认支付是否成功
  66. * 2、jsapi支付时需要填入用户openid,WxPay.JsApiPay.php中有获取openid流程 (文档可以参考微信公众平台“网页授权接口”,
  67. * 参考http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html)
  68. */
  69. function wlog($result){
  70. file_put_contents('./order_detail.txt', date("Y-m-d H:i:s")." ".$result.PHP_EOL,FILE_APPEND);//订单详细日志
  71. }
  72. ?>
  73. <script type="text/javascript">
  74. window.onload = function(){
  75. if (typeof WeixinJSBridge == "undefined"){
  76. if( document.addEventListener ){
  77. document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
  78. }else if (document.attachEvent){
  79. document.attachEvent('WeixinJSBridgeReady', jsApiCall);
  80. document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
  81. }
  82. }else{
  83. jsApiCall();
  84. }
  85. };
  86. //调用微信JS api 支付
  87. function jsApiCall()
  88. {
  89. WeixinJSBridge.invoke(
  90. 'getBrandWCPayRequest',
  91. <?php echo $jsApiParameters; ?>,
  92. function(res){
  93. WeixinJSBridge.log(res.err_msg);
  94. if(res.err_msg =='get_brand_wcpay_request:ok'){
  95. var orderno=<?php echo $orderno ?>;
  96. alert('支付成功');
  97. location.href='http://xmwxc.zhizhuchuxing.cn/core/order_confirm1.asp?orderno='+orderno;
  98. }else if(res.err_msg =='get_brand_wcpay_request:cancel'){
  99. alert('取消支付');
  100. window.history.go(-1);
  101. }else{
  102. alert('支付失败');
  103. window.history.go(-1);
  104. }
  105. }
  106. );
  107. }
  108. function callpay()
  109. {
  110. if (typeof WeixinJSBridge == "undefined"){
  111. if( document.addEventListener ){
  112. document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
  113. }else if (document.attachEvent){
  114. document.attachEvent('WeixinJSBridgeReady', jsApiCall);
  115. document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
  116. }
  117. }else{
  118. jsApiCall();
  119. }
  120. }
  121. </script>