package cn.pluss.platform.controller.test;

import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.pay.ApiPayService;
import cn.pluss.platform.util.IpUtils;
import cn.pluss.platform.util.MD5Util;
import cn.pluss.platform.util.SignUtils;
import cn.pluss.platform.util.StringUtil;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;


@Controller
@RequestMapping(value = "/test")
public class HelloController {

    @Resource
    private ApiPayService apiPayService;

    @RequestMapping(value = "/callbackV1")
    @ResponseBody
    public JSONObject callbackV1(){
        JSONObject result = new JSONObject();
        result.put("code","SUCCESS");
        result.put("msg","成功");
        return result;
    }

    @RequestMapping(value = "/callbackV2")
    @ResponseBody
    public String callbackV2(){
        return "SUCCESS";
    }

    @RequestMapping(value = "testPay")
    @ResponseBody
    public Result<Object> payTest(Double payAmt, HttpServletRequest request){
        String key = "4c8d277b0f274604af68e590cc0b3e6b";
        JSONObject params = new JSONObject();
        params.put("mercOrderNo","TEST100"+ StringUtil.genRandomNum(20));
        params.put("merchantCode","M800202107223018811");
        params.put("method","trans_wap_pay");
        params.put("payAmt", BigDecimal.valueOf(payAmt).setScale(2,BigDecimal.ROUND_HALF_UP));
        params.put("ip","171.83.125.71");
        params.put("notifyUrl","http://bzgcrystal.natapp1.cc/wap/test/callbackV1");
        params.put("payWay","WXZF");
        params.put("subject","测试");
        params.put("returnUrl","http://www.baidu.com");
        String sigStr = SignUtils.getSignContent(params)+"&key="+key;
        String sign = MD5Util.MD5Encode(sigStr, "UTF-8");
        params.put("sign",sign);
        JSONObject pay = apiPayService.pay(params.toString(), IpUtils.getIpAddr(request));
        return ResultGenerator.genSuccessResult("获取成功",pay.getJSONObject("data").getString("orderNumber"));
    }
}
