package com.iformall.pay;

import java.util.Map;

import com.iformall.utils.HttpUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class WxProfitSharing {
	protected static Logger log = LoggerFactory.getLogger(WxProfitSharing.class);

	// 单次分账接口
	private static final String PROFIT_SHARING_URL = "https://api.mch.weixin.qq.com/secapi/pay/profitsharing";
	// 多次分账接口
	private static final String PROFIT_MULTI_SHARING_URL = "https://api.mch.weixin.qq.com/secapi/pay/multiprofitsharing";
	// 分账完结接口
	private static final String PROFIT_SHARING_FINISH_URL = "https://api.mch.weixin.qq.com/secapi/pay/profitsharingfinish";
	// 查询接口
	private static final String PROFIT_SHARING_QUERY_URL = "https://api.mch.weixin.qq.com/pay/profitsharingquery";
	// 添加分账账户
	private static final String RPOFIT_SHARING_ADD_RECEIVER_URL = "https://api.mch.weixin.qq.com/pay/profitsharingaddreceiver";
	// 删除分账账户
	private static final String RPOFIT_SHARING_REMOVE_RECEIVER_URL = "https://api.mch.weixin.qq.com/pay/profitsharingremovereceiver";


	private WxProfitSharing() {
	}

	/**
	 * 单次分账下单
	 * @param params
	 * @return
	 */
	public static String pushOrder(Map<String, String> params, String certPath, String certPass) {
		return doPostSSL(PROFIT_SHARING_URL, params, certPath, certPass);
	}

	/**
	 * 多次分账下单
	 * @param params
	 * @return
	 */
	public static String pushMultiOrder(Map<String, String> params, String certPath, String certPass) {
		return doPostSSL(PROFIT_MULTI_SHARING_URL, params, certPath, certPass);
	}

	/**
	 * 分账完结
	 * @param params
	 * @return
	 */
	public static String finishOrder(Map<String, String> params, String certPath, String certPass) {
		return doPostSSL(PROFIT_SHARING_FINISH_URL, params, certPath, certPass);
	}

	/**
	 * 分账查询
	 * @param params
	 * @return
	 */
	public static String queryOrder(Map<String, String> params, String certPath, String certPass) {
		return doPostSSL(PROFIT_SHARING_QUERY_URL, params, certPath, certPass);
	}


	/**
	 * 分账接收方添加
	 * @param params
	 * @return
	 */
	public static String addReceiver(Map<String, String> params) {return doPost(RPOFIT_SHARING_ADD_RECEIVER_URL, params); }

	/**
	 * 分账接收方删除
	 * @param params
	 * @return
	 */
	public static String delReceiver(Map<String, String> params) {return doPost(RPOFIT_SHARING_REMOVE_RECEIVER_URL, params); }

	public static String doPost(String url, Map<String, String> params) {
		return HttpUtil.payPost(url, WxPayment.toXml(params));
	}

	public static String doPostSSL(String url, Map<String, String> params, String certPath, String certPass) {
		return HttpUtil.payPostSSL(url, WxPayment.toXml(params), certPath, certPass);
	}
}
