一、创建maven项目
springboot项目接入支付宝(一)
springboot项目接入支付宝(三)
二、获取依赖
1、springboot
<!--导入SpringBoot的父工程 把系统中的版本号做了一些定义! -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.4</version>
</parent>
2、alipay-easysdk
<!-- https://mvnrepository.com/artifact/com.alipay.sdk/alipay-easysdk -->
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-easysdk</artifactId>
<version>2.2.0</version>
</dependency>
3、web
<!--导入SpringBoot的Web场景启动器 Web相关的包导入!-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
4、plugin
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
三、创建启动类
package com.tony.alipay;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Created with IntelliJ IDEA.
*
* @Title: PayApplication
* @Auther: 皮蛋布丁
* @Date: 2021/06/24/21:25
* @Description:
*/
@SpringBootApplication
public class PayApplication {
public static void main(String[] args) {
SpringApplication.run(PayApplication .class,args);
}
}
四、SDK的使用
五、创建配置文件
#支付宝-沙箱
alipay:
appId: 2021002121608816
#协议(固定)
protocol: https
#域名
gatewayHost: openapi.alipaydev.com
#签名类型(固定)
signType: RSA2
#应用私钥(不能有空格)
merchantPrivateKey: 填入自己应用私钥
#支付宝公钥(不能有空格)
alipayPublicKey: 填入自己支付宝公钥
#异步通知地址
notifyUrl: http://localhost:8080/alipay/notifyUrl
#同步通知地址
returnUrl: http://localhost:8080/alipay/returnUrl
注:配置域名gatewayHost的时候,注意沙箱环境alipay后带dev,正式环境不带dev。
五、定义配置类
1、设置参数
package com.tony.alipay.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
/**
* Created with IntelliJ IDEA.
*
* @Title: AlipayConfig
* @Auther: 皮蛋布丁
* @Date: 2021/06/24/21:42
* @Description:
*/
@Configuration
public class AlipayConfig {
//@Value():将配置文件的值配置过来
//appid
@Value("${alipay.appId}")
private String appId;
//协议
@Value("${alipay.protocol}")
private String protocol;
//网关
@Value("${alipay.gatewayHost}")
private String gatewayHost;
//RSA2
@Value("${alipay.signType}")
private String signType;
//私钥
@Value("${alipay.merchantPrivateKey}")
private String merchantPrivateKey;
//支付宝公钥字符串即可
@Value("${alipay.alipayPublicKey}")
private String alipayPublicKey;
//可设置异步通知接收服务地址
@Value("${alipay.notifyUrl}")
private String notifyUrl;
}
2、参数赋值
启动Application启动类后,扫描@bean,就会调用此方法进行赋值。
@Bean
public Config getAlipayConfig(){
Config config=new Config();
config.appId=appId;
config.protocol=protocol;
config.gatewayHost=gatewayHost;
config.signType=signType;
config.merchantPrivateKey=merchantPrivateKey;
config.alipayPublicKey=alipayPublicKey;
config.notifyUrl=notifyUrl;
Factory.setOptions(config);
return config;
}
六、构建支付请求
1、api列表
2、创建service接口
/**
* @Description: webPagePay web端订单支付
* @Param: [subject 商品名称, outTradeNo 订单编号(唯一), totalAmount 订单价格]
* @return: java.lang.String
* @Author: 皮蛋布丁
* @Date: 2021/6/24 21:59
*/
String webPagePay(String subject, String outTradeNo, String totalAmount);
3、实现接口
注:记得在类外面添加@Service注解。
package com.tony.alipay.serice;
import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.kernel.util.ResponseChecker;
import com.alipay.easysdk.payment.page.models.AlipayTradePagePayResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
/**
* Created with IntelliJ IDEA.
*
* @Title: AlipayServiceImpl
* @Auther: 皮蛋布丁
* @Date: 2021/06/24/22:00
* @Description:
*/
@Service
public class AlipayServiceImpl implements AlipayService {
Logger log = LoggerFactory.getLogger(AlipayServiceImpl.class);
@Value("${alipay.returnUrl}")
private String returnUrl;
@Override
public String webPagePay(String subject, String outTradeNo, String totalAmount) {
//returnUrl:同步通知地址
try {
AlipayTradePagePayResponse response = Factory.Payment.Page().pay(subject, outTradeNo, totalAmount, returnUrl);
if (ResponseChecker.success(response)) {
log.info("调用成功!");
return response.getBody();
} else {
log.error("调用失败!原因:" + response.getBody());
}
} catch (Exception e) {
log.error("调用异常!原因:" + e.getMessage());
}
return null;
}
}
4、创建controller接口
package com.tony.alipay.controller;
import com.tony.alipay.serice.AlipayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
/**
* Created with IntelliJ IDEA.
*
* @Title: AlipayController
* @Auther: 皮蛋布丁
* @Date: 2021/06/24/22:14
* @Description:
*/
@RestController
@ResponseBody
@RequestMapping("/alipay")
public class AlipayController {
@Autowired
AlipayService alipayService;
/**
* @Description: WebPagePay 发起支付
* @Param: [outTradeNo, subject, totalAmount]
* @return: java.lang.String
* @Author: 皮蛋布丁
* @Date: 2021/6/24 22:22
*/
@PostMapping("/pay")
public String WebPagePay(String outTradeNo,String subject,String totalAmount) {
return alipayService.webPagePay(subject,outTradeNo,totalAmount);
}
}
5、创建网页
<form name=alipayment action=/alipay/pay method=post
target="_blank">
<div id="body1" class="show" name="divcontent">
<dl class="content">
<dt>商户订单号 :</dt>
<dd>
<input id="WIDout_trade_no" name="outTradeNo" />
</dd>
<hr class="one_line">
<dt>订单名称 :</dt>
<dd>
<input id="WIDsubject" name="subject" />
</dd>
<hr class="one_line">
<dt>付款金额 :</dt>
<dd>
<input id="WIDtotal_amount" name="totalAmount" />
</dd>
<hr class="one_line">
<dt></dt>
<dd id="btn-dd">
<span class="new-btn-login-sp">
<button class="new-btn-login" type="submit"
style="text-align: center;">支付</button>
</span> <span class="note-help"></span>
</dd>
</dl>
</div>
</form>
七、启动运行
1、访问主页
访问地址为:
http://localhost:8080/index.html
主页:
2、支付操作
点击【支付】后:
3、异常解决
钓鱼网站异常解决方法:把浏览器上打开的所有沙箱支付、支付宝官方等的页面全部关闭,然后Crtl+Shift+delete,清空浏览器缓存。再试一下果然就打开了。
此处显示扫码支付就说明调用成功啦!
注:能力有限,还请谅解,争取早日能够写出有质量的文章!