123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- <?php
- namespace Com;
- class JsSdkPay {
- private $appId;
- private $appSecret;
- public $debug = false;
- public $weObj;
- public $parameters;
-
- public $MCHID = '';
-
- public $KEY = '';
-
-
- public $JS_API_CALL_URL = '';
-
-
- public $SSLCERT_PATH = '/xxx/xxx/xxxx/WxPayPubHelper/cacert/apiclient_cert.pem';
- public $SSLKEY_PATH = '/xxx/xxx/xxxx/WxPayPubHelper/cacert/apiclient_key.pem';
-
-
-
- public $NOTIFY_URL = '';
-
-
- public $CURL_TIMEOUT = 30;
- public $prepay_id;
- public function __construct($options) {
- $this->appId = $options['appid'];
- $this->appSecret = $options['appsecret'];
- $this->weObj = new TPWechat($options);
- }
-
-
- function formatBizQueryParaMap($paraMap, $urlencode)
- {
- $buff = "";
- ksort($paraMap);
- foreach ($paraMap as $k => $v)
- {
- if($urlencode)
- {
- $v = urlencode($v);
- }
-
- $buff .= $k . "=" . $v . "&";
- }
- $reqPar = "";
- if (strlen($buff) > 0)
- {
- $reqPar = substr($buff, 0, strlen($buff)-1);
- }
- return $reqPar;
- }
-
- public function getParameters()
- {
- $jsApiObj["appId"] = $this->appId;
- $timeStamp = time();
- $jsApiObj["timeStamp"] = "$timeStamp";
- $jsParamObj["timestamp"] = $timeStamp;
- $jsParamObj["nonceStr"] = $jsApiObj["nonceStr"] = $this->weObj->generateNonceStr();
- $jsParamObj["package"] = $jsApiObj["package"] = "prepay_id=$this->prepay_id";
- $jsParamObj["signType"] = $jsApiObj["signType"] = "MD5";
- $jsParamObj["paySign"] = $jsApiObj["paySign"] = $this->getSign($jsApiObj);
- $jsParam = json_encode($jsParamObj);
- return $jsParam;
- }
-
- function getPrepayId()
- {
- $result = $this->xmlToArray($this->postXml());
- $prepay_id = $result["prepay_id"];
- return $prepay_id;
- }
-
- public function xmlToArray($xml)
- {
-
- $array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
- return $array_data;
- }
-
- function postXml()
- {
- $xml = $this->createXml();
- return $this->postXmlCurl($xml,"https://api.mch.weixin.qq.com/pay/unifiedorder",$this->CURL_TIMEOUT);
- }
-
- public function postXmlCurl($xml,$url,$second=30)
- {
-
- $ch = curl_init();
-
- curl_setopt($ch,CURLOP_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_HEADER, FALSE);
-
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
-
- curl_setopt($ch, CURLOPT_POST, TRUE);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
-
- $data = curl_exec($ch);
- curl_close($ch);
-
- if($data)
- {
- curl_close($ch);
- return $data;
- }
- else
- {
- $error = curl_errno($ch);
- echo "curl出错,错误码:$error"."<br>";
- echo "<a href='http://curl.haxx.se/libcurl/c/libcurl-errors.html'>错误原因查询</a></br>";
- curl_close($ch);
- return false;
- }
- }
-
- function createXml()
- {
- $this->parameters["appid"] = $this->appId;
- $this->parameters["mch_id"] = $this->MCHID;
- $this->parameters["nonce_str"] = $this->weObj->generateNonceStr();
- $this->parameters["sign"] = $this->getSign($this->parameters);
- return $this->arrayToXml($this->parameters);
- }
-
- function arrayToXml($arr)
- {
- $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;
- }
-
- public function getSign($Obj)
- {
- foreach ($Obj as $k => $v)
- {
- $Parameters[$k] = $v;
- }
-
- ksort($Parameters);
- $String = $this->formatBizQueryParaMap($Parameters, false);
-
-
- $String = $String."&key=".$this->KEY;
-
-
- $String = md5($String);
-
-
- $result_ = strtoupper($String);
-
- return $result_;
- }
-
- }
|