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

至此支付宝支付接入完成,经过测试,注意参数配置.

 

WEB端第三方支付接入 - 支付宝 - 扫码支付

上一篇:Anaconda + Djongo + spyder 网站开发 (三)


下一篇:jsp中basa标签的使用