예제 #1
0
 public function test2()
 {
     $openInfo = WxSDK::getOpenInfo('snsapi_userinfo', WX_APP_ID, WX_APP_SECRET);
     var_dump($openInfo);
     echo '<br/>';
     echo '<br/>';
     var_dump(WxSDK::getUserInfo($openInfo['openid'], 'snsapi_userinfo', $openInfo['access_token']));
 }
예제 #2
0
 public function wxUnified()
 {
     if ($_SERVER['REQUEST_METHOD'] != 'POST') {
         exit;
     }
     $data = file_get_contents('php://input');
     libxml_disable_entity_loader(true);
     $data = simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA);
     $data = json_encode($data);
     Log::pay('wexin unified pay notify raw data: ' . $data);
     $data = json_decode($data, true);
     $sign = WxSDK::sign($data, WX_PAY_KEY);
     if ($data['sign'] != $sign) {
         Log::pay('wexin unified pay notify error : sign failed! ' . json_encode($data));
         echo '<xml><return_code>FAIL</return_code><return_msg>sign fail</return_msg></xml>';
         return;
     }
     if ($data['return_code'] != 'SUCCESS') {
         Log::pay('wexin unified pay notify fail : ' . json_encode($data));
         echo '<xml><return_code>FAIL</return_code><return_msg>return fail</return_msg></xml>';
         return;
     }
     if ($data['result_code'] != 'SUCCESS') {
         Log::pay('wexin unified pay notify fail : ' . json_encode($data));
         echo '<xml><return_code>FAIL</return_code><return_msg>result fail</return_msg></xml>';
         return;
     }
     $transactionId = $data['transaction_id'];
     $nk = Nosql::NK_PAY_NOTIFY_DE_DUPLICATION . $data['out_trade_no'];
     $nk = Nosql::get($nk);
     if (!empty($nk)) {
         echo '<xml><return_code>SUCCESS</return_code><return_msg>OK</return_msg></xml>';
         Log::pay('wexin unified pay notify ok(had handled) : ' . json_encode($data));
         return;
     }
     if ($this->onPayNotifyOk($data['out_trade_no'], $data['total_fee'], $data['cash_fee']) === true) {
         // 订单现金支付金额
         Nosql::setex($nk, Nosql::NK_PAY_NOTIFY_DE_DUPLICATION_EXPIRE, 'x');
         echo '<xml><return_code>SUCCESS</return_code><return_msg>OK</return_msg></xml>';
         Log::pay('wexin unified pay notify success : ' . json_encode($data));
         return;
     }
     if ($data['is_subscribe'] == 'N' && $data['trade_type'] == 'JSAPI') {
         $this->onWxPayOkUnSubscribe($data['openid'], $data['out_trade_no']);
     }
     echo '<xml><return_code>FAIL</return_code><return_msg>handle fail</return_msg></xml>';
     Log::pay('wexin unified pay notify fail : ' . json_encode($data));
 }
예제 #3
0
 public function wxJsApiPay($orderId, $orderDesc, $totalAmount)
 {
     $openInfo = WxSDK::getOpenInfo('snsapi_base', WX_PAY_APP_ID, WX_PAY_APP_SECRET);
     if (empty($openInfo['openid'])) {
         $this->ajaxReturn(ERR_SYSTEM_ERROR, '向微信请求支付信息失败,稍重试');
         return;
     }
     $openid = $openInfo['openid'];
     PayModel::onCreateOrderOk($orderId, array('pay_type' => PayModel::PAY_TYPE_WX));
     $jsApiParameters = WxSDK::jsApiPay(WX_PAY_MCHID, WX_PAY_APP_ID, WX_PAY_KEY, $openid, $orderId, $orderDesc, ceil($totalAmount * 100), Util::getIp(), APP_URL_BASE . '/pay/PayNotify/wxUnified');
     if ($jsApiParameters === false) {
         $this->ajaxReturn(ERR_SYSTEM_ERROR, '向微信申请支付失败,稍重试');
         return;
     }
     $this->ajaxReturn(0, '', '', array('jsapiparams' => $jsApiParameters));
 }
예제 #4
0
 private function onSubscribe($openid, $from)
 {
     $wxUserInfo = WxSDK::getUserInfo($openid, 'snsapi_base');
     if (empty($wxUserInfo)) {
         Log::warng('first get wxuinfo:' . $openid . ' userinfo fail from ' . $from);
         $wxUserInfo = WxSDK::getUserInfo($openid, 'snsapi_base');
         if (empty($wxUserInfo)) {
             Log::warng('second get wxuinfo:' . $openid . ' userinfo fail from ' . $from);
             return false;
         }
     }
     $userInfo = WxUserModel::findUserByOpenId($openid);
     if (empty($userInfo)) {
         WxUserModel::newOne($wxUserInfo, $from);
     } else {
         WxUserModel::updateWxUserInfo($userInfo, $wxUserInfo, $from);
     }
 }
예제 #5
0
 private function sendKfMsg($data)
 {
     $ret = true;
     if ($data['msgtype'] == 'text') {
         $ret = WxSDK::sendKfTextMsg($data['openid'], $data['content']);
     } else {
         if ($data['msgtype'] == 'image') {
             $ret = WxSDK::sendKfImageMsg($data['openid'], $data['content']);
         } else {
             if ($data['msgtype'] == 'news') {
                 $news = $data['content'];
                 $ret = WxSDK::sendKfNewsMsg($data['openid'], $news);
             }
         }
     }
     return $ret;
 }
예제 #6
0
 private function toWxLogin()
 {
     $openInfo = WxSDK::getOpenInfo('snsapi_base', WX_APP_ID, WX_APP_SECRET);
     if (empty($openInfo['openid'])) {
         // TODO 这里要显示的告诉用户
         // header('Location: /TODO');
         // exit(0);
         return false;
     }
     $wxUserInfo = WxSDK::getUserInfo($openInfo['openid'], 'snsapi_base');
     if (empty($wxUserInfo)) {
         //
         Log::warng('first get wxuinfo:' . $openInfo['openid'] . ' fail when autologin');
         $wxUserInfo = WxSDK::getUserInfo($openInfo['openid'], 'snsapi_base');
         if (empty($wxUserInfo)) {
             //
             Log::warng('second get wxuinfo:' . $openInfo['openid'] . ' fail when autologin');
             // TODO 这里要显示的告诉用户
             // header('Location: /TODO');
             // exit(0);
             return false;
         }
     }
     $wxDBUserInfo = WxUserModel::findUserByOpenId($openInfo['openid']);
     if (empty($wxDBUserInfo)) {
         // new one
         $from = WxUserModel::SUBSCRIBE_FROM_ALREADY;
         if ((int) $wxUserInfo['subscribe'] == 0) {
             $from = WxUserModel::SUBSCRIBE_FROM_UNSUBSCRIBE;
         }
         $ret = WxUserModel::newOne($wxUserInfo, $from);
         if ($ret === false) {
             // TODO 这里要显示的告诉用户
             // header('Location: /TODO');
             // exit(0);
             return false;
         }
     }
     return $openInfo['openid'];
 }
예제 #7
0
    public function wxJsApiPay()
    {
        $openInfo = WxSDK::getOpenInfo('snsapi_base', WX_PAY_APP_ID, WX_PAY_APP_SECRET);
        if (empty($openInfo['openid'])) {
            echo '向微信请求支付失败,稍重试';
            return;
        }
        $openid = $openInfo['openid'];
        $orderId = date('Ymd') . mt_rand(1000000000, 1999999999);
        PayModel::onCreateOrderOk($orderId, array('pay_type' => PayModel::PAY_TYPE_WX));
        $jsApiParameters = WxSDK::jsApiPay(WX_PAY_MCHID, WX_PAY_APP_ID, WX_PAY_KEY, $openid, $orderId, '测试1分钱商品', 1, Util::getIp(), APP_URL_BASE . '/pay/PayNotify/wxUnified');
        if ($jsApiParameters === false) {
            echo '获取js参数失败';
            exit;
        }
        echo <<<HTML
            <html>
            <head>
            <meta http-equiv="content-type" content="text/html;charset=utf-8"/>
            <meta name="viewport" content="width=device-width, initial-scale=1"/> 
            <title>微信支付样例-支付</title>
            <script type="text/javascript">
            //调用微信JS api 支付
            function jsApiCall()
            {
                WeixinJSBridge.invoke(
                    'getBrandWCPayRequest',
                    {$jsApiParameters},
                    function(res){
                        WeixinJSBridge.log(res.err_msg);
                        if (res.err_msg == "get_brand_wcpay_request:ok") {
                            setTimeout(function() {
                                location.href = "http://host/pay/OrderPay/wxPayReturn";
                                }, 500);
                        } else {
                            if (res.err_msg != "get_brand_wcpay_request:cancel") {
                                alert(res.err_code+res.err_desc+res.err_msg);
                            }
                        }
                     }
                );
            }

        function callpay()
        {
            if (typeof WeixinJSBridge == "undefined"){
                if( document.addEventListener ){
                    document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
                }else if (document.attachEvent){
                    document.attachEvent('WeixinJSBridgeReady', jsApiCall); 
                    document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
                }
            }else{
                jsApiCall();
            }
        }
        </script>
            </head>
            <body>
            <br/>
            <font color="#9ACD32"><b>该笔订单支付金额为<span style="color:#f00;font-size:50px">1分</span>钱</b></font><br/><br/>
            <div align="center">
            <button style="width:210px; height:50px; border-radius: 15px;background-color:#FE6714; border:0px #FE6714 solid; cursor: pointer;  color:white;  font-size:16px;" type="button" onclick="callpay()" >立即支付</button>
            </div>
            </body>
            </html>
HTML;
    }
예제 #8
0
 private function toWxLogin()
 {
     $openInfo = WxSDK::getOpenInfo('snsapi_base', WX_APP_ID, WX_APP_SECRET);
     if (empty($openInfo['openid'])) {
         // TODO 这里要显示的告诉用户
         // header('Location: /TODO');
         // exit(0);
         return;
     }
     $wxUserInfo = WxSDK::getUserInfo($openInfo['openid'], 'snsapi_base');
     if (empty($wxUserInfo)) {
         //
         Log::warng('first get wxuinfo:' . $openInfo['openid'] . ' fail when autologin');
         $wxUserInfo = WxSDK::getUserInfo($openInfo['openid'], 'snsapi_base');
         if (empty($wxUserInfo)) {
             //
             Log::warng('second get wxuinfo:' . $openInfo['openid'] . ' fail when autologin');
             // TODO 这里要显示的告诉用户
             // header('Location: /TODO');
             // exit(0);
             return;
         }
     }
     $ret = WxUserModel::findUserByOpenId($openInfo['openid']);
     if (!empty($ret)) {
         $this->doLoginInWx($openInfo['openid']);
         return;
     } else {
         // create one
         $from = WxUserModel::SUBSCRIBE_FROM_ALREADY;
         if ((int) $wxUserInfo['subscribe'] == 0) {
             $from = WxUserModel::SUBSCRIBE_FROM_UNSUBSCRIBE;
         }
         $ret = WxUserModel::newOne($wxUserInfo, $from);
         if ($ret === false) {
             // TODO 这里要显示的告诉用户
             // header('Location: /TODO');
             // exit(0);
             return;
         }
         $ret = $this->doLoginInWx($openInfo['openid']);
         if ($ret === false) {
             Log::error("create wx user fail! " . json_encode($wxUserInfo, JSON_UNESCAPED_UNICODE));
             // TODO 这里要显示的告诉用户
             // header('Location: /TODO');
             // exit(0);
         }
     }
 }