您好,登錄后才能下訂單哦!
這篇文章主要介紹Springboot項目如何實現接入微信app支付,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
1.1請先完成微信APP支付接入商戶服務中心
1.2詳情請參考微信官方文檔:https://open.weixin.qq.com/
#微信支付配置 tenpayconfig: #商戶APPID appId: asdfg12345 #商戶號 mchId: 12345678 #商戶的key(API密匙) key: qwertyuiop #API支付請求地址 payUrl: https://api.mch.weixin.qq.com/pay/unifiedorder #API查詢請求地址 queryUrl: https://api.mch.weixin.qq.com/pay/orderquery #package packageValue: Sign=WXPay
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import lombok.Data;
/**
* @Description: 微信支付配置類
* @Param:
* @return:
* @Author: zengXianKang
* @Date: 2019/7/28
*/
@Data
@Component
@ConfigurationProperties(prefix = "tenpayconfig")
public class TenpayConfig {
//appId
private String appId;
//商戶號
private String mchId;
//商戶的key(API密匙)
private String key;
//API支付請求地址
private String payUrl;
//API查詢請求地址
private String queryUrl;
//Sign=WXPay
private String packageValue;
}
3.1新建一個TenPayVO
import java.math.BigDecimal;
import lombok.Data;
/**
* @description: TenPayVO
* @author: zengXianKang
* @create: 2019-07-28
**/
@Data
public class TenPayVO {
//商戶訂單號
private String outTradeNo;
//業務結果
private String resultCode;
//簽名方式
private String signType;
//簽名
private String sign;
//交易類型
private String tradeType;
//交易狀態
private String tradeState;
//商戶號
private String mchId;
//付款銀行
private String bankType;
//支付金額
private BigDecimal totalFee;
//幣種
private String feeType;
//微信支付訂單號
private String transactionId;
//支付完成時間
private String timeEnd;
}
3.2由于微信支付和回調的報文都是xml,先在maven中添加xstream的jar依賴
<!--xstream-->
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.7</version>
</dependency>
3.3TenPayUtils工具類,直接拿去用吧,該用到的我基本都寫好了,拿走不謝
import com.github.pagehelper.util.StringUtil;
import com.huaku.ecom.common.config.TenpayConfig;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.io.xml.DomDriver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.net.ssl.HttpsURLConnection;
import java.io.*;
import java.net.URL;
import java.security.NoSuchAlgorithmException;
import java.util.*;
import java.util.Map.Entry;
/**
* TenPayUtils
* @author :zengXianKang
*/
@Component
public class TenPayUtils {
private static TenPayUtils tenPayUtils;
@Autowired
private TenpayConfig tenpayConfig;
@PostConstruct
public void init(){
tenPayUtils = this;
tenPayUtils.tenpayConfig = this.tenpayConfig;
}
/**
* @Description: 微信支付簽名
*
* @Param: [paramsMap, charSetName]
* @return: java.lang.String
* @Author: zengXianKang
* @Date: 2019/7/28
*/
public String createSign(SortedMap<String, Object> paramsMap, String charSetName) throws UnsupportedEncodingException,
NoSuchAlgorithmException {
StringBuffer buffer = new StringBuffer();
//參數按照ACCSII排序(升序)
Set set = paramsMap.entrySet();
Iterator iterator = set.iterator();
while (iterator.hasNext()){
Map.Entry entry = (Map.Entry) iterator.next();
String key = (String) entry.getKey();
String value = (String) entry.getValue();
if(!key.equals("sign") && StringUtil.isNotEmpty(value)){
buffer.append(key + "=" + value + "&");
}
}
buffer.append("key=" + tenPayUtils.tenpayConfig.getKey());
String sign = MDUtils.MD5EncodeForHex(buffer.toString(), charSetName).toUpperCase();
return sign;
}
/**
* @Description: 組裝微信支付請求報文
*
* @Param: [paramsMap]
* @return: java.lang.String
* @Author: zengXianKang
* @Date: 2019/7/28
*/
public static String tenPayXmlInfo(SortedMap<String, Object> paramsMap){
StringBuffer buffer = new StringBuffer();
if(paramsMap != null){
buffer.append("<xml>");
for(Map.Entry<String, Object> entry : paramsMap.entrySet()){
buffer.append("<").append(entry.getKey()).append("><![CDATA[").append(entry.getValue()).append("]]></").append(entry.getKey()).append(">");
}
buffer.append("</xml>");
}
return buffer.toString();
}
/**
* @Description: 請求調用URL
*
* @Param: [requestUrl, requestMethod, output]
* @return: java.lang.String
* @Author: zengXianKang
* @Date: 2019/7/28
*/
public static String httpsRequest(String requestUrl, String requestMethod, String output) throws Exception {
URL url = new URL(requestUrl);
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.setRequestMethod(requestMethod);
if(StringUtil.isNotEmpty(output)){
OutputStream outputStream = connection.getOutputStream();
outputStream.write(output.getBytes("UTF-8"));
outputStream.close();
}
InputStream inputStream = connection.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String str = null;
StringBuffer buffer = new StringBuffer();
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}
bufferedReader.close();
inputStreamReader.close();
inputStream.close();
connection.disconnect();
return buffer.toString();
}
/**
* @Description: 解析xml
*
* @Param: [xml, rootName, rowName]
* @return: java.lang.Object
* @Author: zengXianKang
* @Date: 2019/7/28
*/
public static Object readXml(String xml, String rootName, String rowName){
XStream xStream = new XStream(new DomDriver());
xStream.alias(rootName, Map.class);
xStream.registerConverter(new TenPayUtils.MapEntryConverter(rowName));
Object object = xStream.fromXML(xml);
return object;
}
/**
* @Description: 內部類,readXml專用
*
* @Param:
* @return:
* @Author: zengXianKang
* @Date: 2019/7/28
*/
public static class MapEntryConverter implements Converter {
private String rowName;
public MapEntryConverter(String rowName) {
this.rowName = rowName;
}
public boolean canConvert(Class clazz) {
return Map.class.isAssignableFrom(clazz) || LinkedHashMap.class.isAssignableFrom(clazz);
}
public void marshal(Object value, HierarchicalStreamWriter writer, MarshallingContext context) {
this._marshal(value, writer, context);
}
private void _marshal(Object value, HierarchicalStreamWriter writer, MarshallingContext context) {
Iterator i$;
Object object;
if (value instanceof Map) {
Map map = (Map) value;
for (i$ = map.entrySet().iterator(); i$.hasNext(); writer.endNode()) {
object = i$.next();
Entry entry = (Entry) object;
Object _key = entry.getKey();
Object _value = entry.getValue();
writer.startNode(entry.getKey().toString());
if (_value instanceof Map) {
this._marshal(_value, writer, context);
} else if (_value instanceof List) {
this._marshal(_value, writer, context);
} else {
writer.setValue(entry.getValue().toString());
}
}
} else if (value instanceof List) {
List list = (List) value;
for (i$ = list.iterator(); i$.hasNext(); writer.endNode()) {
object = i$.next();
writer.startNode(this.rowName);
if (!(object instanceof Map) && !(object instanceof List)) {
writer.setValue(object.toString());
} else {
this._marshal(object, writer, context);
}
}
}
}
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
return this._unmarshal(reader, context);
}
public Object _unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
Map map = new HashMap();
List list = new ArrayList();
boolean isList;
for (isList = false; reader.hasMoreChildren(); reader.moveUp()) {
reader.moveDown();
String nodeName = reader.getNodeName();
if (reader.hasMoreChildren()) {
if (isList) {
list.add(this._unmarshal(reader, context));
} else if (map.containsKey(nodeName)) {
isList = true;
list.add(map.remove(nodeName));
list.add(this._unmarshal(reader, context));
} else if (this.rowName.equals(nodeName)) {
isList = true;
list.add(this._unmarshal(reader, context));
} else {
map.put(nodeName, this._unmarshal(reader, context));
}
} else {
String value = reader.getValue();
if (isList) {
list.add(value);
} else if (map.containsKey(nodeName)) {
isList = true;
list.add(map.remove(nodeName));
list.add(value);
} else if (this.rowName.equals(nodeName)) {
isList = true;
list.add(value);
} else {
map.put(nodeName, value);
}
}
}
return isList ? list : map;
}
}
}
在很多電商的項目中都有類似保存訂單,充值等一系列微信支付場景;我們需要發起支付返回給前端支付sdk;該篇博文為微信支付的博文,支付寶支付的博文我也已編寫,若有興趣的話可以去我的博文中看看,希望能幫到你。
4.1發起支付請求
/**
* @Description: 支付請求
* @Param: [payType, outTradeNo, totalAmount]
* @return: java.util.Map<java.lang.String,java.lang.String>
* @Author: zengXianKang
* @Date: 2019/7/28
*/
@Override
public Map<String, String> payRequest(String payType, String outTradeNo, BigDecimal totalAmount) throws Exception {
Map<String, String> map = new HashMap<String, String>();
switch (PayTypeEnum.valueOf(payType)) {
case TENPAY://財付通
SortedMap<String, Object> paramsMap = new TreeMap<String, Object>();
//公眾賬號ID
paramsMap.put("appid", tenpayConfig.getAppId());
//商戶號
paramsMap.put("mch_id", tenpayConfig.getMchId());
//隨機字符串
paramsMap.put("nonce_str", Convert.getUUID());
//描述
paramsMap.put("body", "名繪優家訂單支付");
//商戶訂單號(支付編號)
paramsMap.put("out_trade_no", outTradeNo);
//支付金額,金額單位為 分
double price = totalAmount.doubleValue();
int totalFee = (int) (price * 100);
paramsMap.put("total_fee", String.valueOf(totalFee));
//回調地址
paramsMap.put("notify_url", ConstantInfo.TENPAY_ORDER_CALLBACK);
//交易類型
paramsMap.put("trade_type", "APP");
//用戶端ip
String spbillCreateIp = "";
InetAddress inetAddress = InetAddress.getLocalHost();
if (inetAddress != null) {
spbillCreateIp = inetAddress.getHostAddress();
}
paramsMap.put("spbill_create_ip", spbillCreateIp);
TenPayUtils tenPayUtils = new TenPayUtils();
//sign簽名
String sign = tenPayUtils.createSign(paramsMap, "UTF-8");
paramsMap.put("sign", sign);
//請求報文
String requestXml = TenPayUtils.tenPayXmlInfo(paramsMap);
//logger.info("微信支付請求報文: " + requestXml);
//發送微信支付post請求
String tenPayPost = TenPayUtils.httpsRequest(tenpayConfig.getPayUrl(), "POST", requestXml);
//獲取返回
Map<String, String> tenPayMap = (Map<String, String>) TenPayUtils.readXml(tenPayPost, "xml", "");
//微信返回狀態碼
if (!tenPayMap.get("return_code").equals("SUCCESS")) {
logger.error("微信支付請求連接失敗: " + tenPayMap.get("return_msg"));
throw new RRException(AppWSConstant.RS_MSG_TENPAY_FALL);
}
//業務結果
if (!tenPayMap.get("result_code").equals("SUCCESS")) {
logger.error("err_code: " + tenPayMap.get("err_code"), "err_code_des: " + tenPayMap.get("err_code_des"));
throw new RRException(AppWSConstant.RS_MSG_TENPAY_FALL);
}
//APPID
map.put("appid", tenPayMap.get("appid"));
//商戶號
map.put("partnerid", tenPayMap.get("mch_id"));
//預支付交易會話ID
map.put("prepayid", tenPayMap.get("prepay_id"));
//擴展字段
map.put("package", tenpayConfig.getPackageValue());
//隨機字符串
map.put("noncestr", tenPayMap.get("nonce_str"));
//時間戳
map.put("timestamp", String.valueOf(new Date().getTime()).substring(0, 10));
SortedMap<String, Object> signMap = new TreeMap<>(map);
String newSign = tenPayUtils.createSign(signMap, "UTF-8");
//簽名
map.put("sign", newSign);
break;
default:
break;
}
return map;
}
4.1.1ConstantInfo中的內容為
/** * 常量 */ public class ConstantInfo { //訂單支付財付通回調地址 public static String TENPAY_ORDER_CALLBACK = "http://mall.gzmhyj.com:8085/huakuEComBuyer/pay/tenPayOrderCallBack"; }
4.2訂單微信支付回調
該接口為為微信異步回調提供的接口
/**
* @Description: 訂單微信支付回調
*
* @Param: [request]
* @return: java.util.Map<java.lang.String,java.lang.String>
* @Author: zengXianKang
* @Date: 2019/7/28
*/
@RequestMapping(value = "/tenPayOrderCallBack", method = RequestMethod.POST)
@ResponseBody
public Map<String, String> tenPayOrderCallBack(HttpServletRequest request){
Map<String, String> map = new HashMap<String, String>();
try {
TenPayVO tenPayVO = payService.tenPayCallBack(request);
payService.tenPayOrderCallBack(tenPayVO);
map.put("return_code", "SUCCESS");
map.put("return_msg", "OK");
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
4.2.1payService的tenPayCallBack,用于解析回調的信息,拼接TenPayVO
/**
* @Description: 財付通回調
* @Param: [request]
* @return: TenPayVO
* @Author: zengXianKang
* @Date: 2019/7/28
*/
@Override
public TenPayVO tenPayCallBack(HttpServletRequest request) throws Exception {
InputStream inputStream = request.getInputStream();
StringBuffer resXml = new StringBuffer();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
String str;
while ((str = bufferedReader.readLine()) != null) {
resXml.append(str);
}
bufferedReader.close();
inputStream.close();
//logger.info("微信回調報文: " + resXml);
TenPayVO tenPayVO = this.tenPayCallBackInfo(resXml.toString(), "xml", "");
return tenPayVO;
}
4.2.2tenPayCallBackInfo,用于解析微信支付回調返回結果,拼接TenPayVO
/**
* @Description: 微信支付回調返回結果
* @Param: [xml, rootName, rowName]
* @return: com.huaku.ecom.system.model.vo.TenPayVO
* @Author: zengXianKang
* @Date: 2019/7/28
*/
private TenPayVO tenPayCallBackInfo(String xml, String rootName, String rowName) throws Exception {
Map<String, Object> resHashMap = (Map<String, Object>) TenPayUtils.readXml(xml, "xml", "");
SortedMap<String, Object> resMap = new TreeMap<String, Object>(resHashMap);
//微信返回狀態碼
if (!resMap.get("return_code").equals("SUCCESS")) {
logger.error("微信支付回調連接失敗: " + resMap.get("return_msg"));
throw new RRException(AppWSConstant.RS_MSG_TENPAY_FALL);
}
//業務結果
if (!resMap.get("result_code").equals("SUCCESS")) {
logger.error("err_code: " + resMap.get("err_code"), "err_code_des: " + resMap.get("err_code_des"));
throw new RRException(AppWSConstant.RS_MSG_TENPAY_FALL);
}
TenPayUtils tenPayUtils = new TenPayUtils();
//校驗簽名
String sign = tenPayUtils.createSign(resMap, "UTF-8");
if (!sign.equals(resMap.get("sign"))) {
logger.error("微信支付回調簽名不正確");
throw new RRException(AppWSConstant.RS_MSG_TENPAY_FALL);
}
TenPayVO tenPayVO = new TenPayVO();
//商戶訂單號
tenPayVO.setOutTradeNo((String) resMap.get("out_trade_no"));
//業務結果
tenPayVO.setResultCode((String) resMap.get("result_code"));
//簽名方式
tenPayVO.setSignType("ASCII");
//簽名
tenPayVO.setSign((String) resMap.get("sign"));
//交易類型
tenPayVO.setTradeType("APP");
//交易狀態
tenPayVO.setTradeState((String) resMap.get("trade_state"));
//商戶號
tenPayVO.setMchId((String) resMap.get("mch_id"));
//付款銀行
tenPayVO.setBankType((String) resMap.get("bank_type"));
//交易金額
BigDecimal totalFee = new BigDecimal((String) resMap.get("total_fee"));
totalFee = totalFee.divide(new BigDecimal(100));
tenPayVO.setTotalFee(totalFee);
//幣種
if (resMap.containsKey("fee_type")) {
tenPayVO.setFeeType((String) resMap.get("fee_type"));
}
//微信支付訂單號
tenPayVO.setTransactionId((String) resMap.get("transaction_id"));
//支付完成時間
tenPayVO.setTimeEnd((String) resMap.get("time_end"));
return tenPayVO;
}
4.2.3payService的tenPayOrderCallBack,用于自己的回調完成的業務邏輯,如修改訂單狀態,存微信支付交易表等操作
/**
* @Description: 微信支付訂單回調
* @Param: [tenPayVO]
* @return: void
* @Author: zengXianKang
* @Date: 2019/7/28
*/
@Override
public void tenPayOrderCallBack(TenPayVO tenPayVO) throws Exception {
if (tenPayVO != null && tenPayVO.getResultCode().equals("SUCCESS") && tenPayVO.getTradeState().equals("SUCCESS")) {
//根據交易編號加鎖,處理高并發
synchronized (tenPayVO.getOutTradeNo()) {
TOrder order = orderMapper.getOneOrderByPayNo(tenPayVO.getOutTradeNo());
if (order.getOrderStatus().equals(OrderStatusEnum.PENDING_PAYMENT.toString())) {
//訂單需支付金額總和
BigDecimal payNumSum = this.getPayNumSumByPayNo(tenPayVO.getOutTradeNo());
String orderStatus = "";
//以防萬一,再次校驗金額
if (payNumSum.compareTo(tenPayVO.getTotalFee()) != 0) {
logger.error("***訂單號: " + tenPayVO.getOutTradeNo() + "***微信支付支付金額與訂單需支付金額總和不一致***微信支付金額為:" + tenPayVO.getTotalFee() + " ***訂單需支付金額總為:" + payNumSum + "***日期:" + new Date());
//金額異常,訂單狀態為支付金額異常
orderStatus = OrderStatusEnum.ABNORMAL_PAYMENT_AMOUNT.toString();
} else {
//金額正常,訂單狀態為已付款(待發貨)
orderStatus = OrderStatusEnum.WAIT_FOR_DELIVERY.toString();
}
//修改訂單狀態
int orderFlag = orderMapper.updatePayOrderStatusByPayNo(orderStatus, tenPayVO.getOutTradeNo());
//微信支付交易記錄表
TTenpayTradeLog tenpayTradeLog = new TTenpayTradeLog();
tenpayTradeLog.setTradeLogId(Convert.createUniqueId(idWorker));
//簽名方式
tenpayTradeLog.setSignType(tenPayVO.getSignType());
//交易方式
tenpayTradeLog.setTradeMode(tenPayVO.getTradeType());
//交易狀態
tenpayTradeLog.setTradeStatus(tenPayVO.getResultCode());
//商戶號
tenpayTradeLog.setPartner(tenPayVO.getMchId());
//銀行類型
tenpayTradeLog.setBankType(tenPayVO.getBankType());
//交易金額
tenpayTradeLog.setTotalFee(tenPayVO.getTotalFee());
//幣種
tenpayTradeLog.setFeeType(tenPayVO.getFeeType());
//微信支付訂單號
tenpayTradeLog.setTransactionId(tenPayVO.getTransactionId());
//商戶訂單號
tenpayTradeLog.setOutTradeNo(tenPayVO.getOutTradeNo());
//支付完成時間
tenpayTradeLog.setTimeEnd(tenPayVO.getTimeEnd());
int payFlag = tenpayTradeLogMapper.insertSelective(tenpayTradeLog);
//若有一個操作出錯,拋錯回滾
if (!(orderFlag > 0 && payFlag == 1)) {
logger.error("微信支付訂單回調失敗");
throw new RRException(AppWSConstant.RS_MSG_TENPAY_FALL);
}
} else {
logger.info("該訂單已支付處理,交易編號為: " + tenPayVO.getOutTradeNo());
throw new RRException(AppWSConstant.RS_MSG_ORDER_PAY_ERROR);
}
}
}
}
4.3定時任務主動查詢微信支付回調,一般微信發起的異步回調都是無序不定時的,所以一般保險起見都會寫一個自己的定時任務主動查詢微信支付回調
/**
* 定時任務:每十五分鐘觸發一次主動調用訂單支付回調
*/
@Scheduled(cron = "0 */15 * * * ?")
public void initiativeOrderPayCallBack(){
//主動調用訂單支付回調
try {
payService.initiativeOrderPayCallBack();
} catch (Exception e) {
logger.error("timer initiativeOrderPayCallBack Error.", e);
e.printStackTrace();
}
}
4.3.1payService的initiativeOrderPayCallBack,用于主動查詢微信支付回調與回調業務邏輯處理
/**
* 主動調用訂單支付回調
*
* @throws Exception
*/
@Override
public void initiativeOrderPayCallBack() throws Exception {
//查詢訂單狀態為orderStatus的支付編號
List<Map<String, String>> payNoList = orderMapper.getPayNoByStatus(OrderStatusEnum.PENDING_PAYMENT.toString());
for (Map<String, String> map : payNoList) {
try {
switch (PayTypeEnum.valueOf(map.get("payType"))) {
case TENPAY://財付通
TenPayVO tenPayVO = this.tenPayQueryCallBack(map.get("payNo"));
//訂單回調處理
this.tenPayOrderCallBack(tenPayVO);
break;
default:
break;
}
} catch (Exception e) {
logger.error(e.getMessage());
e.printStackTrace();
}
}
}
4.3.2payService的tenPayQueryCallBack,用于主動查詢微信支付回調,拼接TenPayVO
/**
* @Description: 微信支付主動查詢回調
* @Param: [payNo]
* @return: com.huaku.ecom.system.model.vo.TenPayVO
* @Author: zengXianKang
* @Date: 2019/5/30
*/
@Override
public TenPayVO tenPayQueryCallBack(String payNo) throws Exception {
SortedMap<String, Object> paramsMap = new TreeMap<String, Object>();
//應用APPID
paramsMap.put("appid", tenpayConfig.getAppId());
//商戶號
paramsMap.put("mch_id", tenpayConfig.getMchId());
//商戶訂單號
paramsMap.put("out_trade_no", payNo);
//隨機字符串
paramsMap.put("nonce_str", Convert.getUUID());
TenPayUtils tenPayUtils = new TenPayUtils();
//簽名
String sign = tenPayUtils.createSign(paramsMap, "UTF-8");
paramsMap.put("sign", sign);
//請求報文
String requestXml = TenPayUtils.tenPayXmlInfo(paramsMap);
//發送微信查詢post請求
String tenQueryPost = TenPayUtils.httpsRequest(tenpayConfig.getQueryUrl(), "POST", requestXml);
TenPayVO tenPayVO = this.tenPayCallBackInfo(tenQueryPost, "xml", "");
return tenPayVO;
}
以上是“Springboot項目如何實現接入微信app支付”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。