赞
踩
<!--支付宝-->
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-easysdk</artifactId>
<version>2.1.2</version>
</dependency>
package com.example.pay.uitls.ali; import com.alipay.easysdk.factory.Factory; import com.alipay.easysdk.kernel.Config; import io.swagger.annotations.ApiModelProperty; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; import java.util.Map; /** * desc: 支付宝支付属性参数 * * @author: 邢阳 * @mail: xydeveloper@126.com * @create 2021-04-07 14:11 */ @Data @Component @ConfigurationProperties(prefix = "alipay") public class AliPayProperties { @ApiModelProperty("应用ID") // @ApiModelProperty是Swagger的注解可以不必使用 private String appId; @ApiModelProperty("请求的协议") private String protocol; @ApiModelProperty("请求网关的路径") private String gatewayHost; @ApiModelProperty("应用私钥") private String merchantPrivateKey; @ApiModelProperty("支付宝公钥") private String alipayPublicKey; @ApiModelProperty("是否忽略ssl") private Boolean ignoreSsl; @ApiModelProperty("签名加密类型") private String signType; @ApiModelProperty("支付回调地址") private String notifyUrl; /** * 初始化工厂配置参数 */ @PostConstruct public void initOptions() { Config config = new Config(); config.protocol = protocol; config.gatewayHost = gatewayHost; config.signType = signType; config.appId = appId; config.merchantPrivateKey = merchantPrivateKey; config.alipayPublicKey = alipayPublicKey; config.notifyUrl = notifyUrl; Factory.setOptions(config); } }
# 支付宝支付属性参数
alipay:
appId: 用户的appId
protocol: https
gatewayHost: openapi.alipaydev.com
alipayPublicKey: 支付宝公钥
ignoreSSL: false
signType: RSA2
notifyUrl: 回调地址(必须外网可以调用,开发阶段建议使用内网穿透工具)
merchantPrivateKey: 商户私钥
package com.example.pay.uitls.ali; import com.alipay.easysdk.factory.Factory; import com.alipay.easysdk.kernel.util.ResponseChecker; import com.alipay.easysdk.payment.app.models.AlipayTradeAppPayResponse; import com.alipay.easysdk.payment.common.models.AlipayTradeCloseResponse; import com.alipay.easysdk.payment.common.models.AlipayTradeQueryResponse; import com.alipay.easysdk.payment.common.models.AlipayTradeRefundResponse; import com.alipay.easysdk.payment.page.models.AlipayTradePagePayResponse; import com.alipay.easysdk.payment.wap.models.AlipayTradeWapPayResponse; import com.example.pay.common.*; import org.springframework.stereotype.Component; import java.util.Objects; /** * desc: 支付宝支付工具类 * * @author: 邢阳 * @mail: xydeveloper@126.com * @create 2021-04-07 14:11 */ @Component public class AliPayUtils { /** * app支付 * * @param subject 订单标题 * @param outTradeNo 交易创建时传入的商户订单号 * @param totalAmount 订单总金额,单位为元,精确到小数点后两位,取值范围[0.01,100000000] * @return */ public AlipayTradeAppPayResponse appPay(String subject, String outTradeNo, String totalAmount) { AlipayTradeAppPayResponse alipayTradeAppPayResponse = null; try { // 主要操作 Factory.Payment.App().pay(subject, outTradeNo, totalAmount); 其他代码是一些判空以及异常处理 alipayTradeAppPayResponse = Factory.Payment.App().pay(subject, outTradeNo, totalAmount); } catch (Exception e) { e.printStackTrace(); } if (Objects.isNull(alipayTradeAppPayResponse)) { MyRuntimeException.ThrowRunTimeException.throwException(ExceptionEnum.PAY_FAIL); } return alipayTradeAppPayResponse; } /** * 手机网站支付 * * @param subject: 订单标题 * @param outTradeNo: 交易创建时传入的商户订单号 * @param totalAmount: 订单总金额,单位为元,精确到小数点后两位,取值范围[0.01,100000000] * @param quitUrl: 用户付款中途退出返回商户网站的地址 * @param returnUrl 支付成功后同步跳转的页面,是一个http/https开头的字符串 * @return */ public AlipayTradeWapPayResponse wapPay(String subject, String outTradeNo, String totalAmount, String quitUrl, String returnUrl) { AlipayTradeWapPayResponse alipayTradeWapPayResponse = null; try { // 主要操作 Factory.Payment.App().pay(subject, outTradeNo, totalAmount); 其他代码是一些判空以及异常处理 alipayTradeWapPayResponse = Factory.Payment.Wap().pay(subject, outTradeNo, totalAmount, quitUrl, returnUrl); } catch (Exception e) { e.printStackTrace(); } if (Objects.isNull(alipayTradeWapPayResponse)) { MyRuntimeException.ThrowRunTimeException.throwException(ExceptionEnum.PAY_FAIL); } return alipayTradeWapPayResponse; } /** * 扫码支付 * * @param subject: 订单标题 * @param outTradeNo: 交易创建时传入的商户订单号 * @param totalAmount: 订单总金额,单位为元,精确到小数点后两位,取值范围[0.01,100000000] * @param returnUrl 支付成功后同步跳转的页面,是一个http/https开头的字符串 * @return */ public AlipayTradePagePayResponse pagePay(String subject, String outTradeNo, String totalAmount, String returnUrl) { AlipayTradePagePayResponse alipayTradePagePayResponse = null; try { // 主要操作 Factory.Payment.App().pay(subject, outTradeNo, totalAmount); 其他代码是一些判空以及异常处理 alipayTradePagePayResponse = Factory.Payment.Page().pay(subject, outTradeNo, totalAmount, returnUrl); } catch (Exception e) { e.printStackTrace(); } if (Objects.isNull(alipayTradePagePayResponse)) { MyRuntimeException.ThrowRunTimeException.throwException(ExceptionEnum.PAY_FAIL); } return alipayTradePagePayResponse; } /** * 支付查询 * * @param outTradeNo 交易创建时传入的商户订单号 * @return */ public AlipayTradeQueryResponse query(String outTradeNo) { AlipayTradeQueryResponse alipayTradeQueryResponse = null; try { // 主要操作 Factory.Payment.App().pay(subject, outTradeNo, totalAmount); 其他代码是一些判空以及异常处理 alipayTradeQueryResponse = Factory.Payment.Common().query(outTradeNo); } catch (Exception e) { e.printStackTrace(); } Assert.isTrue(ResponseChecker.success(alipayTradeQueryResponse), ExceptionEnum.SYSTEM_EXCEPTION); return alipayTradeQueryResponse; } /** * 退款 * * @param outTradeNo 交易创建时传入的商户订单号 * @param refundAmount 退款金额 * @return */ public AlipayTradeRefundResponse refund(String outTradeNo, String refundAmount) { AlipayTradeRefundResponse alipayTradeRefundResponse = null; try { // 主要操作 Factory.Payment.App().pay(subject, outTradeNo, totalAmount); 其他代码是一些判空以及异常处理 alipayTradeRefundResponse = Factory.Payment.Common().refund(outTradeNo, refundAmount); } catch (Exception e) { e.printStackTrace(); } Assert.isTrue(ResponseChecker.success(alipayTradeRefundResponse), ExceptionEnum.SYSTEM_EXCEPTION); return alipayTradeRefundResponse; } /** * 取消交易 * * @param outTradeNo 交易创建时传入的商户订单号 * @return */ public AlipayTradeCloseResponse close(String outTradeNo) { AlipayTradeCloseResponse alipayTradeCloseResponse = null; try { // 主要操作 Factory.Payment.App().pay(subject, outTradeNo, totalAmount); 其他代码是一些判空以及异常处理 alipayTradeCloseResponse = Factory.Payment.Common().close(outTradeNo); } catch (Exception e) { e.printStackTrace(); } Assert.isTrue(ResponseChecker.success(alipayTradeCloseResponse), ExceptionEnum.SYSTEM_EXCEPTION); return alipayTradeCloseResponse; } } ;
package com.example.pay.service.impl; import cn.hutool.core.net.URLDecoder; import com.alipay.easysdk.factory.Factory; import com.alipay.easysdk.payment.page.models.AlipayTradePagePayResponse; import com.example.pay.common.Constant; import com.example.pay.common.ExceptionEnum; import com.example.pay.common.MyRuntimeException; import com.example.pay.config.UserMain; import com.example.pay.entity.PayOrder; import com.example.pay.entity.vo.QueryVo; import com.example.pay.entity.vo.AliPagePayVo; import com.example.pay.enums.PayEnum; import com.example.pay.enums.PayOrderEnum; import com.example.pay.service.PayService; import com.example.pay.uitls.ali.AliPayUtils; import com.example.pay.uitls.fuiou.FuiouPayUtils; import com.example.pay.uitls.wechat.WeChatUtils; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.servlet.http.HttpServletRequest; import java.nio.charset.StandardCharsets; import java.util.*; import static com.example.pay.common.Result.success; /** * desc: 聚合支付服务实现类 * * @author: 邢阳 * @mail: xydeveloper@126.com * @create 2021-04-07 14:11 */ @Slf4j @Service public class PayServiceImpl implements PayService { @Autowired private AliPayUtils aliPayUtils; /** * 手机网站支付 * * @param aliPagePayVo * @return */ @Override public String pagePay(AliPagePayVo aliPagePayVo, UserMain userMain) { String outTradeNo = String.valueOf(UUID.randomUUID()); AlipayTradePagePayResponse alipayTradePagePayResponse = aliPayUtils.pagePay(aliPagePayVo.getSubject(), outTradeNo, aliPagePayVo.getTotalAmount(), aliPagePayVo.getReturnUrl()); String body = alipayTradePagePayResponse.getBody(); return body; } /** * 回调通知 * * @param httpServletRequest * @return */ @Override public Map notice(HttpServletRequest httpServletRequest) { try { Map<String, String> params = getAllRequestParam(httpServletRequest); if (!Factory.Payment.Common().verifyNotify(params)) { MyRuntimeException.ThrowRunTimeException.throwException(ExceptionEnum.PARAM_PARSING_EXCPTION); } if (params.get("trade_status").equals(Constant.TRADE_SUCCESS)) { String orderId = URLDecoder.decode(params.get("passback_params"), StandardCharsets.UTF_8); // todo 业务处理... return null; } } catch (Exception e) { log.error(e.getMessage(), e); } return null; } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。