天天看點

app微信支付 伺服器端,2015最新微信支付APP 服務端處理

class WeiXin

{

private $config;

public function __construct()

{

$this->config = array(

'partner_id' => '',

'partner_key' => '',

'appid' => '',

'secret' => '',

'pay_sign_key' => '',

'notify_url' => '',

);

}

public function Get_App_Code($order)

{

if(!empty($order['subject'])) $order['subject'] = str_replace(array(' ', "\n", "\r"), '', $order['subject']);

$access_token = $this->Get_Access_Token();

$param = array(

'appid' => $this->config['appid'],

'traceid' => $order['out_trade_no'],

'noncestr' => md5(time().rand()),

'package' => $this->GetParamData($order),

'timestamp' => time(),

'sign_method' => 'sha1',

);

$param['app_signature'] = $this->GetAppSign($param);

$data = $this->GenprePayInsert($param, $access_token);

if(!empty($data['prepayid']) && !empty($data['errmsg']) && $data['errmsg'] == 'Success')

{

$pay = array(

'appid' => $this->config['appid'],

'noncestr' => $param['noncestr'],

'package' => 'Sign=WXPay',

'partnerid' => $this->config['partner_id'],

'prepayid' => $data['prepayid'],

'timestamp' => $param['timestamp']

);

$pay['sign'] = $this->GetAppSign($pay);

return $pay;

}

return '';

}

private function GenprePayInsert($param, $access_token)

{

return json_decode($this->Curl_Post('https://api.weixin.qq.com/pay/genprepay?access_token='.$access_token, json_encode($param)), true);

}

private function GetAppSign($param)

{

unset($param['sign_method']);

$param['appkey'] = $this->config['pay_sign_key'];

ksort($param);

return sha1($this->SetParam($param));

}

private function GetParamData($data)

{

$order = array(

'bank_type' => 'WX',

'body' => $data['subject'],

'total_fee' => $data['total_fee']*100,

'spbill_create_ip' => $GLOBALS['pz_log']->Getip(),

'out_trade_no' => $data['out_trade_no'],

'notify_url' => $this->config['notify_url'],

'partner' => $this->config['partner_id'],

'fee_type' => 1,

'input_charset' => 'UTF-8',

'attach' => 'weixin',

);

ksort($order);

$sgin = strtoupper(md5($this->SetParam($order).'&key='.$this->config['partner_key']));

return $this->SetParam($order, true).'&sign='.$sgin;

}

private function Curl_Post($url, $post) {

$options = array(

CURLOPT_RETURNTRANSFER => true,

CURLOPT_HEADER => false,

CURLOPT_POST => true,

CURLOPT_POSTFIELDS => $post,

);

$ch = curl_init($url);

curl_setopt_array($ch, $options);

$result = curl_exec($ch);

curl_close($ch);

return $result;

}

private function SetParam($param, $is_urlencode = false)

{

$str = '';

foreach($param as $k=>$v)

{

if($is_urlencode)

{

$str .= $k.'='.urlencode($v).'&';

} else {

$str .= $k.'='.$v.'&';

}

}

return substr($str, 0, -1);

}

private function Get_Access_Token()

{

if(file_exists('/tmp/weixin_pay_token.json'))

{

$temp = json_decode(file_get_contents('/tmp/weixin_pay_token.json'), true);

if($temp['time'] > time()) $token = $temp['token'];

}

if(empty($token)) $token = $this->Set_Access_Token();

return $token;

}

private function Set_Access_Token()

{

$temp = json_decode(file_get_contents('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$this->config["appid"].'&secret='.$this->config['secret']), true);

if(!empty($temp['access_token']))

{

$data = array(

'token' => $temp['access_token'],

'time' => time()+7000

);

if(!is_dir('/tmp')) mkdir('/tmp');

file_put_contents('/tmp/weixin_pay_token.json', json_encode($data));

return $temp['access_token'];

}

return '';

}

public function Respond()

{

$param = $_GET;

if(empty($param)) return;

$param_sign = $param['sign']; unset($param['sign']);

ksort($param);

$sign = strtoupper(md5($this->SetParam($param).'&key='.$this->config['partner_key']));

if($param_sign != $sign) return;

if(isset($param['trade_state']) && $param['trade_state'] == 0 && check_money($param['out_trade_no'], $param['total_fee']/100))

{

//如果成功這裡就可以處理自己的訂單了,辨別符是 $param['out_trade_no']

}

}

}

?>