PHP Micro-Credit Payment class demo_php instance

Source: Internet
Author: User
Tags cas openid
All in the code, the Code is annotated, welcome to the reference.

<?phpclass wxpayservice{protected $mchid;  protected $appid;  protected $key; Public function __construct ($mchid, $appid, $key) {$this->mchid = $mchid;//payment merchant number Partnerid sent by mail after payment of merchant information $t His->appid = $appid;   Public number AppID through the payment of merchant data after the mail sent $this->key = $key; Https://pay.weixin.qq.com Account Settings-security settings-api Security-api key-set API Key}/** * @param string $openid call the "Web page authorization Get user Information" interface to obtain the user's OP under that public number Enid * @param float $totalFee Total Payment unit * @param string $outTradeNo Unique order number * @param string erName order name * @para M string $notifyUrl payment result notification URL do not have a question mark * https://mp.weixin.qq.com/Payment-Development Configuration-Test Catalog * Test Catalog Http://mp.izhanlue.com/paytest /FINAL slash required (requires precision to level two or level three) * @return String */Public function createjsbizpackage ($openid, $totalFee, $outTradeNo, Ername, $NOTIFYURL, $timestamp) {$config = array (' mch_id ' = = $this->mchid, ' appid ' = + $this->a    Ppid, ' key ' = $this->key,);     $unified = Array (' appid ' = = $config [' AppID '], ' Attach ' + ' pay ',//merchant packet, return as-is ' body ' + erName, ' mch_id ' + $config [' mch_id '], ' Nonce_str ' + self::createnoncestr (), ' notify_url ' + $notifyUrl, ' OpenID ' = $openid,//rad E_TYPE=JSAPI, this parameter must pass ' Out_trade_no ' and $outTradeNo, ' spbill_create_ip ' = ' 127.0.0.1 ', ' Total_fee ' =&gt ;    Intval ($totalFee * 100),//unit converted to ' trade_type ' = ' JSAPI ',);    $unified [' sign '] = Self::getsign ($unified, $config [' key ']);    $RESPONSEXML = Self::curlpost (' Https://api.mch.weixin.qq.com/pay/unifiedorder ', Self::arraytoxml ($unified));     /*
 
   
    SUCCESS 
    
    OK 
    
    
    1220647301 
    
    1LHBROsdmqfXoWQR /nonce_str> 
     ACA7BC8A9164D1FBED06C7DFC13EC839 
     
     SUCCESS  
      wx2015032016590503f1bcd9c30421762652 
      
      
       
      
    
   
  
     */$unifiedOrder = simplexml_load_string ($responseXml, ' simplexmlelement ', libxml_nocdata);    if ($unifiedOrder = = = False) {die (' Parse XML error ');    } if ($unifiedOrder->return_code! = ' SUCCESS ') {die ($unifiedOrder->return_msg);      } if ($unifiedOrder->result_code! = ' SUCCESS ') {die ($unifiedOrder->err_code); /* Noauth merchant does not have this interface right Notenough insufficient balance Orderpaid merchant order has been paid orderclosed order closed Systemerror system error APPID      _not_exist APPID does not exist mchid_not_exist Mchid does not exist Appid_mchid_not_match APPID and mch_id mismatch lack_params missing parameters out_trade_no_used Merchant Order number duplicate Signerror signature error xml_format_error XML format error Require_post_method Please use the POST method post_d Ata_empty Post data is empty NOT_UTF8 encoded format error */}//$unifiedOrder->trade_type transaction type the transaction type submitted by the interface is called, the value is as follows: Jsapi,native, APP//$unifiedOrder->prepay_id Pre-payment session identification generated for use in subsequent interface calls, this value is valid for 2 hours//$unifiedOrder->code_url QR Code link Trad E_type for native is a return, the value of the parameter can be generated two-dimensional codeDisplay the scan code to pay $arr = Array ("AppId" and "= $config [' appId ']," TimeStamp "and $timestamp," Noncestr " Self::createnoncestr (), "package" = "prepay_id=".    $unifiedOrder->prepay_id, "signtype" = ' MD5 ',);    $arr [' paysign '] = self::getsign ($arr, $config [' key ']);  return $arr; The Public Function notify () {$config = array (' mch_id ' = = $this->mchid, ' appid ' = = $this->appid    , ' key ' = $this->key,);    $POSTSTR = $GLOBALS ["Http_raw_post_data"];    Error_log ($POSTSTR, 3, './str.txt '); /* $postStr = '
 
   wx00e5904efec77699 
    
    CMB_CREDIT 
    
    1  
     CNY 
     
     Y  
      1220647301 
      
      a0tZ41phiHm8zfmO 
      
      oU3OCt5O46PumN7IE87WcoYZY9r0  
       550bf2990c51f 
       result_code> SUCCESS  
       SUCCESS 
       
       
        
       
       20150320181606 
       
      
        1 
       
       JSAPI 
       
       
        
       
     
    
   
  
 ';    */$POSTOBJ = simplexml_load_string ($postStr, ' simplexmlelement ', libxml_nocdata);    if ($POSTOBJ = = = False) {die (' Parse XML error ');    } if ($postObj->return_code! = ' SUCCESS ') {die ($postObj->return_msg);    } if ($postObj->result_code! = ' SUCCESS ') {die ($postObj->err_code);    } $arr = (array) $postObj;    unset ($arr [' sign ']); if (Self::getsign ($arr, $config [' key ']) = = $POSTOBJ->sign) {//$mch _id = $POSTOBJ->mch_id;//The merchant number of the payment assignment// $appid = $POSTOBJ->appid; Assigned public account ID//$openid = $POSTOBJ->openid; User's unique identity under merchant AppID//$transaction _id = $POSTOBJ->transaction_id;//Payment order number//$out _trade_no = $POSTOBJ->out_ trade_no;//Merchant Order number//$total _fee = $POSTOBJ->total_fee; Total order amount in cents per//$is _subscribe = $POSTOBJ->is_subscribe; Whether the user is concerned about the public account, Y-concern, N-no concern, only in the public account type payment valid//$attach = $POSTOBJ->attach;//merchant packet, return as-is//$time _end = $POSTOBJ->t ime_end;//Payment completion time Echo '
 
   
    SUCCESS 
    
    OK 
    
   
 ';    return $POSTOBJ;  }}/** * Curl Get * * @param string $url * @param array $options * @return Mixed */public static function    Curlget ($url = ", $options = Array ()) {$ch = Curl_init ($url);    curl_setopt ($ch, Curlopt_returntransfer, 1);    curl_setopt ($ch, Curlopt_timeout, 30);    if (!empty ($options)) {Curl_setopt_array ($ch, $options);    }//https request does not validate the certificate and host curl_setopt ($ch, Curlopt_ssl_verifypeer, false);    curl_setopt ($ch, Curlopt_ssl_verifyhost, false);    $data = curl_exec ($ch);    Curl_close ($ch);  return $data; The public static function curlpost ($url = ", $postData =", $options = Array ()) {if (Is_array ($postData)) {$p    Ostdata = Http_build_query ($postData);    } $ch = Curl_init ();    curl_setopt ($ch, Curlopt_url, $url);    curl_setopt ($ch, Curlopt_returntransfer, 1);    curl_setopt ($ch, Curlopt_post, 1);    curl_setopt ($ch, Curlopt_postfields, $postData); curl_setopt ($ch, Curlopt_timeout, 30);  Set the maximum number of seconds that curl is allowed to execute  if (!empty ($options)) {Curl_setopt_array ($ch, $options);    }//https request does not validate the certificate and host curl_setopt ($ch, Curlopt_ssl_verifypeer, false);    curl_setopt ($ch, Curlopt_ssl_verifyhost, false);    $data = curl_exec ($ch);    Curl_close ($ch);  return $data; } public static function createnoncestr ($length = +) {$chars = ' abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxy    Z0123456789 ';    $str = ";    for ($i = 0; $i < $length; $i + +) {$str. = substr ($chars, Mt_rand (0, strlen ($chars)-1), 1);  } return $STR; public static function Arraytoxml ($arr) {$xml = "
 
  
    "; foreach ($arr as $key = + $val) {if (Is_numeric ($val)) {$xml. = "<". $key. ">". $val.      "!--". $key. "-->"; } else $xml. = "<". $key.    ">
   !--". $key. "-->"; } $xml. = "
  
 ";  return $xml; }/** * For example: * APPID:WXD930EA5D5A258F4F * mch_id:10000100 * device_info:1000 * body:test * nonce_str:i Buaivckdprxkhja * First step: The parameters according to the format of Key=value, and according to the name of the ASCII dictionary order of the following: * stringa= "appid=wxd930ea5d5a258f4f&body=test&   Device_info=1000&mch_i * D=10000100&nonce_str=ibuaivckdprxkhja "; * Step Two: Stitching the payment key: * stringsigntemp= "stringa&key=192006250b4c09247ec02edce69f6a2d" * SIGN=MD5 (STRINGSIGNTEMP). toUpperCase () = "9A0A8659F005D6984697E2CA0A9CF3B7" */public static function Getsign ($params, $key) {Ksort ($params,    sort_string);    $unSignParaString = Self::formatqueryparamap ($params, false); $SIGNSTR = Strtoupper (MD5 ($unSignParaString. "&key=".    $key));  return $signStr;    } protected static function Formatqueryparamap ($paraMap, $urlEncode = False) {$buff = "";    Ksort ($PARAMAP); foreach ($paraMap as $k = + $v) {if (null! = $v && "null"! = $v) {if ($urlEncode) {$v = UrlEncode ($v); } $buff. = $k. "=" . $v.      "&";    }} $reqPar = ';    if (strlen ($buff) > 0) {$reqPar = substr ($buff, 0, strlen ($buff)-1);  } return $reqPar; }}

The above code everyone can understand, where do not understand the place welcome to my message, I will be in the first time to get in touch with you. Thank you for your support of the Scripting House website.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.