zoukankan      html  css  js  c++  java
  • springboot+支付宝条码支付开发详解

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

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

    方法:

      一、官方文档: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.实现代码

            

      1 package com.bhp.aaa.bbb.service;
      2 
      3 import com.alibaba.fastjson.JSONObject;
      4 import com.alipay.api.AlipayApiException;
      5 import com.alipay.api.AlipayClient;
      6 import com.alipay.api.DefaultAlipayClient;
      7 import com.alipay.api.request.AlipayTradeCancelRequest;
      8 import com.alipay.api.request.AlipayTradePayRequest;
      9 import com.alipay.api.request.AlipayTradeQueryRequest;
     10 import com.alipay.api.request.AlipayTradeRefundRequest;
     11 import com.alipay.api.response.AlipayTradeCancelResponse;
     12 import com.alipay.api.response.AlipayTradePayResponse;
     13 import com.alipay.api.response.AlipayTradeQueryResponse;
     14 import com.alipay.api.response.AlipayTradeRefundResponse;
     15 import com.google.common.collect.ImmutableMap;
     16 import org.springframework.beans.factory.annotation.Autowired;
     17 import org.springframework.stereotype.Service;
     18 import org.wustrive.java.core.request.ViewResult;
     19 import org.wustrive.java.dao.jdbc.dao.BaseDao;
     20 
     21 import javax.servlet.http.HttpServletRequest;
     22 import java.util.Map;
     23 
     24 @Service
     25 public class AlipayService1 {
     26     // 正式
     27     /*private static String gateway = SysConstants.Alipay.gateway;
     28     private static String appId = SysConstants.Alipay.app_id;
     29     private static String privateKey = SysConstants.Alipay.app_private_key;
     30     private static String publicKey = SysConstants.Alipay.alipay_public_key;*/
     31 
     32     // 沙箱
     33     //网关
     34     private static String gateway = "https://openapi.alipaydev.com/gateway.do";
     35     //应用ID
     36     private static String appId = "你自己的APPID";
     37     //应用秘钥
     38     private static String privateKey = "你自己的应用秘钥";
     39     //支付宝公钥
     40     private static String publicKey = "你自己的支付宝公钥";
     41 
     42     @Autowired
     43     private BaseDao baseDao;
     44     
     45     // 初始化一个统一的客户端
     46     AlipayClient alipayClient = new DefaultAlipayClient(gateway,appId,privateKey,"json","utf-8",publicKey,"RSA2");
     47 
     48     // 发起支付
     49     public  ViewResult alipayPay(AppUser appUser, String outTradeNo, String payMoney, String authCode){
     50         
     51         ViewResult viewResult = ViewResult.newInstance();
     52         String sql = "SELECT short_name FROM sys_merchants WHERE id=:merchantsId";
     53         String short_name =  baseDao.queryForString(sql, ImmutableMap.of("merchantsId",appUser.getMerchantsId()));
     54 
     55         JSONObject data = new JSONObject();
     56         data.put("out_trade_no",outTradeNo);
     57         data.put("scene","bar_code");
     58         data.put("auth_code",authCode);
     59         data.put("subject",short_name+"消费");
     60         data.put("store_id",appUser.getShopId());
     61         data.put("total_amount",payMoney);
     62 
     63         AlipayTradePayRequest request = new AlipayTradePayRequest();
     64         //request.setNotifyUrl(notifyUrl);
     65         request.setBizContent(data.toJSONString());
     66         try {
     67             AlipayTradePayResponse response = alipayClient.execute(request);
     68             if(response.isSuccess()){
     69                 viewResult.success(response.getBody());
     70             } else {
     71                 viewResult.fail(response.getBody());
     72             }
     73         } catch (AlipayApiException e) {
     74             viewResult.fail(e);
     75             e.printStackTrace();
     76         }finally {
     77             return viewResult;
     78         }
     79     }
     80 
     81     // 发起查询
     82     public ViewResult alipayQuery(String trade_no ){
     83         ViewResult viewResult = ViewResult.newInstance();
     84         JSONObject data = new JSONObject();
     85         data.put("trade_no",trade_no);
     86         AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
     87         request.setBizContent(data.toJSONString());
     88         try {
     89             AlipayTradeQueryResponse response = alipayClient.execute(request);
     90             if(response.isSuccess()){
     91                 viewResult.success(response.getBody());
     92             } else {
     93                 viewResult.fail(response.getBody());
     94             }
     95         } catch (AlipayApiException e) {
     96             viewResult.fail(e);
     97             e.printStackTrace();
     98         } finally {
     99             return viewResult;
    100         }
    101     }
    102 
    103     // 发起撤销
    104     public ViewResult alipayCancel(String trade_no){
    105         ViewResult viewResult = ViewResult.newInstance();
    106         JSONObject data = new JSONObject();
    107         data.put("trade_no",trade_no);
    108         AlipayTradeCancelRequest request = new AlipayTradeCancelRequest();
    109         request.setBizContent(data.toJSONString());
    110         try {
    111             AlipayTradeCancelResponse response = alipayClient.execute(request);
    112             if(response.isSuccess()){
    113                 viewResult.success(response.getBody());
    114             } else {
    115                 viewResult.fail(response.getBody());
    116             }
    117         } catch (AlipayApiException e) {
    118             viewResult.fail(e);
    119             e.printStackTrace();
    120         } finally {
    121             return viewResult;
    122         }
    123     }
    124 
    125     // 发起退款
    126     public ViewResult alipayRefund(String trade_no,String refund_amount){
    127         ViewResult viewResult = ViewResult.newInstance();
    128         JSONObject data = new JSONObject();
    129         data.put("trade_no",trade_no);
    130         data.put("refund_amount",refund_amount);
    131         AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
    132         request.setBizContent(data.toJSONString());
    133         try {
    134             AlipayTradeRefundResponse response = alipayClient.execute(request);
    135             if(response.isSuccess()){
    136                 viewResult.success(response.getBody());
    137             } else {
    138                 viewResult.fail(response.getBody());
    139             }
    140         } catch (AlipayApiException e) {
    141             viewResult.fail(e);
    142             e.printStackTrace();
    143         } finally {
    144             return viewResult;
    145         }
    146     }
    147 
    148 
    149 
    150 
    151 }
    支付宝条码支付方法(一)

     未完,待续……

     

     

  • 相关阅读:
    [转载] 使用DBProxy读写分离
    [转载] 数据库索引类型及实现方式
    [转载] 分布式数据库ID生成系统
    将博客搬至CSDN
    ERROR:通过端口 1433 连接到主机 localhost 的 TCP/IP 连接失败。错误:“Connection refused: connect。请验证连接属性,并检查 SQL Server 的实例正在主机上运行,且在此端口接受 TCP/IP 连接,还要确保防火墙没有阻止到此端口的 TCP
    JAVA:对数据库的一系列操作代码
    JAVA与 SQL server2008进行连接
    怎么样将数据库的表在Java中界面中显示出来
    c#与c++交互的一些东西
    WPF实现摄像头镜像翻转
  • 原文地址:https://www.cnblogs.com/java-bhp/p/11385250.html
Copyright © 2011-2022 走看看