WeChat payment APP payment

Source: Internet
Author: User
Tags flock md5 hash
The integrated payment function of the platform's mobile applications is available. For more information, see www.360us.netarticle23.html. Only the consumption function is provided. Other functions are not added. The payment on the open platform is different from that on the public account. None? PhpnamespacecommonservicesWechatPay; classWec

The integrated payment function of the platform's mobile applications is available. Specific use step: http://www.360us.net/article/23.html is only a consumption function, other features are not added. The payment on the open platform is different from that on the public account. None? Phpnamespace common \ services \ WechatPay; class Wec

The integrated payment function of the platform's mobile applications is available.
Specific use step: http://www.360us.net/article/23.html
It is only a consumption function, but other functions are not added.

The payment on the open platform is different from that on the public account. <无>
  File = _ DIR __. '/payAccessToken.txt';}/*** the final return parameter for creating an APP payment * @ throws \ Exception * @ return multitype: string NULL */public function createAppPayData () {$ this-> generateConfig (); $ prepayid = $ this-> getPrepayid (); try {$ array = ['appid '=> $ this-> appid, 'appkey' => $ this-> paySignkey, 'noncestr' => $ this-> getRandomStr (), 'package' => 'sign = wxpay ', 'ererid' => $ this-> partnerid, 'prepayid' => $ prepayid, 'timesta Mp '=> (string) time (),]; $ array ['sign'] = $ this-> sha1Sign ($ array ); unset ($ array ['appkey']);} catch (\ Exception $ e) {throw new \ Exception ($ e-> getMessage ();} return $ array ;} /*** notification parameter after successful payment is verified ** @ throws \ Exception * @ return boolean */public function verifyNotify () {try {$ staySignStr = $ this-> Policy; unset ($ staySignStr ['sign']); $ sign = $ this-> signData ($ staySignStr ); return $ this-> your Y ['sign'] ===$ sign ;} Catch (\ Exception $ e) {throw new \ Exception ($ e-> getMessage () ;}}/*** magic method, add the payment parameter ** @ param string $ name parameter name * @ param string $ value parameter value */public function _ set ($ name, $ value) {$ this-> $ name = $ value ;} /*** set access token * @ param string $ token * @ throws \ Exception * @ return boolean */public function setAccessToken () {try {if (! File_exists ($ this-> file) |! Is_file ($ this-> file) {$ f = fopen ($ this-> file, 'A'); fclose ($ f );} $ content = file_get_contents ($ this-> file); if (! Empty ($ content) {$ info = json_decode ($ content, true); if (time ()-$ info ['gettime'] <7150) {$ this-> accessToken = $ info ['accessstoken']; return true ;}// the file content is null or the access token is invalid, obtain $ this-> outputAccessTokenToFile ();} catch (\ Exception $ e) {throw new \ Exception ($ e-> getMessage ();} return true ;} /*** write access token to the file * @ throws \ Exception * @ return boolean */protected function outputAccessTokenToFile () {t Ry {$ f = fopen ($ this-> file, 'wb '); $ token = ['accesstoken' => $ this-> getAccessToken (), 'gettime' => time (),]; flock ($ f, LOCK_EX); fwrite ($ f, json_encode ($ token); flock ($ f, LOCK_UN ); fclose ($ f); $ this-> accessToken = $ token ['accessstoken'];} catch (\ Exception $ e) {throw new \ Exception ($ e-> getMessage ();} return true ;} /*** access token *** @ throws \ Exception * @ return string */protected function getAccessTok En () {$ url = sprintf (self: ACCESS_TOKEN_URL, $ this-> appid, $ this-> appSecret ); $ result = json_decode ($ this-> getUrl ($ url), true); if (isset ($ result ['errorcode']) {throw new \ Exception ("get access token failed: {$ result ['errmsg ']}");} return $ result ['Access _ token'];} /*** get the pre-payment session identifier ** @ throws \ Exception * @ return string */protected function getPrepayid () {$ data = json_encode ($ this-> config ); $ url = sprintf (s Elf: POST_ORDER_URL, $ this-> accessToken); $ result = json_decode ($ this-> postUrl ($ url, $ data), true ); if (isset ($ result ['errcode']) & $ result ['errcode']! = 0) {throw new \ Exception ($ result ['errmsg ']);} if (! Isset ($ result ['prepayid']) {throw new \ Exception ('get prepayid failed, url request error. ');} return $ result ['prepayid'];}/*** assemble the pre-payment parameter ** @ throws \ Exception */protected function generateConfig () {try {$ this-> config = ['appid '=> $ this-> appid, 'traceid' => $ this-> traceid, 'noncestr' => $ this-> getRandomStr (), 'timestamp' => time (), 'package' => $ this-> generatePackage (), 'sign _ method' => $ this-> sign_method,]; $ this-> config ['app _ signature'] = $ this-> generateSign ();} catch (\ Exception $ e) {throw new \ Exception ($ e-> getMessage ();}/*** generate the package field ** generation rule: * 1. Generate the signValue of sign * 2. splice the package parameter into a query string, urlencode * 3. concatenate sign = signValue to the string generated in step 2 to obtain the final package string. ** in step 2, urlencode spaces must be encoded into % 20 instead of + ** RFC 2nd + * RFC 3986 encodes the space into % 20 ** @ return string */protected function generatePackage () {$ this-> package ['sign'] = $ this-> signData ($ this-> package); return http_build_query ($ this-> package ,'', '&', PHP_QUERY_RFC3986);}/*** generate the signature ** @ return string */protected function generateSign () {$ signArray = ['appid '=> $ this-> appid, 'appkey' => $ this-> paySignkey, 'noncestr' => $ this-> config ['noncestr '], 'package' => $ this-> config ['package'], 'timestamp' => $ this-> config ['timestamp'], 'traceid' => $ this-> traceid,]; return $ this-> sha1Sign ($ signArray);}/*** signature data ** generation rule: * 1. Sort the dictionary and splice it into the query string format, urlencode * 2. concatenate the string obtained in the previous step with key = paternerKey * 3. Convert the MD5 hash string to the value signValue of sign in uppercase. ** @ param array $ data to be signed data * @ return string final signature result */protected function signData ($ data) {ksort ($ data); $ str = $ this-> arrayToString ($ data); $ str. = "& key = {$ this-> partnerKey}"; return strtoupper ($ this-> signMd5 ($ str ));} /*** sha1 signature * signature Rule * 1. dictionary sorting * 2. concatenated query string * 3. sha1 calculation ** @ param array $ arr * @ return string */protected function sha1Sign ($ arr) {ksort ($ arr); return sha1 ($ this-> arrayToString ($ arr ));}}

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.