com.gson.web.WeChatPayServlet.java Source code

Java tutorial

Introduction

Here is the source code for com.gson.web.WeChatPayServlet.java

Source

/**
 * ???(JAVA) SDK
 * (c) 2012-2013 ____? <wmails@126.cn>, MIT Licensed
 * http://www.jeasyuicn.com/wechat
 */
package com.gson.web;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;

import com.gson.WeChat;
import com.gson.oauth.Pay;
import com.gson.util.ConfKit;

/**
 * ?
 * @author L.cm
 * email: 596392912@qq.com
 * site:  http://www.dreamlu.net
 * @date 2014-4-23 ?10:10:36
 */
public class WeChatPayServlet extends HttpServlet {

    private static final long serialVersionUID = 4638321491356022766L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // ?, 5.0 ??
        boolean isweixin = WeChat.isWeiXin(req);
        if (isweixin) {
            String productName = "?001";
            String total_fee = "100";
            String orderid = "123456";

            // package ?------------------------------  //
            Map<String, String> params = new HashMap<String, String>();
            // ????,  
            productName = productName.replaceAll(" ", "");
            productName = productName.length() > 17 ? productName.substring(0, 17) + "..." : productName;

            params.put("body", productName); //???
            params.put("total_fee", total_fee); //??
            params.put("fee_type", "1"); //??,? 1 ?
            params.put("out_trade_no", orderid); //??
            params.put("spbill_create_ip", getIp(req)); // ip
            // ?
            String timeStamp = System.currentTimeMillis() + "";

            String nonceStr = RandomStringUtils.random(8, "123456789"); // 8??
            String packagestring = Pay.getPackage(params); //?
            String paySign = Pay.paySign(timeStamp, nonceStr, packagestring); // ??

            // appId
            req.setAttribute("appid", ConfKit.get("AppId"));
            // timeStamp
            req.setAttribute("timeStamp", timeStamp);
            // nonceStr
            req.setAttribute("nonceStr", nonceStr);
            // package
            req.setAttribute("package", packagestring);
            // paySign
            req.setAttribute("paySign", paySign);
            // 
            req.setAttribute("isweixin", 1);
            req.getRequestDispatcher("/pay.jsp").forward(req, resp);
        } else {
            resp.sendRedirect("/index.jsp");
        }
    }

    /**
     * ?ip
     * @param request
     * @return
     */
    public static String getIp(HttpServletRequest request) {
        if (request == null)
            return "";
        String ip = request.getHeader("X-Requested-For");
        if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("X-Forwarded-For");
        }
        if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
}