首先要去支付宝开发者平台申请APP_ID,和秘钥。
然后准备工作做完以后,就开始写接口了。
创建订单接口 https://docs.open.alipay.com/api_1/alipay.trade.create
参数根据自己的业务需求,其中商户订单号是自己生成的,项目中我是使用公司抬头+系统当前时间+两位随机数
订单创建具体逻辑
==========================================================================================
1. 根据自己的业务需求生成数据库字段后,然后创建对应的实体类,再从中拆分出需要进行前后端传入的参数对象【实际业务中数据库中的所有字段不一定都要用到,所有要定义一个数据传入对象。也就是DTO】
DTO代码:
/**
* 商品ID
*/
@NotNull(message = "商品ID不能为空")
private Integer productId;
/**
* 地址ID
*/
@NotNull(message = "地址ID不能为空")
private Integer addressId;
/**
* 用户ID
*/
@NotNull(message = "用户ID不能为空")
private Integer uId;
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
/**
* 商品数量
*/
@NotNull(message = "商品数量不能为空")
private Integer productNumber;
/**
* 订单总金额 =《商品单价*商品数量》
*/
private Double totalSum;
/**
* 订单标题
*/
private String subject;
/**
* 买家的支付宝唯一用户号(2088开头的16位纯数字)
*/
@NotBlank(message = "支付宝唯一用户ID不能为空")
private String buyerId;
2 . 编写服务调用支付宝创建订单接口 https://docs.open.alipay.com/api_1/alipay.trade.create
首先要集成支付宝提供的SDK,我这里使用的是maven,毕竟是非常方便的,
https://search.maven.org/search?q=g:com.alipay.sdk%20AND%20a:alipay-sdk-java&core=gav
小伙伴们可以自己点击上面的链接,然后添加到自己的pom.xml文件中就可以了。
这里需要传入的参数 有:
out_trade_no :商户订单号 【必传】 total_amount :订单总金额 【必传】 subject :订单标题 【必传】 buyer_id :买家支付宝唯一用户号 【不是必传的】
这里除了buyer_id 不是必须传的以外,其他四个参数 都是必须要传的 ,至于其他的参数根据自己的需求,也可以查看支付宝开发文档https://docs.open.alipay.com/api_1
|
3. 服务层业务逻辑处理
/**
* 创建订单
* @return
* @throws Exception
*/
@Override
@Transactional //事务管理
public ReturnResult generateOrder(OrderDto orderDto) throws Exception {
SimpleDateFormat format = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");
// 1.补全实例对象
// 1.1 生成订单号 订单号生成规则:《PPDZN+系统当前时间+两位随机数》
String orderNo = GenerateOrderNoUtil.generateOrderNo();
POrder pOrder = new POrder();
pOrder.setOrderId(orderNo);
pOrder.setOrderTime(format.format(new Date()));
//获取商品ID,根据商品ID去查询商品的单价,然后乘以数量,计算订单总金额
Integer productId = orderDto.getProductId();
ProductExample example = new ProductExample();
example.createCriteria().andIdEqualTo(productId);
List products = productMapper.selectByExample(example);
if (products.size() <= 0 || products == null) {
//如果没有查询到则直接返回
return ReturnResult.build(200, "success", ParamEnum.COMMODITY_SHELVES.getMsg());
}
//查询到对应商品并取出单价
Product product = products.get(0);
//取出零售价
Double retailPrice = product.getRetailPrice();
//计算总金额 = 商品单价 * 商品数量
Double totalSum = retailPrice * orderDto.getProductNumber();
pOrder.setTotalSum(totalSum);
//使用BeanUtils,将dto中的属性拷贝到po中,序列化到DB
BeanUtils.copyProperties(orderDto, pOrder);
// 2.根据订单号调用支付宝创建订单接口
// 2.2 实例化客户端
AlipayClient alipayClient = new DefaultAlipayClient(url, appId, privateKey, "json", "GBK", alipayPublicKey, "RSA2");
// 2.3 实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.create.
AlipayTradeCreateRequest request = new AlipayTradeCreateRequest();
request.setBizContent("{" +
"\"out_trade_no\":\"" + "\"," +
"\"total_amount\":" + totalSum + "," +
"\"buyer_id\":\"" + orderDto.getBuyerId() + "\"," +
"\"subject\":\"" + orderDto.getSubject() + "\"}");
AlipayTradeCreateResponse response = null;
try {
response = alipayClient.execute(request);
} catch (AlipayApiException e) {
log.error(e.getErrMsg());
e.printStackTrace();
}
if (response.isSuccess()) {
System.out.println("调用成功");
System.out.println("商户订单号是:" + response.getOutTradeNo());
System.out.println("支付宝交易号:" + response.getTradeNo());
// 3.插入数据库
try {
orderMapper.insert(pOrder);
} catch (Exception e) {
log.error(e.getMessage());
e.getStackTrace();
return null;
}
// 4.返回订单号和支付宝交易单号
return ReturnResult.build(200, "success", "商户订单号:" + response.getOutTradeNo() + "支付宝交易号:" + response.getTradeNo());
} else {
System.out.println("调用失败");
return null;
}
4. controller层 提供接口
这里使用的是spring校验参数 ,有不了解的同学可以自己去查看资料学习一下。