This article mainly introduces the php payment APP payment method. The example analyzes the php payment interface file and usage skills. For more information, see
This article mainly introduces the php payment APP payment method. The example analyzes the php payment interface file and usage skills. For more information, see
This article describes the integrated payment function of the open platform mobile app. Share it with you for your reference. The specific analysis is as follows:
The WechatAppPay file code is as follows:
The Code is as follows:
<? Php
Namespace common \ services \ WechatPay;
Class WechatAppPay extends WechatPayBase
{
// Package Parameters
Public $ package = [];
// Asynchronous notification parameters
Public $ policy = [];
// Push pre-payment order parameters
Protected $ config = [];
// Stores the access token and the object with the obtained time
Protected $ file;
// Access token
Protected $ accessToken;
// Obtain the access token url
Const ACCESS_TOKEN_URL = 'https: // api.weixin.qq.com/cgi-bin/token? Grant_type = client_credential & appid = % s & secret = % s ';
// Generate the address for submitting the pre-payment order
Const POST_ORDER_URL = 'https: // api.weixin.qq.com/pay/genprepay? Access_token = % s ';
Public function _ construct ()
{
$ This-> file = _ DIR _. '/payAccessToken.txt ';
}
/**
* Final response parameters for creating 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,
'Timestamp' => (string) time (),
];
$ Array ['sign'] = $ this-> sha1Sign ($ array );
Unset ($ array ['appkey']);
} Catch (\ Exception $ e ){
Throw new \ Exception ($ e-> getMessage ());
}
Return $ array;
}
/**
* Verify the notification parameters after successful payment
*
* @ Throws \ Exception
* @ Return boolean
*/
Public function verifypolicy ()
{
Try {
$ StaySignStr = $ this-> running y;
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;
}
}
// If the file content is null or the access token is invalid, obtain it again.
$ 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 ()
{
Try {
$ 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 getAccessToken ()
{
$ 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'];
}
/**
* Pre-payment session ID
*
* @ Throws \ Exception
* @ Return string
*/
Protected function getPrepayid ()
{
$ Data = json_encode ($ this-> config );
$ Url = sprintf (self: 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 pre-payment parameters
*
* @ 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 rules:
* 1. Generate the signValue of the sign.
* 2. concatenate the package parameter into a query string and urlencode the value.
* 3. concatenate sign = signValue into the string generated by string 2 to obtain the final package string.
*
* In step 2, The urlencode space must be encoded as % 20 instead of +
*
* RFC 1738 encodes spaces into +
* RFC 3986 encodes spaces into % 20.
*
* @ Return string
*/
Protected function generatePackage ()
{
$ This-> package ['sign'] = $ this-> signData ($ this-> package );
Return http_build_query ($ this-> package, '', '&', PHP_QUERY_RFC3986 );
}
/**
* Generate a 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 rules:
* 1. Sort dictionaries and splice them into the query string format. urlencode is not required.
* 2. The string obtained in the previous step is spliced with key = paternerKey.
* 3. Convert the MD5 hash string into a big-write string to obtain the signValue of sign.
*
* @ Param array $ data the data to be signed
* @ Return string the 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 rules
* 1. dictionary sorting
* 2. concatenate query strings
* 3. sha1 operations
*
* @ Param array $ arr
* @ Return string
*/
Protected function sha1Sign ($ arr)
{
Ksort ($ arr );
Return sha1 ($ this-> arrayToString ($ arr ));
}
}
I hope this article will help you with php programming.
,