集成微信支付的代码。兼容小程序,扫码,app,公众号。h5 支付 ,以及 服务商提现

 
/**
* 因为微信总是很多个商户号很多和appid。很多个密钥文件,所以全部改成手动传值的方式,就可以支持多商户调用
*
* @param appId 商户的appid
* @param mchId 商户的商户号
* @param key 商户的支付用的密钥
* @param certPath 商户的退款用的退款文件地址,非必传,没有就传null,具体看开发文档是否需退款文件
* @throws Exception
*/ 调用示例测试代码。扫码支付返回的是一个base64格式的二维码。需要拼接前缀的(有逗号) : data:image/jpeg;base64, public static void main(String[] args) throws Exception {
WXPayEntrance wxPayEntrance = new WXPayEntrance("wxab8acb865bb1637e", "11473623", "2ab9071b06b9f739b950ddb41db2690d", null);
WXPayVO wxPayVO = new WXPayVO();
wxPayVO.setBody("1231231666623");
wxPayVO.setTotalFee("1");
wxPayVO.setNotifyUrl("https://********/api/wxpayreturn");
wxPayVO.setTradeType("NATIVE");
wxPayVO.setOutTradeNo("ty190523151415047046");
wxPayVO.setUid(123123123);
wxPayVO.setSpbillCreateIp("192.168.17.214");
Map<String, String> map = wxPayEntrance.doUnifiedOrder(wxPayVO);
System.out.println(map); // PayRefundVo payRefundVo = new PayRefundVo();
// payRefundVo.setOutRefundNo("A190523155119103");
// payRefundVo.setOutTradeNo("ty190523155047046");
// payRefundVo.setRefundDesc("退款");
// payRefundVo.setRefundFee("10");
// payRefundVo.setUid(123123123);
// payRefundVo.setRefundFeeType("CNY");
// payRefundVo.setTotalFee("120");
// Map<String, String> map = wxPayEntrance.doRefund(payRefundVo);
// System.out.println(map);
}

  

异步回调代码如下:

package com.huis.portal.controller;

import com.huis.portal.weixinpay.WXPayUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap; @RestController
@Api(description = "支付模块模块")
@CrossOrigin(origins = "*", maxAge = 3600)
@Transactional(rollbackFor = Exception.class)
public class PayRestController { @ApiOperation(value = "微信回调接口", notes = "微信回调接口")
@RequestMapping(value = "/api/wxpayreturn", method = RequestMethod.POST)
public String WeixinParentNotifyPage(HttpServletRequest request, HttpServletResponse response) throws Exception {
// 返回给微信的处理结果
String result = null;
String inputLine;
String notityXml = "";
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
response.setHeader("Access-Control-Allow-Origin", "*");
// 微信给返回的东西
try {
while ((inputLine = request.getReader().readLine()) != null) {
notityXml += inputLine;
}
request.getReader().close();
} catch (Exception e) {
e.printStackTrace();
result = setXml("fail", "xml获取失败");
return result;
}
if (StringUtils.isEmpty(notityXml)) {
result = setXml("fail", "xml为空");
return result;
}
Map<String, String> map = WXPayUtil.xmlToMap(notityXml);
String out_trade_no = (String) map.get("out_trade_no");// 获取商户订单号
String result_code = (String) map.get("result_code");// 业务结果
String total_fee = (String) map.get("total_fee");// 获取订单金额
String appid = (String) map.get("appid");// 获取订单金额
if (map.get("result_code").equals("SUCCESS")) {
//TODO result = setXml("SUCCESS", "OK");
return result;
}else {
result = setXml("fail", "xml为空");
return result;
} }
// 通过xml 发给微信消息
public static String setXml(String return_code, String return_msg) {
SortedMap<String, String> parameters = new TreeMap<String, String>();
parameters.put("return_code", return_code);
parameters.put("return_msg", return_msg);
return "<xml><return_code><![CDATA[" + return_code + "]]>" + "</return_code><return_msg><![CDATA[" + return_msg
+ "]]></return_msg></xml>";
} }

  

密钥文件通过maven打包的过程中会被maven编译,修改文件内容,需要在pom文件中加入忽略插件

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>p12</nonFilteredFileExtension>
<nonFilteredFileExtension>pem</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>

  

微信支付代码下载地址:链接:https://pan.baidu.com/s/1kZs5hxQuzxmRR50N-DOKSg  密码:u4rj

上一篇:iOS 8自动调整UITableView和UICollectionView布局


下一篇:Linux C 程序 基础语法(1)