SpringBoot电商项目之购物车下单(沙箱支付)

目录

一、购物车结算前端功能实现

二、购物车结算后端功能实现

1.从session中获取购物车对象

2.筛选出要结算的订单项列表集合

3.订单页前台展示

三、结算页的下单前端

生成订单

1.前端相关处理

四、结算页的下单后端

2.后台进行下单操作

五、支付宝支付简介、初步接入以及支付宝支付回调

1、配置沙箱支付

沙箱支付应用

根据官方网站开发文档进行支付宝支付接入

2、支付成功后变更订单状态


一、购物车结算前端功能实现

实现思路

1.购物车页面实现结算功能,主要是拿到传入后台的gids
2.跳转订单页后台,主要是拿到订单页展示数据
3.订单页前台数据展示

购物车页面实现结算功能

注:使用jquery动态实现结算功能,必须勾选购物车中的商品进行结算,没有勾选无法完成结算功能!!!

 

cart.js进行整改:关于结算按钮的绑定事件

//计算总共几件商品
	function zg(){
		let gids="";
		var zsl = 0;
		var index = $(".th input[type='checkbox']:checked").parents(".th").find(".num span");
		var len =index.length;
		if(len==0){
			$("#sl").text(0);
		}else{
			let gidarr=new Array();
			index.each(function(){
				zsl+=parseInt($(this).text());
				$("#sl").text(zsl);
				//TODO	获取当前行的索引
				let idx = $(this).parents(".th").index()-1;
				console.log(idx);
				gidarr.push($(".th").eq(index).find('div:eq(0) input[type=hidden]').val());
			});
			gids=gidarr.join(",");
		}
		if($("#sl").text()>0){
			$(".count").css("background","#c10000");
			$(".count").bind("click",function () {
			//	拿到gids
			location.href='/order/toOrder?gids='+gids
			});
		}else{
			$(".count").css("background","#8e8e8e");
			$(".count").unbind("click");
		}
	}

测试结果,当点击结算,会跳转访问地址http://localhost:8080/order/toOrder?gids=2,22

说明gids的值是已经拿到了;

然后再次去生成订单、订单项的代码:

 记得在Order类修改时间的类型:

 

二、购物车结算后端功能实现

1.从session中获取购物车对象


2.筛选出要结算的订单项列表集合

OrderController .java:

package com.ycx.spbootpro.controller;


import com.ycx.spbootpro.model.Goods;
import com.ycx.spbootpro.model.User;
import com.ycx.spbootpro.model.vo.ShopCar;
import com.ycx.spbootpro.model.vo.ShopCarItem;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 订单信息表 前端控制器
 * </p>
 *
 * @author yangzong
 * @since 2022-11-08
 */
@Controller
@RequestMapping("/order")
public class OrderController {

    @RequestMapping("/toOrder")
    public ModelAndView toOrder(User user,
                                String gids,
                                HttpServletRequest request){
        ModelAndView mv=new ModelAndView();
//      拿到购物车中的所有商品
        ShopCar shopCar = getShopCar(user, request);
//        通过gids从购物车中筛选出指定的商品
        List<ShopCarItem> list=getGoods(shopCar,gids);
        mv.addObject("goods",list);
        mv.setViewName("order.html");
        return mv;
    }

    //通过gids从购物车中筛选出指定的商品
    private List<ShopCarItem> getGoods(ShopCar shopCar, String gids) {
        List<String> gidList = Arrays.asList(gids.split(","));
        List<ShopCarItem> items=shopCar.getItems();//购物车里的商品
        List<ShopCarItem> itemsNew=new ArrayList<>();//购物车中要结算的商品
        for (ShopCarItem item:items){
            if(gidList.contains(item.getGid()+"")){
                itemsNew.add(item);
            }
        }
        return itemsNew;
    }

    //    从session中获取购物车对象
    private ShopCar getShopCar(User user,
                               HttpServletRequest request) {
        HttpSession session = request.getSession();
        ShopCar shopCar = (ShopCar) session.getAttribute(user.getId() + "_shopCar");
        if (shopCar == null) {
            shopCar = new ShopCar();
            session.setAttribute(user.getId() + "_shopCar", shopCar);
        }
        return shopCar;
    }
}

3.订单页前台展示

order.html:

<!DOCTYPE html>
<html>
	<head lang="en">
		<#include "common/head.html">
		<link rel="stylesheet" type="text/css" href="css/public.css"/>
		<link rel="stylesheet" type="text/css" href="css/proList.css" />
		<link rel="stylesheet" type="text/css" href="css/mygxin.css" />
	</head>
	<body>
		<!----------------------------------------order------------------>
		<div class="head ding">
			<div class="wrapper clearfix">
				<div class="clearfix" id="top">
					<h1 class="fl"><a href="${ctx}/"><img src="img/logo.png"/></a></h1>
					<div class="fr clearfix" id="top1">
						<form action="#" method="get" class="fl">
							<input type="text" placeholder="搜索" />
							<input type="button" />
						</form>
					</div>
				</div>
			</div>
		</div>
		<!-----------------site------------------->
		<div class="order cart mt">
		<div class="site">
				<p class="wrapper clearfix">
					<span class="fl">订单确认</span>
					<img class="top" src="img/temp/cartTop02.png">
				</p>
			</div>
			<!-----------------orderCon------------------->
			<div class="orderCon wrapper clearfix">
				<div class="orderL fl">
					<!--------h3---------------->
					<h3>收件信息<a href="#" class="fr">新增地址</a></h3>
					<!--------addres---------------->
					<div class="addres clearfix">
						<div class="addre fl on">
							<div class="tit clearfix">
								<p class="fl">张三1</p>
								<span class="default">[默认地址]</span>
								<p class="fr">
									<a href="#">删除</a>
									<span>|</span>
									<a href="#" class="edit">编辑</a>
								</p>
							</div>
							<div class="addCon">
								<p>河北省&nbsp;唐山市&nbsp;路北区&nbsp;大学生公寓村</p>
								<p>15732570937</p>
							</div>
						</div>
						<div class="addre fl">
							<div class="tit clearfix">
								<p class="fl">张三2
								</p>
								<p class="fr">
									<a href="#" class="setDefault">设为默认</a>
									<span>|</span>
									<a href="#">删除</a>
									<span>|</span>
									<a href="#" class="edit">编辑</a>
								</p>
							</div>
							<div class="addCon">
								<p>河北省&nbsp;唐山市&nbsp;路北区&nbsp;大学生公寓村</p>
								<p>15732570937</p>
							</div>
						</div>
						<div class="addre fl">
							<div class="tit clearfix">
								<p class="fl">张三3
								</p>
								<p class="fr">
									<a href="#" class="setDefault">设为默认</a>
									<span>|</span>
									<a href="#">删除</a>
									<span>|</span>
									<a href="#" class="edit">编辑</a>
								</p>
							</div>
							<div class="addCon">
								<p>河北省&nbsp;唐山市&nbsp;路北区&nbsp;大学生公寓村</p>
								<p>15732
  • 4
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要在SpringBoot中接入支付沙箱支付,您需要完成以下步骤: 1. 注册一个支付沙箱账号并登录到开发者平台(https://openhome.alipay.com/platform/appDaily.htm)。 2. 创建一个应用程序并获取应用程序ID和商户私钥。 3. 在SpringBoot应用程序中添加alipay-sdk依赖项。您可以通过Gradle或Maven添加此依赖项。 4. 创建一个支付工具类,并实现沙箱环境的初始化和支付支付API的调用。 5. 在SpringBoot应用程序中创建一个控制器,并定义处理支付请求的端点。 6. 在控制器中使用支付工具类处理支付请求,并返回支付结果。 下面是一个示例支付工具类: ```java import com.alipay.api.AlipayClient; import com.alipay.api.AlipayConstants; import com.alipay.api.DefaultAlipayClient; import com.alipay.api.request.AlipayTradePagePayRequest; import com.alipay.api.response.AlipayTradePagePayResponse; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class AlipayUtil { @Value("${alipay.app-id}") private String appId; @Value("${alipay.private-key}") private String privateKey; @Value("${alipay.public-key}") private String publicKey; @Value("${alipay.gateway-url}") private String gatewayUrl; @Value("${alipay.notify-url}") private String notifyUrl; public String pay(String outTradeNo, String subject, String totalAmount) throws Exception { AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl, appId, privateKey, AlipayConstants.FORMAT_JSON, AlipayConstants.CHARSET_UTF8, publicKey, AlipayConstants.SIGN_TYPE_RSA2); AlipayTradePagePayRequest request = new AlipayTradePagePayRequest(); request.setReturnUrl(notifyUrl); request.setNotifyUrl(notifyUrl); request.setBizContent("{\"out_trade_no\":\"" + outTradeNo + "\"," + "\"total_amount\":\"" + totalAmount + "\"," + "\"subject\":\"" + subject + "\"," + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}"); AlipayTradePagePayResponse response = alipayClient.pageExecute(request); return response.getBody(); } } ``` 在这个示例中,我们使用AlipayClient类创建了一个Alipay客户端,并使用AlipayTradePagePayRequest类设置了支付请求参数。我们还使用AlipayTradePagePayResponse类处理Alipay的支付响应。 下面是一个示例控制器: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; @RestController public class PaymentController { @Autowired private AlipayUtil alipayUtil; @PostMapping("/pay") public String pay(@RequestBody Payment payment) throws Exception { return alipayUtil.pay(payment.getOutTradeNo(), payment.getSubject(), payment.getTotalAmount()); } } ``` 在这个示例中,我们使用@Autowired注释注入了AlipayUtil类,并在/pay端点上定义了处理支付请求的方法。 现在您可以使用Postman或其他HTTP客户端向/pay端点发送JSON请求,以进行支付。以下是一个示例请求: ``` POST /pay HTTP/1.1 Host: localhost:8080 Content-Type: application/json Cache-Control: no-cache { "outTradeNo": "123456789", "subject": "Test Payment", "totalAmount": "100.00" } ``` 在这个示例中,我们向/pay端点发送一个包含outTradeNo,subject和totalAmount的JSON请求。在支付工具类中,我们使用这些参数调用了支付宝API,并返回了支付响应。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

酒醉猫(^・ェ・^)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值