支付宝线下门店扫码支付开发
Posted elvesfish
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了支付宝线下门店扫码支付开发相关的知识,希望对你有一定的参考价值。
支付宝扫码支付
一.开发前准备
https://doc.open.alipay.com/doc2/detail.htm?spm=0.0.0.0.B6xA20&treeId=26&articleId=103239&docType=1
1.开通支付宝企业号
2.签约认证
3.创建应用,审核通过。
4.上传应用公钥。
二.开发demo从支付宝官网下载
alipay-sdk-java-ALL20150604113758-source.jar
alipay-sdk-java-ALL20150604113758.jar
/**
* 调用支付宝扫码支付二维码生成
*
* @return
*/
public String aliQr() {
String orderId = httpServletRequest.getParameter("orderId");
String out_trade_no = getTradeNo();
String total_amount = httpServletRequest.getParameter("total_amount");
String subject = "";
String companyId = getCompanyInfo().getCompanyId();
Order o = null;
try {
o = orderService.getOrderById(Integer.parseInt(orderId));
subject = CommonCompanyName(o.getCompanyId(), o.getSubCompanyId())
+ "的商品" + total_amount + "元";
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
AlipayTradePrecreateResponse response = qrPay(out_trade_no,
total_amount, subject, companyId, orderId, o.getSubCompanyId());
if (null != response && response.isSuccess()) {
ResponseWriteUtil.writehtml("{\\"code\\":\\"" + response.getQrCode()
+ "\\"," + "\\"outTradeNo\\":\\"" + response.getOutTradeNo()
+ "\\"}");
// 生成自定义二维码图片
// String path = "/qrcode/" + companyId + "/" + out_trade_no +
// ".png";
// String qrCodePath = ServletActionContext.getServletContext()
// .getRealPath(path);
// ZXingQrCode.createQrCodeImg(300, 300, response.getQrCode(),
// qrCodePath);
// 保存二维码值
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("orderId", orderId);
map.put("aliqrcodeUrl", response.getQrCode());
// map.put("aliqrcodeUrl", ConfigUtil.SYS_BASE_URL + path);
orderService.updateAliQrCodeByOrderId(map);
} else {
ResponseWriteUtil.writeHTML("{\\"code\\":\\"0\\"}");// 不存在
}
return null;
}
public String CommonCompanyName(String companyId, String subCompanyId) {
Seller seller = sellerService.findSellerByCompanyId(companyId);
String commonMsg = "";
SubCompany subCompany = null;
if (StringUtils.isNotBlank(subCompanyId)) {
subCompany = subCompanyService.fetchSubCompanyBySid(subCompanyId);
}
if (subCompany == null) {// 主店
commonMsg = seller.getCompanyName();
} else {// 分店
commonMsg = subCompany.getCompanyShortName();
}
return commonMsg;
}
/**
* 是否配置了支付宝二维码支付
*
* @return
*/
public String isExistByAliPayQrCode() {
String companyId = httpServletRequest.getParameter("companyId");
String subCompanyId = httpServletRequest.getParameter("subCompanyId");
HashMap<String, Object> map2 = new HashMap<String, Object>();
map2.put("companyId", companyId);
map2.put("subCompanyId", subCompanyId);
ALIPayConfigure aliPayConfigure = aliPayConfigureService
.selectAppId(map2);
if (aliPayConfigure != null) {
ResponseWriteUtil.writeHTML("{\\"code\\":\\"" + 1 + "\\"}");
} else {
ResponseWriteUtil.writeHTML("{\\"code\\":\\"" + 0 + "\\"}");
}
return null;
}
/**
* 回调通知处理
*
* @return
*/
public String notifyMsgByAliPay() {
String orderId = httpServletRequest.getParameter("orderId");
String totalAmount = httpServletRequest.getParameter("totalAmount");
String outTradeNo = httpServletRequest.getParameter("outTradeNo");
System.out.println("订单号:" + orderId + ",订单金额:" + totalAmount
+ ",商户交易号:" + outTradeNo);
System.out.println("支付宝异步回调通知:");
// 判断付款是否成功,已成功则不再记录付款信息
if (orderService.ifOrderPaid(Integer.parseInt(orderId)) > 0) {
log.info("付款已成功,不再记录付款信息");
ResponseWriteUtil.writeHTML("success");
return null;
}
aliPayService.aliPayFinsh(Integer.parseInt(orderId), totalAmount,
outTradeNo);
System.out.println("系统订单业务处理完成");
ResponseWriteUtil.writeHTML("success");// 告诉支付宝我已处理,不需要重复发异步通知
System.out.println("支付宝异步回调通知成功结束");
return null;
}
js页面
以上是关于支付宝线下门店扫码支付开发的主要内容,如果未能解决你的问题,请参考以下文章