近来在开发个人站的时候,遇到支付问题,对于个人站长而言,没有公司,也没有营业执照,免签个人支付系统尤为重要,网上也有很多,这里简单介绍Java搭建支付宝当面付接口功能。
个人能力有限,仅供个人参考,勿用到生产环境
测试及下载项目源码 测试站点
当面付开通很简单,直接百度一张店铺照片就可以开通
支付宝当面付限制(无营业执照):
单笔收款 ≤2000元,单日收款 ≤20000元,不区分借记或贷记渠道。
下面简单来看下Java服务器端代码:
用到的官方Java版sdk jar包自行下载
core.java 调用支付宝官方接口核心功能
很简单,注释很清楚,不过多解释,具体看代码
public class core {
AlipayClient client = null;
/**支付宝回调的接口地址*/
private static String aliNotifyUrl = "yourweb/pay/alinotify";
public core() {
getConfig();
}
/**
* 创建付款订单
*/
public HashMap<String, String> createOrder(double num,String title){
if(num<=0) return null;
HashMap<String, String> rs = new HashMap<>();
AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
//生成不重复的商家订单号(我用的时间+随机数)
String h=new Date().getTime()+Utils.randomNum(10, 99)+"";
AlipayTradePrecreateModel model = new AlipayTradePrecreateModel();
model.setOutTradeNo(h);//订单号(保证不重复)
model.setTotalAmount(num+"");//订单金额
model.setSubject(title);//标题(关键字)不可用特殊标点
model.setQrCodeTimeoutExpress("10m");
request.setBizModel(model);
request.setNotifyUrl(aliNotifyUrl);//应用网关(异步通知)
AlipayTradePrecreateResponse response = null;
try {
response = client.execute(request);
} catch (AlipayApiException e) {e.printStackTrace();}
if(response.isSuccess()){
//订单存入数据库 根据自己需求建表存入(此处我使用的自己封装的SQL工具)
DBUtil db = new DBUtil();
int i = db.execUpdate("insert into _orders(out_trade_no,otitle,onum,qrcode) values("+ response.getOutTradeNo()+",?,"+num+",?)", new String[]{title,response.getQrCode()});
if(i==1){
rs.put("code", "1");
rs.put("qrcode", response.getQrCode());
//将qrcode转为base64格式图片
rs.put("qrimg", ImgUtils.QrImgB64(response.getQrCode(), 300));
rs.put("out_trade_no", response.getOutTradeNo());
}else rs.put("code", "-1");
} else {
rs.put("code", "-1");
}
return rs;
}
/**查询订单状态*/
public Map<String, Object> queryOrder(String orderId){
if(orderId==null||orderId.trim().length()<1) return null;
DBUtil db = new DBUtil();
//查数据库
//先检查数据库是否有这个数据,若查不到或者等待付款,就去更新订单状态,交易成功和交易结束直接返回,订单关闭就把数据库数据删除
List<Map<String, Object>> rs = db.execQuery("select * from _orders where out_trade_no = ?", new String[]{orderId});
if(rs!=null&&rs.size()>0){
Map<String, Object> o = rs.get(0);
if("0".equals(o.get("ostatus")+"")||"1".equals(o.get("ostatus")+"")){
AlipayTradeQueryResponse r = updateOrder(orderId);
if(r==null) return null;
return queryOrder(orderId);
}
else if("2".equals(o.get("ostatus")+"")) {
db.execUpdate("delete from _orders where out_trade_no = ?", new String[]{orderId});
return null;
}
return o;
}else{
AlipayTradeQueryResponse r = updateOrder(orderId);
if(r==null) return null;
return queryOrder(orderId);
}
}
/**更新订单状态*/
public AlipayTradeQueryResponse updateOrder(String orderId){
if(orderId==null||orderId.trim().length()<1) return null;
DBUtil db = new DBUtil();
AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
AlipayTradeQueryModel model = new AlipayTradeQueryModel();
model.setOutTradeNo(orderId);
request.setBizModel(model);
AlipayTradeQueryResponse response = null;
try {
response = client.execute(request);
} catch (AlipayApiException e) {e.printStackTrace();}
int s=0;//对应 0:无订单,1:待付款,2:超时删除,3:支付成功,4:订单结束
if("WAIT_BUYER_PAY".equals( response.getTradeStatus())) s=1;
else if("TRADE_CLOSED".equals( response.getTradeStatus())) s=2;
else if("TRADE_SUCCESS".equals( response.getTradeStatus())) s=3;
else if("TRADE_FINISHED".equals( response.getTradeStatus())) s=4;
//查询不到就删除数据库这条数据
if(response.getTradeNo()==null||response.getTradeNo().trim().length()<5){
db.execUpdate("delete from _orders where out_trade_no = ?", new String[]{orderId});
return null;
}
//更新数据库订单
db.execUpdate("update _orders set trade_no=?,ostatus="+s+",zfbuser=?,paynum="+response.getBuyerPayAmount()+",getnum="+response.getReceiptAmount()+",zfbuserid=?,getbody=? where out_trade_no = ?", new String[]{response.getTradeNo(), response.getBuyerLogonId(),response.getBuyerUserId(),response.getBody(),orderId});
return response;
}
/**
* 接口调用配置
*/
public void getConfig() {
String appid="APPID";
String private_key="私钥";
String public_key="支付宝公钥";
String sign_type="RSA2";
client =new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",appid,private_key,"json","utf-8",public_key,sign_type);
}
}
有了这一段代码之后其实就可以根据自己的业务需求去做逻辑实现了。
另外支付宝的回调接口也需要自己写一下,代码附上:
Controller.java 这里就是正常的逻辑调用了,支付宝的回调通知我也写在此处了
很多返回值都是转成json数据,这里就不过多展示了
/**
* 支付宝通知回调<br>
* 设置中的“应用网关”
* @param param
* @param request
* @param response
*/
@RequestMapping(value = "/alinotify", produces = "text/html;charset=UTF-8")
public void alinotify(
@RequestParam(required = false, defaultValue = "") Map<String, String> param,
HttpServletRequest request,
HttpServletResponse response
) {
DBUtil db = new DBUtil();
core core=new core();
//只有支付成功后,支付宝才会回调应用接口,可直接获取支付宝响应的参数
String order_id = param.get("out_trade_no");
int s =0;
String ss = param.get("trade_status");
if("WAIT_BUYER_PAY".equals(ss)) s=1;
else if("TRADE_CLOSED".equals(ss)) s=2;
else if("TRADE_SUCCESS".equals(ss)) s=3;
else if("TRADE_FINISHED".equals(ss)) s=4;
//检查数据库是否有本数据,有就修改,没有新增
int i = db.execSql("select * from _orders where out_trade_no = ?", new String[]{order_id});
if(i>0) core.updateOrder(order_id);
else{
db.execUpdate("insert into _orders(out_trade_no,trade_no,otitle,onum,ostatus,zfbuser,paynum,getnum,zfbuserid,getbody) "
+ "values("+order_id+",?,?,"+param.get("total_amount")+","+s+",?,"+param.get("buyer_pay_amount")+","+param.get("receipt_amount")+",?,?)", new String[]{param.get("trade_no"),param.get("subject"),param.get("buyer_logon_id"),param.get("buyer_id"),param.toString()});
}
}
/**
* 获取付款码
* @param param
* @param request
* @param response
* @return
*/
@RequestMapping(value = "/createOrder", produces = "text/html;charset=UTF-8")
public String createOrder(
@RequestParam(required = false, defaultValue = "") String n,
@RequestParam(required = false, defaultValue = "") String t,
HttpServletRequest request,
HttpServletResponse response
) {
double num = Utils.getNumDouble(n);
if(num<0.01||num>2000) return "{\"code\":\"-1\",\"msg\":\"0<金额<2000\"}";
core core=new core();
HashMap<String, String> order = core.createOrder(num, t);
return GsonUtils.ObjectToJson(order);
}
/**
* 查询订单
* @param id
* @param request
* @param response
* @return
*/
@RequestMapping(value = "/queryOrder", produces = "text/html;charset=UTF-8")
public String queryOrder(
@RequestParam(required = false, defaultValue = "") String id,
HttpServletRequest request,
HttpServletResponse response
) {
core core=new core();
Map<String, Object> order = core.queryOrder(id);
return GsonUtils.ObjectToJson(order);
}
/**
* 查询支付是否已完成
* @param id
* @param request
* @param response
* @return
*/
@RequestMapping(value = "/queryPay", produces = "text/html;charset=UTF-8")
public String queryPay(
@RequestParam(required = false, defaultValue = "") String id,
HttpServletRequest request,
HttpServletResponse response
) {
core core=new core();
Map<String, Object> order = core.queryOrder(id);
if(order!=null){
if("3".equals(order.get("ostatus")+"")||"4".equals(order.get("ostatus")+"")) return "{\"msg\":\"已支付\"}";
else return "{\"msg\":\"未支付\"}";
}else{
return "{\"msg\":\"未支付\"}";
}
}
网友评论