【在线支付】在线支付代码详解

前言:    

   上一篇文章中(【在线支付】在线支付流程分析)说道,为了保证数据安全,网站把源数据和hmac码发送到第三方,那么,在代码中是如何实现的呢?接收到付款成功的消息,又是如何响应的呢?以向易宝支付为例

1、向易宝发送支付请求

导入算法工具类PaymentUtil

import cn.itcast.utils.PaymentUtil;

	public class PayServlet extends HttpServlet {
	
		public void doGet(HttpServletRequest request, HttpServletResponse response)
				throws ServletException, IOException {
	
		//获取的支付信息:ID,交易金额,订单号
		String orderid = request.getParameter("orderid");
		String money = request.getParameter("money");
		String pd_Id = request.getParameter("pd_FrpId");

		//源数据的规范,顺序不能变(参见“易宝支付产品通用接口帮助文档”支付请求参数说明)
		String p0_Cmd = "Buy";  //业务类型
		String p1_MerId = "10001126856";// 商户编号(银行卡号),向此卡号支付
		String p2_Order = orderid;      //商户订单号
		String p3_Amt = money;           // 付款金额
		String p4_Cur = "CNY";          // 交易币种
		String p5_Pid = "";                // 商品名称
		String p6_Pcat = "";             // 商品种类
		String p7_Pdesc = "";           // 商品描述
		String p8_Url = "http://localhost/day20pay/callback";  // 商户接收支付成功数据的地址
                 String p9_SAF = ""; // 送货地址
		String pa_MP = ""; // 商户扩展信息
		String pd_FrpId = this.pd_FrpId;// 支付通道编码
		String pr_NeedResponse = "1"; // 应答机制
		
		//秘钥(在算法工具类PaymentUtil中,需用到秘钥)
		String keyValue = "69cl522AV6q613Ii4W6u8K6XuW8vM1N6bFgyv769220IuYe9u37N4y7rI4Pl";
		//根据源数据生成hmac码
		String hmac = PaymentUtil.buildHmac(p0_Cmd, p1_MerId, p2_Order, p3_Amt, p4_Cur,
				p5_Pid, p6_Pcat, p7_Pdesc, p8_Url, p9_SAF, pa_MP, pd_FrpId,
				pr_NeedResponse, keyValue);
              // 将所有参数 发送给 易宝指定URL
		request.setAttribute("pd_FrpId", pd_FrpId);
		request.setAttribute("p0_Cmd", p0_Cmd);
		request.setAttribute("p1_MerId", p1_MerId);
		request.setAttribute("p2_Order", p2_Order);
		request.setAttribute("p3_Amt", p3_Amt);
		request.setAttribute("p4_Cur", p4_Cur);
		request.setAttribute("p5_Pid", p5_Pid);
		request.setAttribute("p6_Pcat", p6_Pcat);
		request.setAttribute("p7_Pdesc", p7_Pdesc);
		request.setAttribute("p8_Url", p8_Url);
		request.setAttribute("p9_SAF", p9_SAF);
		request.setAttribute("pa_MP", pa_MP);
		request.setAttribute("pr_NeedResponse", pr_NeedResponse);
		request.setAttribute("hmac", hmac);

		request.getRequestDispatcher("/confirm.jsp").forward(request, response);
		
	}

2、付款成功后,回调程序

public class CallbackServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 验证请求来源和数据有效性
		// 阅读支付结果参数说明
		// System.out.println("==============================================");
		String p1_MerId = request.getParameter("p1_MerId");
		String r0_Cmd = request.getParameter("r0_Cmd");
		String r1_Code = request.getParameter("r1_Code");
		String r2_TrxId = request.getParameter("r2_TrxId");
		String r3_Amt = request.getParameter("r3_Amt");
		String r4_Cur = request.getParameter("r4_Cur");
		String r5_Pid = request.getParameter("r5_Pid");
		String r6_Order = request.getParameter("r6_Order");
		String r7_Uid = request.getParameter("r7_Uid");
		String r8_MP = request.getParameter("r8_MP");
		String r9_BType = request.getParameter("r9_BType");
		String rb_BankId = request.getParameter("rb_BankId");
		String ro_BankOrderId = request.getParameter("ro_BankOrderId");
		String rp_PayDate = request.getParameter("rp_PayDate");
		String rq_CardNo = request.getParameter("rq_CardNo");
		String ru_Trxtime = request.getParameter("ru_Trxtime");

		// hmac
		String hmac = request.getParameter("hmac");
		// 利用本地密钥和加密算法 加密数据
		String keyValue = "69cl522AV6q613Ii4W6u8K6XuW8vM1N6bFgyv769220IuYe9u37N4y7rI4Pl";
		boolean isValid = PaymentUtil.verifyCallback(hmac, p1_MerId, r0_Cmd,
				r1_Code, r2_TrxId, r3_Amt, r4_Cur, r5_Pid, r6_Order, r7_Uid,
				r8_MP, r9_BType, keyValue);
		if (isValid) {
			// 有效
			if (r9_BType.equals("1")) {
				// 浏览器重定向
				response.setContentType("text/html;charset=utf-8");
				response.getWriter().println(
						"支付成功!订单号:" + r6_Order + "金额:" + r3_Amt);
			} else if (r9_BType.equals("2")) {
				
				// 服务器点对点,来自于易宝的通知
				System.out.println("收到易宝通知,修改订单状态!");//
				// 回复给易宝success,如果不回复,易宝会一直通知
				response.getWriter().print("success");
			}

		} else {
			throw new RuntimeException("数据被篡改!");
		}
	}

}


   整个支付的思路很简单,发送请求和支付后相应,具体的解释,还是要看代码注释,顺着代码走一遍,就可以理清了。


参考:《网上商城SSH》,在线支付demo



  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 18
    评论
支付宝沙箱环境是用于模拟支付宝正式环境的测试环境,可以进行支付支付的测试。本文将介绍如何使用Spring Boot实现支付宝沙箱支付前后端案例。 1. 创建Spring Boot项目 使用IDEA或Eclipse创建一个新的Spring Boot项目,并添加以下依赖: ``` <dependency> <groupId>com.alipay.sdk</groupId> <artifactId>alipay-sdk-java</artifactId> <version>4.9.5.ALL</version> </dependency> ``` 2. 配置支付宝沙箱环境 在支付宝开放平台注册并创建应用后,进入沙箱环境,获取沙箱环境的AppID、RSA私钥和公钥,并将其配置到项目中。 ``` #支付宝沙箱环境配置 alipay.gateway=https://openapi.alipaydev.com/gateway.do alipay.app-id=应用ID alipay.private-key=应用私钥 alipay.public-key=支付宝公钥 ``` 3. 创建支付支付接口 创建一个支付支付接口,用于生成支付支付的请求参数。 ``` @RestController public class AlipayController { @Autowired private AlipayProperties alipayProperties; @GetMapping("/alipay") public String alipay(HttpServletRequest request, HttpServletResponse response) throws Exception { //实例化客户端 AlipayClient alipayClient = new DefaultAlipayClient(alipayProperties.getGateway(), alipayProperties.getAppId(), alipayProperties.getPrivateKey(), "json", "utf-8", alipayProperties.getPublicKey(), "RSA2"); //创建API对应的request AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest(); //在公共参数中设置回跳和通知地址 alipayRequest.setReturnUrl(alipayProperties.getReturnUrl()); alipayRequest.setNotifyUrl(alipayProperties.getNotifyUrl()); //填充业务参数 alipayRequest.setBizContent("{\"out_trade_no\":\"" + 1 + "\"," + "\"total_amount\":\"" + 0.01 + "\"," + "\"subject\":\"" + "测试商品" + "\"," + "\"body\":\"" + "测试商品详情" + "\"," + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}"); //调用SDK生成表单 String form = alipayClient.pageExecute(alipayRequest).getBody(); //直接将完整的表单html输出到页面 response.setContentType("text/html;charset=utf-8"); response.getWriter().write(form); response.getWriter().flush(); response.getWriter().close(); return null; } } ``` 4. 创建支付宝回调接口 创建一个支付宝回调接口,用于接收支付支付的异步通知。 ``` @RestController public class AlipayNotifyController { @PostMapping("/notify") public String notify(HttpServletRequest request) throws Exception { //获取支付宝POST过来反馈信息 Map<String, String> params = new HashMap<String, String>(); Map<String, String[]> requestParams = request.getParameterMap(); for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext(); ) { String name = iter.next(); String[] values = requestParams.get(name); String valueStr = ""; for (int i = 0; i < values.length; i++) { valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ","; } //乱码解决,这段代码在出现乱码时使用 valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8"); params.put(name, valueStr); } //调用SDK验证签名 boolean signVerified = AlipaySignature.rsaCheckV1(params, alipayProperties.getPublicKey(), "utf-8", "RSA2"); if (signVerified) { //验证成功,处理业务逻辑 return "success"; } else { //验证失败 return "fail"; } } } ``` 5. 创建支付宝前端页面 创建一个支付宝前端页面,用于调用支付支付的接口。 ``` <html> <head> <meta charset="UTF-8"> <title>支付宝沙箱支付</title> </head> <body> <div> <button onclick="alipay()">支付支付</button> </div> <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script> <script> function alipay() { $.get("/alipay", function (data) { $("body").html(data); }) } </script> </body> </html> ``` 6. 测试支付宝沙箱支付 启动Spring Boot项目,访问支付宝前端页面,点击“支付支付”按钮,跳转到支付支付页面,输入支付宝账号和密码进行支付支付成功后,会触发支付宝回调接口中的逻辑。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 18
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小王师傅66

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值