最后的结果是这样的,然后就不知道接下来怎么办才好了,麻烦知道的给说一下,麻烦了
<?php
class Wxpayandroid
{
public $config = array(
'appid' => "wx426b3015555a46be",
'mch_id' => "1225312702",
'api_key' => "e10adc3949ba59abbe56e057f20f883e",
);
public $notify_url = 'http://www.weixin.qq.com/wxpay/pay.php';
public $out_trade_no = '20150806125346';
public $body = '测试商品';
public $total_fee = 1;
public $time_expire = '';
private $WxPayHelper;
public function Weixinpayandroid($total_fee,$tade_no)
{
$this->total_fee = intval($total_fee);
$this->out_trade_no = $tade_no;
$this->body = '微信支付的描述信息';
$this->time_expire = date('YmdHis', time() + 86400);
$this->notify_url = "http://wei.com/book/wxpay-app.php";
$app_response = $this->doPay();
if (isset($app_response['return_code']) && $app_response['return_code'] == 'FAIL') {
$errorCode = 100;
$errorMsg = $app_response['return_msg'];
$this->echoResult($errorCode, $errorMsg);
} else {
$errorCode = 0;
$errorMsg = 'success';
$responseData = array(
'notify_url' => $this->notify_url,
'app_response' => $app_response,
);
$this->echoResult($errorCode, $errorMsg, $responseData);
}
}
function echoResult($errorCode = 0, $errorMsg = 'success', $responseData = array())
{
$arr = array(
'errorCode' => $errorCode,
'errorMsg' => $errorMsg,
'responseData' => $responseData,
);
exit(json_encode($arr));
}
function getVerifySign($data, $key)
{
$String = $this->formatParameters($data, false);
$String = $String . "&key=" . $key;
$String = md5($String);
$result = strtoupper($String);
return $result;
}
function formatParameters($paraMap, $urlencode)
{
$buff = "";
ksort($paraMap);
foreach ($paraMap as $k => $v) {
if($k=="sign"){
continue;
}
if ($urlencode) {
$v = urlencode($v);
}
$buff .= $k . "=" . $v . "&";
}
$reqPar;
if (strlen($buff) > 0) {
$reqPar = substr($buff, 0, strlen($buff) - 1);
}
return $reqPar;
}
function getSign($obj, $api_key)
{
foreach ($obj as $k => $v)
{
$Parameters[strtolower($k)] = $v;
}
ksort($Parameters);
$String = $this->formatBizQueryParaMap($Parameters, false);
$String = $String."&key=".$api_key;
$result = strtoupper(md5($String));
return $result;
}
function getRandChar($length){
$str = null;
$strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
$max = strlen($strPol)-1;
for($i=0;$i<$length;$i++){
$str.=$strPol[rand(0,$max)];
}
return $str;
}
function arrayToXml($arr)
{
$xml = "<xml>";
foreach ($arr as $key=>$val)
{
if (is_numeric($val))
{
$xml.="<".$key.">".$val."</".$key.">";
}
else
$xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
}
$xml.="</xml>";
return $xml;
}
function postXmlCurl($xml, $url, $second=30, $useCert=false, $sslcert_path='', $sslkey_path='')
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_TIMEOUT, $second);
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
if($useCert == true){
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,TRUE);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,2);
curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
curl_setopt($ch,CURLOPT_SSLCERT, $sslcert_path);
curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
curl_setopt($ch,CURLOPT_SSLKEY, $sslkey_path);
}
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
$data = curl_exec($ch);
if($data){
curl_close($ch);
return $data;
} else {
$error = curl_errno($ch);
curl_close($ch);
return false;
}
}
function get_client_ip()
{
if (isset($_SERVER['REMOTE_ADDR'])) {
$cip = $_SERVER['REMOTE_ADDR'];
} elseif (getenv("REMOTE_ADDR")) {
$cip = getenv("REMOTE_ADDR");
} elseif (getenv("HTTP_CLIENT_IP")) {
$cip = getenv("HTTP_CLIENT_IP");
} else {
$cip = "127.0.0.1";
}
return $cip;
}
function formatBizQueryParaMap($paraMap, $urlencode)
{
$buff = "";
ksort($paraMap);
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;
}
function xmlToArray($xml)
{
$array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
return $array_data;
}
public function chkParam()
{
if (empty($this->out_trade_no)) {
die('out_trade_no error');
}
if (empty($this->body)) {
die('body error');
}
if (empty($this->time_expire)){
die('time_expire error');
}
if (empty($this->total_fee) || !is_numeric($this->total_fee)) {
die('total_fee error');
}
if (empty($this->notify_url)) {
die('notify_url error');
}
if (!preg_match("#^http:\/\/#i", $this->notify_url)) {
$this->notify_url = "http://" . $_SERVER['HTTP_HOST'] . $this->notify_url;
}
return true;
}
public function doPay() {
$this->chkParam();
return $this->createAppPara();
}
private function createAppPara()
{
$url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
$data["appid"] = $this->config['appid'];
$data["body"] = $this->body;
$data["mch_id"] = $this->config['mch_id'];
$data["nonce_str"] = $this->getRandChar(32);
$data["notify_url"] = $this->notify_url;
$data["out_trade_no"] = $this->out_trade_no;
$data["spbill_create_ip"] = $this->get_client_ip();
$data["total_fee"] = $this->total_fee;
$data["time_expire"] = $this->time_expire;
$data["trade_type"] = "APP";
$data["sign"] = $this->getSign($data, $this->config['api_key']);
$xml = $this->arrayToXml($data);
$response = $this->postXmlCurl($xml, $url);
$responseArr = $this->xmlToArray($response);
if(isset($responseArr["return_code"]) && $responseArr["return_code"]=='SUCCESS'){
return $this->getOrder($responseArr['prepay_id']);
}
return $responseArr;
}
public function getOrder($prepayId)
{
$data["appid"] = $this->config['appid'];
$data["noncestr"] = $this->getRandChar(32);
$data["package"] = "Sign=WXPay";
$data["partnerid"] = $this->config['mch_id'];
$data["prepayid"] = $prepayId;
$data["timestamp"] = time();
$data["sign"] = $this->getSign($data, $this->config['api_key']);
$data["packagestr"] = "Sign=WXPay";
return $data;
}
public function verifyNotify()
{
$xml = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : '';
if(!$xml){
return false;
}
$wx_back = $this->xmlToArray($xml);
if(empty($wx_back)){
return false;
}
$checkSign = $this->getVerifySign($wx_back, $this->config['api_key']);
if($checkSign=$wx_back['sign']){
return $wx_back;
}else{
return false;
}
}
}
$wxpayandroid = new \Wxpayandroid;
var_dump( $wxpayandroid->Weixinpayandroid(1,date('YmdHis')));
版权声明:本文为 熟悉的新风景原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。