wxpay_jsapi.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * 微信支付接口类
  4. * JSAPI 适用于微信内置浏览器访问WAP时支付
  5. */
  6. class wxpay_jsapi {
  7. public function __construct($payment_info = array()) {
  8. define('WXN_APPID', $payment_info['payment_config']['wx_appid']);
  9. define('WXN_APPSECRET', $payment_info['payment_config']['wx_appsecret']);
  10. define('WXN_MCHID', $payment_info['payment_config']['wx_mch_id']);
  11. define('WXN_KEY', $payment_info['payment_config']['wx_key']);
  12. }
  13. public function get_payform($order_info) {
  14. //引入PC端微信公共类
  15. require_once PLUGINS_PATH . '/payments/wxpay_native/lib/WxPay.Api.php';
  16. require_once PLUGINS_PATH . '/payments/wxpay_native/WxPay.JsApiPay.php';
  17. //获取用户openid
  18. $tools = new JsApiPay();
  19. $openId = $tools->GetOpenid();
  20. //统一下单
  21. $input = new WxPayUnifiedOrder();
  22. $input->SetBody(config('ds_config.site_name') . $order_info['pay_sn'] . '订单');
  23. $input->SetAttach($order_info['order_type']);
  24. $input->SetOut_trade_no($order_info['pay_sn'].'_'.TIMESTAMP);//31个字符,微信限制为32字符以内 TIMESTAMP 用来防止做随机数,用户支付订单后取消,已产生的订单不能重复支付
  25. $input->SetTotal_fee(bcmul($order_info['api_pay_amount'] , 100,0));
  26. $input->SetTime_start(date("YmdHis"));
  27. $input->SetTime_expire(date("YmdHis", TIMESTAMP + 600));
  28. $input->SetGoods_tag("");
  29. $input->SetNotify_url(str_replace('/index.php', '', HOME_SITE_URL) . '/payment/wxpay_jsapi_notify.html');
  30. $input->SetTrade_type("JSAPI");
  31. $input->SetOpenid($openId);
  32. $order = WxPayApi::unifiedOrder($input);
  33. if($order['return_code']=='FAIL'){
  34. halt($order);
  35. }
  36. $jsApiParameters = $tools->GetJsApiParameters($order);
  37. //不同订单支付成功对应的跳转界面
  38. if($order_info['order_type'] == 'real_order'){
  39. $url = config('ds_config.h5_site_url').'/pages/member/order/OrderList';
  40. }elseif ($order_info['order_type'] == 'vr_order') {
  41. $url = config('ds_config.h5_site_url').'/pages/member/vrorder/OrderList';
  42. } elseif ($order_info['order_type'] == 'pd_order') {
  43. $url = config('ds_config.h5_site_url').'/pages/member/recharge/RechargeList';
  44. } elseif ($order_info['order_type'] == 'sj_order') {
  45. $url = config('ds_config.h5_site_url').'/pages/member/index/Index';
  46. }
  47. $str = <<<EOT
  48. <!DOCTYPE html>
  49. <html>
  50. <head>
  51. <meta http-equiv="Content-type" content="text/html;charset=utf-8" />
  52. <title>微信安全支付</title>
  53. </head>
  54. <body>
  55. 正在加载…
  56. <script type="text/javascript">
  57. function jsApiCall()
  58. {
  59. WeixinJSBridge.invoke(
  60. 'getBrandWCPayRequest',
  61. $jsApiParameters,
  62. function(res){
  63. if (res.err_msg == 'get_brand_wcpay_request:ok') {
  64. //alert(lang.WeChat_pays_off);
  65. self.location = "$url";
  66. }else if (res.err_msg == 'get_brand_wcpay_request:cancel') {
  67. //alert(lang.cancel_WeChat_payment);
  68. self.location = "$url";
  69. } else {
  70. //alert(lang.WeChat_payments_fail);
  71. self.location = "$url";
  72. }
  73. //WeixinJSBridge.log(res.err_msg);
  74. //alert(res.err_code+res.err_desc+res.err_msg);
  75. }
  76. );
  77. }
  78. window.onload = function() {
  79. if (typeof WeixinJSBridge == "undefined"){
  80. if( document.addEventListener ){
  81. document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
  82. }else if (document.attachEvent){
  83. document.attachEvent('WeixinJSBridgeReady', jsApiCall);
  84. document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
  85. }
  86. }else{
  87. jsApiCall();
  88. }
  89. }
  90. </script>
  91. </body>
  92. </html>
  93. EOT;
  94. echo $str;
  95. exit;
  96. }
  97. }