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.com/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 = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", "2021003108655493", "应用私钥", "json", "UTF-8", "支付宝共钥", "RSA2");
AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
request.setBizContent("{" +
"\"out_trade_no\":\""+id+"\"}");
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.com)
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'
});
}
})
}
|