300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > 支付宝小程序Java后台创建订单服务接口

支付宝小程序Java后台创建订单服务接口

时间:2019-08-06 01:19:29

相关推荐

支付宝小程序Java后台创建订单服务接口

首先要去支付宝开发者平台申请APP_ID,和秘钥。

然后准备工作做完以后,就开始写接口了。

创建订单接口 https://docs./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./api_1/alipay.trade.create

首先要集成支付宝提供的SDK,我这里使用的是maven,毕竟是非常方便的,

/search?q=g:com.alipay.sdk%20AND%20a:alipay-sdk-java&core=gav

小伙伴们可以自己点击上面的链接,然后添加到自己的pom.xml文件中就可以了。

这里需要传入的参数 有:

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<Product> products = productMapper.selectByExample(example);if (products.size() <= 0 || products == null) {//如果没有查询到则直接返回return ReturnResult.build(200, "success", MODITY_SHELVES.getMsg());}//查询到对应商品并取出单价Product product = products.get(0);//取出零售价Double retailPrice = product.getRetailPrice();//计算总金额 = 商品单价 * 商品数量Double totalSum = retailPrice * orderDto.getProductNumber();pOrder.setTotalSum(totalSum);//使用BeanUtils,将dto中的属性拷贝到po中,序列化到DBBeanUtils.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校验参数 ,有不了解的同学可以自己去查看资料学习一下。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。