123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- <?php
- require_once "WxPay.Api.php";
- class JsApiPay
- {
-
- public $data = null;
- public function __construct($config)
- {
- $this->config = $config;
- }
-
-
- public function GetOpenid()
- {
-
- if (!isset($_GET['code'])){
-
- $baseUrl = urlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].$_SERVER['QUERY_STRING']);
- $url = $this->_CreateOauthUrlForCode($baseUrl);
- Header("Location: $url");
- exit();
- } else {
-
- $code = $_GET['code'];
- $openid = $this->getOpenidFromMp($code);
- return $openid;
- }
- }
-
-
- public function GetJsApiParameters($UnifiedOrderResult)
- {
- if(!array_key_exists("appid", $UnifiedOrderResult)
- || !array_key_exists("prepay_id", $UnifiedOrderResult)
- || $UnifiedOrderResult['prepay_id'] == "")
- {
- throw new WxPayException("参数错误");
- }
- $jsapi = new WxPayJsApiPay();
- $jsapi->SetAppid($UnifiedOrderResult["appid"]);
- $timeStamp = time();
- $jsapi->SetTimeStamp("$timeStamp");
- $jsapi->SetNonceStr(WxPayApi::getNonceStr());
- $jsapi->SetPackage("prepay_id=" . $UnifiedOrderResult['prepay_id']);
- $jsapi->SetPaySign($jsapi->MakeSign($this->config));
- $parameters = json_encode($jsapi->GetValues());
- return $parameters;
- }
-
-
- public function GetOpenidFromMp($code)
- {
- $url = $this->__CreateOauthUrlForOpenid($code);
-
- $ch = curl_init();
- $curlVersion = curl_version();
- $ua = "WXPaySDK/3.0.9 (".PHP_OS.") PHP/".PHP_VERSION." CURL/".$curlVersion['version']." "
- .$this->config->GetMerchantId();
-
- curl_setopt($ch, CURLOPT_TIMEOUT, $this->curl_timeout);
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,FALSE);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,FALSE);
- curl_setopt($ch, CURLOPT_USERAGENT, $ua);
- curl_setopt($ch, CURLOPT_HEADER, FALSE);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
- $proxyHost = "0.0.0.0";
- $proxyPort = 0;
- $this->config->GetProxy($proxyHost, $proxyPort);
- if($proxyHost != "0.0.0.0" && $proxyPort != 0){
- curl_setopt($ch,CURLOPT_PROXY, $proxyHost);
- curl_setopt($ch,CURLOPT_PROXYPORT, $proxyPort);
- }
-
- $res = curl_exec($ch);
- curl_close($ch);
-
- $data = json_decode($res,true);
- $this->data = $data;
- $openid = $data['openid'];
- return $openid;
- }
-
-
- private function ToUrlParams($urlObj)
- {
- $buff = "";
- foreach ($urlObj as $k => $v)
- {
- if($k != "sign"){
- $buff .= $k . "=" . $v . "&";
- }
- }
-
- $buff = trim($buff, "&");
- return $buff;
- }
-
-
- public function GetEditAddressParameters()
- {
- $getData = $this->data;
- $data = array();
- $data["appid"] = $this->config->GetAppId();
- $data["url"] = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
- $time = time();
- $data["timestamp"] = "$time";
- $data["noncestr"] = WxPayApi::getNonceStr();
- $data["accesstoken"] = $getData["access_token"];
- ksort($data);
- $params = $this->ToUrlParams($data);
- $addrSign = sha1($params);
-
- $afterData = array(
- "addrSign" => $addrSign,
- "signType" => "sha1",
- "scope" => "jsapi_address",
- "appId" => $this->config->GetAppId(),
- "timeStamp" => $data["timestamp"],
- "nonceStr" => $data["noncestr"]
- );
- $parameters = json_encode($afterData);
- return $parameters;
- }
-
-
- private function _CreateOauthUrlForCode($redirectUrl)
- {
- $urlObj["appid"] = $this->config->GetAppId();
- $urlObj["redirect_uri"] = "$redirectUrl";
- $urlObj["response_type"] = "code";
- $urlObj["scope"] = "snsapi_base";
- $urlObj["state"] = "STATE"."#wechat_redirect";
- $bizString = $this->ToUrlParams($urlObj);
- return "https://open.weixin.qq.com/connect/oauth2/authorize?".$bizString;
- }
-
-
- private function __CreateOauthUrlForOpenid($code)
- {
- $urlObj["appid"] = $this->config->GetAppId();
- $urlObj["secret"] = $this->config->GetAppSecret();
- $urlObj["code"] = $code;
- $urlObj["grant_type"] = "authorization_code";
- $bizString = $this->ToUrlParams($urlObj);
- return "https://api.weixin.qq.com/sns/oauth2/access_token?".$bizString;
- }
- }
|