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();
// 成功返回 - SUCCESS,就是"SUCCESS".equals(code)
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"; } }

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM