Java实现⼩程序⽀付(⽀付,提现,退款)1.添加WXpayCommon类⽤以具体实现功能,代码如下:
1package com.karat.wxCommon;
2
3import java.io.IOException;
4import java.util.HashMap;
5import java.util.Map;
6
7import com.karat.util.WXPay;
8import com.karat.util.WXPayConfigImpl;
9import com.karat.util.WXPayConstants;
10import com.karat.util.WXPayUtil;
11import com.karat.vo.VoUnifiedOrderWx;
12
13/**
14 * ⽀付,提现,退款
15 * @author开发
16 *
17*/
18public class WXpayCommon {
19
20private static WXPay wxpay;
21private static WXPayConfigImpl config;
22
23
24public static void WXpayCommonIns() throws Exception {
25        config = Instance();
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*/
38public static VoUnifiedOrderWx payOrderWx(WxWithdrawOrder orderInfo, String openid, String ip, boolean flag) throws IOException { 39        VoUnifiedOrderWx voUnifiedOrderWx = null;
40try {
41            Map<String, String> respData = null;
42if (flag) {
43                HashMap<String, String> data = new HashMap<String, String>();
44                data.put("body", OrderNo());
45                data.put("out_trade_no", OrderNo());//订单号
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
59if (null == wxpay) {
60                    WXpayCommonIns();
61                }
62                respData = wxpay.unifiedOrder(data);//统⼀下单
63                orderInfo.setRemark(respData);
64            } else {
65                respData = Remark();
66            }
67            System.out.println(respData);
68// 返回状态码
69            String return_code = ("return_code");
70if (return_code.equals(WXPayConstants.SUCCESS)) {
71// 业务结果
72                String result_code = ("result_code");
73if (result_code.equals(WXPayConstants.SUCCESS)) {
74                    voUnifiedOrderWx = new VoUnifiedOrderWx();
75// 时间戳
76                    voUnifiedOrderWx.setTimeStamp(BaseToolsUtil.systemtimeLong()/1000 + "");
77// 随机串
78                    voUnifiedOrderWx.("nonce_str"));
79// 数据包
80                    voUnifiedOrderWx.setPackageValue("prepay_id=" + ("prepay_id"));
81
82// ⼆次签名
83                    HashMap<String, String> dataPay = new HashMap<String, String>();
84                    dataPay.put("appId", ("appid"));
85                    dataPay.put("nonceStr", NonceStr());
86                    dataPay.put("package", PackageValue());
87                    dataPay.put("timeStamp", TimeStamp());
88                    dataPay.put("signType", "MD5");
89                    String sign = ateSignature(dataPay, Key());
90// 签名
91                    voUnifiedOrderWx.setSign(sign);
92                }
93            }
94        } catch (Exception e) {
95            e.printStackTrace();
96        }
97return voUnifiedOrderWx;
98    }
99
100
101/**
102    * 企业付款(提现)
103    * @param wxWithdrawOrder
104    * @param openid
105    * @param ip
106    * @throws Exception
107*/
108public 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", OrderNo());
112// ⽤户openid
113        data.put("openid", openid);
114// 校验⽤户姓名选项
115        data.put("check_name", "NO_CHECK");
116// ⾦额
117        data.put("amount", Money() + "");
118// 企业付款描述信息
119// TODO
120        data.put("desc", "提现");
121// Ip地址
122        data.put("spbill_create_ip", ip);
123
124if (null == wxpay) {
125            WXpayCommonIns();
125            WXpayCommonIns();
126        }
127        String respXml = ansfers(data);//企业付款
128        System.out.println(respXml);
129        Map<String, String> respData = lToMap(respXml);
130        wxWithdrawOrder.setRemark(respData);
131        wxWithdrawOrder.setRemark2(respXml);
132// 返回状态码
133        String return_code = ("return_code");
134        System.out.println(return_code);
135if (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*/
149public static Boolean refundOrder(WxWithdrawOrder orderInfo,Double refundAmount) throws Exception{ 150if (null == wxpay) {
151            WXpayCommonIns();
152        }
153        HashMap<String, String> data = new HashMap<String, String>();
154            data.put("out_trade_no", OrderNo());
155            data.put("out_refund_no", OrderNo());
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", MchID());
160try {
161                Map<String, String> r = fund(data);//订单退款
162                System.out.println(r);
163return Boolean.TRUE;
164            } catch (Exception e) {
165                e.printStackTrace();
166            }
167return Boolean.FALSE;
168    }
169
170 }
View Code
2.接⼝⼤概内容如下:
1package com.karat.action;
2
3import java.io.BufferedOutputStream;
4import java.io.ByteArrayOutputStream;
5import java.io.IOException;
6import java.io.InputStream;
7import java.util.HashMap;
8import java.util.Map;
9
10import javax.servlet.http.HttpServletRequest;
11import javax.servlet.http.HttpServletResponse;
12
13import org.springframework.stereotype.Controller;
14import org.springframework.web.bind.annotation.RequestMapping;
14import org.springframework.web.bind.annotation.RequestMapping;
15
16import com.karat.util.WXPayConfigImpl;
17import com.karat.util.WXPayConstants;
18import com.karat.util.WXPayUtil;
19import com.karat.wxCommon.IpAddressUtil;
20import com.karat.wxCommon.WXpayCommon;
21import com.karat.wxCommon.WxWithdrawOrder;
22/**
23 * 接⼝模板
24 * @author开发
25 *
26*/
27 @Controller
28 @RequestMapping("wx")
29public class WxPayAction {
30
31private HttpServletRequest request;
32private HttpServletResponse response;
33private WXPayConfigImpl config;
34/**
35    * ⽀付
36    * @return
37*/
38    @RequestMapping("/pay")
39public String pay(){
40        Map<String, String> remark=new HashMap<String, String>();
41//新建订单
42        WxWithdrawOrder order=new WxWithdrawOrder("1","1",1,"1","1",remark,"");
43try {
44            WXpayCommon.payOrderWx(order, "⽤户openId",IpAddr(request), true);
45        } catch (IOException e) {
46// TODO Auto-generated catch block
47            e.printStackTrace();
48        }
49return "";
50    }
51/**
52    * 回调接⼝notify(⽀付填写的⽀付成功回调地址)
53    * @throws Exception
54*/
55    @RequestMapping("/notifyPayWx")
56public void notifyPayWx() throws Exception {
57boolean state = false;
58        System.out.println("回调");
59        InputStream inStream = InputStream();
60        ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
61byte[] buffer = new byte[1024];
62int len = 0;
63while ((len = ad(buffer)) != -1) {
64            outSteam.write(buffer, 0, len);
65        }
66        outSteam.close();
67        inStream.close();
68        String resultXml = new ByteArray(), "utf-8");
69        System.out.println("resultXml:" + resultXml);
70        Map<String, String> resultMap = lToMap(resultXml);
71// 签名验证
72if (WXPayUtil.isSignatureValid(resultXml, Key())) {
73            String resXml = "";
74// 业务结果
75            String result_code = ("result_code");
76if (result_code.equals(WXPayConstants.SUCCESS)) {
77// 订单号
78//String out_trade_no = ("out_trade_no");
79// ⽀付成功处理业务逻辑
79// ⽀付成功处理业务逻辑
80                state =true;//根据订单号查询⾃⼰数据库该条数据进⾏业务操作,操作成功返回true
81            }
82if (state) {
83//通知.异步确认成功.必写.不然会⼀直通知后台.⼋次之后就认为交易失败了.
84                resXml = "<xml>" + "<return_code><![CDATA[SUCCESS]]></return_code>"
85                        + "<return_msg><![CDATA[OK]]></return_msg>" + "</xml> ";
86            } else {
87                resXml = "<xml>" + "<return_code><![CDATA[FAIL]]></return_code>"
88                        + "<return_msg><![CDATA[报⽂为空]]></return_msg>" + "</xml> ";
89            }
90            System.out.println("resXml:" + resultXml);
91            BufferedOutputStream out = new OutputStream());
92            out.Bytes());
93            out.flush();
94            out.close();
95        } else {
96// 签名验证失败
97            System.out.println("签名验证失败");
98        }
99    }
100
101/**
102    * 退款
103    * @return
104*/
105    @RequestMapping("/returnPay")
106public String returnPay(){
107        Map<String, String> remark=new HashMap<String, String>();
108//新建订单
109        WxWithdrawOrder order=new WxWithdrawOrder("1","1",1,"1","1",remark,"");
110try {
111            fundOrder(order,0.01);
112        } catch (Exception e) {
113// TODO Auto-generated catch block
114            e.printStackTrace();
115        }
116return "";
117    }
118/**
119    * 提现
120    * @return
121*/
122    @RequestMapping("/withdrawDeposit")
入门的java游戏小程序123public String withdrawDeposit(){
124        Map<String, String> remark=new HashMap<String, String>();
125//新建订单
126        WxWithdrawOrder order=new WxWithdrawOrder("1","1",1,"1","1",remark,"");
127try {
128            ansfers(order,"⽤户openId", "服务器地址");
129        } catch (Exception e) {
130// TODO Auto-generated catch block
131            e.printStackTrace();
132        }
133return "";
134    }
135public HttpServletRequest getRequest() {
136return request;
137    }
138public void setRequest(HttpServletRequest request) {
140    }
141public HttpServletResponse getResponse() {
142return response;
143    }
144public void setResponse(HttpServletResponse response) {

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。