com.roncoo.utils.MerchantApiUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.roncoo.utils.MerchantApiUtil.java

Source

/*
 * Copyright 2015-2102 RonCoo(http://www.roncoo.com) Group.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.roncoo.utils;

import org.apache.commons.lang.StringUtils;

import java.util.*;

/**
 * <b>:API
 * </b>
 * @author  Peter
 * <a href="http://www.roncoo.com">(www.roncoo.com)</a>
 */
public class MerchantApiUtil {

    /**
     * ????
     * @param paramMap  ???
     * @param paySecret ??
     * @return
     */
    public static String getSign(Map<String, Object> paramMap, String paySecret) {
        SortedMap<String, Object> smap = new TreeMap<String, Object>(paramMap);
        StringBuffer stringBuffer = new StringBuffer();
        for (Map.Entry<String, Object> m : smap.entrySet()) {
            Object value = m.getValue();
            if (value != null && StringUtils.isNotBlank(String.valueOf(value))) {
                stringBuffer.append(m.getKey()).append("=").append(m.getValue()).append("&");
            }
        }
        stringBuffer.delete(stringBuffer.length() - 1, stringBuffer.length());

        String argPreSign = stringBuffer.append("&paySecret=").append(paySecret).toString();
        String signStr = MD5Util.encode(argPreSign).toUpperCase();

        return signStr;
    }

    /**
     * ??
     * @param paramMap
     * @return
     */
    public static String getParamStr(Map<String, Object> paramMap) {
        SortedMap<String, Object> smap = new TreeMap<String, Object>(paramMap);
        StringBuffer stringBuffer = new StringBuffer();
        for (Map.Entry<String, Object> m : smap.entrySet()) {
            Object value = m.getValue();
            if (value != null && StringUtils.isNotBlank(String.valueOf(value))) {
                stringBuffer.append(m.getKey()).append("=").append(value).append("&");
            }
        }
        stringBuffer.delete(stringBuffer.length() - 1, stringBuffer.length());

        return stringBuffer.toString();
    }

    /**
     * ???
     * @param paramMap  ???
     * @param paySecret ???
     * @param signStr   ??
     * @return
     */
    public static boolean isRightSign(Map<String, Object> paramMap, String paySecret, String signStr) {

        if (StringUtils.isBlank(signStr)) {
            return false;
        }

        String sign = getSign(paramMap, paySecret);
        if (signStr.equals(sign)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * ?HTML?
     * @param sParaTemp ?
     * @param strMethod ????post?get
     * @param strButtonName 
     * @return ???HTML
     */
    public static String buildRequest(Map<String, Object> sParaTemp, String strMethod, String strButtonName,
            String actionUrl) {
        //?
        List<String> keys = new ArrayList<String>(sParaTemp.keySet());
        StringBuffer sbHtml = new StringBuffer();

        sbHtml.append("<form id=\"rppaysubmit\" name=\"rppaysubmit\" action=\"" + actionUrl + "\" method=\""
                + strMethod + "\">");

        for (int i = 0; i < keys.size(); i++) {
            String name = (String) keys.get(i);
            Object object = sParaTemp.get(name);
            String value = "";

            if (object != null) {
                value = (String) sParaTemp.get(name);
            }

            sbHtml.append("<input type=\"hidden\" name=\"" + name + "\" value=\"" + value + "\"/>");
        }

        //submit???name
        sbHtml.append("<input type=\"submit\" value=\"" + strButtonName + "\" style=\"display:none;\"></form>");
        sbHtml.append("<script>document.forms['rppaysubmit'].submit();</script>");

        return sbHtml.toString();
    }

}