php微信支付回调验证

//字典排序拼接字符串
function getWxPaySignature($arr){
    ksort($arr);
    $str = '';
    foreach ($arr as $k=>$a){
        $str .= $k;
        $str .= '='.$a.'&';
    }
    $str = trim($str,'&');
    return $str;
}
//微信回调验证
function weCallbackCheck($xml){
    $sign = $xml->sign;
    $appid = $xml->appid;
    $bank_type = $xml->bank_type;
    $cash_fee = $xml->cash_fee;
    $fee_type = $xml->fee_type;
    $is_subscribe = $xml->is_subscribe;
    $mch_id = $xml->mch_id;
    $nonce_str = $xml->nonce_str;
    $openid = $xml->openid;
    $out_trade_no = $xml->out_trade_no;
    $result_code = $xml->result_code;
    $return_code = $xml->return_code;
    $time_end = $xml->time_end;
    $total_fee = $xml->total_fee;
    $trade_type = $xml->trade_type;
    $transaction_id = $xml->transaction_id;
    $device_info = $xml->device_info;
    $signArr = array(
        'appid' => "$appid",
        'bank_type' => "$bank_type",
        'cash_fee' => "$cash_fee",
        'device_info' => "$device_info",
        'fee_type' => "$fee_type",
        'is_subscribe' => "$is_subscribe",
        'mch_id' => "$mch_id",
        'nonce_str' => "$nonce_str",
        'openid' => "$openid",
        'out_trade_no' => "$out_trade_no",
        'result_code' => "$result_code",
        'return_code' => "$return_code",
        'time_end' => "$time_end",
        'total_fee' => "$total_fee",
        'trade_type' => "$trade_type",
        'transaction_id' => "$transaction_id"
    );
    foreach ($signArr as $key => $value) {
        if($value == ''){
            unset($signArr[$key]);
        }
    }
    $stringA = getWxPaySignature($signArr);//数组字典排序拼接字符串
    $stringSignTemp = $stringA.'&key=???';//设置的key
    $signValue = md5($stringSignTemp);
    $signValue = strtoupper($signValue);
    if($signValue == $sign){
        return 'success';//支付成功
    }else{
     file_put_contents('failure.txt',json_encode($signArr).PHP_EOL,FILE_APPEND);
        return 'failure';
    }

}
$xmlstr = file_get_contents("php://input");
$xml=simplexml_load_string($xmlstr, 'SimpleXMLElement', LIBXML_NOCDATA);
if(weCallbackCheck($xml)=="success"){
    echo "支付成功";
}

  

猜你喜欢

转载自www.cnblogs.com/mracale/p/9437829.html