2023年7月2日发(作者:)
Java实现微信⼩程序⽀付(⽀付,提现,退款)1.添加WXpayCommon类⽤以具体实现功能,代码如下:
1 package on; 2
3 import ption; 4 import p; 5 import ; 6
7 import ; 8 import onfigImpl; 9 import onstants; 10 import til; 11 import iedOrderWx; 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 = tance(); 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 respData = null; 42 if (flag) { 43 HashMap data = new HashMap(); 44 ("body", erNo()); 45 ("out_trade_no", erNo());//订单号 46 ("fee_type", "CNY"); 47 ("total_fee", "1");//⽀付⾦额(分) 48 ("spbill_create_ip", ip); 49 ("notify_url", "******************");//⽀付成功回调地址 50 // 交易类型--JSAPI:公众号⽀付、NATIVE:原⽣扫码⽀付、APP:APP⽀付 51 // 公众号⽀付 52 ("trade_type", "JSAPI");//⽀付⽅式 53 // 设备号--⾃定义参数,可以为终端设备号(门店号或收银设备ID),PC⽹页或公众号内⽀付可以传"WEB" 54 ("device_info", "WEB"); 55 // ⽤户openid 56 ("openid", openid); 57 ("product_id", "1000" + ""); 58
59 if (null == wxpay) { 60 WXpayCommonIns(); 61 } 62 respData = dOrder(data);//统⼀下单 63 ark(respData); 64 } else { 65 respData = ark(); 66 } 67 n(respData); 68 // 返回状态码 69 String return_code = ("return_code"); 70 if (return_(S)) { 71 // 业务结果 72 String result_code = ("result_code"); 73 if (result_(S)) { 74 voUnifiedOrderWx = new VoUnifiedOrderWx(); 75 // 时间戳 76 eStamp(timeLong()/1000 + ""); 77 // 随机串 78 ceStr(("nonce_str")); 79 // 数据包 80 kageValue("prepay_id=" + ("prepay_id")); 81
82 // ⼆次签名 83 HashMap dataPay = new HashMap(); 84 ("appId", ("appid")); 85 ("nonceStr", ceStr()); 86 ("package", kageValue()); 87 ("timeStamp", eStamp()); 88 ("signType", "MD5"); 89 String sign = teSignature(dataPay, ()); 90 // 签名 91 n(sign); 92 } 93 } 94 } catch (Exception e) { 95 tackTrace(); 96 } 97 return voUnifiedOrderWx; 98 } 99
100
101 /**102 * 企业付款(提现)103 * @param wxWithdrawOrder104 * @param openid
105 * @param ip106 * @throws Exception107 */108 public static void transfers(WxWithdrawOrder wxWithdrawOrder, String openid, String ip) throws Exception {109 HashMap data = new HashMap();110 // 商户订单号111 ("partner_trade_no", erNo());112 // ⽤户openid113 ("openid", openid);114 // 校验⽤户姓名选项115 ("check_name", "NO_CHECK");116 // ⾦额117 ("amount", ey() + "");118 // 企业付款描述信息119 // TODO120 ("desc", "提现");121 // Ip地址122 ("spbill_create_ip", ip);123
124 if (null == wxpay) {125 WXpayCommonIns();125 WXpayCommonIns();126 }127 String respXml = ers(data);//企业付款128 n(respXml);129 Map respData = ap(respXml);130 ark(respData);131 ark2(respXml);132 // 返回状态码133 String return_code = ("return_code");134 n(return_code);135 if (return_(S)) {136 // 业务结果处理137 }138 }139
140
141 /**142 * @description:微信退款业务封装(⽀付押⾦退还);143 * @param PayLog order:⽀付订单信息144 * Double refundAmount:退款⾦额145 * @return 微信退款接⼝返回数据 true 退款成功 false 退款失败146 * @author:FanHaoJian147 * @throws Exception
148 */149 public static Boolean refundOrder(WxWithdrawOrder orderInfo,Double refundAmount) throws Exception{150 if (null == wxpay) {151 WXpayCommonIns();152 }153 HashMap data = new HashMap();154 ("out_trade_no", erNo());155 ("out_refund_no", erNo());156 ("total_fee", "1000");//价钱分157 ("refund_fee", f((int)(refundAmount*100)));158 ("refund_fee_type", "CNY");159 ("op_user_id", ID());160 try {161 Map r = (data);//订单退款162 n(r);163 return ;164 } catch (Exception e) {165 tackTrace();166 }167 return ;168 }169
170 }View Code2.接⼝⼤概内容如下:
1 package ; 2
3 import edOutputStream; 4 import rayOutputStream; 5 import ption; 6 import tream; 7 import p; 8 import ; 9
10 import rvletRequest; 11 import rvletResponse; 12
13 import ller; 14 import tMapping; 14 import tMapping; 15
16 import onfigImpl; 17 import onstants; 18 import til; 19 import essUtil; 20 import ommon; 21 import drawOrder; 22 /** 23 * 接⼝模板 24 * @author 开发 25 * 26 */ 27 @Controller 28 @RequestMapping("wx") 29 public class WxPayAction { 30
31 private HttpServletRequest request; 32 private HttpServletResponse response; 33 private WXPayConfigImpl config; 34 /** 35 * ⽀付 36 * @return 37 */ 38 @RequestMapping("/pay") 39 public String pay(){ 40 Map remark=new HashMap(); 41 //新建订单 42 WxWithdrawOrder order=new WxWithdrawOrder("1","1",1,"1","1",remark,""); 43 try { 44 erWx(order, "⽤户openId",ddr(request), true); 45 } catch (IOException e) { 46 // TODO Auto-generated catch block 47 tackTrace(); 48 } 49 return ""; 50 } 51 /** 52 * 微信回调接⼝notify(⽀付填写的⽀付成功回调地址) 53 * @throws Exception 54 */ 55 @RequestMapping("/notifyPayWx") 56 public void notifyPayWx() throws Exception { 57 boolean state = false; 58 n("微信回调"); 59 InputStream inStream = utStream(); 60 ByteArrayOutputStream outSteam = new ByteArrayOutputStream(); 61 byte[] buffer = new byte[1024]; 62 int len = 0; 63 while ((len = (buffer)) != -1) { 64 (buffer, 0, len); 65 } 66 (); 67 (); 68 String resultXml = new String(Array(), "utf-8"); 69 n("resultXml:" + resultXml); 70 Map resultMap = ap(resultXml); 71 // 签名验证 72 if (atureValid(resultXml, ())) { 73 String resXml = ""; 74 // 业务结果 75 String result_code = ("result_code"); 76 if (result_(S)) { 77 // 订单号 78 //String out_trade_no = ("out_trade_no"); 79 // ⽀付成功处理业务逻辑 79 // ⽀付成功处理业务逻辑 80 state =true;//根据订单号查询⾃⼰数据库该条数据进⾏业务操作,操作成功返回true 81 }
82 if (state) { 83 //通知微信.异步确认成功.必写.不然会⼀直通知后台.⼋次之后就认为交易失败了.
84 resXml = "" + ""
85 + "" + " "; 86 } else { 87 resXml = "" + ""
88 + "" + " "; 89 } 90 n("resXml:" + resultXml); 91 BufferedOutputStream out = new BufferedOutputStream(putStream()); 92 (es()); 93 (); 94 (); 95 } else { 96 // 签名验证失败 97 n("签名验证失败"); 98 } 99 }100
101 /**102 * 退款103 * @return104 */105 @RequestMapping("/returnPay")106 public String returnPay(){107 Map remark=new HashMap();108 //新建订单109 WxWithdrawOrder order=new WxWithdrawOrder("1","1",1,"1","1",remark,"");110 try {111 Order(order,0.01);112 } catch (Exception e) {113 // TODO Auto-generated catch block114 tackTrace();115 }116 return "";117 }118 /**119 * 提现120 * @return121 */122 @RequestMapping("/withdrawDeposit")123 public String withdrawDeposit(){124 Map remark=new HashMap();125 //新建订单126 WxWithdrawOrder order=new WxWithdrawOrder("1","1",1,"1","1",remark,"");127 try {128 ers(order,"⽤户openId", "服务器地址");129 } catch (Exception e) {130 // TODO Auto-generated catch block131 tackTrace();132 }133 return "";134 }135 public HttpServletRequest getRequest() {136 return request;137 }138 public void setRequest(HttpServletRequest request) {139 t = request;140 }141 public HttpServletResponse getResponse() {142 return response;143 }144 public void setResponse(HttpServletResponse response) {144 public void setResponse(HttpServletResponse response) {145 se = response;146 }147
148
149
150 }View Code3.接⼝中主要根据业务逻辑调⽤WXpayCommon类中的功能实现⽅法即可。项⽬中的相关代码,线上可⽤,copy整理出的未实测(主要需要相关参数测试,不⽅便),⽬测没⽑病
发布者:admin,转转请注明出处:http://www.yc00.com/web/1688278416a112339.html
评论列表(0条)