//微信支付
Route::get('wechat', 'ZhifuController@wechat');
//微信支付成功完成后回调地址
Route::post('wenotify','ZhifuController@wenotify');
//微信支付成功后跳转页面
Route::get('wechat_ok', 'ZhifuController@wechat_ok');
==================================================
$.ajax({
type:'get',
dataType:'html',
//async : true,
data: {
'zhifujiage': $('#zhifujiage').val(),
'erweima' : $('#erweima').val(),
'zhifufangshi': 'wechat'
},
url:'{{url('wechat')}}',
success:function(data) {
//alert(data);
window.location.href = data;
}
});
=====================================================
public function wechat()
{
//微信支付
$outTradeNum = 'wechat'.time();
$payAmount = request::input('zhifujiage');
$subject = '商品内容';
$appid = "wx72703c3036123456"; //应用 APPID
$mch_id = "1549212345"; //微信支付商户号
$out_trade_no = $outTradeNum; //平台内部订单号
$nonce_str = $this->createNoncestr(); //随机字符串
$body = $subject; //内容
$total_fee = $payAmount * 100; //金额,转化成分单位
$spbill_create_ip = $this->Ip(); //IP
$notify_url = "http://www.xxxxx.com/wenotify"; //回调地址
$trade_type = 'MWEB';//交易类型 具体看 API 里面有详细介绍
$scene_info ='{"h5_info":{"type":"Wap","wap_url":"http://www.xxxxx.com","wap_name":"创咖寄卖"}}';//场景信息 必要参数
//拼接字符串 注意顺序微信有个测试网址 顺序按照他的来 直接点下面的校正测试 包括下面 XML 是否正确
$signA ="appid=$appid&attach=$out_trade_no&body=$body&mch_id=$mch_id&nonce_str=$nonce_str¬ify_url=$notify_url&out_trade_no=$out_trade_no&scene_info=$scene_info&spbill_create_ip=$spbill_create_ip&total_fee=$total_fee&trade_type=$trade_type";
//微信商户 API 密钥, 申请支付后有给予一个商户账号和密码,登陆后自己设置key
$key = "wwwxxxxxjimaicom000000000000000";
$strSignTmp = $signA . "&key = $key";
// MD5 后转换成大写
$sign = strtoupper(MD5($strSignTmp));
//拼接成 XML 格式
$post_data = "
$body
//微信传参地址
$url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
//后台 POST 微信传参地址 同时取得微信返回的参数
$dataxml = $this->postXmlCurl($post_data, $url);
//将微信返回的 XML 转换成数组
$objectxml = (array)simplexml_load_string($dataxml, 'SimpleXMLElement', LIBXML_NOCDATA);
// 可以在 MWEB_URL 后拼接上 redirect_url 参数,来指定回调页面
$returnUrl = "http://www.xxxxx.com/wechat_ok";
$return_Url = urlencode($returnUrl);
return $objectxml['mweb_url'] . '&redirect_url=' . $return_Url;
}
=====================================================
public function wenotify()
{
//微信支付回调页面,静默执行
//回调地址接收的数据
//{"appid":"wx72703c3036a8197b","bank_type":"CFT","cash_fee":"1","fee_type":"CNY","is_subscribe":"Y","mch_id":"1549232891","nonce_str":"vhJeE1k7lxz5Ankz","openid":"ouzAowGMNiks9R62cw5NZas9BvlQ","out_trade_no":"wechat1566399864","result_code":"SUCCESS","return_code":"SUCCESS","sign":"E5E1040F24C88E48130F5275BD19EA7C","time_end":"20190821230428","total_fee":"1","trade_type":"MWEB","transaction_id":"4200000374201908217238359011"}
//获得返回的XML数据
$xml = file_get_contents("php://input");
//将服务器返回的XML数据转化为数组,引用格式 $data['appid'];
$data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
// 保存微信服务器返回的签名sign
$data_sign = $data['sign'];
// sign不参与签名算法
unset($data['sign']);
$sign = $this->signs($data);
$result='';
// 判断签名是否正确 判断支付状态
if ( ($sign===$data_sign) && ($data['return_code']=='SUCCESS') && ($data['result_code']=='SUCCESS') )
{
//$data为获取服务器返回的数据
$result = $data;
//这里进行业务逻辑处理。。。。。。
}else{
$str='
}
// 返回状态给微信服务器
if ($result) {
$str='
}else{
$str='
}
return $str;
}
==============================================================
public function wechat_ok()
{
//跳转后的处理。。。。
}
==============================================================
public function Ip()
{
//获得ip
if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
$ip = getenv('HTTP_CLIENT_IP');
} elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
$ip = getenv('HTTP_X_FORWARDED_FOR');
} elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
$ip = getenv('REMOTE_ADDR');
} elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
$ip = $_SERVER['REMOTE_ADDR'];
}
return preg_match ( '/[\d\.]{7,15}/', $ip, $matches ) ? $matches [0] : '';
}
-----------------------------------------------------
public function createNoncestr( $length = 32 )
{
//随机32位长度的字符串
$chars = "abcdefghijklmnopqrstuvwxyz0123456789";
$str ="";
for ( $i = 0; $i < $length; $i++ ) {
$str.= substr($chars, mt_rand(0, strlen($chars)-1), 1);
}
return $str;
}
----------------------------------------------------------
function postXmlCurl($xml, $url, $second = 30){
$ch = curl_init();
//设置超时
curl_setopt($ch, CURLOPT_TIMEOUT, $second);
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
//设置 header
curl_setopt($ch, CURLOPT_HEADER, FALSE);
//要求结果为字符串且输出到屏幕上
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
//post 提交方式
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
//运行 curl
$data = curl_exec($ch);
//返回结果
if($data){
curl_close($ch);
return $data;
}else{
$error = curl_errno($ch);
curl_close($ch);
echo "curl 出错,错误码:$error"."
";
}
}
-----------------------------------------
private function signs($data)
{
//签名算法
$stringA = '';
foreach ($data as $key=>$value){
if(!$value) continue;
if($stringA) $stringA .= '&'.$key."=".$value;
else $stringA = $key."=".$value;
}
//申请支付后有给予一个商户账号和密码,登陆后自己设置key
$wx_key = 'wwwxxxxxjimaicom1234567891234';
$stringSignTemp = $stringA.'&key='.$wx_key;
return strtoupper(md5($stringSignTemp));
}
-------------------------------------------