java 微信公衆号微信支付
微信支付開發
一、引入需要的庫
首先引入github 微信支付和公衆号支援庫
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-mp</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-pay</artifactId>
<version>4.1.0</version>
</dependency>
第二步
配置
代碼如下(示例):
#公衆号微信配置
application.wechat.mp.configs[0].appId=
application.wechat.mp.configs[0].secret=
application.wechat.mp.configs[0].token=
application.wechat.mp.configs[0].aesKey=
application.wechat.mp.useRedis=false
#公衆号微信支付配置
application.wechat.pay.appId=
application.wechat.pay.mchId=
application.wechat.pay.mchKey=
#keyPath p12證書的位置,可以指定絕對路徑,也可以指定類路徑(以classpath:開頭)
application.wechat.pay.keyPath=
二、配置微信參數
公衆号的自行找demo配置
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author Binary Wang
*/
@Configuration
@ConditionalOnClass(WxPayService.class)
@EnableConfigurationProperties(WxPayProperties.class)
@AllArgsConstructor
public class WxPayConfiguration {
private WxPayProperties properties;
@Bean
@ConditionalOnMissingBean
public WxPayService wxService() {
WxPayConfig payConfig = new WxPayConfig();
payConfig.setAppId(StringUtils.trimToNull(this.properties.getAppId()));
payConfig.setMchId(StringUtils.trimToNull(this.properties.getMchId()));
payConfig.setMchKey(StringUtils.trimToNull(this.properties.getMchKey()));
payConfig.setSubAppId(StringUtils.trimToNull(this.properties.getSubAppId()));
payConfig.setSubMchId(StringUtils.trimToNull(this.properties.getSubMchId()));
payConfig.setKeyPath(StringUtils.trimToNull(this.properties.getKeyPath()));
// 可以指定是否使用沙箱環境
payConfig.setUseSandboxEnv(false);
WxPayService wxPayService = new WxPayServiceImpl();
wxPayService.setConfig(payConfig);
return wxPayService;
}
}
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* wxpay pay properties.
*
* @author Binary Wang
*/
@Data
@ConfigurationProperties(prefix = "application.wechat.pay")
public class WxPayProperties {
/**
* 設定微信公衆号或者小程式等的appid
*/
private String appId;
/**
* 微信支付商戶号
*/
private String mchId;
/**
* 微信支付商戶密鑰
*/
private String mchKey;
/**
* 服務商模式下的子商戶公衆賬号ID,普通模式請不要配置,請在配置檔案中将對應項删除
*/
private String subAppId;
/**
* 服務商模式下的子商戶号,普通模式請不要配置,最好是請在配置檔案中将對應項删除
*/
private String subMchId;
/**
* apiclient_cert.p12檔案的絕對路徑,或者如果放在項目中,請以classpath:開頭指定
*/
private String keyPath;
}
3前端調用預訂單擷取
背景統一下單擷取jsapi支付參數封裝一些必要參數
@ApiOperation(value = "原生的統一下單接口")
@PostMapping("/unifiedOrder")
public Result unifiedOrder(@RequestBody Object object) throws WxPayException {
WxPayUnifiedOrderRequest request = new WxPayUnifiedOrderRequest();
request.setDeviceInfo("WEB");
request.setOpenid("111111");//必須要
request.setSignType("MD5");
request.setBody("測試1"); //必填項 産品資訊
request.setFeeType("CNY");//必填項 預設cny
request.setTotalFee(1);//必填項 訂單總金額,機關為分,詳見支付金額
return payService.createOrder(request);
}
service處理訂單資訊
import com.alibaba.fastjson.JSONObject;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
import com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderResult;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import lombok.AllArgsConstructor;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
@Service
@AllArgsConstructor
public class OrderService implements IOrderService {
private final static Log logger = LogFactory.getLog(OrderService.class);
private WxPayService wxService;
private WxPayProperties wxPayProperties;
private SystemCfg systemCfg;
@Override
public Result createOrder(WxPayUnifiedOrderRequest request) throws WxPayException {
Result result = new Result();
//生成訂單号
request.setNotifyUrl("127.0.0.1/pay/notify/order"); //回調位址
request.setSpbillCreateIp("127.0.0.1");//伺服器ip 必填
request.setTradeType("JSAPI"); //必填JSAPI
WxPayUnifiedOrderResult wxResult =this.wxService.unifiedOrder(request);
logger.info("\n微信發起預訂單接收到請求消息,内容:"+JsonUtils.toJson(wxResult));
if(wxResult.getReturnCode().equals("SUCCESS")){
// wxOrder.add
signOrder(result, wxResult);
}else {
result.setErrCode(1);
result.setErrMsg("微信發起訂單失敗");
}
return result;
}
/**
* 封裝微信預訂單生成成功後傳回的方法
* @param result
* @param wxResult
*/
private void signOrder(Result result, WxPayUnifiedOrderResult wxResult) {
//封裝JSAPI調起支付需要的參數
TreeMap<String, String> stringStringTreeMap = new TreeMap<>();
String timeStamp = getSecondTimestampTwo(new Date());
stringStringTreeMap.put("appId",wxResult.getAppid());
stringStringTreeMap.put("timeStamp",timeStamp);
stringStringTreeMap.put("nonceStr",wxResult.getNonceStr());
stringStringTreeMap.put("package","prepay_id="+wxResult.getPrepayId());
stringStringTreeMap.put("signType","MD5");
String sign = MD5Utils.encodeSign(stringStringTreeMap, wxPayProperties.getMchKey());
WxSignVo vo = new WxSignVo();
vo.setAppId(wxResult.getAppid());
vo.setTimeStamp(timeStamp);
vo.setNonceStr(wxResult.getNonceStr());
vo.setPrepay_id(wxResult.getPrepayId());
vo.setSignType("MD5");
vo.setPaySign(sign);
String encode="";
//這裡是我加密的可以不用
try {
encode = EncodeUtil.encryptDES(JSONObject.toJSONString(vo),"12345");
} catch (Exception e) {
e.printStackTrace();
}
result.setData(encode);
}
/**
* 擷取精确到秒的時間戳
* @param date
* @return
*/
public static String getSecondTimestampTwo(Date date){
if (null == date) {
return "";
}
String timestamp = String.valueOf(date.getTime()/1000);
return timestamp;
}
}
這裡是需要用到的MD5UTILS
import org.apache.commons.lang3.StringUtils;
import java.security.MessageDigest;
import java.util.*;
public class MD5Utils {
/**
* sign 簽名 (參數名按ASCII碼從小到大排序(字典序)+key+MD5+轉大寫簽名)
* @param map
* @return
*/
public static String encodeSign(SortedMap<String,String> map, String key){
if(StringUtils.isEmpty(key)){
throw new RuntimeException("簽名key不能為空");
}
Set<Map.Entry<String, String>> entries = map.entrySet();
Iterator<Map.Entry<String, String>> iterator = entries.iterator();
List<String> values =new ArrayList();
while(iterator.hasNext()){
Map.Entry entry = (Map.Entry) iterator.next();
String k = String.valueOf(entry.getKey());
String v = String.valueOf(entry.getValue());
if (StringUtils.isNotEmpty(v) && entry.getValue() !=null && !"sign".equals(k) && !"key".equals(k)) {
values.add(k + "=" + v);
}
}
values.add("key="+ key);
String sign = StringUtils.join(values, "&");
return encodeByMD5(sign).toUpperCase();
}
/**
* 通過MD5加密
*
* @param algorithmStr
* @return String
*/
public static String encodeByMD5(String algorithmStr) {
if (algorithmStr==null) {
return null;
}
try {
MessageDigest messageDigest = MessageDigest.getInstance("md5");
messageDigest.update(algorithmStr.getBytes("utf-8"));
return getFormattedText(messageDigest.digest());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private static String getFormattedText(byte[] digest){
StringBuffer buffer = new StringBuffer();
//把每一個byte,做一個與運算,0xff
for (byte b :
digest) {
int number=b & 0xff;//加鹽
String str = Integer.toHexString(number);
if (str.length() == 1){
buffer.append("0");
}
buffer.append(str);
}
//标準的md5加密後的結果
return buffer.toString();
}
public static void main(String[] args) {
TreeMap<String, String> stringStringTreeMap = new TreeMap<>();
stringStringTreeMap.put("appId","appId");
stringStringTreeMap.put("timeStamp","timeStamp");
stringStringTreeMap.put("nonceStr","nonceStr");
stringStringTreeMap.put("package","prepay_id=111");
stringStringTreeMap.put("signType","MD5");
String s = MD5Utils.encodeSign(stringStringTreeMap, "111");
System.out.println(s);
}
}
附帶前端喚起支付的js
function onBridgeReady(){
WeixinJSBridge.invoke(
'getBrandWCPayRequest', {
"appId":"wx2421b1c4370ec43b", //公衆号ID,由商戶傳入
"timeStamp":"1395712654", //時間戳,自1970年以來的秒數
"nonceStr":"e61463f8efa94090b1f366cccfbbb444", //随機串
"package":"prepay_id=u802345jgfjsdfgsdg888",
"signType":"MD5", //微信簽名方式:
"paySign":"70EA570631E4BB79628FBCA90534C63FF7FADD89" //微信簽名
},
function(res){
if(res.err_msg == "get_brand_wcpay_request:ok" ){
// 使用以上方式判斷前端傳回,微信團隊鄭重提示:
//res.err_msg将在使用者支付成功後傳回ok,但并不保證它絕對可靠。
}
});
}
if (typeof WeixinJSBridge == "undefined"){
if( document.addEventListener ){
document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
}else if (document.attachEvent){
document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
}
}else{
onBridgeReady();
}
總結
本文僅僅簡單介紹了微信喚起支付的使用 回顯回調的話函數和方法自己出處理邏輯。