您当前的位置: 首页 >  spring

梁云亮

暂无认证

  • 1浏览

    0关注

    1211博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

【精品】Spring2.7 采用easysdk方式 整合 aplipay

梁云亮 发布时间:2022-07-04 17:03:03 ,浏览量:1

本博客相关代码: CSDN:https://download.csdn.net/download/lianghecai52171314/85908379 GITEE:https://gitee.com/hcshow/alipay-demo

准备工作
  1. 注册真正的支付宝账号

  2. 在百度搜索支付宝开放平台 搜索到的网址:https://open.alipay.com/dev/workspace/ 在这里插入图片描述

  3. 用真正的支付宝账号登录 在这里插入图片描述

  4. 选择沙箱环境 在这里插入图片描述

  5. 打开沙箱应用页面 在控制台中查看到APPID: 在这里插入图片描述

  6. 沙箱账号 这个账号一会项目启动起来,支付时会使用到 在这里插入图片描述

  7. 启用系统默认密钥 在这里插入图片描述 在这里插入图片描述

  8. 查看 在这里插入图片描述

具体编码工作 第一步:创建SpringBoot项目,添加依赖:

    org.springframework.boot
    spring-boot-starter-web



    org.projectlombok
    lombok
    true




    com.alipay.sdk
    alipay-easysdk
    2.2.0

第二步:修改application.yml
server:
  port: 80
  servlet:
    context-path: /wego
alipay:
  #阿里支付
  appId: 2021000118674150
  #应用私钥
  appPrivateKey: 
  #支付宝公钥
  alipayPublicKey: 
  notifyUrl: 

其中: 在这里插入图片描述 notifyUrl是成功之后,阿里巴巴将支付结果回传到我们自己项目中的url的地址,此时需要网络穿透,请参看博客:内网穿透工具 netapp

第三步:读取alipay配置文件的配置类:AlipayConfig
@Getter
@Setter
@Component
@ConfigurationProperties(prefix = "alipay")
public class AlipayConfig {
   private String appId;
   private String appPrivateKey;
   private String alipayPublicKey;
   private String notifyUrl;

   @PostConstruct
   public void init(){
      Config options = getOptions();
      options.appId = this.appId;
      options.merchantPrivateKey=this.appPrivateKey;
      options.alipayPublicKey = this.alipayPublicKey;
      options.notifyUrl = this.notifyUrl;
      Factory.setOptions(options);
      System.out.println("支付宝初始化成功!");
   }

   private Config getOptions(){
      Config config = new Config();
      config.protocol="https";
      config.gatewayHost="openapi.alipaydev.com";
      config.signType="RSA2";
      return config;
   }
}
第四步:定义支付相关的控制器:AlipayController
@RestController
@RequestMapping("/v1/alipay")
public class AlipayController {
    //http://localhost/wego/v1/alipay/pay?subject=aabbcc&tradeNo=1001&totalAmount=8888
    @GetMapping(value = "/pay", produces = "text/html;charset=utf-8")
    String pay(String subject, String tradeNo, String totalAmount) {
        AlipayTradePagePayResponse response = null;
        //发起API调用
        try {
            response = Factory.Payment.Page().pay(subject, tradeNo, totalAmount, "");
        } catch (Exception e) {
            System.err.println("支付时遇到异常:");
            e.printStackTrace();
        }
        System.out.println(response.getBody());
        return response.getBody();
    }


    @PostMapping("/notify")
    String payNotify(HttpServletRequest request) throws Exception {
        System.out.println("pay notify");
        String tradeStatus = request.getParameter("trade_status");
        System.out.println(tradeStatus);
        if ("TRADE_SUCCESS".equals(tradeStatus)) {
            System.out.println("支付宝异步回调");
            Map params = new HashMap();
            Map requestParams = request.getParameterMap();
            for (String name : requestParams.keySet()) {
                params.put(name, request.getParameter(name));
            }
            String tradeNo = params.get("out_trade_no");
            String gmtPayment = params.get("gmt_payment");

            //支付宝验签
            if (Factory.Payment.Common().verifyNotify(params)) {
                System.out.println("交易名称:" + params.get("subject"));
                System.out.println("交易状态:" + params.get("trade_status"));
                System.out.println("支付宝交易凭证号:" + params.get("trade_no"));
                System.out.println("商户订单号:" + params.get("out_trade_no"));
                System.out.println("交易金额:" + params.get("total_amount"));
                System.out.println("买家在支付宝唯一ID:" + params.get("buyer_id"));
                System.out.println("买家付款时间:" + params.get("gmt_payment"));
                System.out.println("买家付款金额:" + params.get("buyer_pay_amount"));
                //更新订单为已支付
                //....
            }
        }
        return "success";
    }
}
第五步:运行程序测试
  • 打开edge浏览器(不要使用chrome),在其中输入:http://localhost/wego/v1/alipay/pay?subject=aabbcc&tradeNo=1021&totalAmount=8888,之后自支打开下图右边的网页 在这里插入图片描述

按图所示输入用户名和密码,单击提交: 在这里插入图片描述

  • 输入支付密码 在这里插入图片描述 单击确认支付: 在这里插入图片描述
  • 自动跳转到页面: 在这里插入图片描述 此时在IDEA控制台中会看到: 在这里插入图片描述
拓展

上述实现在准备工作中,采用的是系统默认密钥,也可以采用自定义密钥的方式: 在这里插入图片描述

第一步:下载支付宝开放平台开发助手

下载地址:https://opendocs.alipay.com/open/02kipk 在这里插入图片描述 下载之后默认安装,之后打开,生成密钥: 在这里插入图片描述

改成自定义密钥的方式

在这里插入图片描述 在这里插入图片描述

在这里插入图片描述 在这里插入图片描述 在这里插入图片描述

关注
打赏
1665409997
查看更多评论
立即登录/注册

微信扫码登录

0.0457s