关闭

支付宝线下门店扫码支付开发

3305人阅读 评论(0) 收藏 举报
分类:
转载自:http://blog.csdn.net/l2000h_ing/article/details/50679363

支付宝扫码支付

一.开发前准备

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


[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /** 
  2.      * 调用支付宝扫码支付二维码生成 
  3.      *  
  4.      * @return 
  5.      */  
  6.     public String aliQr() {  
  7.         String orderId = httpServletRequest.getParameter("orderId");  
  8.         String out_trade_no = getTradeNo();  
  9.         String total_amount = httpServletRequest.getParameter("total_amount");  
  10.         String subject = "";  
  11.         String companyId = getCompanyInfo().getCompanyId();  
  12.         Order o = null;  
  13.         try {  
  14.             o = orderService.getOrderById(Integer.parseInt(orderId));  
  15.             subject = CommonCompanyName(o.getCompanyId(), o.getSubCompanyId())  
  16.                     + "的商品" + total_amount + "元";  
  17.         } catch (Exception e) {  
  18.             // TODO Auto-generated catch block  
  19.             e.printStackTrace();  
  20.         }  
  21.         AlipayTradePrecreateResponse response = qrPay(out_trade_no,  
  22.                 total_amount, subject, companyId, orderId, o.getSubCompanyId());  
  23.         if (null != response && response.isSuccess()) {  
  24.             ResponseWriteUtil.writeHTML("{\"code\":\"" + response.getQrCode()  
  25.                     + "\"," + "\"outTradeNo\":\"" + response.getOutTradeNo()  
  26.                     + "\"}");  
  27.             // 生成自定义二维码图片  
  28.             // String path = "/qrcode/" + companyId + "/" + out_trade_no +  
  29.             // ".png";  
  30.             // String qrCodePath = ServletActionContext.getServletContext()  
  31.             // .getRealPath(path);  
  32.             // ZXingQrCode.createQrCodeImg(300, 300, response.getQrCode(),  
  33.             // qrCodePath);  
  34.             // 保存二维码值  
  35.             HashMap<String, Object> map = new HashMap<String, Object>();  
  36.             map.put("orderId", orderId);  
  37.             map.put("aliqrcodeUrl", response.getQrCode());  
  38.             // map.put("aliqrcodeUrl", ConfigUtil.SYS_BASE_URL + path);  
  39.             orderService.updateAliQrCodeByOrderId(map);  
  40.         } else {  
  41.             ResponseWriteUtil.writeHTML("{\"code\":\"0\"}");// 不存在  
  42.         }  
  43.         return null;  
  44.     }  
  45.   
  46.     public String CommonCompanyName(String companyId, String subCompanyId) {  
  47.         Seller seller = sellerService.findSellerByCompanyId(companyId);  
  48.         String commonMsg = "";  
  49.         SubCompany subCompany = null;  
  50.         if (StringUtils.isNotBlank(subCompanyId)) {  
  51.             subCompany = subCompanyService.fetchSubCompanyBySid(subCompanyId);  
  52.         }  
  53.         if (subCompany == null) {// 主店  
  54.             commonMsg = seller.getCompanyName();  
  55.         } else {// 分店  
  56.             commonMsg = subCompany.getCompanyShortName();  
  57.         }  
  58.         return commonMsg;  
  59.     }  
  60.   
  61.     /** 
  62.      * 是否配置了支付宝二维码支付 
  63.      *  
  64.      * @return 
  65.      */  
  66.     public String isExistByAliPayQrCode() {  
  67.         String companyId = httpServletRequest.getParameter("companyId");  
  68.         String subCompanyId = httpServletRequest.getParameter("subCompanyId");  
  69.         HashMap<String, Object> map2 = new HashMap<String, Object>();  
  70.         map2.put("companyId", companyId);  
  71.         map2.put("subCompanyId", subCompanyId);  
  72.         ALIPayConfigure aliPayConfigure = aliPayConfigureService  
  73.                 .selectAppId(map2);  
  74.         if (aliPayConfigure != null) {  
  75.             ResponseWriteUtil.writeHTML("{\"code\":\"" + 1 + "\"}");  
  76.         } else {  
  77.             ResponseWriteUtil.writeHTML("{\"code\":\"" + 0 + "\"}");  
  78.         }  
  79.         return null;  
  80.     }  

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /** 
  2.      * 回调通知处理 
  3.      *  
  4.      * @return 
  5.      */  
  6.     public String notifyMsgByAliPay() {  
  7.         String orderId = httpServletRequest.getParameter("orderId");  
  8.         String totalAmount = httpServletRequest.getParameter("totalAmount");  
  9.         String outTradeNo = httpServletRequest.getParameter("outTradeNo");  
  10.         System.out.println("订单号:" + orderId + ",订单金额:" + totalAmount  
  11.                 + ",商户交易号:" + outTradeNo);  
  12.         System.out.println("支付宝异步回调通知:");  
  13.         // 判断付款是否成功,已成功则不再记录付款信息  
  14.         if (orderService.ifOrderPaid(Integer.parseInt(orderId)) > 0) {  
  15.             log.info("付款已成功,不再记录付款信息");  
  16.             ResponseWriteUtil.writeHTML("success");  
  17.             return null;  
  18.         }  
  19.         aliPayService.aliPayFinsh(Integer.parseInt(orderId), totalAmount,  
  20.                 outTradeNo);  
  21.         System.out.println("系统订单业务处理完成");  
  22.         ResponseWriteUtil.writeHTML("success");// 告诉支付宝我已处理,不需要重复发异步通知  
  23.         System.out.println("支付宝异步回调通知成功结束");  
  24.         return null;  
  25.     }  

js页面
[javascript] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. //*************start*********aliPayByQr******************************  
  2. //是否配置了支付宝扫码  
  3. function isExistByAliPayQrCode(){  
  4.     var companyId=$("#companyId").val();  
  5.     var subCompanyId=$("#subCompanyId").val();  
  6.     if(subCompanyId==null||subCompanyId=='null'||subCompanyId==undefined){  
  7.         subCompanyId="";  
  8.     }  
  9.     $.ajax({  
  10.         url : '<%=basePath%>/aliPay!isExistByAliPayQrCode.action?companyId='+companyId+'&subCompanyId='+subCompanyId,  
  11.         async : false,  
  12.         type : "post",  
  13.         dataType : "json",  
  14.         success : function(result) {  
  15.              if(result.code!='0'){  
  16.                 $("#aliQrBtn").show();  
  17.              }else{  
  18.                 $("#aliQrBtn").hide();  
  19.                 layer.msg("商家没有配置支付宝扫码支付!");  
  20.              }  
  21.         }  
  22.     });  
  23. }  
  24.       
  25. function aliQrClick(){  
  26.     var total_amount=$("#paymentAmount").val();  
  27.     var orderId=$("#myorderId").val();  
  28.     //check  
  29.     var paySize=$(".payMoneyC").length;  
  30.     var ptypeNum=$(".ptype:checked").length;  
  31.     var paySum=0;  
  32.     if(ptypeNum<=0){  
  33.         layer.msg("支付方式至少选择一个!");  
  34.         return;  
  35.     }  
  36.     if(ptypeNum!=1){  
  37.         layer.msg("支付方式只能是支付宝扫码支付!");  
  38.         return;  
  39.     }  
  40.     var companyId=$("#companyId").val();  
  41.     var subCompanyId=$("#subCompanyId").val();  
  42.     if(subCompanyId==null||subCompanyId=='null'||subCompanyId==undefined){  
  43.         subCompanyId="";  
  44.     }  
  45.     $.ajax({  
  46.         url : '<%=basePath%>/aliPay!aliQr.action',  
  47.         async : false,  
  48.         type : "post",  
  49.         dataType : "json",  
  50.         data:{"total_amount":total_amount,"orderId":orderId},  
  51.         success : function(result) {  
  52.             if(result.code!='0'){  
  53.                 $("#aliQrBtn").hide();  
  54.                 $("#outputALIQr").show();  
  55.                 <span style="color:#ff0000;">jQuery('#outputALIQr').qrcode({width:200,height:200,text:result.code});//</span><span style="color: rgb(255, 0, 0); font-family: Arial, Helvetica, sans-serif;">js</span><span style="color: rgb(255, 0, 0); font-family: Arial, Helvetica, sans-serif;">二维码生成</span><span style="color:#ff0000;">  
  56. </span>               window.setInterval(finshAliQrPay, 8000);   
  57.             }else{  
  58.                 layer.msg("支付宝二维码生成出错!");  
  59.             }  
  60.         }  
  61.     });  
  62. }  
  63.   
  64. function qrQueryRetry(out_trade_no){  
  65.     $.ajax({  
  66.         url : '<%=basePath%>/aliPay!aliQueryRetry.action',  
  67.         async : false,  
  68.         type : "post",  
  69.         dataType : "json",  
  70.         data:{"out_trade_no":out_trade_no},  
  71.         success : function(result) {  
  72.         }  
  73.     });  
  74. }  
  75. var aliNum=0;  
  76. function finshAliQrPay(){  
  77.     var orderId=$("#myorderId").val();  
  78.     $.ajax({  
  79.         url : '<%=basePath%>/aliPay!<span style="color:#cc0000;background-color: rgb(255, 255, 255);">notifyOrderCashFinshed</span>.action?orderId='+orderId,  
  80.         async : false,  
  81.         type : "post",  
  82.         dataType : "json",  
  83.         data:$('#finishForm').serialize(),  
  84.         success : function(result) {  
  85.              if(result.code!='0'){  
  86.                 layer.msg("支付宝扫码支付交易成功,订单3秒后即将关闭!");  
  87.                 window.setTimeout(function(){  
  88.                 //关闭弹出窗之前,跳转到其他页面  
  89.                 parent.window.location.href="<%=path%>/"+result.redirectUrl;  
  90.                 closeLayerDialog();  
  91.                 },3000);   
  92.              }else{  
  93.                 aliNum=aliNum+1;  
  94.                 $("#aliQrPayNotifyNum").val(aliNum);  
  95.              }  
  96.         }  
  97.     });  
  98. }  
  99.   
  100. //*************end*******aliPayByQr********************************  


5.aliPayService 实现类
[java] view plain copy
  1. import java.text.SimpleDateFormat;  
  2. import java.util.ArrayList;  
  3. import java.util.Date;  
  4. import java.util.HashMap;  
  5. import java.util.List;  
  6.   
  7. import javax.annotation.Resource;  
  8.   
  9. import org.apache.log4j.Logger;  
  10. import org.springframework.stereotype.Service;  
  11.   
  12. import com.alipay.api.AlipayApiException;  
  13. import com.alipay.api.AlipayClient;  
  14. import com.alipay.api.DefaultAlipayClient;  
  15. import com.alipay.api.request.AlipayTradePrecreateRequest;  
  16. import com.alipay.api.request.AlipayTradeQueryRequest;  
  17. import com.alipay.api.request.AlipayTradeRefundRequest;  
  18. import com.alipay.api.response.AlipayTradePrecreateResponse;  
  19. import com.alipay.api.response.AlipayTradeQueryResponse;  
  20. import com.alipay.api.response.AlipayTradeRefundResponse;  
  21.   
  22.   
  23. @Service("aliPayService")  
  24. public class ALIPayServiceImpl implements ALIPayService {  
  25.     Logger log = Logger.getLogger(ALIPayServiceImpl.class);  
  26.   
  27.     @Resource  
  28.     private OrderDAO orderDAO;  
  29.     @Resource  
  30.     private SerialNumMngDAO serialNumMngDAO;  
  31.     @Resource  
  32.     private ALIPayTradeLogsDAO aliPayTradeLogsDAO;  
  33.     @Resource  
  34.     private ALIPayConfigureDAO aliPayConfigureDAO;  
  35.   
  36.     @Resource  
  37.     private ALIPayTradeLogsService aliPayTradeLogsService;  
  38.     @Resource  
  39.     private ALIPayConfigureService aliPayConfigureService;  
  40.     @Resource  
  41.     private OrderService orderService;  
  42.   
  43.     @Override  
  44.     public void aliPayFinsh(Integer orderId, String totalAmount,  
  45.             String outTradeNo) {  
  46.         // TODO Auto-generated method stub  
  47.         Order o = null;  
  48.         float paymentAmount = Float.parseFloat(totalAmount);  
  49.         try {  
  50.             o = orderDAO.getOrderbyId(orderId);  
  51.             // 系统交易流水记录  
  52.             List<OrderPlus> ops = new ArrayList<OrderPlus>();  
  53.             OrderPlus op = new OrderPlus();  
  54.             op.setOrderId(o.getId());  
  55.             op.setOrderAmount(o.getAmount());  
  56.             op.setPaymentAmount(paymentAmount);  
  57.             op.setPaymentTime(new Date());  
  58.             op.setPaymentType(EnumUtil.PAYMENT_TYPE.alipayqrbycompay.getCode());  
  59.             op.setNotes("支付宝扫码支付:支付金额为" + paymentAmount);  
  60.             ops.add(op);  
  61.             orderDAO.insertOrderPlusPitch(ops);  
  62.   
  63.             // 更新系统订单业务  
  64.             HashMap<String, Object> map = new HashMap<String, Object>();  
  65.             map.put("orderId", o.getId());  
  66.             // 外卖  
  67.             if (o.getIsTakeOut() == 1) {  
  68.                 map.put("dealSts", EnumUtil.ORDER_dealSts.finished.getCode());  
  69.             } else {  
  70.                 map.put("dealSts", EnumUtil.ORDER_dealSts.paid.getCode());  
  71.             }  
  72.             map.put("finishTime"new Date());  
  73.             map.put("paymentAmount", paymentAmount);  
  74.             map.put("orderPersonNum"0);  
  75.             map.put("orderTime", o.getOrderTime());  
  76.             serialNumMngDAO.finishOrder(map);  
  77.   
  78.             // 支付宝交易流水记录  
  79.             HashMap<String, Object> map2 = new HashMap<String, Object>();  
  80.             map2.put("companyId", o.getCompanyId());  
  81.             map2.put("subCompanyId", o.getSubCompanyId());  
  82.             ALIPayConfigure alipc = aliPayConfigureDAO.selectAppId(map2);  
  83.             ALIPayTradeLogs aliPaytl = new ALIPayTradeLogs();  
  84.             aliPaytl.setOutTradeNo(outTradeNo);  
  85.             if (alipc != null) {  
  86.                 aliPaytl.setAppId(alipc.getAppId());  
  87.             }  
  88.             aliPaytl.setOrderId(String.valueOf(orderId));  
  89.             aliPaytl.setTotalAmount(paymentAmount);  
  90.             aliPaytl.setChannel("ALI_OFFLINE_QRCODE");  
  91.             aliPaytl.setTradeDate(new Date());  
  92.             aliPayTradeLogsDAO.insert(aliPaytl);  
  93.   
  94.         } catch (Exception e) {  
  95.             // TODO Auto-generated catch block  
  96.             e.printStackTrace();  
  97.         }  
  98.     }  
  99.   
  100.     @Override  
  101.     public HashMap<String, Object> refundOrderByALIQr(String orderId,  
  102.             String refund_amount) {  
  103.         // TODO Auto-generated method stub  
  104.         HashMap<String, Object> map = new HashMap<String, Object>();  
  105.         ALIPayTradeLogs ali = aliPayTradeLogsService.selectByOrderId(Integer  
  106.                 .parseInt(orderId));  
  107.         if (ali != null) {  
  108.             Order o = null;  
  109.             try {  
  110.                 o = orderService.getOrderById(Integer.parseInt(orderId));  
  111.             } catch (Exception e) {  
  112.                 e.printStackTrace();  
  113.             }  
  114.             HashMap<String, Object> queryMap = query(ali.getOutTradeNo(),  
  115.                     o.getCompanyId(), o.getSubCompanyId());  
  116.             int flag = (Integer) queryMap.get("flag");  
  117.             String tradeNo = (String) queryMap.get("tradeNo");  
  118.             if (flag == 1) {  
  119.                 boolean refundFlag = refundOrder(tradeNo, refund_amount, "",  
  120.                         o.getCompanyId(), o.getSubCompanyId());  
  121.                 map.put("refundFlag", refundFlag);  
  122.                 map.put("refundMsg""支付宝扫码(企业)提示:该支付款按原路退到买家帐号,请注意查收!");  
  123.             } else if (flag == 2) {  
  124.                 map.put("refundFlag"false);  
  125.                 map.put("refundMsg""平台提示:支付宝扫码等待付款");  
  126.             } else if (flag == 3) {  
  127.                 map.put("refundFlag"false);  
  128.                 map.put("refundMsg""平台提示:支付宝扫码未付款关闭,或已付款的订单全额退款后关闭");  
  129.             } else if (flag == 4) {  
  130.                 map.put("refundFlag"false);  
  131.                 map.put("refundMsg""平台提示:支付宝扫码订单不可退款或撤销");  
  132.             } else if (flag == 0) {  
  133.                 map.put("refundFlag"false);  
  134.                 map.put("refundMsg""平台提示:支付宝扫码请求未成功,请重试");  
  135.             }  
  136.         }  
  137.         return map;  
  138.     }  
  139.   
  140.     public HashMap<String, Object> query(final String out_trade_no,  
  141.             String companyId, String subCompanyId) {  
  142.         String tradeNo = "";  
  143.         HashMap<String, Object> map = new HashMap<String, Object>();  
  144.         AlipayClient alipayClient = getAlipayClientByCompanyId(companyId,  
  145.                 subCompanyId);  
  146.         AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();  
  147.         String biz_content = "{\"out_trade_no\":\"" + out_trade_no + "\"}";  
  148.         request.setBizContent(biz_content);  
  149.         AlipayTradeQueryResponse response = null;  
  150.         try {  
  151.             response = alipayClient.execute(request);  
  152.             System.out.println(response.getBody());  
  153.             System.out.println(response.getCode());  
  154.             if (null != response && response.isSuccess()) {  
  155.                 System.out.println("买家账号:" + response.getBuyerLogonId());  
  156.                 System.out.println("商户订单号:" + response.getOutTradeNo());  
  157.                 System.out.println("支付宝交易号:" + response.getTradeNo());  
  158.                 System.out.println("总金额:" + response.getTotalAmount());  
  159.                 System.out.println("订单状态:" + response.getTradeStatus());  
  160.                 if (response.getCode().equals("10000")) {  
  161.                     tradeNo = response.getTradeNo();  
  162.                     if ("TRADE_SUCCESS".equalsIgnoreCase(response  
  163.                             .getTradeStatus())) {  
  164.                         map.put("tradeNo", tradeNo);  
  165.                         map.put("flag"1);  
  166.                         // 表示已付款  
  167.                     } else if ("WAIT_BUYER_PAY".equalsIgnoreCase(response  
  168.                             .getTradeStatus())) {  
  169.                         map.put("tradeNo", tradeNo);  
  170.                         map.put("flag"2);  
  171.                         // 表示等待付款  
  172.                     } else if ("TRADE_CLOSED".equalsIgnoreCase(response  
  173.                             .getTradeStatus())) {  
  174.                         map.put("tradeNo", tradeNo);  
  175.                         map.put("flag"3);  
  176.                         // 表示未付款关闭,或已付款的订单全额退款后关闭  
  177.                     } else if ("TRADE_FINISHED".equalsIgnoreCase(response  
  178.                             .getTradeStatus())) {  
  179.                         // 此状态,订单不可退款或撤销  
  180.                         map.put("tradeNo", tradeNo);  
  181.                         map.put("flag"4);  
  182.                     }  
  183.                 } else {  
  184.                     // 如果请求未成功,请重试  
  185.                     map.put("tradeNo", tradeNo);  
  186.                     map.put("flag"0);  
  187.                 }  
  188.             }  
  189.         } catch (AlipayApiException e) {  
  190.             // TODO Auto-generated catch block  
  191.             e.printStackTrace();  
  192.         }  
  193.         return map;  
  194.     }  
  195.   
  196.     /** 
  197.      * 申请退单 
  198.      *  
  199.      * @param trade_no 
  200.      * @author jinlong.rhj 
  201.      * @date 2015年4月27日 
  202.      * @version 1.0 
  203.      * @return 
  204.      */  
  205.     public boolean refundOrder(String trade_no, String refund_amount,  
  206.             String out_request_no, String companyId, String subCompanyId) {  
  207.         boolean flag = false;  
  208.         AlipayClient alipayClient = getAlipayClientByCompanyId(companyId,  
  209.                 subCompanyId);  
  210.         AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();  
  211.   
  212.         String biz_content = "{\"trade_no\":\""  
  213.                 + trade_no  
  214.                 + "\",\"refund_amount\":\""  
  215.                 + refund_amount  
  216.                 + "\",\"out_request_no\":\""  
  217.                 + out_request_no  
  218.                 + "\",\"refund_reason\":\"正常退单\",\"store_id\":\"\",\"terminal_id\":\"\"}";  
  219.         System.out.println(biz_content);  
  220.         request.setBizContent(biz_content);  
  221.   
  222.         AlipayTradeRefundResponse response = null;  
  223.   
  224.         try {  
  225.             response = alipayClient.execute(request);  
  226.             // System.out.println(response.getBody());  
  227.             // System.out.println(response.getCode());  
  228.             // System.out.println(response.getMsg());  
  229.             // System.out.println(response.getSubCode());  
  230.             // System.out.println(response.getSubMsg());  
  231.             // System.out.println(response.getBuyerLogonId());  
  232.             // System.out.println(response.getFundChange());  
  233.             // System.out.println(response.getOpenId());  
  234.             // System.out.println(response.getOutTradeNo());  
  235.             // System.out.println(response.getRefundFee());  
  236.             // System.out.println(response.getGmtRefundPay());  
  237.             // System.out.println(response.getOpenId());  
  238.             if (null != response && response.isSuccess()) {  
  239.                 if (response.getCode().equals("10000")) {  
  240.                     if (response.getFundChange().equals("Y")) {  
  241.                         // 退款成功,资金有变动,做业务及账务处理  
  242.                         flag = true;  
  243.                     } else {  
  244.                         // 资金无变动,不必做账务处理  
  245.                     }  
  246.                 } else {  
  247.                     // 没有撤销成功,需要重试几次  
  248.                     // refundOrderRetry(trade_no, refund_amount, out_request_no,  
  249.                     // 6);  
  250.                 }  
  251.             }  
  252.         } catch (AlipayApiException e) {  
  253.             e.printStackTrace();  
  254.         }  
  255.         return flag;  
  256.     }  
  257.   
  258.     @Override  
  259.     public AlipayClient getAlipayClientByCompanyId(String companyId,  
  260.             String subCompanyId) {  
  261.         HashMap<String, Object> map2 = new HashMap<String, Object>();  
  262.         map2.put("companyId", companyId);  
  263.         map2.put("subCompanyId", subCompanyId);  
  264.         ALIPayConfigure aliPayConfigure = aliPayConfigureService  
  265.                 .selectAppId(map2);  
  266.         DefaultAlipayClient alipayClient = null;  
  267.         if (aliPayConfigure != null) {  
  268.             alipayClient = new DefaultAlipayClient(  
  269.                     AlipayServiceEnvConstants.ALIPAY_GATEWAY,  
  270.                     aliPayConfigure.getAppId(),  
  271.                     aliPayConfigure.getPrivateKey(), "json",  
  272.                     AlipayServiceEnvConstants.CHARSET,  
  273.                     AlipayServiceEnvConstants.ALIPAY_PUBLIC_KEY);  
  274.         }  
  275.         return alipayClient;  
  276.     }  
  277.   
  278.     /** 
  279.      * 支付宝扫码支付二维码生成接口 
  280.      *  
  281.      * @param out_trade_no 
  282.      * @param total_amount 
  283.      * @param subject 
  284.      * @param companyId 
  285.      * @return 
  286.      */  
  287.     public AlipayTradePrecreateResponse qrPay(String out_trade_no,  
  288.             String total_amount, String subject, String companyId,  
  289.             String orderId, String subCompanyId) {  
  290.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  291.         String time_expire = sdf.format(System.currentTimeMillis() + 24 * 60  
  292.                 * 60 * 1000);  
  293.         StringBuilder sb = new StringBuilder();  
  294.         sb.append("{\"out_trade_no\":\"" + out_trade_no + "\",");  
  295.         sb.append("\"total_amount\":\"" + total_amount  
  296.                 + "\",\"discountable_amount\":\"0.00\",");  
  297.         sb.append("\"subject\":\"" + subject + "\",");  
  298.         sb.append("\"time_expire\":\"" + time_expire + "\"}");  
  299.         System.out.println(sb.toString());  
  300.   
  301.         AlipayClient alipayClient = getAlipayClientByCompanyId(companyId,  
  302.                 subCompanyId);  
  303.   
  304.         // 使用SDK,构建群发请求模型  
  305.         AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();  
  306.         request.setBizContent(sb.toString());  
  307.         request.setNotifyUrl(ConfigUtil.SYS_BASE_URL  
  308.                 + "/aliPay!notifyMsgByAliPay.action" + "?orderId=" + orderId  
  309.                 + "&totalAmount=" + total_amount + "&outTradeNo="  
  310.                 + out_trade_no);  
  311.         AlipayTradePrecreateResponse response = null;  
  312.         // 商家配置不成功  
  313.         if (alipayClient == null) {  
  314.             return response;  
  315.         }  
  316.         try {  
  317.             // 使用SDK,调用交易下单接口  
  318.             response = alipayClient.execute(request);  
  319.   
  320.             System.out.println(response.getBody());  
  321.             System.out.println(response.isSuccess());  
  322.             System.out.println(response.getMsg());  
  323.             // 这里只是简单的打印,请开发者根据实际情况自行进行处理  
  324.             if (null != response && response.isSuccess()) {  
  325.                 if (response.getCode().equals("10000")) {  
  326.                     System.out.println("商户订单号:" + response.getOutTradeNo());  
  327.                     System.out.println("二维码值:" + response.getQrCode());// 商户将此二维码值生成二维码,然后展示给用户,用户用支付宝手机钱包扫码完成支付  
  328.                     // 二维码的生成,网上有许多开源方法,可以参看:http://blog.csdn.net/feiyu84/article/details/9089497  
  329.   
  330.                 } else {  
  331.                     // 打印错误码  
  332.                     System.out.println("错误码:" + response.getSubCode());  
  333.                     System.out.println("错误描述:" + response.getSubMsg());  
  334.                 }  
  335.             }  
  336.         } catch (AlipayApiException e) {  
  337.         }  
  338.         return response;  
  339.     }  
  340.   
  341. }  


ALIPayService接口
[java] view plain copy
  1. import java.util.HashMap;  
  2.   
  3. import com.alipay.api.AlipayClient;  
  4. import com.alipay.api.response.AlipayTradePrecreateResponse;  
  5.   
  6. public interface ALIPayService {  
  7.   
  8.     /** 
  9.      * 支付宝扫码支付成功后,订单业务处理 
  10.      *  
  11.      * @param orderId 
  12.      */  
  13.     public void aliPayFinsh(Integer orderId, String totalAmount,  
  14.             String outTradeNo);  
  15.   
  16.     /** 
  17.      * 支付宝扫码支付申请退款业务处理 
  18.      *  
  19.      * @param orderId 
  20.      * @param refund_amount 
  21.      * @return 
  22.      */  
  23.     public HashMap<String, Object> refundOrderByALIQr(String orderId, String refund_amount);  
  24.   
  25.     /** 
  26.      * 各商家的获得API调用客户端 
  27.      *  
  28.      * @param companyId 
  29.      * @param subCompanyId 
  30.      * @return 
  31.      */  
  32.     public AlipayClient getAlipayClientByCompanyId(String companyId,  
  33.             String subCompanyId);  
  34.   
  35.     /** 
  36.      * 支付宝扫码支付二维码生成接口 
  37.      *  
  38.      * @param out_trade_no 
  39.      * @param total_amount 
  40.      * @param subject 
  41.      * @param companyId 
  42.      * @return 
  43.      */  
  44.     public AlipayTradePrecreateResponse qrPay(String out_trade_no,  
  45.             String total_amount, String subject, String companyId,  
  46.             String orderId, String subCompanyId);  
  47.   
  48. }  

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:78895次
    • 积分:1042
    • 等级:
    • 排名:千里之外
    • 原创:2篇
    • 转载:167篇
    • 译文:0篇
    • 评论:11条
    最新评论