package com.tenpay;

import java.io.IOException;
import java.io.PrintWriter;

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

/**
 * 中介担保应答类
 * ============================================================================
 * api说明：
 * getKey()/setKey(),获取/设置密钥
 * getParameter()/setParameter(),获取/设置参数值
 * getAllParameters(),获取所有参数
 * isTenpaySign(),是否财付通签名,true:是 false:否
 * doShow(),显示处理结果
 * getDebugInfo(),获取debug信息
 * 
 * ============================================================================
 *
 */
public class MediPayResponseHandler extends ResponseHandler {

	public MediPayResponseHandler(HttpServletRequest request,
			HttpServletResponse response) {
		super(request, response);
	}
	
	/**
	 * 打印meta值,告诉财付通处理成功.
	 * @throws IOException
	 */
	public void doShow() throws IOException {
		String strHtml = "<html><head>\r\n" +
		"<meta name=\"TENCENT_ONLINE_PAYMENT\" content=\"China TENCENT\">\r\n" +
		"</head><body></body></html>";
		
		PrintWriter out = this.getHttpServletResponse().getWriter();
		out.println(strHtml);
		out.flush();
		out.close();
	}

	/**
	 * 签名规则,按字母a-z排序,遇到空值不参加签名
	 * @return boolean
	 */
	public boolean isTenpaySign() {
		String[] signParameterArray = {
				"attach",
				"buyer_id",
				"cft_tid",
				"chnid",
				"cmdno",
				"mch_vno",
				"retcode",
				"seller",
				"status",
				"total_fee",
				"trade_price",
				"transport_fee",
				"version"
		};
		
		//按a-z升序排序
		java.util.Arrays.sort(signParameterArray);
		
		return this.isTenpaySign(signParameterArray);
		
	}
	
}
