第一步、準備必須數據,這些數據須要從註冊的微信受權的支付信息,加載進Spring容器中
pom座標html
<dependency> <groupId>com.github.wxpay</groupId> <artifactId>wxpay-sdk</artifactId> <version>0.0.3</version> </dependency>
weixinpay.propertiesjava
appid=wx********** partner=********** partnerkey=******* notifyurl=http://******.ngrok.io/WeChatPay/WeChatPayNotify
第二步、調用接口實現訪問微信接口得到相關信息
import com.alibaba.dubbo.config.annotation.Service; import com.github.wxpay.sdk.WXPayUtil; import com.pinyougou.mapper.TbOrderMapper; import com.pinyougou.mapper.TbPayLogMapper; import com.pinyougou.pay.service.PayService; import com.pinyougou.pojo.TbOrder; import com.pinyougou.pojo.TbPayLog; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.data.redis.core.RedisTemplate; import utils.HttpClientUtil; import javax.jws.Oneway; import java.util.Date; import java.util.HashMap; import java.util.Map; @Service public class PayServiceImpl implements PayService { @Autowired private RedisTemplate redisTemplate; @Value("${appid}") private String appid; //商家id @Value("${partner}") private String partner; //商戶號 @Value("${partnerkey}") private String partnerkey; //商戶密鑰 @Value("${notifyurl}") private String notifyurl; //回調地址,不是必要參數,可是必須傳 @Autowired private TbPayLogMapper payLogMapper; @Autowired private TbOrderMapper orderMapper; @Override public Map createNative(String out_trade_no, String total_fee) { //準備請求微信的api try { //參數設置微信支付鏈接地址 HttpClientUtil clientUtil = new HttpClientUtil("https://api.mch.weixin.qq.com/pay/unifiedorder"); //設置請求的參數 Map map = new HashMap(); map.put("appid", appid); map.put("mch_id", partner); map.put("nonce_str", WXPayUtil.generateUUID()); //32位要求的字符串 map.put("body", "隨便寫"); map.put("out_trade_no", out_trade_no); //內部訂單id map.put("total_fee", total_fee);//總價格 map.put("spbill_create_ip", "127.0.0.1");//寫個ip就行 map.put("notify_url", notifyurl); map.put("trade_type", "NATIVE"); //NATIVE是二維碼原生方式 String signedXml = WXPayUtil.generateSignedXml(map, partnerkey);//微信提供的方法 System.out.println("=請求內容=:"+signedXml); clientUtil.setHttps(true); //微信接口是https協議 clientUtil.setXmlParam(signedXml); //須要將參數設置爲xml格式的string字符串 clientUtil.post(); //發送post請求 //獲取返回值xml類型的string字符串 String content = clientUtil.getContent(); System.out.println("=返回內容="+content); //經過微信工具類將xml轉成map Map responseMap = WXPayUtil.xmlToMap(content);//微信提供的方法 Map returnMap = new HashMap<>(); //封裝map是爲了指定返回的參數內容 returnMap.put("out_trade_no", out_trade_no); returnMap.put("total_fee",total_fee ); returnMap.put("code_url", responseMap.get("code_url")); //微信支付地址 return returnMap; } catch (Exception e) { e.printStackTrace(); return null; } } //查詢支付狀態 @Override public Map queryPayStatus(String out_trade_no) { try { //準備map請求對象 Map map = new HashMap(); map.put("appid", appid); map.put("mch_id", partner); map.put("out_trade_no", out_trade_no); map.put("nonce_str", WXPayUtil.generateUUID()); //再經過微信工具類將map轉xml,並帶上簽名 String signedXml = WXPayUtil.generateSignedXml(map, partnerkey); System.out.println("=請求內容=:"+signedXml); //準備發送請求 HttpClientUtil clientUtil = new HttpClientUtil("https://api.mch.weixin.qq.com/pay/orderquery"); //設置請求方式是https clientUtil.setHttps(true); //設置請求參數 clientUtil.setXmlParam(signedXml); //發送請求 clientUtil.post(); //獲取返回結果而且將xml轉map對象 String content = clientUtil.getContent();//xml的string字符串 System.out.println("=返回內容="+content); Map<String, String> returnMap = WXPayUtil.xmlToMap(content); return returnMap; } catch (Exception e) { e.printStackTrace(); return null; } } }
第三步、測試
略git
會使用一個自定義工具類HttpClientUtilgithub