[Java] 企业支付宝账号开发接口教程SpringMVC+JSP) →→→→→进入此内容的聊天室

来自 , 2019-09-22, 写在 Java, 查看 116 次.
URL http://www.code666.cn/view/287e03db
  1. Date date = new Date();
  2.         //支付类型  必填 不必修改
  3.         String payment_type = "1";
  4.         
  5.         //服务器异步通知页面路径
  6.         //需http://格式的完整路径,不能加?id=123这类自定义参数
  7.         String notify_url = "http://localhost:8080/alipay/async";
  8.         
  9.         //页面跳转同步通知页面路径
  10.         //需http://格式的完整路径,不能加?id=123这类自定义参数,不能写成http://localhost/
  11.         String return_url = "http://192.168.0.222:8080/alipay/return_url";
  12.         
  13.         //订单名称
  14.         //必填
  15. //        String subject = new String(request.getParameter("WIDsubject").getBytes("ISO-8859-1"),"UTF-8");
  16.         String subject = new String("测试商品".getBytes("ISO-8859-1"),"UTF-8");
  17.         
  18.         //付款金额
  19.         //必填
  20. //        String total_fee = new String(request.getParameter("WIDtotal_fee").getBytes("ISO-8859-1"),"UTF-8");
  21.         String total_fee = "0.01";
  22.         
  23.         
  24.         Map<String, String> sParaTemp = new HashMap<String, String>();
  25.         sParaTemp.put("service""create_direct_pay_by_user");//接口服务----即时到账
  26.         sParaTemp.put("partner", AlipayConfig.partner);//支付宝PID
  27.         sParaTemp.put("_input_charset", AlipayConfig.input_charset);//统一编码
  28.         sParaTemp.put("payment_type", payment_type);//支付类型
  29.         sParaTemp.put("notify_url", notify_url);//异步通知页面
  30.         sParaTemp.put("return_url", return_url);//页面跳转同步通知页面
  31.         sParaTemp.put("seller_email", Constants.SELLER_EMAIL);//卖家支付宝账号
  32.         sParaTemp.put("out_trade_no", date.getTime()+payment_type);//商品订单编号
  33.         sParaTemp.put("subject", subject);//商品名称
  34.         sParaTemp.put("total_fee", total_fee);//价格
  35.         
  36.         //建立请求
  37.         String sHtmlText = AlipaySubmit.buildRequest(sParaTemp, "get""立即付款");
  38.         request.setAttribute("sHtmlText", sHtmlText);
  39.         
  40.         return null;
  41.  
  42.  
  43.  
  44.  
  45.  
  46.         Map<String , String> params = new HashMap<String, String>();
  47.         Map requestParams = request.getParameterMap();
  48.         for (Iterator iter = requestParams.keySet().iterator(); iter.hasNext();) {
  49.             String name = (String) iter.next();
  50.             String[] values = (String[]) requestParams.get(name);
  51.             String valueStr = "";
  52.             for (int i = 0; i < values.length; i++) {
  53.                 valueStr = (== values.length - 1 )? valueStr + values[i]:valueStr+values[i]+",";
  54.             }
  55.             params.put(name, valueStr);
  56.         }
  57.         String tradeNo = request.getParameter("out_trade_no");
  58.         String tradeStatus = request.getParameter("trade_status");
  59.         if(AlipayNotify.verify(params)){
  60.             if(tradeStatus.equals("TRADE_FINISHED") || tradeStatus.equals("TRADE_SUCCESS")){
  61.                 System.out.println("ok!>>>>>>>>" + tradeNo);
  62.             }
  63.             return "success";
  64.         }else{
  65.             return "fail";
  66.         }
  67.     }
  68.  
  69.  
  70.  
  71.  
  72.  
  73. /**
  74.  * 支付宝Controller
  75.  * @author 宗潇帅
  76.  * @修改日期 2014-9-25下午1:29:53
  77.  */
  78. @Controller
  79. @RequestMapping(value="/aliapi")
  80. public class AlipayApiController {
  81.  
  82.     private static Log log = LogFactory.getLog(AlipayApiController.class);
  83.     
  84.     @RequestMapping(value="/index")
  85.     public String index(HttpServletRequest request,
  86.             HttpServletResponse response){
  87.                 return "web/pay/fund";//付款的页面。本页面是为了测试而使用的
  88.     }
  89.     
  90.     @RequestMapping(value = "/deposit", method = RequestMethod.POST,produces = "application/json")  
  91.     public String deposit(HttpServletRequest request,HttpServletResponse response,Model model) throws Exception { 
  92. //        PrintWriter out = response.getWriter();
  93.         String result = "";
  94.         Date date = new Date();  
  95.         // 支付类型  
  96.         // 必填,不能修改  
  97.         String payment_type = "1";  
  98.         // 服务器异步通知页面路径  
  99.         // 需http://格式的完整路径,不能加?id=123这类自定义参数  
  100.         String notify_url = "http://192.168.0.222:8080/live/aliapi/async";  
  101.         // 页面跳转同步通知页面路径  
  102.         // 需http://格式的完整路径,不能加?id=123这类自定义参数,不能写成http://localhost/  
  103.         String return_url = "http://192.168.0.222:8080/live/aliapi/return_url";  
  104.         // 商户订单号.  
  105.         // 商户网站订单系统中唯一订单号,必填  
  106.         //String out_trade_no = date.getTime() + "";  
  107.         // 订单名称  
  108.         // 必填  
  109.         String subject = "充值测试";  
  110.         // 防钓鱼时间戳  
  111.         // 若要使用请调用类文件submit中的query_timestamp函数  
  112.         String anti_phishing_key = "";  
  113.         // 客户端的IP地址  
  114.         // 非局域网的外网IP地址,如:221.0.0.1  
  115.         String exter_invoke_ip = "";  
  116.           
  117.           
  118.         String total_fee = ServletRequestUtils.getStringParameter(request, "amount","");
  119.         
  120.         String body = ServletRequestUtils.getStringParameter(request, "body","test");
  121.         //商品展示地址
  122.         String show_url = ServletRequestUtils.getStringParameter(request, "show_url","http://www.elve.cn");
  123.         //需以http://开头的完整路径,例如:http://www.xxx.com/myorder.html
  124.  
  125.         
  126.         
  127.         
  128.         Map<String, String> sParaTemp = new HashMap<String, String>();
  129.         sParaTemp.put("service""create_direct_pay_by_user");//接口服务----即时到账
  130.         sParaTemp.put("partner", AlipayConfig.partner);//支付宝PID
  131.         sParaTemp.put("_input_charset", AlipayConfig.input_charset);//统一编码
  132.         sParaTemp.put("payment_type", payment_type);//支付类型
  133.         sParaTemp.put("notify_url", notify_url);//异步通知页面
  134.         sParaTemp.put("return_url", return_url);//页面跳转同步通知页面
  135.         sParaTemp.put("seller_email", Constants.SELLER_EMAIL);//卖家支付宝账号
  136.         sParaTemp.put("out_trade_no", date.getTime()+payment_type);//商品订单编号
  137.         sParaTemp.put("subject", subject);//商品名称
  138.         sParaTemp.put("total_fee", total_fee);//价格
  139.         sParaTemp.put("body", body);
  140.         sParaTemp.put("show_url", show_url);
  141.         sParaTemp.put("anti_phishing_key", anti_phishing_key);
  142.         sParaTemp.put("exter_invoke_ip", exter_invoke_ip);
  143.          
  144.         //建立请求
  145.         try {
  146.             String sHtmlText = AlipaySubmit.buildRequest(sParaTemp,"post","确认");  
  147.             Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
  148.             String s = gson.toJson(sHtmlText);
  149.             model.addAttribute("sHtmlText", s);
  150.             request.setAttribute("sHtmlText", s);
  151.             result = "{\"success\":true,\"msg\":\"跳转成功\"}";
  152.             StringUtil.writeToWeb(sHtmlText, "html", response);
  153.             return null;
  154.         } catch (Exception e) {
  155.             if(log.isErrorEnabled()){
  156.                 log.error("ddddddddddddddddddddd");
  157.             }
  158.             result = "{\"success\":false,\"msg\":\"跳转失败,请稍候再试!\"}";
  159.             StringUtil.writeToWeb(result, "html", response);
  160.             return null;
  161.         }
  162.     }  
  163.     /**
  164.      * 同步通知的页面的Controller
  165.      * @param request
  166.      * @param response
  167.      * @return
  168.      * @author 宗潇帅
  169.      */
  170.     @RequestMapping(value="/return_url")
  171.     public String Return_url(HttpServletRequest request,HttpServletResponse response){
  172.         return "web/pay/success";
  173.     }
  174.     /**
  175.      * 异步通知付款状态的Controller
  176.      * @param request
  177.      * @param response
  178.      * @return
  179.      * @author 宗潇帅
  180.      */
  181.     @SuppressWarnings("rawtypes")
  182.     @RequestMapping(value="/async",method = RequestMethod.GET)
  183.     public String async(HttpServletRequest request,
  184.             HttpServletResponse response){
  185.          Map<String,String> params = new HashMap<String,String>();  
  186.             Map requestParams = request.getParameterMap();  
  187.             for (Iterator iter = requestParams.keySet().iterator(); iter.hasNext();) {  
  188.                 String name = (String) iter.next();  
  189.                 String[] values = (String[]) requestParams.get(name);  
  190.                 String valueStr = "";  
  191.                 for (int i = 0; i < values.length; i++) {  
  192.                     valueStr = (== values.length - 1) ? valueStr + values[i]: valueStr + values[i] + ",";  
  193.                 }  
  194.                 params.put(name, valueStr);  
  195.             }  
  196.             String tradeNo = request.getParameter("out_trade_no");  
  197.             String tradeStatus = request.getParameter("trade_status");  
  198.             //String notifyId = request.getParameter("notify_id");  
  199.             if(AlipayNotify.verify(params)){//验证成功  
  200.                 if(tradeStatus.equals("TRADE_FINISHED") || tradeStatus.equals("TRADE_SUCCESS")) {  
  201.                     //要写的逻辑。自己按自己的要求写
  202.                     log.error("ok.......");
  203.                     System.out.println(">>>>>充值成功" + tradeNo);  
  204.                 }  
  205.                 return "web/pay/success";  
  206.             }else{//验证失败  
  207.                 return "web/pay/fail";  
  208.             }  
  209.         
  210.     }
  211. }
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218. <%@ page contentType="text/html;charset=UTF-8" %>
  219. <!DOCTYPE html>
  220. <html xmlns="http://www.w3.org/1999/xhtml">
  221. <head>
  222. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  223. <%@ include file="/common/webMeta.jsp"%>
  224. <title>www.elve.cn</title>
  225. <meta content="www.elve.cn" name="keywords" />
  226. <meta content="www.elve.cn" name="description" />
  227. </head>
  228. <script type="text/javascript">
  229.     $(function(){
  230.     $("#amount").focus();
  231.     $("#doSubmit").click(function(){
  232.         form1.submit();
  233.     });
  234. });
  235. </script>
  236. <body>
  237.  
  238.     <form id="form1" action= "${ctx }/aliapi/deposit" method="post" target="_blank">  
  239.                 <table cellpadding="10">  
  240.                     <tr>  
  241.                         <td>充值测试</td>  
  242.                         <td class="balance" id="userBalance"></td>  
  243.                     </tr>  
  244.                     <tr>  
  245.                         <td><class="zfb"></i></td>  
  246.                         <td style="padding-bottom: 0px;">亲爱的<span  
  247.                             class="suppliment_user" id="suppliment_user"></span>,您可以使用支付宝充值积善分,请填写以下信息  
  248.                         </td>  
  249.                     </tr>  
  250.                     <tr>  
  251.                         <td></td>  
  252.                         <td>
  253.                                 金额<input type="text" name="amount" id="amount">
  254.                         </td>  
  255.                     </tr>  
  256.   
  257.                     <tr>  
  258.                         <td></td>  
  259.                         <td><a href="javascript:void(0);" id="doSubmit" class="blank_btn">确认</a></td>  
  260.                     </tr>  
  261.                 </table>  
  262.     </form>  
  263. </body>
  264. </html>
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  

回复 "企业支付宝账号开发接口教程SpringMVC+JSP)"

这儿你可以回复上面这条便签

captcha