1. 程式人生 > 實用技巧 >WEB端第三方支付接入 - 微信 - 掃碼支付

WEB端第三方支付接入 - 微信 - 掃碼支付

需要接入微信支付了

微信那邊需要支付商戶號.

百度搜微信支付,按照官網操作就可以了.

直接上程式碼:

1 - 引入依賴

<dependency>
    <groupId>com.github.binarywang</groupId>
    <artifactId>weixin-java-pay</artifactId>
    <version>3.8.0</version>
</dependency>

2 - 配置

import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService; import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration
public class WxpayConfig { @Value("${***}") private String appId; @Value("${***}") private String mchId; @Value("${***}") private String key; @Bean public WxPayService wxService() { WxPayConfig payConfig = new WxPayConfig(); payConfig.setAppId(appId); payConfig.setMchId(mchId); payConfig.setMchKey(key); WxPayService wxPayService
= new WxPayServiceImpl(); wxPayService.setConfig(payConfig); return wxPayService; } }

3 - 微信PC支付Native方式,介面就不寫了,呼叫方法而已,返回的是用於生成二維碼的url

// 注入wxNotifyUrl,wxTradeType,wxRequestIp,具體引數含義請參考官方說明

//
微信支付 public String wxNativePay(WXpayPara para) throws WxPayException { WxPayUnifiedOrderRequest orderRequest = new WxPayUnifiedOrderRequest(); orderRequest.setDeviceInfo("WEB"); orderRequest.setNotifyUrl(wxNotifyUrl); orderRequest.setOutTradeNo(uuid()); orderRequest.setSpbillCreateIp(wxRequestIp); orderRequest.setTotalFee(BaseWxPayRequest.yuanToFen(para.getTotalFee().toString())); orderRequest.setProductId(outTradeNo); orderRequest.setBody(para.getBody()); orderRequest.setTradeType(wxTradeType);
// 可以進行訂單記錄相關處理
// 返回一個地址,給前端生成二維碼,使用者掃碼支付
return result.getCodeUrl(); }

4 - 微信支付回撥,如果不成功就會按照時間回撥,官網有說明

// 注入WxPayService

public
void wxPayNotify(HttpServletRequest request) throws IOException, WxPayException, SQLException { String xmlRequest = IOUtils.toString(request.getInputStream(), request.getCharacterEncoding()); WxPayOrderNotifyResult notifyResult = wxPayService.parseOrderNotifyResult(xmlRequest); String outTradeNo = notifyResult.getOutTradeNo(); if (!isSuccess(notifyResult.getResultCode())) { throw new WxPayException("WX Notify status is not SUCCESS"); } // 根據outTradeNo查詢訂單是否存在,因為我在支付的時候先建立訂單,省略查詢程式碼
if (order == null) { throw new SQLException(); } // 如果已支付,就不做處理了,因為可能回撥多次 if (order.isPay()) { return; } String totalFee = BaseWxPayResult.fenToYuan(notifyResult.getTotalFee()); BigDecimal totalAmount = new BigDecimal(totalFee);
// 判斷金額是否一致
if (order.getAmount().compareTo(totalAmount) != 0) { throw new WxPayException("WX Notify totalFee is not equal"); } String tradeNo = notifyResult.getTransactionId(); // 記錄交易號,省略程式碼 }

5 - 介面,返回訊息給微信

// 微信非同步通知
@RequestMapping(value = "***")
public String wxPayNotify(HttpServletRequest request, HttpServletResponse response) {
try { payService.wxPayNotify(request); String resultXml = "<xml>" + "<return_code><![CDATA[SUCCESS]]></return_code>" + "<return_msg><![CDATA[OK]]></return_msg>" + "</xml> "; response.setContentType("text/xml"); BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream()); out.write(resultXml.getBytes()); out.flush(); out.close(); return resultXml; } catch (Exception e) { e.printStackTrace(); return "failure"; } }

微信接入完成,經過測試,注意配置引數.