This article mainly introduces the PHP version of the JS-SDK payment interface class usage, combined with instance form analysis of the PHP version of the JS-SDK payment interface class definition and related use skills, the need for friends can refer to the next
This payment class is based on the Official document modification! The main implementation of the generated JS API, native package signature packages and native response XML format data. The comments are labeled with the intent of each method. Because the package signature, slightly complex, this should spend more time to correspond to see and log out of the file to one by one contrast! Of course, as long as you use the tutorial class, set the corresponding parameters can be correctly generated package parameters, etc.
<?phpif (Isset ($ARGC) && $argc >= 1 && $argv [0] = = __file__) {//Initialize pay necessary information $pay = new Wechatpay ( Array (wechatpay::appid = ' Wx99dabzpiuq83985b8 ', wechatpay::appsercer = ' Ac12e7e4abaer63hkoa0cc36a9663fa ', Wechatpay::P artnerkey = ' bae4sfa3562rsfaq23s2045 ', Wechatpay::P artnerid = ' 1268969802 ', Wechatpay::P aysign KEY = ' 9fqsxb3pk4ivocec4ycquy5zecs9leemjf2nn4b4ykooxpwaqdfwmezkt8onlbyawcut ', Wechatpay::signtype = ' SHA1 ', )); Set the package necessary parameters Jsapi native are universal $pay->setparams (Wechatpay::bank_type, "WX"); $pay->setparams (wechatpay::body, "test"); $pay->setparams (wechatpay::P artner, $pay->partnerid); $pay->setparams (Wechatpay::out_trade_no, Commonutil::createnoncestr ()); $pay->setparams (Wechatpay::total_fee, "1"); $pay->setparams (Wechatpay::fee_type, "1"); $pay->setparams (Wechatpay::timestamp, Time ()); $pay->setparams (Wechatpay::notify_url, "http://www.demo.com/notify"); $pay->setparams (WecHATPAY::SPBILL_CREATE_IP, "127.0.0.1"); $pay->setparams (Wechatpay::input_charset, "UTF-8"); Jsapi's signature json print_r ($pay->createjsapipackage ()); Generate native XML Print_r ($pay->createnativepackage ()); Generate native URL Print_r ($pay->createnativeurl ("9701"));}
JS API generated Packages signature Package parameters:
{ "appId": "Wx9998ff5f4dede5b7", "package": "Bank_type=wx&body=test&fee_type=1&input_charset =utf-8¬ify_url=http%3a%2f%2fwww.demo.com%2fnotify&out_trade_no=vf5qsswtu0hc2loh&partner= wx9998ff5f4dede5b7&spbill_create_ip=127.0.0.1×tamp=1409295711&total_fee=1&sign= Fee0167bd0d89a88bf6850590ea889b6 ", " TimeStamp ": 1409295711, " noncestr ":" Vf5qsswtu0hc2loh ", " paysign ":" f816264c750923863c370a1739640244b0c2d39c "," Signtype ":" SHA1 "}
XML format for Native response:
<xml> <appid><![ cdata[wx9998ff5f4dede5b7]]></appid> <Package> <![ cdata[bank_type=wx&body=test&fee_type=1&input_charset=utf-8¬ify_url=http%3a%2f% 2fwww.demo.com%2fnotify&out_trade_no=gdl3what4saldead&partner=wx9998ff5f4dede5b7&spbill_create_ip= 127.0.0.1×tamp=1409296124&total_fee=1&sign=bf949b85570644b939b369fd44b0c4a9]]> </ package> <TimeStamp>1409296124</TimeStamp> <noncestr><![ cdata[gdl3what4saldead]]></noncestr> <RetCode>0</RetCode> <reterrmsg><![ Cdata[ok]]></reterrmsg> <appsignature><![ Cdata[ca4a2467b817a62c38a9801fcf451f51692027bf]]></appsignature> <signmethod><![ Cdata[sha1]]></signmethod></xml>
URL links for native:
weixin://wxpay/bizpayurl?appid=wx9998ff5f4dede5b7&noncestr=vy7cva6mtvrc1bvq&productid=9701&sign= 43508b65b50e1d7e1089be66d55a709469155d73×tamp=1409296323
Either way, we have to use SetParams to set the necessary initialization parameters and commodity prices and status, etc.!
Wechatpay class:
<?phpclass Wechatpay {Const Bank_type = ' Bank_type ', BODY = ' body ', PARTNER = ' PARTNER ', out_trade_no = ' Out_trade_no ', TIMESTAMP = ' TIMESTAMP ', Total_fee = ' Total_fee ', Fee_type = ' Fee_type ', Notify_url = ' Notify_u RL ', spbill_create_ip = ' spbill_create_ip ', input_charset = ' Input_charset ', APPID = ' APPID ', appsercer = ' app Sercer ', Paysignkey = ' Appkey ', Partnerid = ' Partnerid ', Partnerkey = ' Partnerkey ', Signtype = ' signtype '; Public $params = Array (), $partnerid = '; static protected $_instance; Protected $_appid, $_appkey, $_signtype, $_partnerkey, $_appsercer; static public Function getinstance (array $options = Array ()) {if (empty (self::$_instance)) {self::$_instance = n EW self ($options); } return self::$_instance; The Public function __construct (array $options = Array ()) {$this->_appid = $options [Self::appid]; $this->_appkey = $options [self::P aysignkey]; $this->_signtype = $optionS[self::signtype]; $this->_partnerkey = $options [self::P artnerkey]; $this->_appsercer = $options [Self::appsercer]; $this->partnerid = $options [Self::appid]; Public Function SetParams ($param, $paramValue) {$this->params[commonutil::trimstring ($param)] = Commonutil::trim String ($paramValue); Public Function Getparams ($param) {return $this->params[$param]; } protected function Createnoncestr ($length = +) {$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01 23456789 "; $str = ""; for ($i = 0; $i < $length; $i + +) {$str. = substr ($chars, Mt_rand (0, strlen ($chars)-1), 1); } return $STR; } public Function Checkparams () {//Required 9 parameters to participate in the signature if ($this->params[self::bank_type] = = NULL | | $this->params[se Lf::body] = = NULL | | $this->params[self::P Artner] = = NULL | | $this->params[self::out_trade_no] = = NULL | | $this->params[self::total_fee] = = NULL | | $this->params[self::fee_type] = = NULL | | $this->params[self::notify_url] = = NULL | | $this->PARAMS[SELF::SPBILL_CREATE_IP] = = NULL | | $this->params[self::input_charset] = = null) {return false; } return true; }/* * Build Package Packages * @params Initialize class using the SetParams method to define the necessary 9 parameters * sort after format URL query form and then Md5signutil class signature, then the URL */protected F Unction getpackagesign () {try {if (null = = $this->_partnerkey | | "= = $this->_partnerkey) {throw new Exception (" The key cannot be empty! " . " <br> "); } $commonUtil = new Commonutil (); Ksort ($this->params); $unSignParaString = $commonUtil->formaturlquery ($this->params, false); $paraString = $commonUtil->formaturlquery ($this->params, true); $MD 5SignUtil = new Md5signutil (); Return $paraString. "&sign=". $MD 5signutil->sign ($unSignParaString, commonutil::trimstring ($this->_partnerkey)); } catch (Exception $e) {echo ($e->getmessage ()); }/* * Generate Signature method * @params AppID Appkey Package TimestThe amp noncestr parameters and the native case code add retcode reterrmsg two parameters */Public Function getpaysign ($SIGNOBJ) {foreach ($signObj as $k = = $v) {$signParams [Strtolower ($k)] = $v; try {if ($this->_appkey = = "") {throw new Exception ("Appkey is empty! " . " <br> "); } $signParams ["Appkey"] = $this->_appkey; Ksort ($signParams, sort_string); $commonUtil = new Commonutil (); $signString = $commonUtil->formatpayurlquery ($signParams, false); Return SHA1 ($signString); } catch (Exception $e) {echo ($e->getmessage ()); }}//js API signature where noncestr is the entire payment process is filled as order number public function createjsapipackage () {try {if ($this->checkparams ( ) = = False) {throw new Exception ("The build package parameter is missing! " . " <br> "); } $PAYOBJ ["appId"] = $this->_appid; $PAYOBJ ["package"] = $this->getpackagesign (); $PAYOBJ ["timeStamp"] = $this->getparams (self::timestamp); $PAYOBJ ["noncestr"] = $this->getparams (self::out_TRADE_NO); $PAYOBJ ["paysign"] = $this->getpaysign ($PAYOBJ); $PAYOBJ ["signtype"] = $this->_signtype; Return Json_encode ($PAYOBJ); } catch (Exception $e) {die ($e->getmessage ()); }/* * Build a shipment status array main three parameters OpenID transid OrderID */Public Function Createdeliverpost (array $params) {$deliver = Array (); $deliver [' appid '] = $this->_appid; $deliver [' OpenID '] = $params [' OpenID ']; $deliver [' transid '] = $params [' Transid ']; $deliver [' out_trade_no '] = $params [' Out_trade_no ']; $deliver [' deliver_timestamp '] = current_time (' timestamp '); $deliver [' deliver_status '] = 1; $deliver [' deliver_msg '] = ' OK '; $deliver [' app_signature '] = $this->getpaysign ($deliver); $deliver [' sign_method '] = ' SHA1 '; return $deliver; }/* * Generate a scan or click the native URL, the response's XML format * @params $retcode $reterrmsg Define the status of the item */Public function createnativepackage ($retc Ode = 0, $reterrmsg = "OK") {try {if ($this->checkparams () = = False && $retcOde = = 0) {//If it is normal to return, check the Tenpay parameter throw new Exception ("Generate package parameter is missing! " . " <br> "); } $NATIVEOBJ ["AppId"] = $this->_appid; $NATIVEOBJ ["package"] = $this->getpackagesign (); $NATIVEOBJ ["TimeStamp"] = $this->getparams (self::timestamp); $NATIVEOBJ ["noncestr"] = $this->getparams (self::out_trade_no); $NATIVEOBJ ["RetCode"] = $retcode; $NATIVEOBJ ["reterrmsg"] = $reterrmsg; $NATIVEOBJ ["appsignature"] = $this->getpaysign ($NATIVEOBJ); $NATIVEOBJ ["signmethod"] = $this->_signtype; $commonUtil = new Commonutil (); $xml = $commonUtil->arraytoxml ($NATIVEOBJ); Exit ($xml); }catch (Exception $e) {echo ($e->getmessage ()); }/* * Generate native URL with order number as parameter this is the entire payment process. */Public Function Createnativeurl ($productid) {$commonUtil = new Commonuti L (); $NATIVEOBJ ["appid"] = $this->_appid; $NATIVEOBJ ["ProductID"] = UrlEncode ($productid); $NATIVEOBJ ["timestamp"] = time (); $NATIVEOBJ ["Noncestr"] = Commonutil::createnoncestr (); $NATIVEOBJ ["sign"] = $this->getpaysign ($NATIVEOBJ); $nativeString = $commonUtil->formatpayurlquery ($NATIVEOBJ, false); Return "Weixin://wxpay/bizpayurl?". $nativeString; }/* * Take IP address */Public function GetIP () {switch (true) {case!empty ($_server["Http_client_ip"]): $ip = $_server["Http_client_ip"]; Break Case!empty ($_server["Http_x_forwarded_for"]): $ip = $_server["Http_x_forwarded_for"]; Break Case!empty ($_server["REMOTE_ADDR"]): $ip = $_server["REMOTE_ADDR"]; Break Default: $ip = "127.0.0.1"; } return $IP; }}class Md5signutil {public Function sign ($content, $key) {try {if (null = = $key) {throw new Exception ("Tenpay signature key cannot be empty!") " . " <br> "); } if (null = = $content) {throw new Exception ("Tenpay signed content cannot be empty". "<br>"); } $signStr = $content. "&key=". $key; Return Strtoupper (MD5 ($SIGNSTR)); } catch (Exception $e) {echo ($e->getmessage ()); }} public static function VerifySignature ($content, $sign, $md 5Key) {$signStr = $content. "&key=". $MD 5Key; $calculateSign = Strtolower (MD5 ($SIGNSTR)); $tenpaySign = Strtolower ($sign); return $calculateSign = = $tenpaySign; }}class commonutil {public Function Genallurl ($toURL, $paras) {$allUrl = null; if (null = = $toURL) {die (' Tourl is null '); } if (Strripos ($toURL, "?") = = "") {$allUrl = $toURL. "?" . $paras; } else {$allUrl = $toURL. "&". $paras; } return $ALLURL; }//order number, according to the actual custom static public function Createorderno () {$nonce = Commonutil::createnoncestr (4); Return Strtoupper (Date (' Ymds '). substr (Microtime (), 2,4). $nonce); }//random string static public function createnoncestr ($length = +) {$chars = "Abcdefghijklmnopqrstuvwxyzabcdefghijklmno PQRSTUVWXYZ0123456789 "; $str = ""; for ($i = 0; $i < $length; $i + +) {$str. = substr ($chars, Mt_rand (0, strlen ($chars)-1), 1); } return $STR; Public Function Splitparastr ($SRC, $token) {$resMap = array (); $items = Explode ($token, $src); foreach ($items as $item) {$paraAndValue = explode ("=", $item); if ($paraAndValue! = "") {$resMap [$paraAndValue [0]] = $paraAndValue [1]; }} return $resMap; } static function Trimstring ($value) {$ret = null; if (null! = $value) {$ret = $value; if (strlen ($ret) = = 0) {$ret = null; }} return $ret; Public Function Formaturlquery ($PARAMAP, $urlencode) {$buff = ""; Ksort ($paraMap, sort_string); foreach ($paraMap as $k = + $v) {if (null! = $v && "null"! = $v && "sign"! = $k) {if ($urle ncode) {$v = UrlEncode ($v); } $buff. = $k. "=" . $v. "&"; }} $reqPar = '; if (strlen ($buff) > 0) {$reqPar = substr ($buff, 0, strlen ($buff)-1); } return $reqPar; } public FunctioN Formatpayurlquery ($paraMap, $urlencode) {$buff = ""; Ksort ($paraMap, sort_string); foreach ($paraMap as $k = + $v) {if ($urlencode) {$v = UrlEncode ($v); } $buff. = Strtolower ($k). "=" . $v. "&"; } $reqPar = '; if (strlen ($buff) > 0) {$reqPar = substr ($buff, 0, strlen ($buff)-1); } return $reqPar; }/* * Output the XML format of the first-level array */Public function Arraytoxml ($arr) {$xml = "<xml>"; foreach ($arr as $key = + $val) {if ($key = = ' TimeStamp ' | | $key = = ' RetCode ') {$xml. = "<". $key. " > ". $val." </". $key." > "; } else $xml. = "<". $key. " ><! [cdata[]. $val. "] ></". $key." > "; } $xml. = "</xml>"; return $xml; }}
Summary: The above is the entire content of this article, I hope to be able to help you learn.