java 支付宝支付
程序员文章站
2022-07-13 16:59:08
...
支付宝支付前提
需开通支付宝支付服务
官网地址
文档地址
项目导入支付宝maven依赖
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>4.8.10.ALL</version>
</dependency>
相关配置文件, 读取配置文件
// 配置文件,在项目resources目录下
#ali
alipay.public.key =
alipay.private.key =
alipay.app.public.key =
alipay.app.pay.notify.url =
alipay.app.id =
gateway_url = https://openapi.alipaydev.com/gateway.do
// 读取配置文件类
@Component
public class SysConfig
{
public static final String ENCODING = "UTF-8";
// 配置文件类名称
public static final String CONFIG_PATH = "";
private Properties props = null;
/**
* 获取全局唯一的SysConfig 对象
*
* @return
*/
public static SysConfig getInstance()
{
SysConfig config = ConfigHolder.INSTANCE.getConf();
config.init();
return config;
}
/**
* 初始化
*/
private void init()
{
props = new Properties();
loadConfigProps();
}
/**
* 加载配置文件
*/
public void loadConfigProps()
{
InputStream is = null;
try
{
is = getClass().getResourceAsStream(CONFIG_PATH);
props.load(is);
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
if (is != null)
{
try
{
is.close();
is = null;
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
}
public String getProperty(String key)
{
String tmp = props.getProperty(key);
if (StringUtils.isNotEmpty(tmp))
{
return tmp.trim();
}
return tmp;
}
enum ConfigHolder
{
INSTANCE;
SysConfig conf;
ConfigHolder()
{
conf = new SysConfig();
}
public SysConfig getConf()
{
return conf;
}
}
}
初始化AlipayClient
/**
* 获得初始化的AlipayClient
*
* @return AlipayClient
*/
private AlipayClient getAlipayClient() {
// 获得初始化的AlipayClient
return new DefaultAlipayClient(
//支付宝网关
SysConfig.getInstance().getProperty(EnumAlipayKey.gateway_url.getKey()),
//appid
SysConfig.getInstance().getProperty(EnumAlipayKey.app_id.getKey()),
//商户私钥
SysConfig.getInstance().getProperty(EnumAlipayKey.alipay_private_key.getKey()),
AlipayConstants.FORMAT_JSON,
//字符编码格式
AlipayConstants.CHARSET_UTF8,
//支付宝公钥
SysConfig.getInstance().getProperty(EnumAlipayKey.alipay_public_key.getKey()),
//签名方式
AlipayConstants.SIGN_TYPE_RSA2
);
}
调用支付的request
` // 获得初始化的AlipayClient
AlipayClient alipayClient = getAlipayClient();
// 设置请求参数
AlipayTradeAppPayRequest alipayRequest = new AlipayTradeAppPayRequest();
// 服务器异步通知,支付宝会回调该路径,必须外网且不能有请求参数,获取回调参数校验后 处理自己业务逻辑(修改订单状态等等)
alipayRequest.setNotifyUrl(SysConfig.getInstance().getProperty(EnumAlipayKey.app_pay_notify_url.getKey()));
// 封装参数
AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
model.setOutTradeNo(orderSn);
model.setTotalAmount(orderInfo.getPayPrice());
model.setSubject(orderInfo.getCourseName());
model.setBody("");
model.setProductCode("FAST_INSTANT_TRADE_PAY");
model.setTimeoutExpress("15m");
alipayRequest.setBizModel(model);
// 请求支付宝进行支付,获取支付结果
String result = alipayClient.sdkExecute(alipayRequest).getBody();
log.info("支付结果:{}", result);
调用查询订单request
// 获得初始化的AlipayClient
AlipayClient alipayClient = getAlipayClient();
AlipayTradeQueryRequest queryRequest = new AlipayTradeQueryRequest();
AlipayTradeQueryModel queryModel = new AlipayTradeQueryModel();
queryModel.setOutTradeNo(orderSn);
queryRequest.setBizModel(queryModel);
// 调用查询
AlipayTradeQueryResponse execute = alipayClient.execute(queryRequest);
log.info("返回结果:{}", execute.getBody()));
退款,退款查询等等类似
好啦, 就到这了,如有不足和疑问请留言
上一篇: 微信小程序 动态修改颜色
下一篇: [Android]取30天之前的日期