<?php defined('BASEPATH') OR exit('No direct script access allowed');
class wxjssdk
{
public $CI;
public $appid;
public $appsecret;
public $mchid;
public $key;
public function __construct(){
if (!isset($this->CI)){
$this->CI =& get_instance();
}
$this->appid=$this->CI->config->item("appid");
$this->appsecret=$this->CI->config->item("appsecret");
$this->key=$this->CI->config->item("key");
$this->mchid=$this->CI->config->item("mchid");
}
public function getSignPackage()
{
$jsapiTicket = $this->getJsApiTicket();
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$timestamp = time();
$nonceStr = $this->createNonceStr();
$string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr×tamp=$timestamp&url=$url";
$signature = sha1($string);
$signPackage = array(
"appId" => $this->appid,
"nonceStr" => $nonceStr,
"timestamp" => $timestamp,
"url" => $url,
"signature" => $signature,
"rawString" => $string
);
return $signPackage;
}
public function createNonceStr($length = 16)
{
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$str = "";
for ($i = 0; $i < $length; $i++) {
$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
}
return $str;
}
public function getJsApiTicket()
{
$data = (object)json_decode(file_get_contents("jssdk_jsapi_ticket.json"));
if ($data->expire_time < time()) {
$accessToken = $this->getAccessToken();
$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken";
$res = json_decode($this->httpGet($url));
$ticket = $res->ticket;
if ($ticket) {
$data->expire_time = time() + 7000;
$data->jsapi_ticket = $ticket;
$fp = fopen("jssdk_jsapi_ticket.json", "w");
fwrite($fp, json_encode($data));
fclose($fp);
}
} else {
$ticket = $data->jsapi_ticket;
}
return $ticket;
}
public function getAccessToken()
{
$data = (object)json_decode(file_get_contents("jssdk_access_token.json"));
if ($data->expire_time < time()) {
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appid&secret=$this->appsecret";
$res = json_decode($this->httpGet($url));
$fp = fopen("jssdk_access_token.json", "w");
$access_token = $res->access_token;
if ($access_token) {
$data->expire_time = time() + 7000;
$data->access_token = $access_token;
$fp = fopen("jssdk_access_token.json", "w");
fwrite($fp, json_encode($data));
fclose($fp);
}
} else {
$access_token = $data->access_token;
}
return $access_token;
}
public function httpGet($url)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 500);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_URL, $url);
$res = curl_exec($curl);
curl_close($curl);
return $res;
}
public function unifiedOrder($total_fee, $timeOut = 6)
{
$url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
$appid=$this->appid;
$attach="test" $body="礼物赠送" $goods_tag="test";
$mch_id=$this->mchid $nonce_str=$this->createNonceStr() $notify_url="http://paysdk.weixin.qq.com/example/notify.php";
$open_id=$this->CI->session->userdata("user_open_id") $out_trade_no=$this->mchid.date("YmdHis") $spbill_create_ip="118.244.206.21" $time_expire=date("YmdHis", time() + 600) $time_start=date("YmdHis") $total_fee=$total_fee*100 $trade_type="JSAPI";
$key=$this->key;
$string="appid=".$appid.
"&attach=".$attach.
"&body=".$body.
"&goods_tag=".$goods_tag.
"&mch_id=".$mch_id.
"&nonce_str=".$nonce_str.
"¬ify_url=".$notify_url.
"&openid=".$open_id.
"&out_trade_no=".$out_trade_no.
"&spbill_create_ip=".$spbill_create_ip.
"&time_expire=".$time_expire.
"&time_start=".$time_start.
"&total_fee=".$total_fee.
"&trade_type=JSAPI".
"&key=".$key;
$string = md5($string);
$signature = strtoupper($string); $xml="<xml>
<appid>$appid</appid>
<attach>$attach</attach>
<body>$body</body>
<goods_tag>$goods_tag</goods_tag>
<mch_id>$mch_id</mch_id>
<nonce_str>$nonce_str</nonce_str>
<notify_url>$notify_url</notify_url>
<openid>$open_id</openid>
<out_trade_no>$out_trade_no</out_trade_no>
<spbill_create_ip>$spbill_create_ip</spbill_create_ip>
<time_expire>$time_expire</time_expire>
<time_start>$time_start</time_start>
<total_fee>$total_fee</total_fee>
<trade_type>JSAPI</trade_type>
<sign>$signature</sign>
</xml>";
$startTimeStamp = $this->getMillisecond() $response = $this->postXmlCurl($xml, $url, false, $timeOut);
$xml_arr=simplexml_load_string($response, 'SimpleXMLElement', LIBXML_NOCDATA);
$xml_arr->timeStamp=time();
$xml_arr->nonceStr=$nonce_str;
$string="appId=".$appid.
"&nonceStr=".$xml_arr->nonceStr.
"&package=prepay_id=".$xml_arr->prepay_id.
"&signType=MD5".
"&timeStamp=".$xml_arr->timeStamp.
"&key=".$key;
$string = md5($string);
$paySign = strtoupper($string); $xml_arr->paySign=$paySign;
$json =json_encode($xml_arr);
return $json;
}
private static function postXmlCurl($xml, $url, $useCert = false, $second = 30)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_TIMEOUT, $second);
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE) curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
if($useCert == true){
curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
curl_setopt($ch,CURLOPT_SSLCERT, "../../cache/cert/apiclient_cert.pem");
curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
curl_setopt($ch,CURLOPT_SSLKEY, "../../cache/cert/apiclient_key.pem");
}
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
$data = curl_exec($ch);
if($data){
curl_close($ch);
return $data;
} else {
$error = curl_errno($ch);
curl_close($ch);
}
}
private static function getMillisecond()
{
$time = explode ( " ", microtime () );
$time = $time[1] . ($time[0] * 1000);
$time2 = explode( ".", $time );
$time = $time2[0];
return $time;
}
public function ToXml($arr)
{
if(!is_array($arr)
|| count($arr) <= 0)
{
die();
}
$xml = "<xml>";
foreach ($arr as $key=>$val)
{
if (is_numeric($val)){
$xml.="<".$key.">".$val."</".$key.">";
}else{
$xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
}
}
$xml.="</xml>";
return $xml;
}
}