简单的测试支付宝支付的接入

DBC 800 0
我们可以定义一个成功返回的配置类
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;



@Configuration
@Data
public class PayUrlConfig {

    /**
     * 支付成功页面跳转
     */
    @Value("${alipay.success_return_url}")
    private String alipaySuccessReturnUrl;


    /**
     * 支付成功,回调通知
     */
    @Value("${alipay.callback_url}")
    private String alipayCallbackUrl;
}
当然,配置文件需要添加
#支付宝配置
alipay:
  #支付成功的跳转页面
  success_return_url: https://xdclass.net

  #支付宝通知回调接口
  callback_url: https://xdclass.net
重点,重头戏,我们在支付宝支付申请到的各种参数,放入进去,只需要修改TODO的位置
package net.xdclass.config;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;



public class AlipayConfig {

    /**
     * 支付宝网关地址  TODO
     */
    public static final  String PAY_GATEWAY="https://openapi.alipaydev.com/gateway.do";

    /**
     * 支付宝 APPID  TODO
     */
    public static final  String APPID="2016092000555936";

    /**
     * 应用私钥 TODO
     */
    public static final String APP_PRI_KEY = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCPULg2k9ugRE3tGQXLTg6Ez7mtpsei9bmHiHomwJ3lxZ19jR6R/BcG9r5UnCDvRl5gKJ4uVAA4Tvf4M+5e3d+0fiC+Mchcg9p5hv1GJvrsmSup3yWFZ4PNKyh0Qdz/yOWNoKo1Ya1IOQ3uIYPz0tpQuBPs3TZjcTsYxVWs6fbChB2Lbo9dxUcFWC7nWOq2ZD9DLa+P0lPMMGOWo41CBumCzn4JrbbQ9cXPdMc33HUO1J9XqRGpmxH1y0idNt9Qcne+QrskMzisBip9YLbdN0hNzJVyqKXBDUVV8OQBPFruD0SDaVf85Ue+k0Moz16LOD6D58tFViL/sLJOElwlFR3vAgMBAAECggEAXl037jfNFOnkpYkkV4k95hUgnUaDjLlfP+6M/5c0YGaOHG0fA+CJCPzOtcdXqDUdTxNvk/Gbc3gLrlIDUlOlSKMDz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JlwIvfHBJNoRPnaaY+KvluvbVWNxvkcW";

    /**
     * 支付宝公钥   TODO
     */
    public static final String ALIPAY_PUB_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAk8OPeu/Bhbxr8chgRXnDSm0FDMZoK3r/qLLzqbv+L2/mYTLxHKw5U3c5XPHXar5SVWRp+IgLsZdTfYeZ+jYFmY+dTYk8mG8GXc1uYwHFADcWQAcR4KA3cbxiaubg7g//ECEhwA5CmaosjJ2p8UAz4j7cDB37TDON7oGMFbkXnSBlGhj5oy6rxdUXS+KeG9YmRPkQU3x3ljK37RRLFhwPPRR218IGDCmCtnI5ddKEq1hMAPnYmKNezerxSGGxxIaggt+sDwN1S3GNNSU7AelLPuvNEza49KaQijsBJpaLKUvqt5KO4IVfvPCmRsIFe7KzgdEmjygIY81qXacPzopJIQIDAQAB";

    /**
     * 返回参数格式
     */
    public static final  String FORMAT="json";

    /**
     * 签名类型
     */
    public static final  String SIGN_TYPE="RSA2";


    /**
     * 字符编码
     */
    public static final  String CHARSET="UTF-8";

    /**
     * 构造函数私有化
     */
    private AlipayConfig(){

    }


    /*
    volatile  与下面进行双重锁校验
     */
    private volatile static AlipayClient instance = null;


    /**
     * 单例模式获取, 双重锁校验
     * @return
     */
    public static AlipayClient getInstance(){

        if(instance==null){
            synchronized (AlipayConfig.class){
                if(instance == null){
                    instance = new DefaultAlipayClient(PAY_GATEWAY,APPID,APP_PRI_KEY,FORMAT,CHARSET,ALIPAY_PUB_KEY,SIGN_TYPE);
                }
            }
        }
        return instance;
    }



}
接下来就是发起请求的测试了
    /**
     * 测试支付方法
     */
    @GetMapping("test_pay")
    public void testAlipay(HttpServletResponse response) throws AlipayApiException, IOException {

        HashMap<String,String> content = new HashMap<>();
        //商户订单号,64个字符以内、可包含字母、数字、下划线;需保证在商户端不重复
        String no = UUID.randomUUID().toString();

        log.info("订单号:{}",no);
        content.put("out_trade_no", no);

        content.put("product_code", "FAST_INSTANT_TRADE_PAY");

        //订单总金额,单位为元,精确到小数点后两位
        content.put("total_amount", String.valueOf("111.99"));

        //商品标题/交易标题/订单标题/订单关键字等。 注意:不可使用特殊字符,如 /,=,&amp; 等。
        content.put("subject", "杯子");

        //商品描述,可空
        content.put("body", "好的杯子");

        // 该笔订单允许的最晚付款时间,逾期将关闭交易。取值范围:1m~15d。m-分钟,h-小时,d-天,1c-当天(1c-当天的情况下,无论交易何时创建,都在0点关闭)。 该参数数值不接受小数点, 如 1.5h,可转换为 90m。
        content.put("timeout_express", "5m");


        AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
        request.setBizContent(JSON.toJSONString(content));
        request.setNotifyUrl(payUrlConfig.getAlipayCallbackUrl());
        request.setReturnUrl(payUrlConfig.getAlipaySuccessReturnUrl());

        AlipayTradeWapPayResponse alipayResponse  = AlipayConfig.getInstance().pageExecute(request);

        if(alipayResponse.isSuccess()){
            System.out.println("调用成功");

            String form = alipayResponse.getBody();

            response.setContentType("text/html;charset=UTF-8");
            response.getWriter().write(form);
            response.getWriter().flush();
            response.getWriter().close();

        } else {
            System.out.println("调用失败");
        }
    }
这样就成功啦!

简单的测试支付宝支付的接入插图
简单的测试支付宝支付的接入插图2

发表评论 取消回复
表情 图片 链接 代码

分享