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


需要接入支付寶支付了

支付寶支付相對於微信支付接入更麻煩一些,要配置密鑰啥的

需要支付寶開放平台賬號,並開通網站支付相關權限,具體查看官方網站

上代碼:

1 - 引入依賴

<dependency>
    <groupId>com.alipay.sdk</groupId>
    <artifactId>alipay-sdk-java</artifactId>
    <version>4.10.0.ALL</version>
</dependency>

2 - 配置,具體參數自行設置

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AlipayConfig {

    @Value("${***}")
    private String appId;

    @Value("${***}")
    private String gateway;

    @Value("${***}")
    private String privateKey;

    @Value("${***}")
    private String alipayPublicKey;

    @Value("${***}")
    private String charset;

    @Value("${***}")
    private String signType;

    @Bean
    public AlipayClient alipayClient() throws AlipayApiException {
        return new DefaultAlipayClient(gateway, appId, privateKey, "json", charset, alipayPublicKey, signType);
    }
}

3 -  支付,參數自行寫入,返回的是一個鏈接,前端直接打開就可以了,接口就不寫了

public String aliPagePay(AlipayPara para) throws AlipayApiException {
    AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
    alipayRequest.setNotifyUrl(aliNotifyUrl);
    JSONObject job = new JSONObject();
    job.put("out_trade_no", uuid());
    job.put("product_code", para.getProductCode());
    job.put("total_amount", para.getTotalAmount().toString());
    job.put("subject", para.getSubject());
    alipayRequest.setBizContent(job.toJSONString());
    // 記錄支付單相關操作,代碼省略
    AlipayResponse response = alipayClient.pageExecute(alipayRequest, "GET");
    return response.getBody();
}

4 - 異步通知,驗簽,同步沒用到,就不寫了

public void aliPagePayNotifyMethod(HttpServletRequest request) throws AlipayApiException, SQLException {
    // 驗簽
if (!aliPagePayRsaCheck(request)) { throw new AlipayApiException("AliPagePay notify Rsa Check Failed"); } String outTradeNo = getParameter(request, "out_trade_no"); // 由於我支付時創建了支付單,這里查詢支付單
if (order == null) { throw new SQLException(); } String tradeStatus = getParameter(request, "trade_status");
// 支付成功,可能回調多次
if ("TRADE_FINISHED".equals(tradeStatus) || "TRADE_SUCCESS".equals(tradeStatus)) { // 查訂單,已支付就不處理了 if (order.isPay()) { return; } String totalAmount = getParameter(request, "total_amount");
// 判斷金額是否一致
if (order.getAmount().compareTo(new BigDecimal(totalAmount)) != 0) { throw new AlipayApiException(); } String tradeNo = getParameter(request, "trade_no"); // 記錄交易號 } else { throw new AlipayApiException(); } }

5 - 驗簽方法

private boolean aliPagePayRsaCheck(HttpServletRequest request) throws AlipayApiException {
    Map<String, String> maps = new HashMap<>();
    Map<String, String[]> requestParams = request.getParameterMap();
    for (String name : requestParams.keySet()) {
        String[] values = requestParams.get(name);
        String valueStr = "";
        for (int i = 0; i < values.length; i++) {
            valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
        }
        maps.put(name, valueStr);
    }
    return AlipaySignature.rsaCheckV1(maps, alipayPublicKey, charset, signType);
}

6 - 獲取參數方法 - 處理亂碼問題 - 有可能 - 沒具體測試,參考網上代碼加的

private String getParameter(HttpServletRequest request, String paramName) {
    return handleEncodeingError(request.getParameter(paramName));
}
private String handleEncodeingError(String str) {
    return new String(str.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
}

7 - 異步接口

// 注入

@RequestMapping(value = "***") public void aliPayNotify(HttpServletRequest request, HttpServletResponse response) throws IOException { try { payService.aliPagePayNotifyMethod(request); response.getWriter().println("success"); } catch (Exception e) {
// 記錄下日志 response.getWriter().println(
"fail"); } }

至此支付寶支付接入完成,經過測試,注意參數配置.

 


免責聲明!

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



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