class WeiXinPay{
  
  
  protected $APPID = appid;
  protected $APPSECRET = secret;
  
  protected $MCHID = '11111111';
  
  protected $KEY = '192006250b4c09247ec02edce69f6a2d';
  
  protected $APPURL =   'https:
  
  protected $TRADETYPE = 'JSAPI';
  
  protected $BODY = 'wx/book';
  
  function __construct($openid,$outTradeNo,$totalFee){
    $this->openid = $openid; 
    $this->outTradeNo = $outTradeNo; 
    $this->totalFee = $totalFee; 
  }
  
  public function pay(){
    $result = $this->weixinapp();
    return $result;
  }
   
   private function weixinapp(){
     $unifiedorder=$this->unifiedorder();
     $parameters=array(
     'appId'=>$this->APPID,
     'timeStamp'=>''.time().'',
     'nonceStr'=>$this->createNoncestr(),
     'package'=>'prepay_id='.$unifiedorder['prepay_id'],
     'signType'=>'MD5'
       );
     $parameters['paySign']=$this->getSign($parameters);
     return $parameters;
   }
  
   
   
  private function unifiedorder(){
    $parameters = array(
      'appid' => $this->APPID,
      'mch_id'=> $this->MCHID,
      'spbill_create_ip'=>$_SERVER['REMOTE_ADDR'],
      'notify_url'=>$this->APPURL, 
      'nonce_str'=> $this->createNoncestr(),
      'out_trade_no'=>$this->outTradeNo,
      'total_fee'=>floatval($this->totalFee), 
      'open_id'=>$this->openid,
      'trade_type'=>$this->TRADETYPE,
      'body' =>$this->BODY, 
    );
    $parameters['sign'] = $this->getSign($parameters);
    $xmlData = $this->arrayToXml($parameters);
    $xml_result = $this->postXmlCurl($xmlData,'https:
    $result = $this->xmlToArray($xml_result);
    return $result;
  }
  
  protected 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;
  }
  protected function xmlToArray($xml){
    $array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
    return $array_data;
  }
  
  private static function postXmlCurl($xml, $url, $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);