diff --git a/kernel-d-pay/pay-sdk-alipay/pom.xml b/kernel-d-pay/pay-sdk-alipay/pom.xml new file mode 100644 index 000000000..3486c066c --- /dev/null +++ b/kernel-d-pay/pay-sdk-alipay/pom.xml @@ -0,0 +1,37 @@ + + + + kernel-d-pay + cn.stylefeng.roses + 7.0.4 + + 4.0.0 + + pay-sdk-alipay + + + 8 + 8 + + + + + + + cn.stylefeng.roses + pay-api + 7.0.4 + + + + + com.alipay.sdk + alipay-easysdk + 2.2.0 + + + + + \ No newline at end of file diff --git a/kernel-d-pay/pay-sdk-alipay/src/main/java/cn/stylefeng/roses/kernel/pay/alipay/config/AlipayConfig.java b/kernel-d-pay/pay-sdk-alipay/src/main/java/cn/stylefeng/roses/kernel/pay/alipay/config/AlipayConfig.java new file mode 100644 index 000000000..c79b7f951 --- /dev/null +++ b/kernel-d-pay/pay-sdk-alipay/src/main/java/cn/stylefeng/roses/kernel/pay/alipay/config/AlipayConfig.java @@ -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. + * + * Guns采用APACHE 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; + } + + +} diff --git a/kernel-d-pay/pay-sdk-alipay/src/main/java/cn/stylefeng/roses/kernel/pay/alipay/constants/AlipayConstants.java b/kernel-d-pay/pay-sdk-alipay/src/main/java/cn/stylefeng/roses/kernel/pay/alipay/constants/AlipayConstants.java new file mode 100644 index 000000000..3a257e671 --- /dev/null +++ b/kernel-d-pay/pay-sdk-alipay/src/main/java/cn/stylefeng/roses/kernel/pay/alipay/constants/AlipayConstants.java @@ -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. + * + * Guns采用APACHE 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"; +} diff --git a/kernel-d-pay/pay-sdk-alipay/src/main/java/cn/stylefeng/roses/kernel/pay/alipay/service/AlipayService.java b/kernel-d-pay/pay-sdk-alipay/src/main/java/cn/stylefeng/roses/kernel/pay/alipay/service/AlipayService.java new file mode 100644 index 000000000..925560096 --- /dev/null +++ b/kernel-d-pay/pay-sdk-alipay/src/main/java/cn/stylefeng/roses/kernel/pay/alipay/service/AlipayService.java @@ -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. + * + * Guns采用APACHE 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); + + +} diff --git a/kernel-d-pay/pay-sdk-alipay/src/main/java/cn/stylefeng/roses/kernel/pay/alipay/service/impl/AlipayServiceImpl.java b/kernel-d-pay/pay-sdk-alipay/src/main/java/cn/stylefeng/roses/kernel/pay/alipay/service/impl/AlipayServiceImpl.java new file mode 100644 index 000000000..329d653a1 --- /dev/null +++ b/kernel-d-pay/pay-sdk-alipay/src/main/java/cn/stylefeng/roses/kernel/pay/alipay/service/impl/AlipayServiceImpl.java @@ -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. + * + * Guns采用APACHE 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()); + } + } +}