package com.cn.ssm.controller;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.cn.ssm.pojo.BillNo;
import com.cn.ssm.service.BillNoService;
import tool.CreateSign;
import tool.HttpRequest;
import tool.ParseXml;
import tool.StrKit;
@Controller
@RequestMapping("/pay")
public class PayUrlController{
@Autowired BillNoService billNoService;
@RequestMapping("/url")
public void pay(HttpServletRequest request,HttpServletResponse response) {
/**
* 获取用户扫描二维码后,微信返回的信息
*/
InputStream inStream;
try {
inStream = request.getInputStream();
ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inStream.read(buffer)) != -1) {
outSteam.write(buffer, 0, len);
}
outSteam.close();
inStream.close();
String result = new String(outSteam.toByteArray(), "utf-8");
//System.out.println(result);
/*String xmla = "<xml><appid><![CDATA[wxcb1fe0d8764f9adb]]></appid>"
+ "<openid><![CDATA[oBhuiwFG5UZfW_rsSzmARE1xYP9Y]]></openid>"
+ "<mch_id><![CDATA[1480408942]]></mch_id>" + "<is_subscribe><![CDATA[Y]]></is_subscribe>"
+ "<nonce_str><![CDATA[jn0PiU3nuUAOTTAo]]></nonce_str>"
+ "<product_id><![CDATA[ 1161106027]]></product_id>"
+ "<sign><![CDATA[6FFDCCF621432EBCAB74DF1220AEEEF4]]></sign>" + "</xml>";*/
/**
* 获取返回的信息内容中各个参数的值
*/
Map<String, String> map =ParseXml.callXMLToMap(result);
String appid = map.get("appid");
String openid = map.get("openid");
String mch_id = map.get("mch_id");
String is_subscribe = map.get("is_subscribe");
String nonce_str = map.get("nonce_str");
String product_id = map.get("product_id");
String sign = map.get("sign");
Map<String, String> packageParams = new HashMap<String, String>();
packageParams.put("appid", appid);
packageParams.put("openid", openid);
packageParams.put("mch_id", mch_id);
packageParams.put("is_subscribe", is_subscribe);
packageParams.put("nonce_str", nonce_str);
packageParams.put("product_id", product_id);
String paternerKey = "KtPNdgMLgpreh0wUbtMeXrXKtZFPLv3i";
String packageSign = CreateSign.getSign(packageParams, paternerKey);
//System.out.println("签名返回 :" + packageSign);
PayUrlBackController.product_id=product_id;
int price = 1;//((int) (Float.valueOf(10) * 100));
BillNo billNo=billNoService.select(product_id);
if(billNo != null){
if(billNo.getAmountrmb() != null && billNo.getAmountrmb().length() > 0){
DecimalFormat df = new DecimalFormat("0");
String a=billNo.getAmountrmb();
float b=Float.valueOf(a);
//System.out.println(df.format(b*100));
price=Integer.valueOf(df.format(b*100));
}else{
price = 1;
}
}
// 统一下单文档地址:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1
//System.out.println("支付的钱为:"+billNo.getAmountrmb());
Map<String, String> params = new HashMap<String, String>();
params.put("appid", appid);
params.put("mch_id", mch_id);
params.put("body", "NO."+product_id);
String out_trade_no = product_id;//Long.toString(System.currentTimeMillis());
params.put("out_trade_no", out_trade_no);
params.put("total_fee", price+"");
//params.put("total_fee", "1");
params.put("attach", out_trade_no);
params.put("spbill_create_ip","8.8.8.8");
params.put("trade_type", "NATIVE");
params.put("nonce_str", System.currentTimeMillis() / 1000 + "");
params.put("notify_url", "http://192.168.1.10:8080/WeChat/pay/back");
params.put("openid", openid);
String paysign = CreateSign.getSign(params, paternerKey);
params.put("sign", paysign);
byte[] byteXml=ParseXml.callMapToXML(params);
String pushXml=new String(byteXml,"UTF-8");
//System.out.println("返回的xml数据:"+pushXml);
String xmlResult=HttpRequest.sendPost("https://api.mch.weixin.qq.com/pay/unifiedorder", pushXml);
//System.out.println("prepay_xml:" + xmlResult);
/**
* 发送信息给微信服务器
*/
Map<String, String> payResult = ParseXml.callXMLToMap(xmlResult);
String return_code = payResult.get("return_code");
String result_code = payResult.get("result_code");
if (StrKit.notBlank(return_code) && StrKit.notBlank(result_code) && return_code.equalsIgnoreCase("SUCCESS")&&result_code.equalsIgnoreCase("SUCCESS")) {
// 以下字段在return_code 和result_code都为SUCCESS的时候有返回
String prepay_id = payResult.get("prepay_id");
Map<String, String> prepayParams = new HashMap<String, String>();
prepayParams.put("return_code", "SUCCESS");
prepayParams.put("appid", appid);
prepayParams.put("mch_id", mch_id);
prepayParams.put("nonce_str", System.currentTimeMillis() + "");
prepayParams.put("prepay_id", prepay_id);
String prepaySign = null;
if (sign.equals(packageSign)) {
prepayParams.put("result_code", "SUCCESS");
}else {
prepayParams.put("result_code", "FAIL");
prepayParams.put("err_code_des", "订单失效"); //result_code为FAIL时,添加该键值对,value值是微信告诉客户的信息
}
prepaySign =CreateSign.getSign(prepayParams, paternerKey);
prepayParams.put("sign", prepaySign);
byte[] byteXmls=ParseXml.callMapToXML(prepayParams);
String xml=new String(byteXmls);
//System.out.println("最终xml:"+xml);
//renderText(xml);
PrintWriter pw=response.getWriter();
pw.println(xml);
pw.flush();
pw.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("处理异常");
}
}
}