package com.zcloud.service.sms.impl;

import com.zcloud.entity.PageData;
import com.zcloud.service.sms.PayService;
import com.zcloud.util.Const;
import com.zcloud.util.PayUtil;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

@Service
public class PayServiceImpl implements PayService {
	//小程序支付参数
//	public static final String XCX_APP_ID = "wx7ff84f44fb881d41";
//	public static final String XCX_MCH_ID = "1531075881";
//	public static final String XCX_KEY = "qidiankeji888888qidiankeji888888";
	public static final String WX_PAY_UNIFIED_ORDER = "https://api.mch.weixin.qq.com/pay/unifiedorder";
	public static final String TRADE_TYPE_JSAPI = "JSAPI";


	@Override
	public Map<String, String> xcxPayment(PageData pd) throws Exception {
		SortedMap<String, String> resultMap = new TreeMap<String, String>();
        Map<String,String> resMap = this.xcxUnifieldOrder(pd.get("ORDER_BODY").toString(),pd.get("ordernumber").toString(),TRADE_TYPE_JSAPI, Double.parseDouble(pd.get("PRICE").toString()),"",pd.get("time_start").toString());
        System.out.println(JSONObject.fromObject(resMap));
        if("SUCCESS".equals(resMap.get("return_code")) && "SUCCESS".equals(resMap.get("result_code"))){
    		resultMap.put("appId",Const.APPID);
    		resultMap.put("timeStamp", PayUtil.getCurrentTimeStamp());
    		resultMap.put("nonceStr", PayUtil.makeUUID(32));
    		resultMap.put("package", "prepay_id="+resMap.get("prepay_id"));
    		resultMap.put("signType", "MD5");
    		resultMap.put("sign", PayUtil.createSign(resultMap,Const.XCX_KEY));
    		resultMap.put("returnCode", "SUCCESS");
    		resultMap.put("returnMsg", "OK");
    		resultMap.put("code_url", resMap.get("code_url"));
        }else{
        	resultMap.put("returnCode", resMap.get("return_code"));
    		resultMap.put("returnMsg", resMap.get("return_msg"));
		}
		return resultMap;
	}

	/**
	 * 小程序支付统一下单
	 */
	private Map<String,String> xcxUnifieldOrder(String ORDER_BODY,String ordernumber,String tradeType, double payAmount,String openid,String time_start) throws Exception{
    	//封装参数
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		Date startTime = sdf.parse(time_start);// 订单开始时间
		startTime.setTime(startTime.getTime() + 5*60*1000);
		String time_expire = sdf.format(startTime); // 订单结束时间
        SortedMap<String,String> paramMap = new TreeMap<String,String>();
        paramMap.put("appid", Const.APPID);
        paramMap.put("mch_id", Const.XCX_MCH_ID);
        paramMap.put("nonce_str", PayUtil.makeUUID(32));
        paramMap.put("body", ORDER_BODY);
        paramMap.put("out_trade_no", ordernumber);
        paramMap.put("total_fee", PayUtil.moneyToIntegerStr(payAmount));
        paramMap.put("spbill_create_ip", PayUtil.getLocalIp());
        paramMap.put("notify_url", "https://gateway.qhdsafety.com/pay/getnotify");
//        paramMap.put("notify_url", "https://qask.qhdsafety.com/qa-prevention/pay/getnotify");
        paramMap.put("trade_type", "NATIVE");
        paramMap.put("sign_type", "MD5");
        paramMap.put("product_id", "1");
        paramMap.put("receipt", "Y");
        paramMap.put("fee_type", "CNY"); // 默认人民币
        paramMap.put("time_start",time_start);
        paramMap.put("time_expire", time_expire);
        paramMap.put("sign", PayUtil.createSign(paramMap,Const.XCX_KEY));
        //转换为xml
        System.out.println(JSONObject.fromObject(paramMap));
        String xmlData = PayUtil.mapToXml(paramMap);
        System.out.println(xmlData);
        //请求微信后台，获取预支付ID
        String resXml = PayUtil.httpRequest(WX_PAY_UNIFIED_ORDER,"POST", xmlData);
        return PayUtil.xmlStrToMap(resXml);
    }

}
