1. 程式人生 > >微信支付4步驟搞定

微信支付4步驟搞定

公眾號 bridge mage 步驟 訂單 notify pack key .class

項目需要搞微信支付,百度了那麽多但是太復雜了,還有騰訊的那些個文檔真是RLGL

下面直接上步驟:

第一步:加入第三方jar包:

<dependency>
<groupId>cn.springboot</groupId>
<artifactId>best-pay-sdk</artifactId>
<version>1.1.0</version>
</dependency>


第二步:在項目根路徑下創建配置文件weixinpay.properties,當然你寫死在代碼裏也可以。(這步就可以省略)
# 公眾賬號ID
weixin.mpAppId=
#商戶號
weixin.mchId=
#商戶密鑰
weixin.mchKey=
商戶證書路徑
weixin.keyPath=
#微信支付異步通知地址
weixin.notifyUrl=

第三步:創建微信支付配置文件類
package com.github.lly835.config;
import com.lly835.bestpay.config.WxPayH5Config;
import com.lly835.bestpay.service.impl.BestPayServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.ResourceBundle;

/**
* 微信支付配置文件
*/
@Component
public class PayConfig {
@Bean
public WxPayH5Config wxPayH5Config() {
WxPayH5Config wxPayH5Config = new WxPayH5Config();
ResourceBundle resource = ResourceBundle.getBundle("weixinpay");//不要加.properties後綴,我加了報錯
        wxPayH5Config.setAppId(resource.getString("weixin.mpAppId")); //這裏的參數你如果寫死,第二步可以省略
wxPayH5Config.setMchId(resource.getString("weixin.mchId"));
wxPayH5Config.setMchKey(resource.getString("weixin.mchKey"));
wxPayH5Config.setKeyPath(resource.getString("weixin.keyPath"));
wxPayH5Config.setNotifyUrl(resource.getString("weixin.notifyUrl"));
return wxPayH5Config;
}
@Bean
public BestPayServiceImpl bestPayService(WxPayH5Config wxPayH5Config) {
BestPayServiceImpl bestPayService = new BestPayServiceImpl();
bestPayService.setWxPayH5Config(wxPayH5Config);
return bestPayService;
}
}

第四步:微信支付調用

/**
* 微信支付
*/
@Controller
@Slf4j
public class PayController {

final static Logger log = LoggerFactory.getLogger(PayController.class);

@Autowired
private BestPayServiceImpl bestPayService;


/**
* 發起微信支付
* @param openid 用戶唯一標識
* @param orderAmount 訂單金額
* @param orderName 訂單描述
* @param orderId 訂單編號
* @param map
* @return
*/
@GetMapping(value = "/pay")
public ModelAndView pay(@RequestParam("openid") String openid,Double orderAmount,String orderName,String orderId,
Map<String, Object> map) {
PayRequest request = new PayRequest();

//支付請求參數
request.setPayTypeEnum(BestPayTypeEnum.WXPAY_H5);
request.setOrderId(orderId);
request.setOrderAmount(orderAmount);
request.setOrderName(orderName);
request.setOpenid(openid);
log.info("【發起支付】request={}", JsonUtil.toJson(request));

PayResponse payResponse = bestPayService.pay(request);
log.info("【發起支付】response={}", JsonUtil.toJson(payResponse));

map.put("payResponse", payResponse);

return new ModelAndView("weixin/create", map);
}

/**
* 異步回調
* @param notifyData <return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg>
* @return
* @throws Exception
*/
@PostMapping(value = "/notify")
public ModelAndView notify(@RequestBody String notifyData) throws Exception {
log.info("【異步回調】request={}", notifyData);
PayResponse response = bestPayService.asyncNotify(notifyData);
log.info("【異步回調】response={}", JsonUtil.toJson(response));
return new ModelAndView("weixin/success");
}
}
前端代碼:
create.ftl代碼

<script>
function onBridgeReady(){
WeixinJSBridge.invoke(
‘getBrandWCPayRequest‘, {
"appId":"${payResponse.appId}", //公眾號名稱,由商戶傳入
"timeStamp":"${payResponse.timeStamp}", //時間戳,自1970年以來的秒數
"nonceStr":"${payResponse.nonceStr}", //隨機串
"package":"${payResponse.packAge}",
"signType":"MD5", //微信簽名方式:
"paySign":"${payResponse.paySign}" //微信簽名
},
function(res){
if(res.err_msg == "get_brand_wcpay_request:ok" ) {
alert(‘支付成功‘);
}else if(res.err_msg == "get_brand_wcpay_request:cancel") {
alert(‘支付過程中用戶取消‘);
}else if(res.err_msg == "get_brand_wcpay_request:fail") {
alert(‘支付失敗‘);
}else {
alert(‘未知異常‘);
}
}
);
}
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();
}
</script>

success.ftl代碼

<xml>
<return_code><![CDATA[SUCCESS]]></return_code>
<return_msg><![CDATA[OK]]></return_msg>
</xml>


整個項目結構圖:

技術分享圖片






微信支付4步驟搞定