package com.yeshi.fanli.controller.wxpay;
|
|
import java.math.BigDecimal;
|
import java.net.URLEncoder;
|
import java.util.HashMap;
|
import java.util.Iterator;
|
import java.util.Map;
|
|
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletResponse;
|
|
import org.springframework.stereotype.Controller;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.servlet.ModelAndView;
|
import org.yeshi.utils.HttpUtil;
|
import org.yeshi.utils.wx.WXUtil;
|
|
import com.yeshi.fanli.util.StringUtil;
|
|
import net.sf.json.JSONObject;
|
|
@Controller
|
@RequestMapping("pay")
|
public class WXPayController {
|
// 板栗快省
|
private WXAPPInfo app = new WXAPPInfo("wxa99686bb65a9f466", "57390718ddedaa1591f6876cdcf96f43", "1520950211",
|
"XYJkJ2018FAfaodCCx899mLl138rfGVd");
|
|
@RequestMapping("login")
|
public String login() {
|
return "wxpay/login";
|
}
|
|
@RequestMapping("loginCallBack")
|
public void loginCallBack(String code, HttpServletRequest request, HttpServletResponse response) {
|
System.out.println("code:" + code);
|
String url = String.format(
|
"https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code",
|
app.getAppId(), app.getAppSecret(), code);
|
String result = HttpUtil.get(url);
|
System.out.println("result:" + result);
|
JSONObject json = JSONObject.fromObject(result);
|
String openId = json.optString("openid");
|
String orderNo = System.currentTimeMillis() + "";
|
|
double d = 0.1 + 0.5 * Math.random();
|
BigDecimal money = new BigDecimal((int) (d * 100)).divide(new BigDecimal(100));
|
Map<String, String> resultMap = produceOrder(orderNo, money, openId, "板栗快省支付", app);
|
if (resultMap != null) {
|
System.out.println("统一下单成功:" + resultMap.get("prepay_id"));
|
Map<String, String> payParams = new HashMap<>();
|
payParams.put("appId", app.getAppId());
|
payParams.put("timeStamp", System.currentTimeMillis() / 1000 + "");
|
payParams.put("nonceStr", StringUtil.getRandomCode(32));
|
payParams.put("package", "prepay_id=" + resultMap.get("prepay_id"));
|
payParams.put("signType", "MD5");
|
payParams.put("paySign", WXUtil.getSignMD5(payParams, app.getMchKey()));
|
JSONObject jaon = new JSONObject();
|
for (Iterator<String> its = payParams.keySet().iterator(); its.hasNext();) {
|
String key = its.next();
|
jaon.put(key, payParams.get(key));
|
}
|
|
try {
|
response.sendRedirect("pay?data=" + URLEncoder.encode(jaon.toString(), "UTF-8"));
|
} catch (Exception e) {
|
}
|
}
|
}
|
|
@RequestMapping("pay")
|
public ModelAndView pay(String data) {
|
JSONObject jsonObject = JSONObject.fromObject(data);
|
ModelAndView modelAndView = new ModelAndView("wxpay/pay");
|
for (Iterator<String> its = jsonObject.keySet().iterator(); its.hasNext();) {
|
String key = its.next();
|
modelAndView.addObject(key, jsonObject.optString(key));
|
}
|
return modelAndView;
|
}
|
|
@RequestMapping("test")
|
public ModelAndView test() {
|
ModelAndView modelAndView = new ModelAndView("test");
|
WXAPPInfo wxappInfo = new WXAPPInfo();
|
wxappInfo.setAppId("appId");
|
modelAndView.addObject(wxappInfo);
|
// modelAndView.addObject("test", 123123);
|
return modelAndView;
|
}
|
|
private static Map<String, String> produceOrder(String orderNo, BigDecimal fee, String openId, String body,
|
WXAPPInfo info) {
|
Map<String, String> map = new HashMap<String, String>();
|
map.put("appid", info.getAppId());
|
map.put("mch_id", info.getMchId());
|
map.put("nonce_str", StringUtil.getRandomCode(32));
|
map.put("body", body);
|
map.put("out_trade_no", orderNo);
|
map.put("total_fee", "" + fee.multiply(new BigDecimal(100)).intValue());
|
map.put("spbill_create_ip", "119.85.112.210");
|
map.put("notify_url", "https://banli.xiaoxiangyingji.com/pay/paySuccess");
|
map.put("trade_type", "JSAPI");
|
map.put("openid", openId);
|
map.put("sign", WXUtil.getSignMD5(map, info.getMchKey()));
|
|
String entity = WXUtil.loadWXMessage(map);
|
|
String result = HttpUtil.post("https://api.mch.weixin.qq.com/pay/unifiedorder", entity);
|
System.out.println("统一下单结果:" + result);
|
Map<String, String> resultMap = WXUtil.parseXML(result);
|
|
return resultMap;
|
}
|
|
}
|