Java实现微信小程序支付(支付,提现,退款)
生活随笔
收集整理的這篇文章主要介紹了
Java实现微信小程序支付(支付,提现,退款)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.添加WXpayCommon類用以具體實現功能,代碼如下:
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 } View Code2.接口大概內容如下:
1 package com.karat.cn.action; 2 3 import java.io.BufferedOutputStream; 4 import java.io.ByteArrayOutputStream; 5 import java.io.IOException; 6 import java.io.InputStream; 7 import java.util.HashMap; 8 import java.util.Map; 9 10 import javax.servlet.http.HttpServletRequest; 11 import javax.servlet.http.HttpServletResponse; 12 13 import org.springframework.stereotype.Controller; 14 import org.springframework.web.bind.annotation.RequestMapping; 15 16 import com.karat.cn.util.WXPayConfigImpl; 17 import com.karat.cn.util.WXPayConstants; 18 import com.karat.cn.util.WXPayUtil; 19 import com.karat.cn.wxCommon.IpAddressUtil; 20 import com.karat.cn.wxCommon.WXpayCommon; 21 import com.karat.cn.wxCommon.WxWithdrawOrder; 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<String, String> remark=new HashMap<String, String>(); 41 //新建訂單 42 WxWithdrawOrder order=new WxWithdrawOrder("1","1",1,"1","1",remark,""); 43 try { 44 WXpayCommon.payOrderWx(order, "用戶openId",IpAddressUtil.getIpAddr(request), true); 45 } catch (IOException e) { 46 // TODO Auto-generated catch block 47 e.printStackTrace(); 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 System.out.println("微信回調"); 59 InputStream inStream = request.getInputStream(); 60 ByteArrayOutputStream outSteam = new ByteArrayOutputStream(); 61 byte[] buffer = new byte[1024]; 62 int len = 0; 63 while ((len = inStream.read(buffer)) != -1) { 64 outSteam.write(buffer, 0, len); 65 } 66 outSteam.close(); 67 inStream.close(); 68 String resultXml = new String(outSteam.toByteArray(), "utf-8"); 69 System.out.println("resultXml:" + resultXml); 70 Map<String, String> resultMap = WXPayUtil.xmlToMap(resultXml); 71 // 簽名驗證 72 if (WXPayUtil.isSignatureValid(resultXml, config.getKey())) { 73 String resXml = ""; 74 // 業務結果 75 String result_code = resultMap.get("result_code"); 76 if (result_code.equals(WXPayConstants.SUCCESS)) { 77 // 訂單號 78 //String out_trade_no = resultMap.get("out_trade_no"); 79 // 支付成功處理業務邏輯 80 state =true;//根據訂單號查詢自己數據庫該條數據進行業務操作,操作成功返回true 81 } 82 if (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 BufferedOutputStream(response.getOutputStream()); 92 out.write(resXml.getBytes()); 93 out.flush(); 94 out.close(); 95 } else { 96 // 簽名驗證失敗 97 System.out.println("簽名驗證失敗"); 98 } 99 } 100 101 /** 102 * 退款 103 * @return 104 */ 105 @RequestMapping("/returnPay") 106 public String returnPay(){ 107 Map<String, String> remark=new HashMap<String, String>(); 108 //新建訂單 109 WxWithdrawOrder order=new WxWithdrawOrder("1","1",1,"1","1",remark,""); 110 try { 111 WXpayCommon.refundOrder(order,0.01); 112 } catch (Exception e) { 113 // TODO Auto-generated catch block 114 e.printStackTrace(); 115 } 116 return ""; 117 } 118 /** 119 * 提現 120 * @return 121 */ 122 @RequestMapping("/withdrawDeposit") 123 public String withdrawDeposit(){ 124 Map<String, String> remark=new HashMap<String, String>(); 125 //新建訂單 126 WxWithdrawOrder order=new WxWithdrawOrder("1","1",1,"1","1",remark,""); 127 try { 128 WXpayCommon.transfers(order,"用戶openId", "服務器地址"); 129 } catch (Exception e) { 130 // TODO Auto-generated catch block 131 e.printStackTrace(); 132 } 133 return ""; 134 } 135 public HttpServletRequest getRequest() { 136 return request; 137 } 138 public void setRequest(HttpServletRequest request) { 139 this.request = request; 140 } 141 public HttpServletResponse getResponse() { 142 return response; 143 } 144 public void setResponse(HttpServletResponse response) { 145 this.response = response; 146 } 147 148 149 150 } View Code3.接口中主要根據業務邏輯調用WXpayCommon類中的功能實現方法即可。項目中的相關代碼,線上可用,copy整理出的未實測(主要需要相關參數測試,不方便),目測沒毛病
轉載于:https://www.cnblogs.com/LJing21/p/10391196.html
總結
以上是生活随笔為你收集整理的Java实现微信小程序支付(支付,提现,退款)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: VCE 题库制作
- 下一篇: 【熵与特征提取】从近似熵,到样本熵,到模