package com.chinaums.netpay.demo;
/**
* Created by faliny on 2017/8/31.
*/
import com.chinaums.netpay.demo.domain.Goods;
import com.chinaums.netpay.demo.util.NotifyUtilTest;
import com.chinaums.netpay.demo.util.Util;
//import net.sf.json.JSON;
import net.sf.json.JSONObject;
import com.alibaba.fastjson.JSON;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.*;
import static com.chinaums.netpay.demo.util.Util.makeSign;
@Controller
@RequestMapping("/minipay")
public class MiniPayDemo {
//读取资源配置参数
@Value("${url}")
private String APIurl;
@Value("${mid}")
private String mid;
@Value("${tid}")
private String tid;
@Value("${instMid}")
private String instMid;
@Value("${msgSrc}")
private String msgSrc;
@Value("${key}")
private String md5Key;
@Value("${msgType_refund}")
private String msgType_refund;
@Value("${msgType_secureCancel}")
private String msgType_secureCancel;
@Value("${msgType_secureComplete}")
private String msgType_secureComplete;
@Value("${msgType_close}")
private String msgType_close;
@Value("${msgType_query}")
private String msgType_query;
@Value("${msgType_order}")
private String msgType_order;
final private static String msgType = "WXPay.jsPay";
final private static String msgSrcId = "3194";
final private static String apiUrl_makeOrder = "https://qr-test2.chinaums.com/netpay-portal/webpay/pay.do";
final private static String notifyUrl = "http://172.27.49.240:8080/publicpay/notifyUrl.do";
final private static String returnUrl = "http://172.27.49.240:8080/publicpay/returnUrl.do";
//跳转至下单页面
@RequestMapping(value = "/toOrderPage.do",method = RequestMethod.GET)
public String toOrderPage(){
return "/WEB-INF/views/jsp/order.jsp";
}
//跳转交易查询页面
@RequestMapping(value = "/toOrderQueryPage.do",method = RequestMethod.GET)
public String toBillQuery(){
return "/WEB-INF/views/jsp/orderQuery.jsp";
}
//跳转交易退款页面
@RequestMapping(value = "/toRefundPage.do",method = RequestMethod.GET)
public String toRefund(){
return "/WEB-INF/views/jsp/refund.jsp";
}
//跳转担保撤销页面
@RequestMapping(value = "/toCancelPage.do",method = RequestMethod.GET)
public String toCancel(){
return "/WEB-INF/views/jsp/secureCancel.jsp";
}
//跳转担保完成页面
@RequestMapping(value = "/toSecureCompletePage.do",method = RequestMethod.GET)
public String toSecureCompletePage(){
return "/WEB-INF/views/jsp/secureComplete.jsp";
}
//跳转担保完成页面
@RequestMapping(value = "/toClosePage.do",method = RequestMethod.GET)
public String toClosePage(){
return "/WEB-INF/views/jsp/close.jsp";
}
/**
* 接收并处理下单请求
*
* @param request
* @param response
* @throws Exception
*/
@RequestMapping(value = "/minipay.do", method = RequestMethod.GET)
public String publicPay(HttpServletRequest request, HttpServletResponse response,@RequestParam(value = "merOrderId",defaultValue = "0") String merOrderId,long totalAmount) throws Exception {
// 准备商品信息
List<Goods> goodsList = new ArrayList<Goods>();
goodsList.add(new Goods("0001", "Goods Name", 1L, 100L, "Auto", "goods body"));
goodsList.add(new Goods("0002", "Goods Name", 2L, 200L, "Auto", "goods body"));
// 组织请求报文,具体参数请参照接口文档,以下仅作模拟
JSONObject json = new JSONObject();
json.put("mid", mid);
json.put("tid", tid);
json.put("msgType", msgType);
json.put("msgSrc", msgSrc);
json.put("instMid", instMid);
json.put("merOrderId", Util.genMerOrderId(msgSrcId));
json.put("totalAmount", 1);
json.put("requestTimestamp", DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
json.put("goods", goodsList);
json.put("notifyUrl", notifyUrl);
json.put("returnUrl", returnUrl);
json.put("secureTransaction", true);
json.put("totalAmount", totalAmount);
json.put("tradeType", "MINI");
json.put("signType", "SHA256");
/* json.put("sceneType", "AND_WAP");
json.put("merAppName", "全民付");
json.put("merAppId", "http://www.chinaums.com");*/
String url = Util.makeOrderRequest(json, md5Key, apiUrl_makeOrder);
response.sendRedirect(url);
return null;
}
/**
* 下单请求模块
*/
//账单查询
@ResponseBody
@RequestMapping(value = "/order.do",method = RequestMethod.POST)
public String order(HttpServletResponse response, @RequestParam(value = "merOrderId",defaultValue = "0") String merOrderId,long totalAmount){
System.out.println("请求参数对象merOrderId{}totalAmount{}:"+merOrderId+" "+totalAmount);
//组织请求报文
JSONObject json = new JSONObject();
json.put("mid", mid);
json.put("tid", tid);
json.put("msgType", msgType_order);
json.put("msgSrc", msgSrc);
json.put("instMid", instMid);
json.put("merOrderId", merOrderId);
json.put("totalAmount", totalAmount);
json.put("tradeType", "MINI");
//是否要在商户系统下单,看商户需求 createBill()
json.put("requestTimestamp", DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
json.put("signType", "SHA256");
Map<String, String> paramsMap = Util.jsonToMap(json);
paramsMap.put("sign", makeSign(md5Key, paramsMap));
System.out.println("paramsMap:"+paramsMap);
String strReqJsonStr = JSON.toJSONString(paramsMap);
System.out.println("strReqJsonStr:"+strReqJsonStr);
//调用银商平台获取二维码接口
HttpURLConnection httpURLConnection = null;
BufferedReader in = null;
PrintWriter out = null;
// OutputStreamWriter out = null;
String resultStr = null;
Map<String,String> resultMap = new HashMap<String,String>();
if (!StringUtils.isNotBlank(APIurl)) {
resultMap.put("errCode","URLFailed");
resultStr = JSONObject.fromObject(resultMap).toString();
return resultStr;
}
try {
URL url = new URL(APIurl);
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.setRequestProperty("Content_Type","application/json");
httpURLConnection.setRequestProperty("Accept_Charset","UTF-8");
httpURLConnection.setRequestProperty("contentType","UTF-8");
//发送POST请求参数
out = new PrintWriter(httpURLConnection.getOutputStream());
// out = new OutputStreamWriter(httpURLConnection.getOutputStream(),"utf-8");