通过易宝实现网上支付

 

网上支付一般有两种支付方式,一种是直接与银行对接的支付方式,另一种与第三方公司对接的方式i,比如易宝等等。第一种一般是现金流量较大的大公司使用。这里采用的是第二种。

第一步实现订单界面

<form action="servlet/PaymentSendServlet" method="POST">  
    <table align="center" width="600" border="6" cellspacing="0" cellpadding="2">  
        <tr>  
            <td align="center" colspan="4" >  
                <b>订单号:</b><input type="text" name="orderID">      
                <b>应付金额:¥</b><input type="text" name="amount" size="6"><b></b>  
            </td>  
        </tr>  
        <tr>  
            <td colspan="4"> </td>  
        </tr>  
        <tr>  
            <td colspan="4" >请选择在线支付银行</td>  
        </tr>  
        <tr>  
            <td height="25" width="24%"><input type="radio" name="pd_FrpId" value="ICBC-NET">工商银行</td>  
            <td height="25" width="24%"><input type="radio" name="pd_FrpId" value="CMBCHINA-NET">招商银行</td>  
            <td height="25" width="24%"><input type="radio" name="pd_FrpId" value="ABC-NET">农业银行</td>  
            <td height="25" width="28%"><input type="radio" name="pd_FrpId" value="CCB-NET">建设银行</td>  
        </tr>  
       
        <tr>  
            <td colspan="4"> </td>  
        </tr>  
        <tr>  
            <td colspan="4" align="center" ><input type="submit" value="  确认支付  "/></td>  
        </tr>  
    </table>  
</form>  

第二步将信息转化为易宝公司接口所要求的数据格式。

这里需要一个商家号和相对应的密钥。(这里的商户号是传智播客的一位老师的,支付时请注意)

主要代码:

request.setCharacterEncoding("GBK");  
        
        //商户编号  
        String accountID = "10001126856";  
        //密钥  
        String keyValue = "69cl522AV6q613Ii4W6u8K6XuW8vM1N6bFgyv769220IuYe9u37N4y7rI4Pl";  
        //商户接收支付成功后返回地址  
        String accountCallbackURL = "http://127.0.0.1:8080/zhifu/PaymentResultServlet";  
          
        String orderID = request.getParameter("orderID");        //获取订单号  
        String amount = request.getParameter("amount");          //获取支付金额  
        String accountBankID = request.getParameter("pd_FrpId"); //获取用户所选择的银行  
        String businessType = "Buy";  //业务类型。Buy为在线支付  
        String currency = "CNY";      //交易币种。CNY为人民币  
        String productDesc = "";      //商品描述  
        String productCategory = "";  //商品种类  
        String productID = "";        //商品ID  
        String addressFlag = "0";     //送货地址。0为不需要,1为需要  
        String accountMoreInfo = "";  //商户扩展信息  
        String pr_NeedResponse = "0"; //应答机制  
          
        String md5hmac = zhifuUtil.buildHmac(  
                businessType, accountID, orderID, amount, currency, productID, productCategory,  
                productDesc, accountCallbackURL, addressFlag, accountMoreInfo, accountBankID,  
                pr_NeedResponse, keyValue);  
          
        request.setAttribute("businessType", businessType);  
        request.setAttribute("accountID", accountID);  
        request.setAttribute("orderID", orderID);  
        request.setAttribute("amount", amount);  
        request.setAttribute("currency", currency);  
        request.setAttribute("productID", productID);  
        request.setAttribute("productCategory", productCategory);  
        request.setAttribute("productDesc", productDesc);  
        request.setAttribute("accountCallbackURL", accountCallbackURL);  
        request.setAttribute("addressFlag", addressFlag);  
        request.setAttribute("accountMoreInfo", accountMoreInfo);  
        request.setAttribute("accountBankID", accountBankID);  
        request.setAttribute("needResponse", pr_NeedResponse);  
        request.setAttribute("md5hmac", md5hmac);  

将数据处理后返回前段jsp中,并以数据隐藏的表单形式将信息提交到易宝接口(注:上述需要易宝的一个加密算法MD5+hmac)

<form action="https://www.yeepay.com/app-merchant-proxy/node" method="POST" name="yeepay">  
        <!-- 以下hidden中的name值为易宝支付规范的固定命名和顺序 -->  
        <input type='hidden' name='p0_Cmd' value="${businessType}">  
        <input type='hidden' name='p1_MerId' value="${accountID}">  
        <input type='hidden' name='p2_Order' value="${orderID}">  
        <input type='hidden' name='p3_Amt' value="${amount}">  
        <input type='hidden' name='p4_Cur' value="${currency}">  
        <input type='hidden' name='p5_Pid' value="${productID}">  
        <input type='hidden' name='p6_Pcat' value="${productCategory}">  
        <input type='hidden' name='p7_Pdesc' value="${productDesc}">  
        <input type='hidden' name='p8_Url' value="${accountCallbackURL}">  
        <input type='hidden' name='p9_SAF' value="${addressFlag}">  
        <input type='hidden' name='pa_MP' value="${accountMoreInfo}">  
        <input type='hidden' name='pd_FrpId' value="${accountBankID}">  
        <input type="hidden" name='pr_NeedResponse' value="${needResponse}">  
        <input type='hidden' name='hmac' value="${md5hmac}">  
        <input type="submit" value="确认支付">
    </form>  

 

点击确定按钮提交表单。进入易宝的支付接口界面:

最后完成支付后,易宝会返回一组数据,最重要的是扣款结果若返回为1:扣款成功,否则扣款失败。我们对得到的数据进行判断即可。

posted on 2016-08-17 10:35  bingoing  阅读(542)  评论(0编辑  收藏  举报

导航