Java支付功能实现

编程知识 行业动态 更新时间:2024-06-13 00:22:05

Java支付功能实现

导入依赖

<dependency>
            <groupId>com.alipay.sdk</groupId>
            <artifactId>alipay-sdk-java</artifactId>
</dependency>
<dependency>
                <groupId>cn.hutool</groupId>
                <artifactId>hutool-all</artifactId>
                <version>5.5.8</version>
</dependency>

生成qrCode

@PostMapping("/show")
	@ApiOperation(value = "添加支付订单", notes = "支付订单添加")
	public String qrCode(@RequestBody @Validated TradeDTO tradeDTO, BindingResult result) throws AlipayApiException{
		if (result.hasErrors()) {
			throw new BindingResultException(result);
		}
		AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay/gateway.do","2021003108655493",
				"应用私钥","json","UTF-8","应用共钥","RSA2");
		AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
		request.setNotifyUrl("");
		JSONObject bizContent = new JSONObject();
		bizContent.put("out_trade_no", tradeDTO.getOutTradeNo());
		bizContent.put("total_amount", tradeDTO.getTotalAmount());
		bizContent.put("subject", tradeDTO.getSubject());
		request.setBizContent(bizContent.toString());
		AlipayTradePrecreateResponse response = alipayClient.execute(request);
		if(response.isSuccess()){
			return  response.getBody();
		} else {
			throw new BusinessException(ResultCode.ADD_ACCOUNT.getCode(), "二维码生成失败失败");
		}
	}

检查是否存在

下载支付宝开放平台开发助手

@GetMapping("/pay/{id}")
	@ApiOperation(value = "查询支付订单", notes = "支付订单查询")
public String PayCheck(String id) throws AlipayApiException {
		//获得初始化的AlipayClient
		AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay/gateway.do", "2021003108655493", "应用私钥", "json", "UTF-8", "支付宝共钥", "RSA2");
		//创建API对应的request类
		AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
		request.setBizContent("{" +
				"\"out_trade_no\":\""+id+"\"}");  //设置业务参数
		//通过alipayClient调用API,获得对应的response类
		AlipayTradeQueryResponse response = alipayClient.execute(request);
		return response.getBody();
	}

添加订单接口

@PostMapping("/add")
	@ApiOperation(value = "添加支付订单", notes = "支付订单添加")
public Result addTrade(Trade trade, BindingResult result) {
		if (result.hasErrors()) {
			throw new BindingResultException(result);
		}
		boolean save=tradeRepository.save(trade);
		if (save) {
			return Result.ok().message("支付订单添加");
		} else {
			throw new BusinessException(ResultCode.ORDER_ADD);
		}
	}

前端Vue

安装qrcodejs2

npm install qrcodejs2 --save
<el-button class="btn" type="text" @click="dialogVisible = true">立即支付</el-button>
<el-dialog
                title="支付"
                :visible.sync="dialogVisible"
                width="30%">
            <div style="margin-left: 150px" id="code"></div>
            <span slot="footer" class="dialog-footer">
        <el-button @click.once="getQRCode">生成二维码</el-button>
    <el-button type="success" @click="testSuccess">检查成功</el-button>
  </span>
        </el-dialog>
import QRCode from 'qrcodejs2'
data() {
            return {
            successInfo: {},
            dialogVisible: false,
            TradeForm: {
                    //订单编号
                    outTradeNo: undefined,
                    //价格
                    totalAmount: undefined,
                    //订单名称
                    subject: undefined
                },
                payForm: {
                    id: undefined,
                    houseId: undefined,
                    houseName: undefined,
                    userId: undefined,
                    userPhone: undefined,
                    leaseTerm: undefined,
                    price: undefined,
                    touristId: undefined,
                    touristName: undefined,
                    touristPhone: undefined
                }
            }
        }

参考QRCode.js:使用 JavaScript 生成二维码 | 菜鸟教程 (runoob)

getQRCode() {
                let qr;
                this.TradeForm.outTradeNo = this.getProjectNum();
                console.log(this.TradeForm.outTradeNo)
                this.TradeForm.totalAmount = this.orderInfo();
                this.TradeForm.subject = this.describeInfo();
                qrCode(this.TradeForm).then(response => {
                    this.page = response;
                    qr = new QRCode("code", {
                        text: this.page.alipay_trade_precreate_response.qr_code,
                        width: 128,
                        height: 128,
                        colorDark: "#000000",
                        colorLight: "#ffffff",
                        correctLevel: QRCode.CorrectLevel.H
                    });
                })
            },

检测成功

testSuccess() {
                console.log(this.TradeForm.outTradeNo)
                payCheck(this.TradeForm.outTradeNo).then(response => {
                    this.successInfo = response;
                    console.log(this.successInfo)
                    if (this.successInfo.alipay_trade_query_response.code == 10000) {
                        this.payForm.id = this.TradeForm.outTradeNo;
                        this.payForm.houseId=this.oInfo().id;
                        this.payForm.houseName=this.oInfo().address+this.oInfo().addressInfo;
                        this.payForm.userId=this.oInfo().userId;
                        this.payForm.userPhone=this.oInfo().phoneNumber;
                        this.payForm.leaseTerm=this.oInfo().leaseTerm;
                        this.payForm.price=this.oInfo().price;
                        this.payForm.touristId=this.loginInfo().id;
                        this.payForm.touristName=this.loginInfo().username;
                        this.payForm.touristPhone=this.loginInfo().phone;
                        addTrade(this.payForm).then(response => {
                            if(response.code===200){
                                clearOrder(this.payForm.touristId).then(response => {
                                    if(response.code===200) {
                                        leaseHouseInfo(this.payForm.houseId).then(response => {
                                            if(response.code===200) {
                                                this.$router.push({path:'/paysuccess'})
                                            }else {
                                                this.$notify({
                                                    title: '警告',
                                                    message: response.message,
                                                    type: 'warning'
                                                });
                                            }
                                        })
                                    }else {
                                        this.$notify({
                                            title: '警告',
                                            message: response.message,
                                            type: 'warning'
                                        });
                                    }
                                })
                            }else {
                                this.$notify({
                                    title: '警告',
                                    message: response.message,
                                    type: 'warning'
                                });
                            }

                        })
                    } else {
                        this.$notify({
                            title: '警告',
                            message: '交易不存在,请重新交易',
                            type: 'warning'
                        });
                    }
                })
            }

更多推荐

Java支付功能实现

本文发布于:2023-04-01 16:51:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/c3d567158ac8d010e13245479e5a8faa.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:功能   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!