一、所需jar包
SDK包说明
- alipay-sdk-java*.jar—————————支付宝SDK编译文件jar
- alipay-sdk-java*-source.jar——————支付宝SDK源码文件jar
- commons-logging-1.1.1.jar——————SDK依赖的日志jar
- commons-logging-1.1.1-sources.jar———SDK依赖的日志源码jar
注意
-
- 集成支付宝接口需要引入的文件是:
alipay-sdk-java*.jar
commons-logging-1.1.1.jar - 若进一步了解代码实现请引入文件:
alipay-sdk-java*-source.jar
commons-logging-1.1.1-sources.jar
二、调用支付
//调用支付
public void toPay() {
String orderid = getRequest().getParameter("id");
if (StringUtils.isBlank(orderid)) {
throw new NullPointerException();
}
Order order = orderService.selectById(orderid);
if (order == null) {
throw new NullPointerException("根据订单号查询不到订单信息!");
}
Ordership ordership = ordershipService.selectOne(new Ordership(orderid));
if (ordership == null) {
throw new NullPointerException("根据订单号查询不到配送信息!");
}
ArrayNode goods_detail = JsonUtils.createArrayNode();// 商品详情设置(json格式-非必须参数,根据业务逻辑处理)
String out_trade_no = order.getPayNo();//支付编号(订单编号-必须唯一)
double total_amount = 0.01;//订单金额
String subject = "订单标题";//订单描述
AlipayTradePrecreateResponse resp = aliPayService.aliPay(out_trade_no, total_amount, subject, goods_detail);// 订单编号--订单金额--订单描述--商品明细(无 )
if ("10000".equals(resp.getCode())) {//若请求成功,则进行逻辑处理
String qr_code = resp.getQrCode();
/**
* 生成二维码
*/
// 支付宝返回Code(url)--用户account--订单--id(拼二维码地址而已)
AttachmentEntity attachmentEntity = this.creatPayCode(qr_code, account, out_trade_no);
if (attachmentEntity == null) {
throw new NullPointerException("订单生成失败!OrdersAction--toPay");
} else {
// 返回二维码地址
getOut().println(attachmentEntity.getAtt_path());
}
} else {
throw new NullPointerException("二维码生成失败!OrdersAction--toPay");
}
}
public AlipayTradePrecreateResponse aliPay(String out_trade_no, double total_amount,
String subject, ArrayNode goods_detail) {
/**
* 创建Json对象 封装信息
*/
ObjectNode biz_content = JsonUtils.createObjectNode();
biz_content.put("out_trade_no", out_trade_no); //订单号
biz_content.put("total_amount", total_amount); //订单总金额
biz_content.put("subject", subject); //订单标题
biz_content.put("timeout_express", "5m"); //该笔订单允许的最晚付款时间,逾期将关闭交易
AlipayTradePrecreateResponse response = null;
//请求支付接口对象
//参数
//1.请求网址 2.自己所注册的应用id(支付宝分配给开发者的应用ID) 3.私钥 4.json 5.字符编码集 6.公钥
//实例化客户端
AlipayClient alipayClient = new DefaultAlipayClient(request_url, app_id, private_key, "json", charset,
public_key);
//实例化具体API对应的request类,类名称和接口名称对应
AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
request.setBizContent(biz_content.toString());
try {
response = alipayClient.execute(request);
} catch (AlipayApiException ex) {
log.error(ex.getMessage());
}
return response;
}
//生成二维码
private AttachmentEntity creatPayCode(String url, String user_id, String order_id) {
String serverClassPath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
String logo_path = serverClassPath + "config/logo.png";
String uploadPath = "payForUser/" + user_id + "/order/" + order_id + "/" + "payCode";
String attName = "payCode.jpg";// 定义原图的名称
BufferedImage barCode = BarCodeService.createImage(url, logo_path);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try {
ImageOutputStream imageOutputStream = ImageIO.createImageOutputStream(byteArrayOutputStream);
ImageIO.write(barCode, "jpg", imageOutputStream);
} catch (IOException iOException) {
iOException.printStackTrace();
}
InputStream inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
String barCode_url = ossService.uploadFile(uploadPath, inputStream, attName);//将二维码文件上传阿里云(可自主处理)
AttachmentEntity attachmentEntity = new AttachmentEntity();
attachmentEntity.setAtt_name(attName);
attachmentEntity.setAtt_path(barCode_url);
attachmentEntity.setMoudel_name("payCode");
attachmentEntity.setAtt_type("jpg");
attachmentEntity.setUpload_time(DateUtils.getSystemDate());
attachmentEntity.setDownload_count(0);
attachmentEntity.setGid(DataUtils.getUUID());
attachmentEntity.setIs_compress(0);
attachmentEntity.setEntity_id(order_id);
return attachmentEntity;
}
三、查询支付状态
public String isPay() {
AlipayTradeQueryResponse alipayTradeQueryResponse = aliPayService.aliQuery(order.getPayNo());//订单编号
if (alipayTradeQueryResponse.isSuccess()) {//若查询状态为success
if ("10000".equals(alipayTradeQueryResponse.getCode())) {
String Body = alipayTradeQueryResponse.getBody();
String out_trade_no = alipayTradeQueryResponse.getOutTradeNo();// 支付编号
ObjectNode objectNode = (ObjectNode) JsonUtils.stringToJsonObject(Body)
.get("alipay_trade_query_response");
String trade_status = objectNode.get("trade_status").textValue();// 支付状态
if (trade_status != null && "TRADE_SUCCESS".equals(trade_status)) {
String trade_no = objectNode.get("trade_no").textValue();//支付宝交易号
//支付记录创建
orderService.createPayInfo(order,trade_no,orderpay.orderpay_paystatus_y,orderpay.orderpay_paymethod_alipayescow,"");//订单--支付宝交易号-支付状态-支付类型
orderService.orderStutasChange(orderId);// 修改订单状态
getOut().print("000000");
return null;
}else if (trade_status != null && "TRADE_CLOSED".equals(trade_status)) {//二维码失效
String trade_no = objectNode.get("trade_no").textValue();//支付宝交易号
//修改订单支付编号。重新生成二维码
String orderId=out_trade_no.substring(0, out_trade_no.length()-13);
e.setId(orderId);
String payNo=orderId+new Date().getTime();
e.setPayNo(payNo);
orderService.update(e);
getOut().print("000001");
return null;
}
}
}
return null;
}