zoukankan      html  css  js  c++  java
  • 支付宝支付之网页支付

    1.支付宝网页支付讲道理很简单,如果下载的sdk管用的话(没错,我使用下载的sdk,官方给出的JAVA版资源,sdk1.5,到了最终也没能把支付完成,可以吊起支付宝,但是一直提示ALI38173,头疼了老久),基本配置就不多少了,按照官方给出的配置流程,完成基本配置,如有不懂,可参考上篇支付宝支付之app支付(在我的文章中)。

    2.配置完成后直接上核心代码

    @RequestMapping(value = "/alipay/trade")
        public ResponseEntity<String> sendAlipay(HttpServletRequest request, HttpServletResponse httpResponse) throws Exception {
            HttpHeaders responseHeaders = new HttpHeaders();
            responseHeaders.set("Content-Type", "application/json;charset=UTF-8");
            
    
            String form1="<form id='alipaysubmit' name='alipaysubmit' action='https://openapi.alipay.com/gateway.do?charset=UTF-8' method='POST'><input type='hidden' name='biz_content' value='{"productCode":"QUICK_WAP_PAY","body":"支付宝公众号充值","subject":"梦想直播充值","out_trade_no":"17021712041001000583","total_amount":"0.01","timeout_express":"1m"}'/><input type='hidden' name='app_id' value='2016122004460499'/><input type='hidden' name='version' value='1.0'/><input type='hidden' name='format' value='json'/><input type='hidden' name='sign_type' value='RSA'/><input type='hidden' name='method' value='alipay.trade.wap.pay'/><input type='hidden' name='timestamp' value='2017-02-17 12:04:10'/><input type='hidden' name='alipay_sdk' value='alipay-sdk-php-20161101'/><input type='hidden' name='notify_url' value='http://api.dreamlive.tv/deposit/notify_alipay5'/><input type='hidden' name='return_url' value='http://api.dreamlive.tv/deposit/notify_alipay5'/><input type='hidden' name='charset' value='UTF-8'/><input type='hidden' name='sign' value='fQAs7/QIQqkWf/0Z7IM9mpHNygx8L5Y/fO5n80l8ueHMPOfsRVptZzHbUE+8gSunm1QTc1E3V1TW0+peA6gS580q3FQrq+b2fkcQf2uueTp/xDuI6V7rRcwiwRWNl0PksRQNrCvzHWl7Ll+QOlumGmgbHMNuGLxld+3Cv7ZP4Vw='/><input type='submit' value='ok' style='display:none;''></form><script>document.forms['alipaysubmit'].submit();</script>";
    
            /**
             * 构造支付参数,form1为示例表单结构,可直接使用
             * */
            String defort_pay_number="15224441122";
            Map<String, String> keyValues = new HashMap<String, String>();
            String current_time=Utils.getCurrentTime();
            keyValues.put("app_id", PayConfigUtils.getApp_id());
            keyValues.put("method", "alipay.trade.wap.pay");
            keyValues.put("charset", "UTF-8");
            keyValues.put("sign_type", "RSA");
            keyValues.put("version", "1.0");
            keyValues.put("timestamp", current_time);
            keyValues.put("biz_content", "{"timeout_express":"30m","product_code":"QUICK_MSECURITY_PAY","total_amount":""+price+"","subject":""+subject+"","body":""+body+"","out_trade_no":"" + defort_pay_number +  ""}");
            keyValues.put("notify_url", PayConfigUtils.getWebUrl()+"alipay/notify");
            keyValues.put("return_url", PayConfigUtils.getWebUrl()+"campaign/76/ddlist");
            String sign=getSign(keyValues,PayConfigUtils.getPrivate_key());
    
            httpResponse.setContentType("text/html;charset=utf-8");
            String form2="<form id='alipaysubmit' name='alipaysubmit' action='https://openapi.alipay.com/gateway.do?charset=UTF-8' method='POST'>
    " +
                    "<input type='hidden' name='biz_content' value='"+keyValues.get("biz_content")+"'/>
    " +
                    "<input type='hidden' name='app_id' value='"+PayConfigUtils.getApp_id()+"'/>
    " +
                    "<input type='hidden' name='version' value='1.0'/>
    " +
                    "<input type='hidden' name='sign_type' value='RSA'/>
    " +
                    "<input type='hidden' name='method' value='alipay.trade.wap.pay'/>
    " +
                    "<input type='hidden' name='timestamp' value='"+current_time+"'/>
    " +
                    "<input type='hidden' name='notify_url' value='"+keyValues.get("notify_url")+"'/>
    " +
                    "<input type='hidden' name='return_url' value='"+keyValues.get("return_url")+"'/>
    " +
                    "<input type='hidden' name='charset' value='utf-8'/>
    " +
                    "<input type='hidden' name='sign' value='"+sign+"'/>
    " +
                    "<input type='submit' value='ok' style='display:none;''></form><script>document.forms['alipaysubmit'].submit();</script>";
            httpResponse.getWriter().write(form2);//直接将完整的表单html输出到页面
            httpResponse.getWriter().flush();
            
           /* String json="";
            JSONObject result=new JSONObject();
            json = ReturnJsonUtil.returnSuccessJsonString(result, "支付!");
            return new ResponseEntity<String>(json, responseHeaders, HttpStatus.OK);*/
    
        }
        public static String getSign(Map<String, String> map, String rsaKey) {
            List<String> keys = new ArrayList<String>(map.keySet());
            // key排序
            Collections.sort(keys);
    
            StringBuilder authInfo = new StringBuilder();
            for (int i = 0; i < keys.size() - 1; i++) {
                String key = keys.get(i);
                String value = map.get(key);
                authInfo.append(buildKeyValue(key, value, false));
                authInfo.append("&");
            }
    
            String tailKey = keys.get(keys.size() - 1);
            String tailValue = map.get(tailKey);
            authInfo.append(buildKeyValue(tailKey, tailValue, false));
    
            String oriSign = SignUtils.sign(authInfo.toString(), rsaKey);
            String encodedSign = "";
    
          /*  try {
                encodedSign = URLEncoder.encode(oriSign, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }*/
            return  oriSign.trim().replace("
    ","");
        }
        /**
         * 拼接键值对
         *
         * @param key
         * @param value
         * @param isEncode
         * @return
         */
        private static String buildKeyValue(String key, String value, boolean isEncode) {
            StringBuilder sb = new StringBuilder();
            sb.append(key);
            sb.append("=");
            if (isEncode) {
                try {
                    sb.append(URLEncoder.encode(value, "UTF-8"));
                } catch (UnsupportedEncodingException e) {
                    sb.append(value);
                }
            } else {
                sb.append(value);
            }
            return sb.toString();
        }
    

    3.支付宝网页支付可以完全由后台完成,所以吊起支付宝的工作不需要前台ajax获取数据,上面代码中的api直接请求即可,链接中传入必要的参数(如支付id,等)(不要使用ajax)! 

  • 相关阅读:
    sed&awk 资料汇总 全是链接
    LeetCode Path 3Sum
    C++ mem_fun
    递归绑定
    查询当天数据
    清除script注入
    防注入查询
    我的最新分页
    群发邮件
    利用缓存
  • 原文地址:https://www.cnblogs.com/kui-technology/p/6433828.html
Copyright © 2011-2022 走看看