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

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"); } }

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