jsapi.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. //初始化日志
  8. $logHandler= new CLogFileHandler("../logs/".date('Y-m-d').'.log');
  9. $log = Log::Init($logHandler, 15);
  10. //打印输出数组信息
  11. function printf_info($data)
  12. {
  13. foreach($data as $key=>$value){
  14. echo "<font color='#00ff55;'>$key</font> : $value <br/>";
  15. }
  16. }
  17. $uid=$_REQUEST['uid'];
  18. $money=$_REQUEST['money'];
  19. $orderid=$_REQUEST['orderid'];
  20. $fee=$money*100;
  21. $money=number_format($money,2,'.','');
  22. //①、获取用户openid
  23. $tools = new JsApiPay();
  24. $openId = $tools->GetOpenid();
  25. //②、统一下单
  26. $input = new WxPayUnifiedOrder();
  27. $input->SetBody("账户充值");
  28. $input->SetAttach("test");
  29. //$input->SetOut_trade_no(WxPayConfig::MCHID.date("YmdHis"));
  30. $input->SetOut_trade_no($orderid);
  31. $input->SetTotal_fee($fee);
  32. $input->SetTime_start(date("YmdHis"));
  33. $input->SetTime_expire(date("YmdHis", time() + 600));
  34. $input->SetGoods_tag($uid);
  35. $input->SetNotify_url("http://".$_SERVER['HTTP_HOST']."/wxpay/pay/notify_jsapi.php");
  36. $input->SetTrade_type("JSAPI");
  37. $input->SetOpenid($openId);
  38. $order = WxPayApi::unifiedOrder($input);
  39. //echo '<font color="#f00"><b>统一下单支付单信息</b></font><br/>';
  40. //printf_info($order);
  41. $jsApiParameters = $tools->GetJsApiParameters($order);
  42. //③、在支持成功回调通知中处理成功之后的事宜,见 notify.php
  43. /**
  44. * 注意:
  45. * 1、当你的回调地址不可访问的时候,回调通知会失败,可以通过查询订单来确认支付是否成功
  46. * 2、jsapi支付时需要填入用户openid,WxPay.JsApiPay.php中有获取openid流程 (文档可以参考微信公众平台“网页授权接口”,
  47. * 参考http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html)
  48. */
  49. ?>
  50. <html>
  51. <head>
  52. <meta http-equiv="content-type" content="text/html;charset=utf-8"/>
  53. <meta name="viewport" content="width=device-width, initial-scale=1"/>
  54. <title>微信支付</title>
  55. <script type="text/javascript">
  56. //调用微信JS api 支付
  57. function jsApiCall()
  58. {
  59. WeixinJSBridge.invoke(
  60. 'getBrandWCPayRequest',
  61. <?php echo $jsApiParameters; ?>,
  62. function(res){
  63. WeixinJSBridge.log(res.err_msg);
  64. alert(res.err_code+res.err_desc+res.err_msg);
  65. location.history.back();
  66. }
  67. );
  68. }
  69. function callpay()
  70. {
  71. if (typeof WeixinJSBridge == "undefined"){
  72. if( document.addEventListener ){
  73. document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
  74. }else if (document.attachEvent){
  75. document.attachEvent('WeixinJSBridgeReady', jsApiCall);
  76. document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
  77. }
  78. }else{
  79. jsApiCall();
  80. }
  81. }
  82. </script>
  83. </head>
  84. <body>
  85. <br/>
  86. <div style="color: #000;text-align: center;font-weight: bold;">
  87. 虚拟充值<br/><br/>
  88. </div>
  89. <div style="color: #000;text-align: center;font-weight: bold;line-height:50px;font-size:40px;">
  90. ¥ <?php echo $money;?>
  91. </div>
  92. <div align="center">
  93. <button style="width:210px; height:50px; border-radius: 5px;background-color:#1aad19; border:0px #179e16 solid; cursor: pointer; color:white; font-size:16px;" type="button" onclick="callpay()" >立即支付</button>
  94. </div>
  95. </body>
  96. </html>