Error message here!

Hide Error message here!

忘记密码?

Error message here!

请输入正确邮箱

Hide Error message here!

密码丢失?请输入您的电子邮件地址。您将收到一个重设密码链接。

Error message here!

返回登录

Close

springboot+支付宝条码支付开发详解

AAFE 2019-08-20 20:03:00 阅读数:20 评论数:0 点赞数:0 收藏数:0

背景:项目原有乐刷聚合支付,无法参加支付宝、微信等支付机构的官方活动

需求:增加原生支付(支付宝条码支付)

 

方法:

  一、官方文档:https://docs.open.alipay.com/194/106039

 

  二、没有真实企业账号的可以使用沙箱环境:https://docs.open.alipay.com/200/105311

 

  三、两种方法实现:

 

    (一)基于支付宝标准SDKalipay-sdk-java

 

      1pom文件中添加SDK依赖

 

        地址:https://mvnrepository.com/artifact/com.alipay.sdk/alipay-sdk-java

 

      2配置支付宝参数

        在系统常量类里追加(我这里配的是沙箱参数,就直接写在实现类里)

        

 

      3.实现代码

        

 package com.bhp.aaa.bbb.service;
 
 import com.alibaba.fastjson.JSONObject;
 import com.alipay.api.AlipayApiException;
 import com.alipay.api.AlipayClient;
 import com.alipay.api.DefaultAlipayClient;
 import com.alipay.api.request.AlipayTradeCancelRequest;
 import com.alipay.api.request.AlipayTradePayRequest;
 import com.alipay.api.request.AlipayTradeQueryRequest;
 import com.alipay.api.request.AlipayTradeRefundRequest;
 import com.alipay.api.response.AlipayTradeCancelResponse;
 import com.alipay.api.response.AlipayTradePayResponse;
 import com.alipay.api.response.AlipayTradeQueryResponse;
 import com.alipay.api.response.AlipayTradeRefundResponse;
 import com.google.common.collect.ImmutableMap;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.wustrive.java.core.request.ViewResult;
 import org.wustrive.java.dao.jdbc.dao.BaseDao;
 
 import javax.servlet.http.HttpServletRequest;
 import java.util.Map;
 
 @Service
 public class AlipayService1 {
     // 正式
     /*private static String gateway = SysConstants.Alipay.gateway;
     private static String appId = SysConstants.Alipay.app_id;
     private static String privateKey = SysConstants.Alipay.app_private_key;
     private static String publicKey = SysConstants.Alipay.alipay_public_key;*/
 
     // 沙箱
     //网关
     private static String gateway = "https://openapi.alipaydev.com/gateway.do";
     //应用ID
     private static String appId = "你自己的APPID";
     //应用秘钥
     private static String privateKey = "你自己的应用秘钥";
     //支付宝公钥
     private static String publicKey = "你自己的支付宝公钥";
 
     @Autowired
     private BaseDao baseDao;
     
     // 初始化一个统一的客户端
     AlipayClient alipayClient = new DefaultAlipayClient(gateway,appId,privateKey,"json","utf-8",publicKey,"RSA2");
 
     // 发起支付
     public  ViewResult alipayPay(AppUser appUser, String outTradeNo, String payMoney, String authCode){
         
         ViewResult viewResult = ViewResult.newInstance();
         String sql = "SELECT short_name FROM sys_merchants WHERE id=:merchantsId";
         String short_name =  baseDao.queryForString(sql, ImmutableMap.of("merchantsId",appUser.getMerchantsId()));
 
         JSONObject data = new JSONObject();
         data.put("out_trade_no",outTradeNo);
         data.put("scene","bar_code");
         data.put("auth_code",authCode);
         data.put("subject",short_name+"消费");
         data.put("store_id",appUser.getShopId());
         data.put("total_amount",payMoney);
 
         AlipayTradePayRequest request = new AlipayTradePayRequest();
         //request.setNotifyUrl(notifyUrl);
         request.setBizContent(data.toJSONString());
         try {
             AlipayTradePayResponse response = alipayClient.execute(request);
             if(response.isSuccess()){
                 viewResult.success(response.getBody());
             } else {
                 viewResult.fail(response.getBody());
             }
         } catch (AlipayApiException e) {
             viewResult.fail(e);
             e.printStackTrace();
         }finally {
             return viewResult;
         }
     }
 
     // 发起查询
     public ViewResult alipayQuery(String trade_no ){
         ViewResult viewResult = ViewResult.newInstance();
         JSONObject data = new JSONObject();
         data.put("trade_no",trade_no);
         AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
         request.setBizContent(data.toJSONString());
         try {
             AlipayTradeQueryResponse response = alipayClient.execute(request);
             if(response.isSuccess()){
                 viewResult.success(response.getBody());
             } else {
                 viewResult.fail(response.getBody());
             }
         } catch (AlipayApiException e) {
             viewResult.fail(e);
             e.printStackTrace();
         } finally {
             return viewResult;
         }
     }
 
     // 发起撤销
     public ViewResult alipayCancel(String trade_no){
         ViewResult viewResult = ViewResult.newInstance();
         JSONObject data = new JSONObject();
         data.put("trade_no",trade_no);
         AlipayTradeCancelRequest request = new AlipayTradeCancelRequest();
         request.setBizContent(data.toJSONString());
         try {
             AlipayTradeCancelResponse response = alipayClient.execute(request);
             if(response.isSuccess()){
                 viewResult.success(response.getBody());
             } else {
                 viewResult.fail(response.getBody());
             }
         } catch (AlipayApiException e) {
             viewResult.fail(e);
             e.printStackTrace();
         } finally {
             return viewResult;
         }
     }
 
     // 发起退款
     public ViewResult alipayRefund(String trade_no,String refund_amount){
         ViewResult viewResult = ViewResult.newInstance();
         JSONObject data = new JSONObject();
         data.put("trade_no",trade_no);
         data.put("refund_amount",refund_amount);
         AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
         request.setBizContent(data.toJSONString());
         try {
             AlipayTradeRefundResponse response = alipayClient.execute(request);
             if(response.isSuccess()){
                 viewResult.success(response.getBody());
             } else {
                 viewResult.fail(response.getBody());
             }
         } catch (AlipayApiException e) {
             viewResult.fail(e);
             e.printStackTrace();
         } finally {
             return viewResult;
         }
     }
 
 
 
 
 }
支付宝条码支付方法(一)

 

 未完,待续……

 

 

 

 

 

版权声明
本文为[AAFE]所创,转载请带上原文链接,感谢
https://www.cnblogs.com/java-bhp/p/11385250.html