使用idea完成支付功能

使用idea完成支付功能

1.下载电脑网站的官方demo:

下载:https://docs.open.alipay.com/270/106291/
在这里插入图片描述
在这里插入图片描述
(1) 注册蚂蚁金服开发者账号(免费,不像苹果会收取费用)

注册地址:https://open.alipay.com ,用你的支付宝账号扫码登录

(2) 设置app_id和gatewayUrl
在这里插入图片描述
在这里插入图片描述

其中密钥需要自己生成,appID和支付宝网关是已经给好的,网关有dev字样,表明是用于开发测试。点那个设置 可以下载生成公钥
在这里插入图片描述
注意部分:
在这里插入图片描述

点击上图的生成方法 下载 跟你系统对用的 程序,然后生成 相应的 密匙
在这里插入图片描述

如果没有设置过,此时显示文本是"设置应用公钥",我这里是已经设置过得。
在这里插入图片描述

下一步:查看你的公钥把 公钥的字符串 复制过去ide AlipayConfig.java 里面
在这里插入图片描述在这里插入图片描述
以上的步骤基本上是完成搭建

这是jsp充值页面(美化没到位)
在这里插入图片描述
源代码

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link href="${pageContext.request.contextPath}/css/bootstrap.min.css" rel="stylesheet">
<script src="${pageContext.request.contextPath}/js/jquery-1.11.0.min.js"></script>
<style>
* {
	color: rgba(255, 255, 255, 0.8);
}

body {
	width: 100%;
	height: 532px;
	background: url(${pageContext.request.contextPath}/imgs/pp.jpg) no-repeat;
	background-size: 100% 100%;
}

.main {
	width: 520px;
	height: 200px;
	margin-left: 420px;
	margin-top: 100px;
	border: 1px solid #8c8c8c;
	border-radius: 5px;
}

h3 {
	padding-top: 20px;
}

.font {
	padding-top: 7px;
	margin-bottom: 0;
	text-align: right;
}
</style>
</head>
<body>
<div class="main">
	<h3 class="text-center">
		<strong>充值</strong>
	</h3>

	<%--action是你的充值方法--%>
	<form action="${pageContext.request.contextPath}/mem/alipay" method="post">

	<div class="form-group col-md-12">
		<label class="col-md-2 font control-label"> 金额:</label>
		<div class="col-md-8">
			<input name="money" type="text" style="background: rgba(0, 0, 0, 0); border: 		       1px solid #8c8c8c; color: white"class="form-control" id="exampleInputName2">
	</div>
</div>
<br />
	<div class="form-group col-md-12">
		<div class="col-md-3"></div>
		<div class="col-md-3">
			<input type="submit" class="btn form-control" value="充值" />
		</div>
		<div class="col-md-3">
			<a href="${pageContext.request.contextPath}/res/list" class="btn form-control">返回	主页面</a>
		</div>
	</div>
</form>
</div>
</body>
</html>

下面是Controller ,之前使用eclipse,HttpServletResponse是new出来的,在idea中不用使用这种方法,直接给参数就好,jsp那部分就给name=“money”,把他传过来,也可以写死的

@RequestMapping(value = "alipay")
public void alipay(@SessionAttribute("mem") Member m, HttpServletResponse response ,Integer money) {
// 获得初始化的AlipayClient
AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id,
AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key,
AlipayConfig.sign_type);

// 设置请求参数
AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
alipayRequest.setReturnUrl(AlipayConfig.return_url);
alipayRequest.setNotifyUrl(AlipayConfig.notify_url);

// 商户订单号,商户网站订单系统中唯一订单号,必填,uuid是全球唯一编码
String out_trade_no = m.getId() + "_" + UUID.randomUUID().toString();
// 付款金额,必填
String total_amount =money+ "";
// 订单名称,必填(写什么随意)
String subject = "sss";
// 商品描述,可空(写什么随意)
String body = "mm";

alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\"," + "\"total_amount\":\"" + total_amount
+ "\"," + "\"subject\":\"" + subject + "\"," + "\"body\":\"" + body + "\","
+ "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
// 请求(加密)
String result;
try {
		result = alipayClient.pageExecute(alipayRequest).getBody();
	
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html");
		PrintWriter out;
		// 输出
		out = response.getWriter();
		out.println(result);
	} catch (Exception e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
	}
}

好了,有什么不懂得就评论,我来帮你解答

猜你喜欢

转载自blog.csdn.net/weixin_42617600/article/details/85313341
今日推荐