zoukankan      html  css  js  c++  java
  • Java Web接入支付宝扫码付款API(使用SDK证书配置properties )

    一、支付宝当面付功能Demo

    官方文档https://docs.open.alipay.com/194/106078/

    ps:因为沙箱环境一经配置了证书公钥则无法重置

    第一步下载项目SKD&Demo

    点击下载

     解压后笔者使用的是eclipse,直接导入

    一个TradePayDemo项目,一个是alipay-trade-sdk-20161215.jar的源码项目- TradePaySDK项目

    笔者直接删掉了alipay-trade-sdk-20161215.jar依赖,使用项目依赖TradePayDemo添加,TradePaySDK项目

    这样就可以直接点击查看源码方法实现的内容,主要实现运行Demo项目里面的main方法,跑得起来不报错就OK了

    二、修改配置

     找到zfbinfo.properties文件配置

    话不多说直接看注释跟下载的对比区别增加了,证书路径配置

    怎么搞证书看这里 证书密钥

    # 支付宝网关名、partnerId和appId
    open_api_domain = https://openapi.alipaydev.com/gateway.do
    mcloud_api_domain = http://mcloudmonitor.com/gateway.do
    pid = #沙箱环境的话有个UID就是了
    appid =  #你的APPID
    
    # RSA私钥、公钥和支付宝公钥
    private_key = MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCiZjPiY1TzonKkgmTDPjiexKvcwgGw0jx7W3+C83BzOaa+ZsPc7rf+hl/93cJTTtUk8mDyPKbxWgJBZ+OBW4eaS2pgl6m1aMTi8/T/+En0kM0MHKnAPhimBFROJ8fUkXMb4PTCZAfcOmGF1i+xnfHpgy+7D96Maw+eWXrO7wL5+U5cB/8ZoeBl9Y+QhKOqNuVh7QHxrbim1X/PTeBrblFcqjDFCOzpRczWVPGqd0khctOF75uFkzEqzF7Mp6ACQckJQlRIFiiv/Ox9N6sc/zfDoXJkxp8UXw0qO9Z7by+dEev7ECSW4NKU39kh2AhNVkDZXZJM8rdQpWwT5crxn/0ZAgMBAAECggEAMQ3WFkgS3RTwtCh+70rYJ30YvLINeK/IfSqCNbf6+5MqftDj0NR613MXg8N8c1WYpHRiPEl90+FwVPlgiy5uo3isRUkKhjWDY1p1YuxIyosCpOnz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PwGw3VPouE2fHM/6fVwaQV/SwL8jGnVHE=
    
    public_key = MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAomYz4mNU86JypIJkwz44nsSr3MIBsNI8e1t/gvNwczmmvmbD3O63/oZf/d3CU07VJPJg8jym8VoCQWfjgVuHmktqYJeptWjE4vP0//hJ9JDNDBypwD4YpgRUTifH1JFzG+D0wmQH3DphhdYvsZ3x6YMvuw/ejGsPnll6zu8C+flOXAf/GaHgZfWPkISjqjblYe0B8a24ptV/z03ga25RXKowxQjs6UXM1lTxqndJIXLThe+bhZMxKsxezKegAkHJCUJUSBYor/zsfTerHP83w6FyZMafFF8NKjvWe28vnRHr+xAkluDSlN/ZIdgITVZA2V2STPK3UKVsE+XK8Z/9GQIDAQAB
     
    #SHA1withRsa对应支付宝公钥
    alipay_public_key = MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDIgHnOn7LLILlKETd6BFRJ0GqgS2Y3mn1wMQmyh9zEyWlz5p1zrahRahbXAfCfSqshSNfqOmAQzSHRVjCqjsAw1jyqrXaPdKBmr90DIpIxmIyKXv4GGAkPyJ/6FTFY99uhpiq0qadD/uSzQsefWo0aTvP/65zi3eof7TcZ32oWpwIDAQAB
    
    #SHA256withRsa对应支付宝公钥
    #alipay_public_key = 
    
    #设置应用公钥证书路径
    app_cert_path = E:\stephen\macauo2o\myAlipay\appCertPublicKey_2016101400687752.crt
    #设置支付宝公钥证书路径
    alipay_cert_path = E:\stephen\macauo2o\myAlipay\alipayCertPublicKey_RSA2.crt
    #设置支付宝根证书路径
    alipay_root_cert_path = E:\stephen\macauo2o\myAlipay\alipayRootCert.crt
    
    # 签名类型: RSA->SHA1withRsa,RSA2->SHA256withRsa
    sign_type = RSA2
    # 当面付最大查询次数和查询间隔(毫秒)
    max_query_retry = 5
    query_duration = 5000
    
    # 当面付最大撤销次数和撤销间隔(毫秒)
    max_cancel_retry = 3
    cancel_duration = 2000
    
    # 交易保障线程第一次调度延迟和调度间隔(秒)
    heartbeat_delay = 5
    heartbeat_duration = 900

    然后改TradePaySKD项目

    里面的初始化方法增加证书字段配置

     代码块

    package com.alipay.demo.trade.config;
    
    import org.apache.commons.configuration.Configuration;
    import org.apache.commons.configuration.ConfigurationException;
    import org.apache.commons.configuration.PropertiesConfiguration;
    import org.apache.commons.lang.StringUtils;
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    
    /**
     * Created by liuyangkly on 15/6/27.
     */
    public class Configs {
        private static Log log = LogFactory.getLog(Configs.class);
        private static Configuration configs;
    
        private static String openApiDomain;   // 支付宝openapi域名
        private static String mcloudApiDomain;  // 支付宝mcloudmonitor域名
        private static String pid;             // 商户partner id
        private static String appid;           // 商户应用id
    
        private static String privateKey;      // RSA私钥,用于对商户请求报文加签
        private static String publicKey;       // RSA公钥,仅用于验证开发者网关
        private static String alipayPublicKey; // 支付宝RSA公钥,用于验签支付宝应答
        
        private static String appCertPath;         //设置应用公钥证书路径
        private static String alipayCertPath;     //设置支付宝公钥证书路径
        private static String alipayRootCertPath;//设置支付宝根证书路径
        
        private static String signType;     // 签名类型  
    
        private static int maxQueryRetry;   // 最大查询次数
        private static long queryDuration;  // 查询间隔(毫秒)
    
        private static int maxCancelRetry;  // 最大撤销次数
        private static long cancelDuration; // 撤销间隔(毫秒)
    
        private static long heartbeatDelay ; // 交易保障线程第一次调度延迟(秒)
        private static long heartbeatDuration ; // 交易保障线程调度间隔(秒)
    
        private Configs() {
            // No Constructor
        }
    
        // 根据文件名读取配置文件,文件后缀名必须为.properties
        public synchronized static void init(String filePath) {
            if (configs != null) {
                return;
            }
    
            try {
                configs = new PropertiesConfiguration(filePath);
            } catch (ConfigurationException e) {
                e.printStackTrace();
            }
    
            if (configs == null) {
                throw new IllegalStateException("can`t find file by path:" + filePath);
            }
    
            openApiDomain = configs.getString("open_api_domain");
            mcloudApiDomain = configs.getString("mcloud_api_domain");
    
            pid = configs.getString("pid");
            appid = configs.getString("appid");
    
            // RSA
            privateKey = configs.getString("private_key");
            publicKey = configs.getString("public_key");
            alipayPublicKey = configs.getString("alipay_public_key");
            signType = configs.getString("sign_type");
            
            // certificate
            appCertPath = configs.getString("app_cert_path");
            alipayCertPath = configs.getString("alipay_cert_path");
            alipayRootCertPath = configs.getString("alipay_root_cert_path");
    
            // 查询参数
            maxQueryRetry = configs.getInt("max_query_retry");
            queryDuration = configs.getLong("query_duration");
            maxCancelRetry = configs.getInt("max_cancel_retry");
            cancelDuration = configs.getLong("cancel_duration");
    
            // 交易保障调度线程
            heartbeatDelay = configs.getLong("heartbeat_delay");
            heartbeatDuration = configs.getLong("heartbeat_duration");
    
            log.info("配置文件名: " + filePath);
            log.info(description());
        }
    
        public static String description() {
            StringBuilder sb = new StringBuilder("Configs{");
            sb.append("支付宝openapi网关: ").append(openApiDomain).append("
    ");
            if (StringUtils.isNotEmpty(mcloudApiDomain)) {
                sb.append(", 支付宝mcloudapi网关域名: ").append(mcloudApiDomain).append("
    ");
            }
    
            if (StringUtils.isNotEmpty(pid)) {
                sb.append(", pid: ").append(pid).append("
    ");
            }
            sb.append(", appid: ").append(appid).append("
    ");
    
            sb.append(", 商户RSA私钥: ").append(getKeyDescription(privateKey)).append("
    ");
            sb.append(", 商户RSA公钥: ").append(getKeyDescription(publicKey)).append("
    ");
            sb.append(", 支付宝RSA公钥: ").append(getKeyDescription(alipayPublicKey)).append("
    ");
            
            sb.append(", 应用公钥证书路径: ").append(getKeyDescription(appCertPath)).append("
    ");
            sb.append(", 设置支付宝公钥证书路径: ").append(getKeyDescription(alipayCertPath)).append("
    ");
            sb.append(", 设置支付宝根证书路径: ").append(getKeyDescription(alipayRootCertPath)).append("
    ");
            
            sb.append(", 签名类型: ").append(signType).append("
    ");
    
            sb.append(", 查询重试次数: ").append(maxQueryRetry).append("
    ");
            sb.append(", 查询间隔(毫秒): ").append(queryDuration).append("
    ");
            sb.append(", 撤销尝试次数: ").append(maxCancelRetry).append("
    ");
            sb.append(", 撤销重试间隔(毫秒): ").append(cancelDuration).append("
    ");
    
            sb.append(", 交易保障调度延迟(秒): ").append(heartbeatDelay).append("
    ");
            sb.append(", 交易保障调度间隔(秒): ").append(heartbeatDuration).append("
    ");
            sb.append("}");
            return sb.toString();
        }
    
        private static String getKeyDescription(String key) {
            int showLength = 6;
            if (StringUtils.isNotEmpty(key) &&
                    key.length() > showLength) {
                return new StringBuilder(key.substring(0, showLength))
                        .append("******")
                        .append(key.substring(key.length() - showLength))
                        .toString();
            }
            return null;
        }
    
        public static Configuration getConfigs() {
            return configs;
        }
    
        public static String getOpenApiDomain() {
            return openApiDomain;
        }
    
        public static String getMcloudApiDomain() {
            return mcloudApiDomain;
        }
    
        public static void setMcloudApiDomain(String mcloudApiDomain) {
            Configs.mcloudApiDomain = mcloudApiDomain;
        }
    
        public static String getPid() {
            return pid;
        }
    
        public static String getAppid() {
            return appid;
        }
    
        public static String getPrivateKey() {
            return privateKey;
        }
    
        public static String getPublicKey() {
            return publicKey;
        }
    
        public static String getAlipayPublicKey() {
            return alipayPublicKey;
        }
    
        public static String getSignType() {
            return signType;
        }
    
        public static int getMaxQueryRetry() {
            return maxQueryRetry;
        }
    
        public static long getQueryDuration() {
            return queryDuration;
        }
    
        public static int getMaxCancelRetry() {
            return maxCancelRetry;
        }
    
        public static long getCancelDuration() {
            return cancelDuration;
        }
    
        public static void setConfigs(Configuration configs) {
            Configs.configs = configs;
        }
    
        public static void setOpenApiDomain(String openApiDomain) {
            Configs.openApiDomain = openApiDomain;
        }
    
        public static void setPid(String pid) {
            Configs.pid = pid;
        }
    
        public static void setAppid(String appid) {
            Configs.appid = appid;
        }
    
        public static void setPrivateKey(String privateKey) {
            Configs.privateKey = privateKey;
        }
    
        public static void setPublicKey(String publicKey) {
            Configs.publicKey = publicKey;
        }
    
        public static void setAlipayPublicKey(String alipayPublicKey) {
            Configs.alipayPublicKey = alipayPublicKey;
        }
    
        public static void setSignType(String signType) {
            Configs.signType = signType;
        }
        
        public static void setMaxQueryRetry(int maxQueryRetry) {
            Configs.maxQueryRetry = maxQueryRetry;
        }
    
        public static void setQueryDuration(long queryDuration) {
            Configs.queryDuration = queryDuration;
        }
    
        public static void setMaxCancelRetry(int maxCancelRetry) {
            Configs.maxCancelRetry = maxCancelRetry;
        }
    
        public static void setCancelDuration(long cancelDuration) {
            Configs.cancelDuration = cancelDuration;
        }
    
        public static long getHeartbeatDelay() {
            return heartbeatDelay;
        }
    
        public static void setHeartbeatDelay(long heartbeatDelay) {
            Configs.heartbeatDelay = heartbeatDelay;
        }
    
        public static long getHeartbeatDuration() {
            return heartbeatDuration;
        }
    
        public static void setHeartbeatDuration(long heartbeatDuration) {
            Configs.heartbeatDuration = heartbeatDuration;
        }
    
        public static String getAppCertPath() {
            return appCertPath;
        }
    
        public static void setAppCertPath(String appCertPath) {
            Configs.appCertPath = appCertPath;
        }
    
        public static String getAlipayCertPath() {
            return alipayCertPath;
        }
    
        public static void setAlipayCertPath(String alipayCertPath) {
            Configs.alipayCertPath = alipayCertPath;
        }
    
        public static String getAlipayRootCertPath() {
            return alipayRootCertPath;
        }
    
        public static void setAlipayRootCertPath(String alipayRootCertPath) {
            Configs.alipayRootCertPath = alipayRootCertPath;
        }
        
        
    }

    注意部分:

     需要增加依赖包

     低版本的删掉,此处为TradePaySDK项目依赖

    重点使用证书时调用的响应方法不同

     原调用的是execute需更改certificateExecute

    运行测试

     生成到本地二维码

     沙箱付款

    百度网盘有修改后的Demo

  • 相关阅读:
    服务器键盘设置错误 完美解决
    windows 彻底删除360文件 360zipext.dll 等等
    VS2005智能设备中无法找到PInvoke DLL
    如何使用DotNet 2.0中的应用程序配置 Settings.settings
    维护应用程序状态(一):使用浏览器cookie
    NHibernate学习导航
    HTML基础(三):基本的HTML标签
    使用Cookie对象保存用户自定义设置
    ASP.NET2.0 个性化用户配置
    sealed修饰符的使用
  • 原文地址:https://www.cnblogs.com/dzcici/p/12036055.html
Copyright © 2011-2022 走看看