public function appletWxPay ( ) {
$openId = I ( 'openid' ) ;
$orderId = I ( 'orderid' ) ;
$wechatAppId = * * * * * * * * * * ;
$wechatMchId = * * * * * * * * * * ;
$wechatKey = * * * * * * * * * * ;
$webConfig = M ( 'web_config' ) - > find ( ) ;
$orderModel = D ( "ProductOrder" ) ;
$order = $orderModel - > selectOneOrderProductByCondition ( array ( 'order_id' = > $orderId ) ) ;
$orderPrice = $order [ 'order_price' ] ;
$wechat [ 'appid' ] = $wechatAppId ;
$wechat [ 'body' ] = '订单支付' ;
$wechat [ 'mch_id' ] = $wechatMchId ;
$wechat [ 'nonce_str' ] = strtoupper ( md5 ( time ( ) . rand ( 100 , 999 ) ) ) ;
$wechat [ 'notify_url' ] = $webConfig [ 'web_url' ] . '/index.php/WxappMall/wechatNotifyUrl?orderid=' . $orderId ;
$wechat [ 'openid' ] = $openId ;
$wechat [ 'out_trade_no' ] = time ( ) ;
$wechat [ 'spbill_create_ip' ] = get_client_ip ( ) ;
$wechat [ 'total_fee' ] = intval ( $orderPrice * 100 ) ;
$wechat [ 'trade_type' ] = 'JSAPI' ;
$wechat [ 'key' ] = $wechatKey ;
$wechat [ 'sign' ] = strtoupper ( md5 ( 'appid=' . $wechat [ 'appid' ] . '&body=' . $wechat [ 'body' ] . '&mch_id=' . $wechat [ 'mch_id' ] . '&nonce_str=' . $wechat [ 'nonce_str' ] . '¬ify_url=' . $wechat [ 'notify_url' ] . '&openid=' . $wechat [ 'openid' ] . '&out_trade_no=' . $wechat [ 'out_trade_no' ] . '&spbill_create_ip=' . $wechat [ 'spbill_create_ip' ] . '&total_fee=' . $wechat [ 'total_fee' ] . '&trade_type=' . $wechat [ 'trade_type' ] . '&key=' . $wechat [ 'key' ] ) ) ;
$wechat [ 'time_stamp' ] = time ( ) ;
$formData = '<xml>' ;
$formData . = '<appid><![CDATA[' . $wechat [ 'appid' ] . ']]></appid>' ;
$formData . = '<body><![CDATA[' . $wechat [ 'body' ] . ']]></body>' ;
$formData . = '<mch_id><![CDATA[' . $wechat [ 'mch_id' ] . ']]></mch_id>' ;
$formData . = '<nonce_str><![CDATA[' . $wechat [ 'nonce_str' ] . ']]></nonce_str>' ;
$formData . = '<notify_url><![CDATA[' . $wechat [ 'notify_url' ] . ']]></notify_url>' ;
$formData . = '<openid><![CDATA[' . $wechat [ 'openid' ] . ']]></openid>' ;
$formData . = '<out_trade_no><![CDATA[' . $wechat [ 'out_trade_no' ] . ']]></out_trade_no>' ;
$formData . = '<spbill_create_ip><![CDATA[' . $wechat [ 'spbill_create_ip' ] . ']]></spbill_create_ip>' ;
$formData . = '<total_fee><![CDATA[' . $wechat [ 'total_fee' ] . ']]></total_fee>' ;
$formData . = '<trade_type><![CDATA[' . $wechat [ 'trade_type' ] . ']]></trade_type>' ;
$formData . = '<sign><![CDATA[' . $wechat [ 'sign' ] . ']]></sign>' ;
$formData . = '</xml>' ;
$xml = api_notice_increment ( 'https:/' . '/api.mch.weixin.qq.com/pay/unifiedorder' , $formData ) ;
if ( $xml ) {
$resultCode = resolveXmlCode ( $xml , 'result_code' ) ;
if ( $resultCode == 'SUCCESS' ) {
$prepay_id = resolveXmlCode ( $xml , 'prepay_id' ) ;
$payData [ 'appId' ] = $wechat [ 'appid' ] ;
$payData [ 'timeStamp' ] = $wechat [ 'time_stamp' ] . '' ;
$payData [ 'nonceStr' ] = $wechat [ 'nonce_str' ] ;
$payData [ 'package' ] = 'prepay_id=' . $prepay_id ;
$payData [ 'signType' ] = 'MD5' ;
$payData [ 'paySign' ] = md5 ( 'appId=' . $wechat [ 'appid' ] . '&nonceStr=' . $wechat [ 'nonce_str' ] . '&package=prepay_id=' . $prepay_id . '&signType=MD5&timeStamp=' . $wechat [ 'time_stamp' ] . '&key=' . $wechat [ 'key' ] ) ;
$this - > ajaxReturn ( array ( 'status' = > 1 , 'info' = > $payData , 'orderid' = > $orderId ) ) ;
} else {
$this - > ajaxReturn ( array ( 'status' = > 0 , 'info' = > resolveXmlCode ( $xml , 'err_code_des' ) ) ) ;
}
} else {
$this - > ajaxReturn ( array ( 'status' = > 0 , 'info' = > '参数错误,请稍后再试!' ) ) ;
}
}
function api_notice_increment ( $url , $data ) {
$ch = curl_init ( ) ;
$header = "Accept-Charset: utf-8" ;
curl_setopt ( $ch , CURLOPT_URL , $url ) ;
curl_setopt ( $ch , CURLOPT_CUSTOMREQUEST , "POST" ) ;
curl_setopt ( $ch , CURLOPT_SSL_VERIFYPEER , FALSE ) ;
curl_setopt ( $ch , CURLOPT_SSL_VERIFYHOST , FALSE ) ;
@curl_setopt ( $curl , CURLOPT_HTTPHEADER , $header ) ;
curl_setopt ( $ch , CURLOPT_USERAGENT , 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)' ) ;
curl_setopt ( $ch , CURLOPT_FOLLOWLOCATION , 1 ) ;
curl_setopt ( $ch , CURLOPT_AUTOREFERER , 1 ) ;
curl_setopt ( $ch , CURLOPT_POSTFIELDS , $data ) ;
curl_setopt ( $ch , CURLOPT_RETURNTRANSFER , true ) ;
$tmpInfo = curl_exec ( $ch ) ;
if ( curl_errno ( $ch ) ) {
return $ch ;
} else {
return $tmpInfo ;
}
curl_close ( $ch ) ;
}
function resolveXmlCode ( $str , $code ) {
header ( "Content-Type: text/html;charset=utf-8" ) ;
$findStartStr = "![CDATA[" ;
$findEndStr = "]]" ;
$tempContent = "" ;
$startLocation = mb_stripos ( $str , $code , 0 , 'utf-8' ) ;
if ( $startLocation === false ) {
return "" ;
}
$targetLocation = mb_stripos ( $str , $findStartStr , $startLocation , 'utf-8' ) ;
if ( $targetLocation !== false ) {
$tempLocation = mb_stripos ( $str , $findEndStr , $targetLocation , 'utf-8' ) ;
$tempContent = mb_substr ( $str , $targetLocation + mb_strlen ( $findStartStr , 'utf-8' ) , $tempLocation - ( $targetLocation + mb_strlen ( $findStartStr , 'utf-8' ) ) , 'utf-8' ) ;
}
return $tempContent ? $tempContent : "" ;
}