支付宝app支付总结

1、app公钥和私钥的生成
通过支付宝提供的工具即可下载并生成公钥和私钥,私钥自己保留,公钥要上传到支付宝账户所在的应用即可。

2、接下来就是通过app支付api调后台接口,后台通过公钥和私钥等 一系列的参数生成一段键值对给前端APP:

package com.qtay.gls.service.impl;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.response.AlipayTradeAppPayResponse;
import com.qtay.gls.dao.entity.AlipayTradeModel;
import com.qtay.gls.service.IPayService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class PayService implements IPayService {

    @Value("${alipay.app_id}")
    private String appId;

    @Value("${alipay.app_private_key}")
    private String appPrivateKey;

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

    @Value("${alipay.alipay_public_key}")
    private String alipayPublicKey;

    @Value("${alipay.sign_type}")
    private String signType;

    @Value("${alipay.notify_url}")
    private String notifyUrl;

    @Value("${alipay.server_url}")
    private String serverUrl;

    @Value("${alipay.format}")
    private String format;

    @Value("${alipay.product_code}")
    private String productCode;
    /**
     * 验签
     * @param alipayTradeModel
     * @return
     * @throws AlipayApiException
     */
    @Override
    public String getSign(AlipayTradeModel alipayTradeModel) throws AlipayApiException {

        //实例化客户端
        AlipayClient alipayClient = new DefaultAlipayClient(
                serverUrl,
                appId,
                appPrivateKey,
                format,
                charset,
                alipayPublicKey,
                signType);

        //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.pay
        AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
        //SDK已经封装掉了公共参数,这里只需要传入业务参数。以下方法为sdk的model入参方式(model和biz_content同时存在的情况下取biz_content)。
        AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
        model.setBody(alipayTradeModel.getBody());
        model.setSubject(alipayTradeModel.getSubject());
        model.setOutTradeNo(alipayTradeModel.getOutTradeNo());
        model.setTimeoutExpress(alipayTradeModel.getTimeoutExpress());
        model.setTotalAmount(alipayTradeModel.getTotalAmount());
        model.setProductCode(productCode);

        request.setBizModel(model);
        request.setNotifyUrl(notifyUrl);
        //这里和普通的接口调用不同,使用的是sdkExecute
        AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);
        System.out.println(response.getBody());//就是orderString 可以直接给客户端请求,无需再做处理。
        return response.getBody();
    }
}

3、前端APP支付成功之后会调上面设置的notifyUrl(注意如果设置了权限要取消该url的jwt用户认证,我这里的url是:/alipay/notify)。调用的时候这里有一个坑,也是我遇到的坑,在这里跟大家分享一下:
1)我的这个controller存在乱码问题,所以要设置

@RequestMapping(value = "/alipay", produces = "application/json; charset=utf-8")

否则 会报乱码错误,空指针什么的

18-01-04 13:49:42.755 DEBUG [http-nio-9090-exec-1] o.s.web.servlet.DispatcherServlet - Could not complete request
java.lang.NullPointerException: null
.antMatchers("/alipay/**").permitAll()

再一个就是,如果你页面本身就出现乱码问题,再通过下面这段代码设置也是没有用的,切记!

            //乱码解决,这段代码在出现乱码时使用。
            valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
package com.qtay.gls.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping(value = "/alipay", produces = "application/json; charset=utf-8")
public class AlipayController {

    private Logger log = LoggerFactory.getLogger(AlipayController.class);

    @Value("${alipay.alipay_public_key}")
    private String alipayPublicKey;

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

    @Value("${alipay.sign_type}")
    private String signType;

    /**
     * 支付宝支付通知接口:
     *
     * @param
     * @return
     */
    @PostMapping("/notify")
    public String notify(HttpServletRequest request) throws AlipayApiException, UnsupportedEncodingException {
        log.info("支付宝支付结果通知" + request.getParameterMap().toString());
        //获取支付宝POST过来反馈信息
        Map<String, String> params = new HashMap<>();
        Map requestParams = request.getParameterMap();
        for (Object o : requestParams.keySet()) {
            String name = (String) o;
            String[] values = (String[]) requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i]
                        : valueStr + values[i] + ",";
            }
            //乱码解决,这段代码在出现乱码时使用。
//            valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
            params.put(name, valueStr);
            log.info(name + "=" + valueStr);
        }
        //切记alipaypublickey是支付宝的公钥,请去open.alipay.com对应应用下查看。
        //boolean AlipaySignature.rsaCheckV1(Map<String, String> params, String publicKey, String charset, String sign_type)
        boolean signVerified = AlipaySignature.rsaCheckV1(params, alipayPublicKey, charset, signType);
        //todo:flag为什么是false
        log.info("================== signVerified ==================" + signVerified);
        if (signVerified) {
            if ("TRADE_SUCCESS".equals(params.get("trade_status"))) {
                //付款金额
                String amount = params.get("buyer_pay_amount");
                //商户订单号
                String out_trade_no = params.get("out_trade_no");
                //支付宝交易号
                String trade_no = params.get("trade_no");

                log.info("amount=" + amount + ",out_trade_no=" + out_trade_no + ",trade_no=" + trade_no);
                return "success";
            }
        }
        return "fail";
    }
}

参考文章:
1、一步一步带你完成支付宝支付功能的集成(超详细)
2、spring Boot 中文返回给浏览器乱码 解析成问号?? fastJson jackJson

    原文作者:Awbeci
    原文地址: https://segmentfault.com/a/1190000012700062
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞