1 package com.karat.cn.wxCommon;
2
3 import java.io.IOException;
4 import java.util.HashMap;
5 import java.util.Map;
6
7 import com.karat.cn.util.WXPay;
8 import com.karat.cn.util.WXPayConfigImpl;
9 import com.karat.cn.util.WXPayConstants;
10 import com.karat.cn.util.WXPayUtil;
11 import com.karat.cn.vo.VoUnifiedOrderWx;
12
13 /**
14 * 支付,提现,退款
15 * @author 开发
16 *
17 */
18 public class WXpayCommon {
19
20 private static WXPay wxpay;
21 private static WXPayConfigImpl config;
22
23
24 public static void WXpayCommonIns() throws Exception {
25 config = WXPayConfigImpl.getInstance();
26 wxpay = new WXPay(config);
27 }
28
29 /**
30 * 支付
31 * @param orderInfo 自己数据库生成的订单
32 * @param openid 用户的标识openId
33 * @param ip
34 * @param flag
35 * @return
36 * @throws IOException
37 */
38 public static VoUnifiedOrderWx payOrderWx(WxWithdrawOrder orderInfo, String openid, String ip, boolean flag) throws IOException {
39 VoUnifiedOrderWx voUnifiedOrderWx = null;
40 try {
41 Map<String, String> respData = null;
42 if (flag) {
43 HashMap<String, String> data = new HashMap<String, String>();
44 data.put("body", orderInfo.getOrderNo());
45 data.put("out_trade_no", orderInfo.getOrderNo());//订单号
46 data.put("fee_type", "CNY");
47 data.put("total_fee", "1");//支付金额(分)
48 data.put("spbill_create_ip", ip);
49 data.put("notify_url", "******************");//支付成功回调地址
50 // 交易类型--JSAPI:公众号支付、NATIVE:原生扫码支付、APP:APP支付
51 // 公众号支付
52 data.put("trade_type", "JSAPI");//支付方式
53 // 设备号--自定义参数,可以为终端设备号(门店号或收银设备ID),PC网页或公众号内支付可以传"WEB"
54 data.put("device_info", "WEB");
55 // 用户openid
56 data.put("openid", openid);
57 data.put("product_id", "1000" + "");
58
59 if (null == wxpay) {
60 WXpayCommonIns();
61 }
62 respData = wxpay.unifiedOrder(data);//统一下单
63 orderInfo.setRemark(respData);
64 } else {
65 respData = orderInfo.getRemark();
66 }
67 System.out.println(respData);
68 // 返回状态码
69 String return_code = respData.get("return_code");
70 if (return_code.equals(WXPayConstants.SUCCESS)) {
71 // 业务结果
72 String result_code = respData.get("result_code");
73 if (result_code.equals(WXPayConstants.SUCCESS)) {
74 voUnifiedOrderWx = new VoUnifiedOrderWx();
75 // 时间戳
76 voUnifiedOrderWx.setTimeStamp(BaseToolsUtil.systemtimeLong()/1000 + "");
77 // 随机串
78 voUnifiedOrderWx.setNonceStr(respData.get("nonce_str"));
79 // 数据包
80 voUnifiedOrderWx.setPackageValue("prepay_id=" + respData.get("prepay_id"));
81
82 // 二次签名
83 HashMap<String, String> dataPay = new HashMap<String, String>();
84 dataPay.put("appId", respData.get("appid"));
85 dataPay.put("nonceStr", voUnifiedOrderWx.getNonceStr());
86 dataPay.put("package", voUnifiedOrderWx.getPackageValue());
87 dataPay.put("timeStamp", voUnifiedOrderWx.getTimeStamp());
88 dataPay.put("signType", "MD5");
89 String sign = WXPayUtil.generateSignature(dataPay, config.getKey());
90 // 签名
91 voUnifiedOrderWx.setSign(sign);
92 }
93 }
94 } catch (Exception e) {
95 e.printStackTrace();
96 }
97 return voUnifiedOrderWx;
98 }
99
100
101 /**
102 * 企业付款(提现)
103 * @param wxWithdrawOrder
104 * @param openid
105 * @param ip
106 * @throws Exception
107 */
108 public static void transfers(WxWithdrawOrder wxWithdrawOrder, String openid, String ip) throws Exception {
109 HashMap<String, String> data = new HashMap<String, String>();
110 // 商户订单号
111 data.put("partner_trade_no", wxWithdrawOrder.getOrderNo());
112 // 用户openid
113 data.put("openid", openid);
114 // 校验用户姓名选项
115 data.put("check_name", "NO_CHECK");
116 // 金额
117 data.put("amount", wxWithdrawOrder.getMoney() + "");
118 // 企业付款描述信息
119 // TODO
120 data.put("desc", "提现");
121 // Ip地址
122 data.put("spbill_create_ip", ip);
123
124 if (null == wxpay) {
125 WXpayCommonIns();
126 }
127 String respXml = wxpay.transfers(data);//企业付款
128 System.out.println(respXml);
129 Map<String, String> respData = WXPayUtil.xmlToMap(respXml);
130 wxWithdrawOrder.setRemark(respData);
131 wxWithdrawOrder.setRemark2(respXml);
132 // 返回状态码
133 String return_code = respData.get("return_code");
134 System.out.println(return_code);
135 if (return_code.equals(WXPayConstants.SUCCESS)) {
136 // 业务结果处理
137 }
138 }
139
140
141 /**
142 * @description:微信退款业务封装(支付押金退还);
143 * @param PayLog order:支付订单信息
144 * Double refundAmount:退款金额
145 * @return 微信退款接口返回数据 true 退款成功 false 退款失败
146 * @author:FanHaoJian
147 * @throws Exception
148 */
149 public static Boolean refundOrder(WxWithdrawOrder orderInfo,Double refundAmount) throws Exception{
150 if (null == wxpay) {
151 WXpayCommonIns();
152 }
153 HashMap<String, String> data = new HashMap<String, String>();
154 data.put("out_trade_no", orderInfo.getOrderNo());
155 data.put("out_refund_no", orderInfo.getOrderNo());
156 data.put("total_fee", "1000");//价钱分
157 data.put("refund_fee", String.valueOf((int)(refundAmount*100)));
158 data.put("refund_fee_type", "CNY");
159 data.put("op_user_id", config.getMchID());
160 try {
161 Map<String, String> r = wxpay.refund(data);//订单退款
162 System.out.println(r);
163 return Boolean.TRUE;
164 } catch (Exception e) {
165 e.printStackTrace();
166 }
167 return Boolean.FALSE;
168 }
169
170 }