当前位置: 首页 > article >正文

SpringBoot沙箱环境支付宝支付

1、创建应用
登录支付宝开放平台
支付宝开放平台网址 :支付宝 (alipay.com)

支付宝开放平台

下拉最下边

2、获取appId、支付宝公钥、应用私钥

3、开始创建spring boot项目

#支付宝支付配置
alipay:
  #你的APPID
  appId: 902************
  #你的应用私钥
  privateKey: MIIE***********
  #支付宝公钥
  publicKey:  MIIB***********
  #生产环境网关地址
  #serverUrl: https://openapi.alipay.com/gateway.do
  #沙箱环境网关地址
  serverUrl: https://openapi-sandbox.dl.alipaydev.com/gateway.do
  #生产环境域
  domain: gw.open.alipay.com
  #回调url
  noticeUrl: https://ie2204wi1077.vicp.fun


4、导入pom依赖


    
    <dependency>
        <groupId>com.alipay.sdk</groupId>
        <artifactId>alipay-sdk-java</artifactId>
        <version>4.39.134.ALL</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.alipay.sdk/alipay-easysdk -->
    <dependency>
        <groupId>com.alipay.sdk</groupId>
        <artifactId>alipay-easysdk</artifactId>
        <version>2.2.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-core</artifactId>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <scope>provided</scope>
    </dependency>


5、创建Controller

准备好密钥

import com.alipay.api.AlipayApiException;
import com.pig4cloud.pigx.alipay.util.PayVo;
import com.pig4cloud.pigx.alipay.service.AliPayService;
import com.pig4cloud.pigx.common.core.util.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Slf4j
@RequestMapping("/alipay")
@RestController
public class AliPayController {
    @Resource
    private AliPayService aliPayService;


    @RequestMapping("/pay")
    public R paymoney(@RequestBody PayVo payVo) throws AlipayApiException {
        return R.ok(aliPayService.pay(payVo));
    }

    @PostMapping("/notify")  // 注意这里必须是POST接口
    public R notify(HttpServletRequest request) throws Exception {
        return R.ok(aliPayService.notify(request));
    }

}


6、创建一个实体类来接受前端传递过来的参数
这是我的


import lombok.Data;

@Data
public class PayVo {
    //订单id
    private String orderId;
    //会诊id
    private String conId;
    //支付金额
    private String money;
    //支付标题
    private String title;
    //成功返回页面
    private String url;

}

7、AliPayService.java

import com.alipay.api.AlipayApiException;
import com.baomidou.mybatisplus.extension.service.IService;
import com.pig4cloud.pigx.alipay.util.PayVo;
import com.pig4cloud.pigx.ybiot.entity.MzRecipe;
import com.pig4cloud.pigx.ybiot.entity.RecipeDTO;
import org.springframework.web.bind.annotation.RequestBody;

import javax.servlet.http.HttpServletRequest;


/**
 * 支付宝支付接口
 *
 * @author gwh
 * @date 2024-11-11 09:08:14
 */
public interface AliPayService extends IService<MzRecipe> {

    /**
     * 支付接口
     * @param payVo
     * @return
     */
    RecipeDTO pay(@RequestBody PayVo payVo) throws AlipayApiException;

    RecipeDTO notify(HttpServletRequest request) throws Exception;

}

8、AliPayServiceImpl.java


import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayTradeWapPayResponse;
import com.alipay.easysdk.factory.Factory;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pigx.alipay.util.AliPayConfig;
import com.pig4cloud.pigx.alipay.util.PayVo;
import com.pig4cloud.pigx.alipay.service.AliPayService;
import com.pig4cloud.pigx.ybiot.entity.MzRecipe;
import com.pig4cloud.pigx.ybiot.entity.RecipeDTO;
import com.pig4cloud.pigx.ybiot.mapper.MzRecipeMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 支付宝支付实现
 *
 * @author gwh
 * @date 2024-11-11 09:39:14
 */
@Slf4j
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class AliPayServiceImpl extends ServiceImpl<MzRecipeMapper, MzRecipe> implements AliPayService {

    @Autowired
    private AliPayConfig aliPayConfig;

    @Override
    public RecipeDTO pay(PayVo payVo) throws AlipayApiException {
        RecipeDTO resultDTO = new RecipeDTO();
        System.out.println(payVo);
        String appId = aliPayConfig.getAppId();// AppId
        String privateKey = aliPayConfig.getPrivateKey(); //必填 自己的应用私钥,即PKCS8格式RSA2私钥
        String alipayPublicKey = aliPayConfig.getPublicKey();  //必填 自己的支付宝公钥
        String serverUrl = aliPayConfig.getServerUrl(); //必填  serverUrl
        String noticeUrl = aliPayConfig.getServerUrl(); //回调url
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl(serverUrl); //沙箱
        alipayConfig.setAppId(appId);   //必填 自己的AppId
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF-8");
        alipayConfig.setSignType("RSA2");
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
        request.setNotifyUrl(noticeUrl);  //自己的内网地址, 这个是我的自己可以下载路由下自行配置
        request.setReturnUrl(payVo.getUrl());  //成功后跳转的地址
        AlipayTradeWapPayModel model = new AlipayTradeWapPayModel();
        model.setOutTradeNo(payVo.getOrderId()); //传过来的订单号, 这个我写的 自己写一个默认的测试一下
        model.setTotalAmount(payVo.getMoney()); //支付金额, 这个我写的 自己写一个默认的测试一下
        model.setSubject(payVo.getTitle());  //支付标题, 这个我写的 自己写一个默认的测试一下
        model.setProductCode("QUICK_WAP_WAY");
        model.setSellerId("2088721041615706");
        request.setBizModel(model);
        AlipayTradeWapPayResponse response = alipayClient.pageExecute(request, "POST");
        // 如果需要返回GET请求,请使用
        // AlipayTradeWapPayResponse response = alipayClient.pageExecute(request, "GET");
        String pageRedirectionData = response.getBody();
        System.out.println(pageRedirectionData);
        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
            // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
            // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
            // System.out.println(diagnosisUrl);
        }
        return resultDTO;
    }

    @Override
    public RecipeDTO notify(HttpServletRequest request) throws Exception {
        RecipeDTO resultDTO = new RecipeDTO();
        if (request.getParameter("trade_status").equals("TRADE_SUCCESS")) {
            System.out.println("=========支付宝异步回调========");
            Map<String, String> params = new HashMap<>();
            Map<String, String[]> requestParams = request.getParameterMap();
            for (String name : requestParams.keySet()) {
                params.put(name, request.getParameter(name));
                // System.out.println(name + " = " + request.getParameter(name));
            }
            String tradeNo = params.get("out_trade_no");
            String gmtPayment = params.get("gmt_payment");
            String alipayTradeNo = params.get("trade_no");
            // 支付宝验签
            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 resultDTO;
    }
}

9、付款页面

10、付款成功页面
这样就支付完成了


http://www.kler.cn/a/390755.html

相关文章:

  • Openstack7--安装消息队列服务RabbitMQ
  • 【C#设计模式(8)——过滤器模式(Adapter Pattern)】
  • 万字长文解读深度学习——ViT、ViLT、DiT
  • MYSQL 库,表 基本操作
  • 推荐一款好用的postman替代工具2024
  • 【Qt-ROS开发】使用 Qt Creator 构建和编译含 ROS 库的 Qt 项目
  • C# 阿里云短信接口调用(不使用SDK,单文件完成)
  • Spring Spring Boot 常用注解总结
  • 前端零基础学习Day-Eight
  • 使用 Java 将 byte[] 转换为 File 对象并上传到外部服务器
  • 【已解决】Postman:Get请求传JSON数据
  • Kafka面试题(三)
  • html的week控件 获取周(星期)的第一天(周一)和最后一天(周日)
  • Pandas | 数据分析时将特定列转换为数字类型 float64 或 int64的方法
  • scikit-learn学习Day30
  • Java基础08(类与对象)
  • Java字符串的处理
  • SSE (Server-Sent Events) 服务器实时推送详解
  • 力扣-Hot100-哈希【算法学习day.30】
  • HTMLCSS: 日落卡片
  • MySQL核心业务大表归档过程
  • Attention is all you need详细解读
  • STM32问题集
  • ES5 和 ES6 数组的操作方法
  • ISAAC SIM踩坑记录--ubuntu 22.04操作系统安装
  • 小水电远程集控运维系统简介及应用价值