1. Obtain the prepay_id from the client
Public function WxpayPost (){
// $ Data = '{"login_id": "479090", "nick_name": "150 *** 1068", "body": "weixin", "source ": "android", "total_detail": "1", "versionNum": "2.58 "}';
// $ Data = json_decode ($ data, true );
$ This-> values ['appid '] = appid;
$ This-> values ['body'] = trim ($ data ['body']);
$ This-> values ['mch _ id'] = MCHID; // merchant id
$ This-> values ['nonce _ str'] = self: getNonceStr ();
$ This-> values ['Your y _ url'] = 'http: // '. $ _ SERVER ['http _ host'].'/App/WxPay/Nofity ';
// $ This-> values ['openid'] = $ openid;
$ This-> values ['out _ trade_no '] = MCHID. date ("YmdHis ");
$ This-> values ['spbill _ create_ip '] = $ _ SERVER ['remote _ ADDR']; // terminal ip
$ This-> values ['total _ region'] = $ data ['total _ region'];
$ This-> values ['trade _ type'] = "APP ";
// $ This-> values ['sign'] = $ data ['sign'];
$ Order = $ this-> unifiedOrder ($ this-> values );
If ($ order ){
// $ Order ['xml'] ['prepay _ id'];
}
}
Public static function unifiedOrder ($ inputObj, $ timeOut = 6)
{
$ Url = "https://api.mch.weixin.qq.com/pay/unifiedorder ";
// Check Required parameters
If (! WxPayService: IsOut_trade_noSet ($ inputObj )){
Throw_exception ("The required parameter out_trade_no is missing for the unified payment interface! ");
} Else if (! WxPayService: IsBodySet ($ inputObj )){
Throw_exception ("The required parameter body is missing for the unified payment interface! ");
} Else if (! WxPayService: IsTotal_feeSet ($ inputObj )){
Throw_exception ("The required parameter total_timeout is missing for the unified payment interface! ");
} Else if (! WxPayService: IsTrade_typeSet ($ inputObj )){
Throw_exception ("The required parameter trade_type is missing for the unified payment interface! ");
}
// Associate parameters
If (WxPayService: GetTrade_type ($ inputObj) = "JSAPI "&&! WxPayService: IsOpenidSet ($ inputObj )){
Throw_exception ("The required parameter openid is missing in the unified payment interface! When trade_type is JSAPI, openid is a required parameter! ");
}
If (WxPayService: GetTrade_type ($ inputObj) = "NATIVE "&&! WxPayService: IsProduct_idSet ($ inputObj )){
Throw_exception ("The required product_id parameter is missing in the unified payment interface! When trade_type is JSAPI, product_id is a required parameter! ");
}
// If the asynchronous notification url is not set, use the url in the configuration file
If (! WxPayService: ispolicy_urlset ($ inputObj )){
WxPayService: SetNotify_url (NOTIFY_URL); // asynchronous notification url
}
// $ InputObj ['appid '] = appid; // public account ID
// $ InputObj ['spbill _ create_ip '] = $ _ SERVER ['remote _ ADDR']; // terminal ip
// $ InputObj-> SetSpbill_create_ip ("1.1.1.1 ");
// $ InputObj ['nonce _ str'] = self: getNonceStr (); // random string
// Signature
$ InputObj ['sign'] = WxPayService: SetSign ($ inputObj );
$ Xml = WxPayService: ToXml ($ inputObj );
$ Response = self: postXmlCurl ($ xml, $ url, false, $ timeOut );
$ Result = WxPayService: Init ($ response, $ inputObj );
$ Results = array ("sign" => $ inputObj ['sign'], "xml" => $ result );
Return $ results;
}
/**
* Submit xml to the corresponding interface url in post mode
* Enter description here...
* @ Param string $ xml the xml data to be post
* @ Param string $ url
* @ Param bool $ useCert whether a certificate is required. It is not required by default.
* @ Param int $ second url execution timeout time, 30 s by default
* @ Throws WxPayException
*/
Private static function postXmlCurl ($ xml, $ url, $ useCert = false, $ second = 30)
{
// Initialize curl
$ Ch = curl_init ();
// Set timeout
Curl_setopt ($ ch, CURLOPT_TIMEOUT, $ second );
// If a proxy is configured, set the proxy here.
// If (self: CURL_PROXY_HOST! = "0.0.0.0"
// & Self: CURL_PROXY_PORT! = 0 ){
// Curl_setopt ($ ch, CURLOPT_PROXY, self: CURL_PROXY_HOST );
// Curl_setopt ($ ch, CURLOPT_PROXYPORT, self: CURL_PROXY_PORT );
//}
Curl_setopt ($ ch, CURLOPT_URL, $ url );
Curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, FALSE );
Curl_setopt ($ ch, CURLOPT_SSL_VERIFYHOST, FALSE );
// Set the header
Curl_setopt ($ ch, CURLOPT_HEADER, FALSE );
// The result must be a string and output to the screen
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, TRUE );
// If ($ useCert = true ){
/// Set the certificate
/// Certificate used: cert and key belong to two. pem files respectively.
// Curl_setopt ($ ch, CURLOPT_SSLCERTTYPE, 'pem ');
// Curl_setopt ($ ch, CURLOPT_SSLCERT, self: SSLCERT_PATH );
// Curl_setopt ($ ch, CURLOPT_SSLKEYTYPE, 'pem ');
// Curl_setopt ($ ch, CURLOPT_SSLKEY, self: SSLKEY_PATH );
//}
// Post submission method
Curl_setopt ($ ch, CURLOPT_POST, TRUE );
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ xml );
// Run curl
$ Data = curl_exec ($ ch );
// Return results
If ($ data ){
Curl_close ($ ch );
Return $ data;
} Else {
$ Error = curl_errno ($ ch );
Curl_close ($ ch );
Throw_exception ("curl error, error code: $ error ");
}
}
II. Asynchronous notification processing
Public function Nofity (){
$ PostStr = $ GLOBALS ['http _ RAW_POST_DATA '];
$ Fp = @ fopen ('./policy.txt', 'W + ');
@ Fwrite ($ fp, $ postStr. "|". time ());
$ Xml = $ this-> verifyNotify ($ postStr );
// Print_r ($ xml );
If ($ xml ['return _ Code'] = 'success '){
$ Orderxml = $ this-> orderQuery ($ postStr );
$ Orders = $ this-> getXmlArray ($ orderxml );
$ User_order = M ("dtb_user_order ","","");
If ($ orders ['result _ Code'] = "SUCCESS "){
$ Time = date ("Y-m-d H: I: s", time ());
$ User_order-> execute ("update dtb_user_order set 'pay _ status' = 1, pay_time = '{$ time}' where order_sn = '{$ orders ['out _ trade_no']} '");
} Else {
$ User_order-> execute ("update dtb_user_order set 'pay _ status' = 0 where order_sn = {$ orders ['out _ trade_no ']}");
}
}
}
Public function orderQuery ($ data ){
$ Xml = $ this-> getXmlArray ($ data );
$ Order ['appid '] = appid;
$ Order ['mch _ id'] = MCHID;
$ Order ['nonce _ str'] = $ xml ['nonce _ str'];
$ Order ['transaction _ id'] = $ xml ['transaction _ id'];
$ Order ['sign'] = WxPayService: SetSign ($ order );
$ Xml = WxPayService: ToXml ($ order );
$ Url = "https://api.mch.weixin.qq.com/pay/orderquery ";
$ TimeOut = 6;
$ Response = self: postXmlCurl ($ xml, $ url, false, $ timeOut );
Return $ response;
// Print_r ($ response );
}