package action;
import com.opensymphony.xwork2.ActionSupport;
import entity.Pay;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.RequestAware;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import util.Httpsrequest;
import util.PayUtil;
import util.Sign;
import util.Xstreamutil;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
public class PayAction extends ActionSupport implements RequestAware{
// �����õ�key
static String Key = "qingdaohonghujizhi01020304050607";
static String appid = "wxe4ce541bc609c3a6";
// �̻�ID
static String mch_id = "1496771942";// 1453210602
static String spbill_create_ip = "116.196.119.238";//117.34.105.176
static String body = "test";
static String trade_type = "JSAPI";
// url
static String notify_url = "https://www.baidu.com";
// ���
static int total_fee =1;
static String openid = "on_Im0YqwmdMQoD-lYy2bkqN3jjo";
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
private String result;
private Map<String, Object> request;
public Map<String, Object> getRequest() {
return request;
}
public void setRequest(Map<String, Object> arg0) {
// TODO Auto-generated method stub
this.request=arg0;
}
public String pay() {
String nonce_str = PayUtil.getRandomString(20);
String out_trade_no = PayUtil.getCurrTime()
+ PayUtil.getRandomString(5);
SortedMap<Object, Object> parameters = new TreeMap<Object, Object>();
parameters.put("appid", appid);
parameters.put("body", body);
parameters.put("mch_id", mch_id);
parameters.put("nonce_str", nonce_str);
parameters.put("notify_url", notify_url);
parameters.put("out_trade_no", out_trade_no);
parameters.put("openid", openid);
parameters.put("spbill_create_ip", spbill_create_ip);
parameters.put("trade_type", trade_type);
parameters.put("total_fee", total_fee);
//jar��ǩ��
// String sign = PayImpl.paySign(parameters, key);
//��дǩ��
String sign = Sign.creatSign(parameters, Key);
Pay pay = new Pay();
pay.setAppid(appid);
pay.setBody(body);
pay.setMch_id(mch_id);
pay.setNonce_str(nonce_str);
pay.setNotify_url(notify_url);
pay.setOpenid(openid);
pay.setSign(sign);
pay.setOut_trade_no(out_trade_no);
pay.setSpbill_create_ip(spbill_create_ip);
pay.setTrade_type(trade_type);
pay.setTotal_fee(total_fee);
Xstreamutil.xstream.alias("xml", Pay.class);
String xml=Xstreamutil.xstream.toXML(pay).replaceAll("__", "_");;
System.out.println(xml);
String requestUrl = "https://api.mch.weixin.qq.com/pay/unifiedorder";
String respxml=null;
String prepay_id=null;
try {
respxml = Httpsrequest.HttpsRequest(requestUrl, "POST",xml);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Document doc=DocumentHelper.parseText(respxml);
Element root=doc.getRootElement();
List<Element>elementList=root.elements();
for (int i = 0; i <elementList.size(); i++) {
if(elementList.get(i).getName().equals("prepay_id")){
prepay_id=elementList.get(i).getTextTrim();
}
}
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String timeStamp=PayUtil.getTempStamp();
String Package="prepay_id="+prepay_id;
SortedMap<Object, Object> h5parameter = new TreeMap<Object, Object>();
h5parameter.put("appId",appid);
h5parameter.put("nonceStr",nonce_str );
h5parameter.put("package", Package);
h5parameter.put("signType","MD5");
h5parameter.put("timeStamp",timeStamp);
String Paysign=util.Sign.creatSign(h5parameter,Key);
System.out.println(Paysign);
result = "{\"login\":\"false\"}";
return "pay";
}
/*public static void main(String[] args) {
pay();
}*/
public void huidiao() throws Exception{
InputStream inputStream = null ;
StringBuffer sb = new StringBuffer();
HttpServletRequest request = ServletActionContext.getRequest();
try {
inputStream = ( request).getInputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String s ;
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
while ((s = in.readLine()) != null){
sb.append(s);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
in.close();
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String respxml=sb.toString();
System.out.println( respxml);
String code;
Document doc=DocumentHelper.parseText(respxml);
Element root=doc.getRootElement();
List<Element>elementList=root.elements();
for (int i = 0; i < elementList.size(); i++) {
if(elementList.get(i).getName().equals("result_code")){
code=elementList.get(i).getTextTrim();
System.out.println(code);
if(code.equals("SUCCESS")){
String resXml = "<xml>" + "<return_code><![CDATA[SUCCESS]]></return_code>"
+ "<return_msg><![CDATA[OK]]></return_msg>" + "</xml> ";
HttpServletResponse response = ServletActionContext.getResponse();
BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
out.write(resXml.getBytes());
out.flush();
out.close();
}
}
}
}
}