当前位置:网站首页>对接支付宝支付
对接支付宝支付
2022-07-28 23:44:00 【IT盛夏的果实】
根据支付宝沙箱配置进行支付
配置文件:
@Configuration
@PropertySource("classpath:alipay-sandbox.properties")
public class AliPayClientConfig {
@Resource
private Environment config;
@Bean
public AlipayClient alipayClient() throws AlipayApiException {
AlipayConfig alipayConfig = new AlipayConfig();
//设置网关地址
alipayConfig.setServerUrl(config.getProperty("alipay.gateway-url"));
//设置应用Id
alipayConfig.setAppId(config.getProperty("alipay.app-id"));
//设置应用私钥
alipayConfig.setPrivateKey(config.getProperty("alipay.merchant-private-key"));
//设置请求格式,固定值json
alipayConfig.setFormat(AlipayConstants.FORMAT_JSON);
//设置字符集
alipayConfig.setCharset(AlipayConstants.CHARSET_UTF8);
//设置支付宝公钥
alipayConfig.setAlipayPublicKey(config.getProperty("alipay.alipay-public-key"));
//设置签名类型
alipayConfig.setSignType(AlipayConstants.SIGN_TYPE_RSA2);
//构造client
AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
return alipayClient;
}
}统一收单下单并支付页面接口:
controller层:
@ApiOperation("统一收单下单并支付页面接口的调用")
@PostMapping("/trade/page/pay/{productId}")
public R tradePagePay(@PathVariable Long productId){
log.info("统一收单下单并支付页面接口的调用");
//支付宝开放平台接受 request 请求对象后
// 会为开发者生成一个html 形式的 form表单,包含自动提交的脚本
String formStr = aliPayService.tradeCreate(productId);
//我们将form表单字符串返回给前端程序,之后前端将会调用自动提交脚本,进行表单的提交
//此时,表单会自动提交到action属性所指向的支付宝开放平台中,从而为用户展示一个支付页面
return R.ok().data("formStr", formStr);
}service层:
@Transactional(rollbackFor = Exception.class)
@Override
public String tradeCreate(Long productId) {
try {
//生成订单
log.info("生成订单");
OrderInfo orderInfo = orderInfoService.createOrderByProductId(productId, PayType.ALIPAY.getType());
//调用支付宝接口
AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
//配置需要的公共请求参数
//支付完成后,支付宝向谷粒学院发起异步通知的地址
request.setNotifyUrl(config.getProperty("alipay.notify-url"));
//支付完成后,我们想让页面跳转回谷粒学院的页面,配置returnUrl
request.setReturnUrl(config.getProperty("alipay.return-url"));
//组装当前业务方法的请求参数
JSONObject bizContent = new JSONObject();
bizContent.put("out_trade_no", orderInfo.getOrderNo());
BigDecimal total = new BigDecimal(orderInfo.getTotalFee().toString()).divide(new BigDecimal("100"));
bizContent.put("total_amount", total);
bizContent.put("subject", orderInfo.getTitle());
bizContent.put("product_code", "FAST_INSTANT_TRADE_PAY");
request.setBizContent(bizContent.toString());
//执行请求,调用支付宝接口
AlipayTradePagePayResponse response = alipayClient.pageExecute(request);
if(response.isSuccess()){
log.info("调用成功,返回结果 ===> " + response.getBody());
return response.getBody();
} else {
log.info("调用失败,返回码 ===> " + response.getCode() + ", 返回描述 ===> " + response.getMsg());
throw new RuntimeException("创建支付交易失败");
}
} catch (AlipayApiException e) {
e.printStackTrace();
throw new RuntimeException("创建支付交易失败");
}
}进行付款:


输入密码就可支付完成。
边栏推荐
- seleniumwire获取百度指数
- [untitled]
- Asynchronous mode worker thread
- 直流无刷电机控制器(换电机霍尔收费多少)
- 保护性拷贝&无状态
- 从零开始实现lmax-Disruptor队列(六)Disruptor 解决伪共享、消费者优雅停止实现原理解析
- 双链表的定义 ~
- 散列表 ~
- Daniel guild Games: summary and future outlook of this year
- Wechat campus bathroom reservation of small program completion work (6) opening defense ppt
猜你喜欢

How to explain JS' bind simulation implementation to your girlfriend

Talk about the cross end technical scheme

Data warehouse construction - ads floor

DDD领域驱动设计如何进行工程化落地
![Cloud function realizes website automatic check-in configuration details [web function /nodejs/cookie]](/img/e3/496247afdb3ea5b9a9cdb8afb0d41b.png)
Cloud function realizes website automatic check-in configuration details [web function /nodejs/cookie]

Techo hub Fuzhou Station dry goods attack | talk with developers about new industrial intelligence technology

浅谈一下跨端技术方案

Necessary interview skills for Android (including interview questions and learning materials)

Daniel guild Games: summary and future outlook of this year

iNFTnews | 元宇宙购物体验将成为吸引消费者的一大利器
随机推荐
Outlier detection and open set identification (2)
B站“崩溃”始末 2021.07.13 我们是这样崩的(转载)
How to explain JS' bind simulation implementation to your girlfriend
浅谈一下跨端技术方案
Isolation level of MySQL, possible problems (dirty reading, unrepeatable reading, phantom reading) and their solutions
自制 | 纯手工自制一个16位RISC架构CPU
Asynchronous mode worker thread
大页内存原理及使用设置
Outlier detection and open set identification (1)
Data warehouse construction - ads floor
[target detection] Introduction to yolor theory + practical test visdrone data set
redis版本怎么查看(查看redis进程)
Implement Lmax disruptor queue from scratch (VI) analysis of the principle of disruptor solving pseudo sharing and consumers' elegant stopping
selenium对接代理与seleniumwire访问开发者工具NetWork
DRF - web development mode, API interface, API interface testing tool, restful specification, serialization and deserialization, DRF installation and use
Anomaly detection and unsupervised learning (1)
Wechat campus bathroom reservation of small program completion work (6) opening defense ppt
Armeabi-v7a architecture (sv7a)
[untitled]
【commons-lang3专题】003- RandomStringUtils 专题