java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Java 支付宝PC支付

Java开发支付宝PC支付完整版

作者:不努力读书长大只能当程序员

这篇文章主要介绍了Java开发支付宝PC支付完整版,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

一:准备工作

1:如没有账号的请先去 支付宝商户平台 注册商户号

2:开发前需要导入支付宝SDK 支付宝SDK下载链接 Maven依赖也在里面

3:生成支付宝密钥、公钥,需要用到支付宝密钥生成工具 密钥生成工具下载链接 ,密钥用于签名使用

4:电脑网站支付结果异步通知(关于支付结果异步通知后续会专门写一个博客,需要重点注意)

注意:请先详细查看官方文档按步骤开发,一切以官方文档为主 支付宝官方开发文档,如使用老接口请移步 老版即时到账文档

5:测试的时候一定要使用内网穿透软件,否则会报错

二:开发代码

博主使用的是沙箱环境,正式上线时请将环境改为正式环境

AlipayConfig:

public class AlipayConfig {
 public static String APPID = "";
 
 //用支付宝密钥生成工具生成的私钥
 public static String RSA_PRIVATE_KEY = "";
 
 //电脑网站支付结果异步通知,可以参考异步文档,一定要是外网能访问的
 public static String notify_url = ""; 
 
 //电脑网站支付结果同步通知,用于转跳到用户自己的页面,一定要是外网能访问的
 public static String return_url = ""; 
 
 //沙箱接口,正式上线时请改用正式支付接口
 public static String URL = "https://openapi.alipaydev.com/gateway.do"; 
 public static String CHARSET = "UTF-8";
 public static String FORMAT = "json";
 
 //用支付宝密钥生成工具生成的公钥
 public static String ALIPAY_PUBLIC_KEY = "";
 public static String log_path = "/log";
 public static String SIGNTYPE = "RSA2";
 
 public AlipayConfig() {
 }
}

AlipayService:

public interface AlipayService {
 public String toAlipay(Map<String, String> sourceMap) throws IOException;
}

 AlipayServiceImpl:

@Service
public class AlipayServiceImpl implements AlipayService {
 
 @Override
 public String toAlipay(Map<String, String> sourceMap) throws IOException {
  AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.URL,AlipayConfig.APPID,AlipayConfig.RSA_PRIVATE_KEY,AlipayConfig.FORMAT,AlipayConfig.CHARSET,AlipayConfig.ALIPAY_PUBLIC_KEY,AlipayConfig.SIGNTYPE);
  AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
  alipayRequest.setReturnUrl(AlipayConfig.return_url);
  alipayRequest.setNotifyUrl(AlipayConfig.notify_url);
  AlipayTradeWapPayModel alipayTradeWapPayModel = new AlipayTradeWapPayModel();
  alipayTradeWapPayModel.setOutTradeNo(sourceMap.get("out_trade_no"));
  alipayTradeWapPayModel.setProductCode("FAST_INSTANT_TRADE_PAY");
  alipayTradeWapPayModel.setTotalAmount(sourceMap.get("total_amount"));
  alipayTradeWapPayModel.setSubject("Alipay");
  alipayTradeWapPayModel.setBody(sourceMap.get("body"));
  alipayRequest.setBizModel(alipayTradeWapPayModel);
  String form="";
  try {
   form = alipayClient.pageExecute(alipayRequest).getBody();
  } catch (AlipayApiException e) {
   e.printStackTrace();
  }
  return form;
 }
 
}

 AlipayController:

@Controller
public class AlipayController {
 
 @Autowired
 public AlipayService alipayService;
 
 
 @RequestMapping("pay")
 public void toAlipay(HttpServletRequest httpRequest , HttpServletResponse httpResponse, String payId, String totalFee, String body) throws IOException {
 
  //生成一笔支付记录,支付完成时将支付状态改为成功
 
 
  Map<String, String> sourceMap = new HashMap<>();
  sourceMap.put("out_trade_no",payId);
  sourceMap.put("total_amount",totalFee);
  sourceMap.put("body",getUTF8XMLString(body));
 
 
  String form = alipayService.toAlipay(sourceMap);
  httpResponse.setContentType("text/html;charset=" + AlipayConfig.CHARSET);
  httpResponse.getWriter().write(form);
  httpResponse.getWriter().flush();
  httpResponse.getWriter().close();
 }
 
 
 //注意异步返回结果通知是以post请求形式返回的
 @RequestMapping("notifyUrl")
 public String notify_url(HttpServletRequest request){
  Map<String, String> paramsMap = convertRequestParamsToMap(request);
  String out_trade_no= paramsMap.get("out_trade_no");
  try {
   boolean signVerified = AlipaySignature.rsaCheckV1(paramsMap, AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.CHARSET, AlipayConfig.SIGNTYPE);
   //无论同步异步都要验证签名
   if(signVerified){
    if(trade_status.equals("TRADE_FINISHED") || trade_status.equals("TRADE_SUCCESS")){
     //处理自己系统的业务逻辑,如:将支付记录状态改为成功,需要返回一个字符串success告知支付宝服务器
     
     return "success";
    } else {
     //支付失败不处理业务逻辑
     return "failure";
    }
   }else {
    //签名验证失败不处理业务逻辑
    return "failure";
   }
  } catch (AlipayApiException e) {
   e.printStackTrace();
   return "failure";
  }
 }
 
 //注意同步返回结果是以get请求形式返回的
 @RequestMapping("returnUrl")
 public String return_url(HttpServletRequest request){
  Map<String, String> paramsMap = convertRequestParamsToMap(request);
  try {
   boolean signVerified = AlipaySignature.rsaCheckV1(paramsMap, AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.CHARSET, AlipayConfig.SIGNTYPE);
   if(signVerified){
    //跳转支付成功界面
    return "支付成功页面";
 
   }else {
    //跳转支付失败界面
    return "failure";
   }
  } catch (AlipayApiException e) {
   e.printStackTrace();
  }
  return "success";
 }
 
 
 //将请求中的参数转换为Map
 public static Map<String, String> convertRequestParamsToMap(HttpServletRequest request) {
  Map<String, String> retMap = new HashMap();
  Set<Entry<String, String[]>> entrySet = request.getParameterMap().entrySet();
  Iterator var3 = entrySet.iterator();
 
  while(true) {
   while(var3.hasNext()) {
    Entry<String, String[]> entry = (Entry)var3.next();
    String name = (String)entry.getKey();
    String[] values = (String[])entry.getValue();
    int valLen = values.length;
    if(valLen == 1) {
     retMap.put(name, values[0]);
    } else if(valLen <= 1) {
     retMap.put(name, "");
    } else {
     StringBuilder sb = new StringBuilder();
     String[] var9 = values;
     int var10 = values.length;
 
     for(int var11 = 0; var11 < var10; ++var11) {
      String val = var9[var11];
      sb.append(",").append(val);
     }
 
     retMap.put(name, sb.toString().substring(1));
    }
   }
 
   return retMap;
  }
 }
 
 //将字符串转换为UTF-8编码以防出现乱码错误
 public static String getUTF8XMLString(String xml) {
  StringBuffer sb = new StringBuffer();
  sb.append(xml);
  String xmString = "";
  String xmlUTF8="";
  try {
   xmString = new String(sb.toString().getBytes("UTF-8"));
   xmlUTF8 = URLEncoder.encode(xmString, "UTF-8");
  } catch (UnsupportedEncodingException e) {
   e.printStackTrace();
  }
  return xmlUTF8;
 }
}

至此代码已经完成!!!

三:演示结果

注意:扫码或者登陆账户时如果用的是沙箱开发环境请使用沙盒支付宝APP扫码,或者使用沙盒账户支付,否则会弹出码已失效错误

支付宝沙箱

支付成功页面是从支付结果同步通知controller中跳转的,后续会更新最详细最完整的微信Navicat支付跟PayPal支付,有兴趣请关注博主

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文