支付宝
官方文档:https://docs.open.alipay.com/270/105899/
依赖pom.xml
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>3.1.0</version>
</dependency>
配置源码
import lombok.Data;
import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import java.io.Serializable;
/**
* 支付宝配置类
*/
@Data
@Entity
@Table(name = "alipay_config")
public class AlipayConfig implements Serializable {
@Id
private Long id;
/**
* 应用ID,APPID,收款账号既是APPID对应支付宝账号
*/
@NotBlank
@Column(name = "app_id")
private String appID;
/**
* 商户私钥,您的PKCS8格式RSA2私钥
*/
@NotBlank
@Column(name = "private_key", columnDefinition = "text")
private String privateKey;
/**
* 支付宝公钥
*/
@NotBlank
@Column(name = "public_key", columnDefinition = "text")
private String publicKey;
/**
* 签名方式,固定格式
*/
@Column(name = "sign_type")
private String signType="RSA2";
/**
* 支付宝开放安全地址,一般不会变
*/
@Column(name = "gateway_url")
private String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";
/**
* 编码,固定格式
*/
private String charset= "utf-8";
/**
* 异步通知地址
*/
@NotBlank
@Column(name = "notify_url")
private String notifyUrl;
/**
* 订单完成后返回的页面
*/
@NotBlank
@Column(name = "return_url")
private String returnUrl;
/**
* 类型,固定格式
*/
private String format="JSON";
/**
* 商户号
*/
@NotBlank
@Column(name = "sys_service_provider_id")
private String sysServiceProviderId;
}
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.validation.constraints.NotBlank;
import java.sql.Date;
import java.sql.Timestamp;
/**
* 交易详情,按需应该存入数据库,这里存入数据库,仅供临时测试
*/
@Data
public class TradeVo {
/**
* (必填)商品描述
*/
@NotBlank
private String body;
/**
* (必填)商品名称
*/
@NotBlank
private String subject;
/**
* (必填)商户订单号,应该由后台生成
*/
@ApiModelProperty(hidden = true)
private String outTradeNo;
/**
* (必填)第三方订单号
*/
@ApiModelProperty(hidden = true)
private String tradeNo;
/**
* (必填)价格
*/
@NotBlank
private String totalAmount;
/**
* 订单状态,已支付,未支付,作废
*/
@ApiModelProperty(hidden = true)
private String state;
/**
* 创建时间,存入数据库时需要
*/
@ApiModelProperty(hidden = true)
private Timestamp createTime;
/**
* 作废时间,存入数据库时需要
*/
@ApiModelProperty(hidden = true)
private Date cancelTime;
}
import me.zhengjie.domain.AlipayConfig;
import me.zhengjie.domain.vo.TradeVo;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
/**
* 支付接口
*
*/
@CacheConfig(cacheNames = "alipay")
public interface AlipayService {
/**
* 处理来自PC的交易请求
* @param alipay
* @param trade
* @return
* @throws Exception
*/
String toPayAsPC(AlipayConfig alipay, TradeVo trade) throws Exception;
/**
* 处理来自手机网页的交易请求
* @param alipay
* @param trade
* @return
* @throws Exception
*/
String toPayAsWeb(AlipayConfig alipay, TradeVo trade) throws Exception;
/**
* 查询配置
* @return
*/
@Cacheable(key = "'1'")
AlipayConfig find();
/**
* 更新配置
* @param alipayConfig
* @return
*/
@CachePut(key = "'1'")
AlipayConfig update(AlipayConfig alipayConfig);
}
import me.zhengjie.domain.AlipayConfig;
import org.springframework.data.jpa.repository.JpaRepository;
/**
* dao
*/
public interface AlipayRepository extends JpaRepository<AlipayConfig,Long> {
}
import cn.hutool.core.util.StrUtil;
import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import me.zhengjie.domain.AlipayConfig;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
* 支付宝工具类
*/
@Component
public class AlipayUtils {
/**
* 生成订单号
* @return
*/
public String getOrderCode() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
int a = (int)(Math.random() * 9000.0D) + 1000;
System.out.println(a);
Date date = new Date();
String str = sdf.format(date);
String[] split = str.split("-");
String s = split[0] + split[1] + split[2];
String[] split1 = s.split(" ");
String s1 = split1[0] + split1[1];
String[] split2 = s1.split(":");
String s2 = split2[0] + split2[1] + split2[2] + a;
return s2;
}
/**
* 校验签名
* @param request
* @return
*/
public boolean rsaCheck(HttpServletRequest request, AlipayConfig alipay){
/**
* 获取支付宝POST过来反馈信息
*/
Map<String,String> params = new HashMap<>(1);
Map requestParams = request.getParameterMap();
for (Iterator iter = requestParams.keySet().iterator(); iter.hasNext();) {
String name = (String) iter.next();
String[] values = (String[]) requestParams.get(name);
String valueStr = "";
for (int i = 0; i < values.length; i++) {
valueStr = (i == values.length - 1) ? valueStr + values[i]
: valueStr + values[i] + ",";
}
params.put(name, valueStr);
}
try {
boolean verifyResult = AlipaySignature.rsaCheckV1(params,
alipay.getPublicKey(),
alipay.getCharset(),
alipay.getSignType());
return verifyResult;
} catch (AlipayApiException e) {
return false;
}
}
public boolean isEmpty(String str){
return StrUtil.isEmpty(str);
}
}
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import me.zhengjie.domain.AlipayConfig;
import me.zhengjie.domain.vo.TradeVo;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.repository.AlipayRepository;
import me.zhengjie.service.AlipayService;
import me.zhengjie.utils.AlipayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.Optional;
/**
* 接口实现
*
*/
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class AlipayServiceImpl implements AlipayService {
@Autowired
AlipayUtils alipayUtils;
@Autowired
private AlipayRepository alipayRepository;
@Override
public String toPayAsPC(AlipayConfig alipay, TradeVo trade) throws Exception {
if(alipay.getId() == null){
throw new BadRequestException("请先添加相应配置,再操作");
}
AlipayClient alipayClient = new DefaultAlipayClient(alipay.getGatewayUrl(), alipay.getAppID(), alipay.getPrivateKey(), alipay.getFormat(), alipay.getCharset(), alipay.getPublicKey(), alipay.getSignType());
double money = Double.parseDouble(trade.getTotalAmount());
/**
* 创建API对应的request(电脑网页版)
*/
AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
/**
* 订单完成后返回的页面和异步通知地址
*/
request.setReturnUrl(alipay.getReturnUrl());
request.setNotifyUrl(alipay.getNotifyUrl());
/**
* 填充订单参数
*/
request.setBizContent("{" +
" \"out_trade_no\":\""+trade.getOutTradeNo()+"\"," +
" \"product_code\":\"FAST_INSTANT_TRADE_PAY\"," +
" \"total_amount\":"+trade.getTotalAmount()+"," +
" \"subject\":\""+trade.getSubject()+"\"," +
" \"body\":\""+trade.getBody()+"\"," +
" \"extend_params\":{" +
" \"sys_service_provider_id\":\""+alipay.getSysServiceProviderId()+"\"" +
" }"+
" }");//填充业务参数
/**
* 调用SDK生成表单
* 通过GET方式,口可以获取url
*/
return alipayClient.pageExecute(request, "GET").getBody();
}
@Override
public String toPayAsWeb(AlipayConfig alipay, TradeVo trade) throws Exception {
if(alipay.getId() == null){
throw new BadRequestException("请先添加相应配置,再操作");
}
AlipayClient alipayClient = new DefaultAlipayClient(alipay.getGatewayUrl(), alipay.getAppID(), alipay.getPrivateKey(), alipay.getFormat(), alipay.getCharset(), alipay.getPublicKey(), alipay.getSignType());
double money = Double.parseDouble(trade.getTotalAmount());
if(money <= 0 || money >= 5000){
throw new BadRequestException("测试金额过大");
}
/**
* 创建API对应的request(手机网页版)
*/
AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
/**
* 订单完成后返回的页面和异步通知地址
*/
request.setReturnUrl(alipay.getReturnUrl());
request.setNotifyUrl(alipay.getNotifyUrl());
/**
* 填充订单参数
*/
request.setBizContent("{" +
" \"out_trade_no\":\""+trade.getOutTradeNo()+"\"," +
" \"product_code\":\"FAST_INSTANT_TRADE_PAY\"," +
" \"total_amount\":"+trade.getTotalAmount()+"," +
" \"subject\":\""+trade.getSubject()+"\"," +
" \"body\":\""+trade.getBody()+"\"," +
" \"extend_params\":{" +
" \"sys_service_provider_id\":\""+alipay.getSysServiceProviderId()+"\"" +
" }"+
" }");//填充业务参数
/**
* 调用SDK生成表单
* 通过GET方式,口可以获取url
*/
return alipayClient.pageExecute(request, "GET").getBody();
}
@Override
public AlipayConfig find() {
Optional<AlipayConfig> alipayConfig = alipayRepository.findById(1L);
if (alipayConfig.isPresent()){
return alipayConfig.get();
} else {
return new AlipayConfig();
}
}
@Override
@Transactional(rollbackFor = Exception.class)
public AlipayConfig update(AlipayConfig alipayConfig) {
return alipayRepository.save(alipayConfig);
}
}
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.aop.log.Log;
import me.zhengjie.domain.AlipayConfig;
import me.zhengjie.domain.vo.TradeVo;
import me.zhengjie.service.AlipayService;
import me.zhengjie.utils.AliPayStatusEnum;
import me.zhengjie.utils.AlipayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
/**
* api
*/
@Slf4j
@RestController
@RequestMapping("/api")
public class AliPayController {
@Autowired
AlipayUtils alipayUtils;
@Autowired
private AlipayService alipayService;
@GetMapping(value = "/aliPay")
public ResponseEntity<AlipayConfig> get(){
return new ResponseEntity<>(alipayService.find(),HttpStatus.OK);
}
@Log("配置支付宝")
@PutMapping(value = "/aliPay")
public ResponseEntity payConfig(@Validated @RequestBody AlipayConfig alipayConfig){
alipayConfig.setId(1L);
alipayService.update(alipayConfig);
return new ResponseEntity(HttpStatus.OK);
}
@Log("支付宝PC网页支付")
@ApiOperation(value = "PC网页支付")
@PostMapping(value = "/aliPay/toPayAsPC")
public ResponseEntity<String> toPayAsPC(@Validated@RequestBody TradeVo trade) throws Exception{
AlipayConfig alipay = alipayService.find();
trade.setOutTradeNo(alipayUtils.getOrderCode());
String payUrl = alipayService.toPayAsPC(alipay,trade);
return ResponseEntity.ok(payUrl);
}
@Log("支付宝手机网页支付")
@ApiOperation(value = "手机网页支付")
@PostMapping(value = "/aliPay/toPayAsWeb")
public ResponseEntity<String> toPayAsWeb(@Validated @RequestBody TradeVo trade) throws Exception{
AlipayConfig alipay = alipayService.find();
trade.setOutTradeNo(alipayUtils.getOrderCode());
String payUrl = alipayService.toPayAsWeb(alipay,trade);
return ResponseEntity.ok(payUrl);
}
@ApiIgnore
@GetMapping("/aliPay/return")
@ApiOperation(value = "支付之后跳转的链接")
public ResponseEntity<String> returnPage(HttpServletRequest request, HttpServletResponse response) throws Exception {
AlipayConfig alipay = alipayService.find();
response.setContentType("text/html;charset=" + alipay.getCharset());
//内容验签,防止黑客篡改参数
if(alipayUtils.rsaCheck(request,alipay)){
//商户订单号
String outTradeNo = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"),"UTF-8");
//支付宝交易号
String tradeNo = new String(request.getParameter("trade_no").getBytes("ISO-8859-1"),"UTF-8");
System.out.println("商户订单号"+outTradeNo+" "+"第三方交易号"+tradeNo);
/**
* 根据业务需要返回数据,这里统一返回OK
*/
return new ResponseEntity<>("payment successful",HttpStatus.OK);
}else{
/**
* 根据业务需要返回数据
*/
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
}
@ApiIgnore
@RequestMapping("/aliPay/notify")
@ApiOperation(value = "支付异步通知(要公网访问),接收异步通知,检查通知内容app_id、out_trade_no、total_amount是否与请求中的一致,根据trade_status进行后续业务处理")
public ResponseEntity notify(HttpServletRequest request) throws Exception{
AlipayConfig alipay = alipayService.find();
Map<String, String[]> parameterMap = request.getParameterMap();
StringBuilder notifyBuild = new StringBuilder("/****************************** pay notify ******************************/\n");
parameterMap.forEach((key, value) -> notifyBuild.append(key + "=" + value[0] + "\n") );
//内容验签,防止黑客篡改参数
if (alipayUtils.rsaCheck(request,alipay)) {
//交易状态
String tradeStatus = new String(request.getParameter("trade_status").getBytes("ISO-8859-1"),"UTF-8");
// 商户订单号
String outTradeNo = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"),"UTF-8");
//支付宝交易号
String tradeNo = new String(request.getParameter("trade_no").getBytes("ISO-8859-1"),"UTF-8");
//付款金额
String totalAmount = new String(request.getParameter("total_amount").getBytes("ISO-8859-1"),"UTF-8");
//验证
if(tradeStatus.equals(AliPayStatusEnum.SUCCESS.getValue())||tradeStatus.equals(AliPayStatusEnum.FINISHED.getValue())){
/**
* 验证通过后应该根据业务需要处理订单
*/
}
return new ResponseEntity(HttpStatus.OK);
}
return new ResponseEntity(HttpStatus.BAD_REQUEST);
}
}