• java对接网银支付案例


    =================提交表单jsp,http://www.cnblogs.com/qgc88================================

    <%@page contentType="text/html;charset=gb2312" language="java"%>
    <!-- import="java.net.URLEncoder" http://www.cnblogs.com/qgc88 -->
    <%@page session="true"
        import="com.gnete.paymentgateway.PayUtils,java.text.SimpleDateFormat,java.util.Date"
        %>
     
    <html>

    <table border=1 bordercolor="ccccff" bgcolor="ccddee" align="center">
        <%
            //定义变量
            String MerId, OrderNo, OrderAmount, CurrCode, BankCode,OrderType, nOrderType, CallBackUrl, ResultMode, Reserved01, Reserved02, SourceText, PKey, SignedMsg, EntryMode;
            boolean ret;
            PKey = "12hi60ohxxxxxxxxxxxxguz";//公司申请的key,http://www.cnblogs.com/qgc88
            SignedMsg = "";

            MerId="xxx"; //公司申请的商户ID参数,3位数,http://www.cnblogs.com/qgc88
            System.out.println("商户ID: " + MerId + "<br>");
            Date myDate = new Date();
            OrderNo = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()); //商户订单号,http://www.cnblogs.com/qgc88
            
            System.out.println("商户订单号不超过20位: " + OrderNo + "<br>");
            OrderAmount = "100.01"; //订单金额,格式:元.角分
            System.out.println("订单金额: " + OrderAmount.toString() + "<br>");
            CurrCode = "CNY"; //货币代码,值为:CNY
            System.out.println("货币代码: " + CurrCode + "<br>");
            CallBackUrl = "http://localhost:8080/demo1/successPay.action"; //支付结果接收URL
            
            EntryMode = request.getParameter("EntryMode") == null ? "": request.getParameter("EntryMode"); //支付方式

            BankCode = request.getParameter("nBankCode") == null ? "" : request.getParameter("nBankCode");

            System.out.println("支付结果接收URL: " + CallBackUrl + "<br>");
            ResultMode = "0"; //支付结果返回方式(0-成功和失败支付结果均返回;1-仅返回成功支付结果)
            Reserved01 = "just"; //保留域1
            Reserved02 = "atest"; //保留域2

            OrderType = "B2C";

            //组合成订单原始数据
            
            SourceText = MerId + OrderNo + OrderAmount + CurrCode + OrderType + CallBackUrl+ ResultMode + BankCode + EntryMode + Reserved01+ Reserved02;
            System.out.println("<br>SourceText=" +SourceText);
            
            PayUtils payUtils = new PayUtils();

            SignedMsg = payUtils.md5(SourceText + payUtils.md5(PKey));

            out.println("<br>" + "加密后的信息: " + SignedMsg + "<br>");
            
            ret = payUtils.checkSign(SourceText,SignedMsg,PKey);
            if(ret){
                out.println("<br>"+"验证签名成功!");
            }else{
                out.println("<br>"+"验证签名失败! ");
            }
            //out.println("<br>"+"验证签名: "+"883ef8a28d9602f8705cafb4d01ca754".equals(SignedMsg)  +"<br>");
        %>
      <!--注意,这个url只能是测试帐号调用,如果是正式的帐号请使用:https://www.gnete.com/bin/scripts/OpenVendor/gnete/V36/GetOvOrder.asp -->     
        <form method="post" name="SendOrderForm" action="http://test.gnete.com:8888/Bin/Scripts/OpenVendor/Gnete/V36/GetOvOrder.asp">
     
            <input type='hidden' name='MerId' value='<%=MerId%>'>
            <input type='hidden' name='OrderNo' value='<%=OrderNo%>'>
            <input type='hidden' name='OrderAmount' value='<%=OrderAmount%>'>
            <input type='hidden' name='CurrCode' value='<%=CurrCode%>'>
            <input type='hidden' name='CallBackUrl' value='<%=CallBackUrl%>'>
            <input type='hidden' name='ResultMode' value='<%=ResultMode%>'>
            <input type='hidden' name='OrderType' value='<%=OrderType%>'>
            <input type='hidden' name='BankCode' value='<%=BankCode%>'>
            <input type='hidden' name='EntryMode' value='<%=EntryMode%>'> 0
            <input type='hidden' name='Reserved01' value='<%=Reserved01%>'>
            <input type='hidden' name='Reserved02' value='<%=Reserved02%>'>
            <input type='hidden' name='SignMsg' value='<%=SignedMsg%>'>

        <div align="center"><input type="submit" name="Submit" value="提交">
        </div>

        </form>
        
        
        <tr>
        </tr>
    </table>

    </html>

    ===============PayConf.java工具类,http://www.cnblogs.com/qgc88================================


    /**,http://www.cnblogs.com/qgc88
     * 名称:支付配置类
     * 功能:配置类
     * 类属性:公共类
     * 版本:1.0
     * 日期:2012-10-23
     * 作者:银联网络互联网团队
     * 版权:银联网络
     * 说明:以下代码只是为了方便商户测试而提供的样例代码,商户可以根据自己网站的需要,按照技术文档编写,并非一定要使用该代码。该代码仅供参考。
     * */

    public class PayConf {
        
        //编码方式
        public final static String charset="GBK";
        //加密方式
        public final static String signType="MD5";
        
    }

    ===============PayUtils.java工具类,http://www.cnblogs.com/qgc88================================


    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.IOException;
    import java.io.UnsupportedEncodingException;
    import java.net.URL;
    import java.net.URLConnection;
    import java.security.MessageDigest;
    import java.security.NoSuchAlgorithmException;
    import java.util.LinkedList;
    import java.util.ListIterator;
    import org.apache.commons.httpclient.methods.PostMethod;


    /**,http://www.cnblogs.com/qgc88
     * 名称:支付工具类
     * 功能:工具类,可以生成付款表单等
     * 类属性:公共类
     * 版本:1.0
     * 日期:2012-10-23
     * 作者:银联网络互联网团队
     * 版权:银联网络
     * 说明:以下代码只是为了方便商户测试而提供的样例代码,商户可以根据自己网站的需要,按照技术文档编写,并非一定要使用该代码。该代码仅供参考。
     * */
    public class PayUtils {
        /**
         * 验证签名
         * @param sourceText
         * @param signature
         * @param strPKey
         * @return
         */
        public  boolean checkSign(String SourceMsg, String signature, String strPKey) {
            String strKey;
            
            if(SourceMsg == null) return false;
            if(signature == null) return false;  
            if(strPKey == null) return false;
            System.out.println("SourceMsg="+SourceMsg);
            strKey = md5(strPKey);
            strKey = SourceMsg +strKey;
            System.out.println("strKey="+strKey);
            strKey = md5(strKey);
            System.out.println("strKey="+strKey);
            System.out.println("signature="+signature);
            if(signature.equals(strKey)){
                return true;
            }else{
                return false;
            }
            
        }
        

        /**
         * get the md5 hash of a string
         *
         * @param str
         * @return
         */
        public String md5(String str) {

            if (str == null) {
                return null;
            }

            MessageDigest messageDigest = null;

            try {
                messageDigest = MessageDigest.getInstance(PayConf.signType);
                messageDigest.reset();
                messageDigest.update(str.getBytes(PayConf.charset));
            } catch (NoSuchAlgorithmException e) {
                
                return str;
            } catch (UnsupportedEncodingException e) {
                return str;
            }

            byte[] byteArray = messageDigest.digest();

            StringBuffer md5StrBuff = new StringBuffer();

            for (int i = 0; i < byteArray.length; i++) {
                if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
                    md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));
                else
                    md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
            }

            return md5StrBuff.toString();
        }
        

        // Clean up resources
        public void destroy() {
        }


        /**
         * 查询方法
         * @param strURL
         * @param req
         * @return
         */
        public String doPostQueryCmd(String strURL, String req) {
            String result = null;
            BufferedInputStream in = null;
            BufferedOutputStream out = null;
            try {
                URL url = new URL(strURL);
                URLConnection con = url.openConnection();
                con.setUseCaches(false);
                con.setDoInput(true);
                con.setDoOutput(true);
                out = new BufferedOutputStream(con.getOutputStream());
                System.out.println("Request:=="+req);
                byte outBuf[] = req.getBytes(PayConf.charset);
                out.write(outBuf);
                out.close();
                in = new BufferedInputStream(con.getInputStream());
                result = ReadByteStream(in);
            } catch (Exception ex) {
                System.out.print(ex);
                return "";
            } finally {
                if (out != null) {
                    try {
                        out.close();
                    } catch (IOException e) {
                    }
                }
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                    }
                }
            }
            if (result == null)
                return "";
            else
                return result;
        }


        private static String ReadByteStream(BufferedInputStream in) throws IOException {
            LinkedList<Mybuf> bufList = new LinkedList<Mybuf>();
            int size = 0;
            byte buf[];
            do {
                buf = new byte[128];
                int num = in.read(buf);
                if (num == -1)
                    break;
                size += num;
                bufList.add(new Mybuf(buf, num));
            } while (true);
            buf = new byte[size];
            int pos = 0;
            for (ListIterator<Mybuf> p = bufList.listIterator(); p.hasNext();) {
                Mybuf b = p.next();
                for (int i = 0; i < b.size;) {
                    buf[pos] = b.buf[i];
                    i++;
                    pos++;
                }

            }

            return new String(buf,PayConf.charset);
        }
        


    }

     
    class UTF8PostMethod extends PostMethod{   
        public UTF8PostMethod(String url){   
            super(url);   
        }   
        @Override  
        public String getRequestCharSet() {   
            return "UTF-8";   
        }   
    }

    class GBKPostMethod extends PostMethod{   
        public GBKPostMethod(String url){   
            super(url);   
        }   
        @Override  
        public String getRequestCharSet() {   
            return "GBK";   
        }   
    }

    class Mybuf
    {

        public byte buf[];
        public int size;

        public Mybuf(byte b[], int s)
        {
            buf = b;
            size = s;
        }
    }


  • 相关阅读:
    1052. 爱生气的书店老板
    766. 托普利茨矩阵
    643.子数组的最大平均数I
    450. 删除二叉搜索树中的节点
    1489.找到最小生成树里的关键边和伪关键边
    839相似字符串
    1631.最小体力消耗路径
    SnowFlake雪花算法源码分析&灵活改造,常见分布式ID生成解决方案
    【目标检测】三、Faster R-CNN与R-FCN
    【目标检测】二、Fast R-CNN与SVD
  • 原文地址:https://www.cnblogs.com/qgc88/p/4570900.html
Copyright © 2020-2023  润新知