springboot实现条形码_springboot+支付宝条码支付开发详解

1 packagecom.bhp.aaa.bbb.service;2

3 importcom.alibaba.fastjson.JSONObject;4 importcom.alipay.api.AlipayApiException;5 importcom.alipay.api.AlipayClient;6 importcom.alipay.api.DefaultAlipayClient;7 importcom.alipay.api.request.AlipayTradeCancelRequest;8 importcom.alipay.api.request.AlipayTradePayRequest;9 importcom.alipay.api.request.AlipayTradeQueryRequest;10 importcom.alipay.api.request.AlipayTradeRefundRequest;11 importcom.alipay.api.response.AlipayTradeCancelResponse;12 importcom.alipay.api.response.AlipayTradePayResponse;13 importcom.alipay.api.response.AlipayTradeQueryResponse;14 importcom.alipay.api.response.AlipayTradeRefundResponse;15 importcom.google.common.collect.ImmutableMap;16 importorg.springframework.beans.factory.annotation.Autowired;17 importorg.springframework.stereotype.Service;18 importorg.wustrive.java.core.request.ViewResult;19 importorg.wustrive.java.dao.jdbc.dao.BaseDao;20

21 importjavax.servlet.http.HttpServletRequest;22 importjava.util.Map;23

24 @Service25 public classAlipayService1 {26 //正式

27 /*private static String gateway = SysConstants.Alipay.gateway;28 private static String appId = SysConstants.Alipay.app_id;29 private static String privateKey = SysConstants.Alipay.app_private_key;30 private static String publicKey = SysConstants.Alipay.alipay_public_key;*/

31

32 //沙箱33 //网关

34 private static String gateway = "https://openapi.alipaydev.com/gateway.do";35 //应用ID

36 private static String appId = "你自己的APPID";37 //应用秘钥

38 private static String privateKey = "你自己的应用秘钥";39 //支付宝公钥

40 private static String publicKey = "你自己的支付宝公钥";41

42 @Autowired43 privateBaseDao baseDao;44

45 //初始化一个统一的客户端

46 AlipayClient alipayClient = new DefaultAlipayClient(gateway,appId,privateKey,"json","utf-8",publicKey,"RSA2");47

48 //发起支付

49 publicViewResult alipayPay(AppUser appUser, String outTradeNo, String payMoney, String authCode){50

51 ViewResult viewResult =ViewResult.newInstance();52 String sql = "SELECT short_name FROM sys_merchants WHERE id=:merchantsId";53 String short_name = baseDao.queryForString(sql, ImmutableMap.of("merchantsId",appUser.getMerchantsId()));54

55 JSONObject data = newJSONObject();56 data.put("out_trade_no",outTradeNo);57 data.put("scene","bar_code");58 data.put("auth_code",authCode);59 data.put("subject",short_name+"消费");60 data.put("store_id",appUser.getShopId());61 data.put("total_amount",payMoney);62

63 AlipayTradePayRequest request = newAlipayTradePayRequest();64 //request.setNotifyUrl(notifyUrl);

65 request.setBizContent(data.toJSONString());66 try{67 AlipayTradePayResponse response =alipayClient.execute(request);68 if(response.isSuccess()){69 viewResult.success(response.getBody());70 } else{71 viewResult.fail(response.getBody());72 }73 } catch(AlipayApiException e) {74 viewResult.fail(e);75 e.printStackTrace();76 }finally{77 returnviewResult;78 }79 }80

81 //发起查询

82 publicViewResult alipayQuery(String trade_no ){83 ViewResult viewResult =ViewResult.newInstance();84 JSONObject data = newJSONObject();85 data.put("trade_no",trade_no);86 AlipayTradeQueryRequest request = newAlipayTradeQueryRequest();87 request.setBizContent(data.toJSONString());88 try{89 AlipayTradeQueryResponse response =alipayClient.execute(request);90 if(response.isSuccess()){91 viewResult.success(response.getBody());92 } else{93 viewResult.fail(response.getBody());94 }95 } catch(AlipayApiException e) {96 viewResult.fail(e);97 e.printStackTrace();98 } finally{99 returnviewResult;100 }101 }102

103 //发起撤销

104 publicViewResult alipayCancel(String trade_no){105 ViewResult viewResult =ViewResult.newInstance();106 JSONObject data = newJSONObject();107 data.put("trade_no",trade_no);108 AlipayTradeCancelRequest request = newAlipayTradeCancelRequest();109 request.setBizContent(data.toJSONString());110 try{111 AlipayTradeCancelResponse response =alipayClient.execute(request);112 if(response.isSuccess()){113 viewResult.success(response.getBody());114 } else{115 viewResult.fail(response.getBody());116 }117 } catch(AlipayApiException e) {118 viewResult.fail(e);119 e.printStackTrace();120 } finally{121 returnviewResult;122 }123 }124

125 //发起退款

126 publicViewResult alipayRefund(String trade_no,String refund_amount){127 ViewResult viewResult =ViewResult.newInstance();128 JSONObject data = newJSONObject();129 data.put("trade_no",trade_no);130 data.put("refund_amount",refund_amount);131 AlipayTradeRefundRequest request = newAlipayTradeRefundRequest();132 request.setBizContent(data.toJSONString());133 try{134 AlipayTradeRefundResponse response =alipayClient.execute(request);135 if(response.isSuccess()){136 viewResult.success(response.getBody());137 } else{138 viewResult.fail(response.getBody());139 }140 } catch(AlipayApiException e) {141 viewResult.fail(e);142 e.printStackTrace();143 } finally{144 returnviewResult;145 }146 }147

148

149

150

151 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值