个人使用微信支付

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_35713752/article/details/82872687

首先在PAYJS申请到商户号和密钥,

然后实现源码如下:

<!DOCTYPE html>
<html lang="zh">
	<head>
		<meta charset="UTF-8">
		<title>Title</title>
		<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.2.1.js"></script>
		<style type="text/css">
			button {width: 150px;height: 50px;bottom: 20px;left: 40px;}
		</style>
	</head>
 
	<body>
		<button class="btn_2" id="btn" />支付</button>
		<script type="text/javascript">
			$("#btn").click(function() {
				var out_trade_no = new Date().getTime()+'aaaaaaa';
				$.ajax({
					type: "GET", 
					dataType: "json", 
					url: "http://www.wenjuanceshi.online/index.php?out_trade_no="+out_trade_no, //url
//					url: "http://localhost:8010/index.php", //url
					success: function(res) {
						console.log('1111:',res);
					},
					error: function(e) {
						console.log('2222:',e);
						var sign =e.responseText.replace(/\s+/g,"");
						pay(sign,out_trade_no)
						//密钥
					}
				});
			});
			//&notify_url=http://www.wenjuanceshi.online/notify_url.html&callback_url=http://www.wenjuanceshi.online/callback_url.html
			function pay(sign,out_trade_no){
				console.log('sign:',sign)
				window.location.href="https://payjs.cn/api/cashier?mchid=1515476391&total_fee=10&out_trade_no="+out_trade_no+"&sign="+sign;

			}
		</script>
 
	</body>
 
</html>
 
 <?php
    header("Content-Type:text/html;charset=utf8");     
    header("Access-Control-Allow-Origin: *"); //解决跨域    
    header('Access-Control-Allow-Methods:GET');// 响应类型      
    header('Access-Control-Allow-Headers:*'); // 响应头设置     
    @mysql_query("SET NAMES utf8");//解决中文乱码问题	
	//error_reporting(0); 
    
    $out_trade_no = $_GET['out_trade_no'];
    
    
    $mchid = '你的商户号';
	$key = '你的密钥';
	$data =[
		'mchid'=>$mchid,
		'body'=>'我是一个测试订单标题',
		'total_fee'=>1,
		'out_trade_no'=>$out_trade_no,
	];
	
    $data['sign']=sign($data,$key);
   $url='https://payjs.cn/api/cashier?'.http_build_query($data);
    echo $url;
    function sign($data,$key){
    	array_filter($data);
    	ksort($data);
    	return strtoupper(md5(urldecode(http_build_query($data).'&key='.$key)));
    }
    
?>

猜你喜欢

转载自blog.csdn.net/qq_35713752/article/details/82872687