微信----小程序支付

$appid = '';//如果是公众号 就是公众号的appid;小程序就是小程序的appid
$body = '描述';
$mch_id = '商户号';
$KEY = '商户平台的key值';
$nonce_str = $this->getRandCode();//随机字符串
$notify_url = 'https://xxx.xxxxxx.com/xxx/xxxx/xxxxx/login/notify_url'; //支付完成回调地址url,不能带参数
$out_trade_no = date('YmdHis', time());//$order_id;//商户订单号
$spbill_create_ip = $_SERVER['SERVER_ADDR'];
$trade_type = 'JSAPI';//交易类型 默认JSAPI

//这里是按照顺序的 因为下面的签名是按照(字典序)顺序 排序错误 肯定出错
$post['appid'] = $appid;
$post['body'] = $body;
$post['mch_id'] = $mch_id;
$post['nonce_str'] = $nonce_str;//随机字符串
$post['notify_url'] = $notify_url;
$post['openid'] = $openid['wx_openid'];
$post['out_trade_no'] = $out_trade_no;
$post['spbill_create_ip'] = $spbill_create_ip;//服务器终端的ip
$post['total_fee'] = intval($total_fee["current_price"]*100); //总金额 最低为一分钱 必须是整数
$post['trade_type'] = $trade_type;
$sign = $this->MakeSign($post,$KEY); //签名s

$post_xml = '<xml>
<appid><![CDATA['.$appid.']]></appid>
<body><![CDATA['.$body.']]></body>
<mch_id><![CDATA['.$mch_id.']]></mch_id>
<nonce_str><![CDATA['.$nonce_str.']]></nonce_str>
<notify_url><![CDATA['.$notify_url.']]></notify_url>
<openid><![CDATA['.$openid["wx_openid"].']]></openid>
<out_trade_no><![CDATA['.$out_trade_no.']]></out_trade_no>
<spbill_create_ip><![CDATA['.$spbill_create_ip.']]></spbill_create_ip>
<total_fee><![CDATA['.intval($total_fee["current_price"]*100).']]></total_fee>
<trade_type><![CDATA['.$trade_type.']]></trade_type>
<sign><![CDATA['.$sign.']]></sign>
</xml>';

//统一下单接口prepay_id
$url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
$xml = ihttp_post($url,$post_xml); //POST方式请求http
$array = $this->xml2array($xml['content']); //将【统一下单】api返回xml数据转换成数组,全要大写if($array['RETURN_CODE'] == 'SUCCESS' && $array['RESULT_CODE'] == 'SUCCESS $time = time();
    $tmp='';                            //临时数组用于签名
$tmp['appId'] = $appid;
$tmp['nonceStr'] = $nonce_str;
$tmp['package'] = 'prepay_id='.$array['PREPAY_ID'];
$tmp['signType'] = 'MD5';
$tmp['timeStamp'] = $time;

$data['timeStamp'] = "$time"; //时间戳
$data['nonceStr'] = $nonce_str; //随机字符串
$data['signType'] = 'MD5'; //签名算法,暂支持 MD5
$data['package'] = 'prepay_id='.$array['PREPAY_ID']; //统一下单接口返回的 prepay_id 参数值,提交格式如:prepay_id=*
$data['paySign'] = $this->MakeSign($tmp,$KEY); //签名,具体签名方案参见微信公众号支付帮助文档;


}

//获取到$nonce_str
public function getRandCode()
{
$charts = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz0123456789";
$max = strlen($charts);
$noncestr = "";
for($i = 0; $i < 16; $i++)
{
$noncestr .= $charts[mt_rand(0, $max)];
}
return $noncestr;
}

/**
* 生成签名, $KEY就是支付key
* @return 签名
*/
public function MakeSign($params,$KEY){
//签名步骤一:按字典序排序数组参数
ksort($params);
$string = $this->ToUrlParams($params); //参数进行拼接key=value&k=v
//签名步骤二:在string后加入KEY
$string = $string . "&key=".$KEY;
//签名步骤三:MD5加密
$string = md5($string);
//签名步骤四:所有字符转为大写
$string = strtoupper($string);
return $string;
}

public function ToUrlParams( $params ){
$string = '';
if( !empty($params) ){
$array = array();
foreach( $params as $key => $value ){
$array[] = $key.'='.$value;
}
$string = implode("&",$array);
}
return $string;
}

//获取xml里面数据,转换成array
public function xml2array($xml){
$p = xml_parser_create();
xml_parse_into_struct($p, $xml, $vals, $index);
xml_parser_free($p);
$data = "";
foreach ($index as $key=>$value) {
if($key == 'xml' || $key == 'XML') continue;
$tag = $vals[$value[0]]['tag'];
$value = $vals[$value[0]]['value'];
$data[$tag] = $value;
}
return $data;
}


//支付成功后的回调url处理 (https://xxx.xxxxxx.com/xxx/xxxx/xxxxx/login/notify_url)

这里需要导入微信提供的SDK
public function notify_url() {
vendor('WeixinPay.lib.WxPay#Api');
$msg = '';
\WxPayApi::notify(function($res){
$log_file = 'b.txt';
//当前链接
//$url = $_SERVER['REQUEST_URI'] ? $_SERVER['REQUEST_URI'] : $_SERVER['PHP_SELF'];
//写入的内容
$con = '';
foreach ($res as $k => $v) {
$con .= "[time:]".date('Y-m-d H:i:s',time()) . " [{$k}] => {$v} \r\n";
}
$con .= "===================================================== \r\n";
//写入到log文本中
file_put_contents($log_file,$con, FILE_APPEND);

if($res['return_code'] == 'SUCCESS' && $res['result_code'] == 'SUCCESS'){
$data = array();
$data['weixin_order_id'] = $res['transaction_id'];
$data['out_trade_no'] = $res['out_trade_no'];
$data['bank_type'] = $res['bank_type'];
$data['status'] = 4;

$cus_id = M('customers')->where('wx_openid="%s"', $res['openid'])->field('id')->find();
$cus_id = $cus_id['id'];
$aa = M('orders')->where('customer_id="%s" and errmsg="" and package=""', $cus_id)->save($data);
if (!$aa) {
echo '';exit;
} else {
$this->return_success();
}
} else {
echo '';exit;
}
}, $msg);
//echo $msg;
}
public function return_success(){
$return['return_code'] = 'SUCCESS';
$return['return_msg'] = 'OK';
$xml_post = '<xml>
<return_code><![CDATA[' . $return["return_code"] . ']]></return_code>
<return_msg><![CDATA['. $return["return_msg"] .']]></return_msg>
</xml>';
echo $xml_post;exit;
}










posted @ 2018-05-31 15:10  千秋伟业  阅读(216)  评论(0编辑  收藏  举报