赞
踩
1.注册一个沙箱号 网站https://open.alipay.com/platform/home.htm
使用真的支付宝扫描 进去 注册 沙箱支付宝
2.使用支付宝的秘钥生成工具生成个人的私钥,附地址:https://opendocs.alipay.com/open/291/sign
3.将秘钥填入支付宝沙箱中,附地址:https://openhome.alipay.com/platform/appDaily.htm?tab=info
4.编程实现,使用ssm,若需要找maven依赖可在https://mvnrepository.com/找不同版本的依赖
5.pom.xml中引入支付宝依赖(使用ssm也要引入spring、spring与web整合的maven依赖)
6.controller
@Controller public class PayController { private final String APP_ID = "应用的APPID"; private final String APP_PRIVATE_KEY = "生成的应用私钥"; private final String CHARSET = "UTF-8"; private final String ALIPAY_PUBLIC_KEY = "应用私钥"; //这是沙箱接口路径,正式路径为https://openapi.alipay.com/gateway.do private final String GATEWAY_URL ="https://openapi.alipaydev.com/gateway.do"; private final String FORMAT = "JSON"; //签名方式 private final String SIGN_TYPE = "RSA2"; //支付宝异步通知路径,付款完毕后会异步调用本项目的方法,必须为公网地址 private final String NOTIFY_URL = "http://1270..0.1/notifyUrl"; //支付宝同步通知路径,也就是当付款完毕后跳转本项目的页面,可以不是公网地址 private final String RETURN_URL = "http://1270..0.1/returnUrl"; @RequestMapping("/pay") public void alipay(HttpServletResponse httpResponse) throws IOException { System.out.println("进入了pay"); Random r=new Random(); //实例化客户端,填入所需参数 AlipayClient alipayClient = new DefaultAlipayClient(GATEWAY_URL, APP_ID, APP_PRIVATE_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY, SIGN_TYPE); AlipayTradePagePayRequest request = new AlipayTradePagePayRequest(); //在公共参数中设置回跳和通知地址 request.setReturnUrl(RETURN_URL); request.setNotifyUrl(NOTIFY_URL); //商户订单号,商户网站订单系统中唯一订单号,必填 //生成随机Id String out_trade_no = UUID.randomUUID().toString(); //付款金额,必填 String total_amount =Integer.toString(r.nextInt(100)+100); //订单名称,必填 String subject ="奥迪A8 2016款 A8L 60 TFSl quattro豪华型"; //商品描述,可空 String body = "尊敬的会员欢迎购买奥迪A8 2016款 A8L 60 TFSl quattro豪华型"; request.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\"," + "\"total_amount\":\""+ total_amount +"\"," + "\"subject\":\""+ subject +"\"," + "\"body\":\""+ body +"\"," + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}"); String form = ""; try { form = alipayClient.pageExecute(request).getBody(); // 调用SDK生成表单 } catch (AlipayApiException e) { e.printStackTrace(); } httpResponse.setContentType("text/html;charset=" + CHARSET); httpResponse.getWriter().write(form);// 直接将完整的表单html输出到页面 httpResponse.getWriter().flush(); httpResponse.getWriter().close(); } @RequestMapping(value = "/returnUrl", method = RequestMethod.GET) public String returnUrl(HttpServletRequest request, HttpServletResponse response) throws IOException, AlipayApiException { System.out.println("=================================同步回调====================================="); // 获取支付宝GET过来反馈信息 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 = (String) iter.next(); String[] values = (String[]) 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("utf-8"), "utf-8"); params.put(name, valueStr); } System.out.println(params);//查看参数都有哪些 boolean signVerified = AlipaySignature.rsaCheckV1(params, ALIPAY_PUBLIC_KEY, CHARSET, SIGN_TYPE); // 调用SDK验证签名 //验证签名通过 if (signVerified) { // 商户订单号 String out_trade_no = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"), "UTF-8"); // 支付宝交易号 String trade_no = new String(request.getParameter("trade_no").getBytes("ISO-8859-1"), "UTF-8"); // 付款金额 String total_amount = new String(request.getParameter("total_amount").getBytes("ISO-8859-1"), "UTF-8"); System.out.println("商户订单号=" + out_trade_no); System.out.println("支付宝交易号=" + trade_no); System.out.println("付款金额=" + total_amount); //支付成功,修复支付状态 // payService.updateById(Integer.valueOf(out_trade_no)); return "ok";//跳转付款成功页面 } else { return "no";//跳转付款失败页面 } } }
7.使用ssm需要配置前端控制器,在web.xml中
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <!--配置监听器--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--配置全局参数:spring配置文件的位置--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <filter> <filter-name>encoding</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--配置前端控制器--> <servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
8.配置springmvc.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--使用注解:开启注解扫描包--> <context:component-scan base-package="com.igeek.controller"/> <mvc:annotation-driven/> <mvc:default-servlet-handler/> </beans>
8.完成后可直接开启tomcat,网页中进行使用http://localhost:8888/alipayDemo/pay,可作为页面跳转写到项目中
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。