阿里支付api实现

pull/20/head
huziyang 2021-05-28 14:15:43 +08:00
parent 55e467b994
commit e5b76e28b0
5 changed files with 379 additions and 0 deletions

View File

@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>kernel-d-pay</artifactId>
<groupId>cn.stylefeng.roses</groupId>
<version>7.0.4</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>pay-sdk-alipay</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!--支付模块的api-->
<dependency>
<groupId>cn.stylefeng.roses</groupId>
<artifactId>pay-api</artifactId>
<version>7.0.4</version>
</dependency>
<!--阿里支付sdk-->
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-easysdk</artifactId>
<version>2.2.0</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,136 @@
/*
* Copyright [2020-2030] [https://www.stylefeng.cn]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* GunsAPACHE LICENSE 2.0使
*
* 1.LICENSE
* 2.Guns
* 3.
* 4. https://gitee.com/stylefeng/guns
* 5. https://gitee.com/stylefeng/guns
* 6.
*/
package cn.stylefeng.roses.kernel.pay.alipay.config;
import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.kernel.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
/**
*
*
* @author huziyang
* @date 2021/04/20 20:43
*/
@Component
public class AlipayConfig implements ApplicationRunner {
/**
* id
*/
@Value("${alipay.appId}")
private String appId;
/**
*
*/
@Value("${alipay.merchantPrivateKey}")
private String merchantPrivateKey;
/**
*
*/
@Value("${alipay.alipayPublicKey}")
private String alipayPublicKey;
/**
*
*/
@Value("${alipay.gatewayHost}")
private String gatewayHost;
/**
*
*/
@Value("${alipay.notifyUrl:#{null}}")
private String notifyUrl;
/**
* AES
*/
@Value("${alipay.encryptKey:#{null}}")
private String encryptKey;
/**
*
*/
@Value("${alipay.merchantCertPath:#{null}}")
private String merchantCertPath;
/**
*
*/
@Value("${alipay.alipayCertPath:#{null}}")
private String alipayCertPath;
/**
*
*/
@Value("${alipay.alipayRootCertPath:#{null}}")
private String alipayRootCertPath;
/**
*
*
* @author huziyang
* @date 2021/04/20 20:43
*/
@Override
public void run(ApplicationArguments args) throws Exception {
Factory.setOptions(getOptions());
}
/**
*
*
* @return
* @author huziyang
* @date 2021/04/20 20:43
*/
private Config getOptions() {
Config config = new Config();
config.protocol = "https";
config.gatewayHost = this.gatewayHost;
config.signType = "RSA2";
config.appId = this.appId;
config.merchantPrivateKey = this.merchantPrivateKey;
// 证书文件路径支持设置为文件系统中的路径或CLASS_PATH中的路径优先从文件系统中加载加载失败后会继续尝试从CLASS_PATH中加载
config.merchantCertPath = this.merchantCertPath;
config.alipayCertPath = this.alipayCertPath;
config.alipayRootCertPath = this.alipayRootCertPath;
// 如果采用非证书模式,则无需赋值上面的三个证书路径,改为赋值如下的支付宝公钥字符串即可
config.alipayPublicKey = this.alipayPublicKey;
// 可设置异步通知接收服务地址(可选)
config.notifyUrl = notifyUrl;
// 可设置AES密钥调用AES加解密相关接口时需要可选
config.encryptKey = this.encryptKey;
return config;
}
}

View File

@ -0,0 +1,36 @@
/*
* Copyright [2020-2030] [https://www.stylefeng.cn]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* GunsAPACHE LICENSE 2.0使
*
* 1.LICENSE
* 2.Guns
* 3.
* 4. https://gitee.com/stylefeng/guns
* 5. https://gitee.com/stylefeng/guns
* 6.
*/
package cn.stylefeng.roses.kernel.pay.alipay.constants;
/**
*
*
* @author huziyang
* @date 2021/04/20 20:43
*/
public interface AlipayConstants {
String ALIPAY_REFUND_SUCCESS_CODE = "10000";
}

View File

@ -0,0 +1,79 @@
/*
* Copyright [2020-2030] [https://www.stylefeng.cn]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* GunsAPACHE LICENSE 2.0使
*
* 1.LICENSE
* 2.Guns
* 3.
* 4. https://gitee.com/stylefeng/guns
* 5. https://gitee.com/stylefeng/guns
* 6.
*/
package cn.stylefeng.roses.kernel.pay.alipay.service;
import cn.stylefeng.roses.kernel.pay.api.pojo.TradeRefundResponse;
/**
*
*
* @author huziyang
* @date 2021/04/20 20:43
*/
public interface AlipayService {
/**
* PC
*
* @param orderName
* @param outTradeNo
* @param total
* @param returnUrl
* @return
* @author huziyang
* @date 2021/04/20 20:43
*/
String page(String orderName, String outTradeNo, String total, String returnUrl);
/**
*
*
* @param orderName
* @param outTradeNo
* @param total
* @param quitUrl 退
* @param returnUrl
* @return
* @author huziyang
* @date 2021/04/20 20:43
*/
String wap(String orderName, String outTradeNo, String total, String quitUrl, String returnUrl);
/**
* 退
*
* @param outTradeNo
* @param refundAmount 退
* @return 退
* @author huziyang
* @date 2021/04/20 20:43
*/
TradeRefundResponse refund(String outTradeNo, String refundAmount);
}

View File

@ -0,0 +1,91 @@
/*
* Copyright [2020-2030] [https://www.stylefeng.cn]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* GunsAPACHE LICENSE 2.0使
*
* 1.LICENSE
* 2.Guns
* 3.
* 4. https://gitee.com/stylefeng/guns
* 5. https://gitee.com/stylefeng/guns
* 6.
*/
package cn.stylefeng.roses.kernel.pay.alipay.service.impl;
import cn.stylefeng.roses.kernel.pay.alipay.constants.AlipayConstants;
import cn.stylefeng.roses.kernel.pay.alipay.service.AlipayService;
import cn.stylefeng.roses.kernel.pay.api.PayApi;
import cn.stylefeng.roses.kernel.pay.api.constants.PayConstants;
import cn.stylefeng.roses.kernel.pay.api.pojo.TradeRefundResponse;
import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.payment.common.models.AlipayTradeRefundResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
/**
*
*
* @author huziyang
* @date 2021/04/20 20:43
*/
@Service
@Slf4j
public class AlipayServiceImpl implements AlipayService, PayApi {
@Override
public String page(String orderName, String outTradeNo, String total, String returnUrl) {
try {
return Factory.Payment.Page().pay(orderName, outTradeNo, total, returnUrl).body;
} catch (Exception e) {
log.error(e.getMessage());
return e.getMessage();
}
}
@Override
public String wap(String orderName, String outTradeNo, String total, String quitUrl, String returnUrl) {
try {
return Factory.Payment.Wap().pay(orderName, outTradeNo, total, quitUrl, returnUrl).body;
} catch (Exception e) {
log.error(e.getMessage());
return e.getMessage();
}
}
@Override
public TradeRefundResponse refund(String outTradeNo, String refundAmount) {
try {
AlipayTradeRefundResponse response = Factory.Payment.Common().refund(outTradeNo, refundAmount);
if (AlipayConstants.ALIPAY_REFUND_SUCCESS_CODE.equals(response.getCode())){
return TradeRefundResponse.builder()
.code(PayConstants.REFUND_SUCCESS_CODE)
.msg(response.getMsg())
.outTradeNo(response.getOutTradeNo())
.refundFee(response.getRefundFee())
.tradeNo(response.getTradeNo())
.gmtRefundPay(response.getGmtRefundPay())
.buyerLogonId(response.buyerLogonId)
.buyerUserId(response.buyerUserId)
.data(response)
.build();
}
return TradeRefundResponse.error(response.msg,response);
} catch (Exception e) {
log.error(e.getMessage());
return TradeRefundResponse.error(e.getMessage());
}
}
}