springboot整合沙箱支付宝

springboot整合沙箱支付宝

效果如下:

springboot整合沙箱支付宝_第1张图片

springboot整合沙箱支付宝_第2张图片 springboot整合沙箱支付宝_第3张图片 springboot整合沙箱支付宝_第4张图片
  1. 首先去 支付宝开放平台 获取APPID 配置私钥 公钥

从里面下载 支付宝开发平台开发助手: RSA2 点击生成密钥 把应用公钥到你沙箱 那里面配置 ! 他会生成一个支付宝公钥

springboot整合沙箱支付宝_第5张图片

环境准备好了开始来实现

1.依赖

<dependency>
    <groupId>com.alipay.sdkgroupId>
    <artifactId>alipay-easysdkartifactId>
    <version>2.2.0version>
dependency>
<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-configuration-processorartifactId>
    <version>2.7.1version>
dependency>
<dependency>
    <groupId>com.alipay.sdkgroupId>
    <artifactId>alipay-sdk-javaartifactId>
    <version>4.31.40.ALLversion>
dependency>

2.在 resources 下面新建文件夹 config 在新建 alipay_is.yml

alipay:
  APPID: APPID
  RSA_PRIVATE_KEY:  应用私钥
  NOTIFY_URL: 异步回调
  RETURN_URL: 同步回调地址
  URL: https://openapi.alipaydev.com/gateway.do 网关
  CHARSET: UTF-8
  FORMAT: json
  ALIPAY_PUBLIC_KEY: 支付宝公钥
  SIGN_TYPE: RSA2

新建对呀注册类: 方便使用

package com.alipay.it.config.yml;
import com.alipay.it.config.factory.YamlPropertySourceFactory;
import lombok.Builder;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.ConstructorBinding;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import java.io.Serializable;
@SuppressWarnings("All")
@Data
@Component
@ConfigurationProperties(prefix = "alipay")
@PropertySource(value = "classpath:config/alipay_is.yml",factory = YamlPropertySourceFactory.class)
public class AlipayIS implements Serializable {
    /**
     * 1.[APPID ]
2.[商户私钥]
3.[异步通知]
4.[同步通知]
5.[请求网关地址]
6.[编码]
7.[返回格式]
8.[支付宝公钥]
9.[RSA2]
*/
private String APPID; private String RSA_PRIVATE_KEY; private String NOTIFY_URL; private String RETURN_URL; private String URL; private String CHARSET; private String FORMAT; private String ALIPAY_PUBLIC_KEY; private String SIGN_TYPE; }

在 resources 在新建 config 会扫描不到:所以我们还要来一个配置类

package com.alipay.it.config.factory;


import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.support.DefaultPropertySourceFactory;
import org.springframework.core.io.support.EncodedResource;

import java.io.IOException;
import java.util.Properties;
@Configuration
public class YamlPropertySourceFactory extends DefaultPropertySourceFactory {
    @Override
    public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
        String sourceName = (name == null) ? resource.getResource().getFilename() : name;
        assert sourceName != null;
        if (sourceName.endsWith(".yml") || sourceName.endsWith(".yaml")) {
            YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
            factory.setResources(resource.getResource());
            factory.afterPropertiesSet();
            Properties properties = factory.getObject();
            assert properties != null;
            return new PropertiesPropertySource(sourceName, properties);
        }
        return super.createPropertySource(name, resource);
    }
}


然后就来实现 Controller

package com.alipay.it.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.ZolozAuthenticationSmilepayInitializeRequest;
import com.alipay.api.response.ZolozAuthenticationSmilepayInitializeResponse;
import com.alipay.it.config.yml.AlipayIS;
import com.alipay.it.tool.MailTool;
import com.alipay.it.tool.RandomCharStr;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RequiredArgsConstructor
@RestController
@RequestMapping("/alipay")
public class AliPayController {
    private final  AlipayIS alipayIS;
     @RequestMapping(value = "/is",method = RequestMethod.GET)
     public Object alipay_is() throws AlipayApiException {
         AlipayClient alipayClient = new DefaultAlipayClient
                 (alipayIS.getURL(),alipayIS.getAPPID(),alipayIS.getRSA_PRIVATE_KEY(),
                  alipayIS.getFORMAT(),alipayIS.getCHARSET(),alipayIS.getALIPAY_PUBLIC_KEY(),
                  alipayIS.getSIGN_TYPE());
         AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
         alipayRequest.setReturnUrl(alipayIS.getRETURN_URL());
         String out_trade_no = RandomCharStr.getRandomCharStr(20); //我生成的订单编号
         //付款金额,必填
         String total_amount = String.valueOf(4*10000);
         //订单名称,必填
         String subject ="用户"+"dsadadadad"+"影票订单";
         //商品描述,可空
         String body = "用户订购商品个数:" + 2;

         // 该笔订单允许的最晚付款时间,逾期将关闭交易。取值范围:1m~15d。m-分钟,h-小时,d-天,1c-当天(1c-当天的情况下,无论交易何时创建,都在0点关闭)。 该参数数值不接受小数点, 如 1.5h,可转换为 90m。
         String timeout_express = "5m";
         alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                         + "\"total_amount\":\""+ total_amount +"\","
                         + "\"subject\":\""+ subject +"\","
                         + "\"body\":\""+ body +"\","
                         + "\"timeout_express\":\""+ timeout_express +"\","
                         + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
         String results = alipayClient.pageExecute(alipayRequest).getBody();

         return results;
     }
     
     //这里可以写你的回调地址
     @RequestMapping(value = "/returnisa",method = RequestMethod.GET)
     public Object returnisa(){
         Map<String, Object> objectHashMap = new HashMap<>();
         objectHashMap.put("Messges","支付成功");
         objectHashMap.put("code",200);
         return objectHashMap;
     }

}

要加载一个 app 沙箱环境的 可以去官网下载

这就实现了!! 是不是超级简单??

你可能感兴趣的:(java,spring,boot,spring)