支付宝支付流程及注意事项(沙箱测试版) ---demo已做升级
项目demo下载-CSDN(新-推荐):https://download.csdn.net/download/qq_39938758/11751843
项目demo下载-CSDN(旧):https://download.csdn.net/download/qq_39938758/10581629
一、准备工作
1、springbootdemo
-
2、idea开发工具(有的跳过)
- idea下载链接
-
3、获得沙箱帐号
-
注册蚂蚁金服开发者账号(免费,不像苹果会收取费用)
注册地址:https://open.alipay.com,用你的支付宝账号扫码登录,完善个人信息,选择服务类型(我选的是自研)。
- 进入沙箱,第一次需要填写信息!(我已经有了所以就不需要了)
- 记得把下面生成的商户公钥保存到沙箱上
-
4、生成2048位商户公私钥(私钥保存与支付宝)
-
下载生成工具
- 生成公私钥,并且保存
- 保存公私钥
- 把支付宝公钥 保存到沙箱环境。
- 5、pom.xml文件依赖alipay得jar包
-
<dependency> <groupId>com.alipay.sdk</groupId> <artifactId>alipay-sdk-java</artifactId> <version>3.0.0</version> </dependency> <!-- 因为sdk有用这个包,所以需要有这个的哦。不过一般springboot项目已经自己引入了,就不用引入哈 --> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.1</version> </dependency> <!-- 这个包是用户参数转成JSON用的 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.33</version> </dependency> <!-- 简化代码插件的包,记得要在idea安装 lombok的插件哦 --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency>
二、支付宝支付代码流程
Demo项目结构: 请下载lombok插件
- 第一步: 配置alipay.properties
-
# 应用ID,您的APPID,收款账号既是您的APPID对应支付宝账号 app_id = 2016091600527203 # 商户私钥,您的PKCS8格式RSA2私钥 merchant_private_key = MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCAWY4SMml0Y9cWjf/19/zTFu9YFsmsV/QiD/9yk37MNrYxElo6hcdWbxBMX984kZVMs9WR0y3LcoENo43dOch+5UWsmHdMdX/DLJ4p4qx7JuYKUIcYT89dzAJe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lQOlE+DhckzxM+eS9T1mMlKhqoINT5zMI92+IuuRAoGAcTPc0/8SBcfjGQOdLuVSSmpwvIYoGRByBi/QayYOBDLYt08EaWCZYOccg80j2eyNasi6Gy7QXZ8tp3rizqyHfYMkbTxGaqrCbOkoKbE6f5eEq4rRGQh/pEnpDvP7DYckcULecJCt76ReZWxPcqyHphwdjI+P7L7bHFRP93iMQYECgYAk/zyG+5qjEcAnyIptvrgxSklBz/2P1AEex4ZcJpkc3EgYB5sSqiSt4tpAb3s+XfQkP/+5djgGdFSIn4QmczSSf/35cck8G+bISOPoZ08wGRv8LzhXXRVpIooAtEYnJk+Xp3E1RcFmuq2IZCRX7IU3AMFRg941vW+4EAdKmk6oPQ== # 支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。 alipay_public_key = MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAgFmOEjJpdGPXFo3/9ff80xbvWBbJrFf0Ig//cpN+zDa2MRJaOoXHVm8QTF/fOJGVTLPVkdMty3KBDaON3TnIfuVFrJh3THV/wyyeKeKseybmClCHGE/PXcwCXuntCzf2+0p4OzKDfUA/4rsxk3x3oWdp1PVagROzltOZu+DH0/axGIy7iUHOyBnsxVd7gWwFI7R1ISM6uIkObITH/CXsG1lwIeORYR2nQ+slRcTw5pMkCUkZXxN/Y1zfiOESC3oDHiSTbuTM3eRpkxrpJLCzqAgVaFqWuQoKD8pgR6LEKVoKY8OSLQieKNLbcZyltdJFkMXBPuRO58EzKV3ct/73kQIDAQAB # 服务器异步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数 notify_url = http://外网ip:端口/error.html # 页面跳转同步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数 return_url = http://外网ip:端口/sccess.html # 签名方式 sign_type = RSA2 # 字符编码格式 charset = utf-8 # 支付宝网关 gatewayUrl = https://openapi.alipaydev.com/gateway.do # 支付宝网关 log_path = "C:\\"
第二步: 编写测试页面 index.html
-
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <H1>支付宝demo测试</H1> <hr> <form action="order/alipay" method="post"> *商户订单 :<br> <input type="text" name="out_trade_no"><br> *订单名称 :<br> <input type="text" name="subject"><br> *付款金额 :<br> <input type="text" name="total_amount"><br> 商品描述 :<br> <input type="text" name="body"><br> <input type="submit" value="支付宝支付"> </form> </body> </html>
第三步: 编写参数bean
-
/*支付实体对象*/ @Data @Accessors(chain = true) public class AlipayBean { /*商户订单号,必填*/ private String out_trade_no; /*订单名称,必填*/ private String subject; /*付款金额,必填*/ private StringBuffer total_amount; /*商品描述,可空*/ private String body; /*超时时间参数*/ private String timeout_express="10m"; private String product_code="FAST_INSTANT_TRADE_PAY"; }
第四步: 编写应用加载完成加载相关配置参数
-
/* 应用启动加载文件*/ @Component public class PropertiesConfig implements ApplicationListener { //保存加载配置参数 private static Map<String, String> aliPropertiesMap = new HashMap<String, String>(); /*获取配置参数值*/ public static String getKey(String key) { return aliPropertiesMap.get(key); } /*监听启动完成,执行配置加载到aliPropertiesMap*/ public void onApplicationEvent(ApplicationEvent event) { if (event instanceof ApplicationReadyEvent) { this.init(aliPropertiesMap);//应用启动加载 } } /*初始化加载aliPropertiesMap*/ public void init(Map<String, String> map) { // 获得PathMatchingResourcePatternResolver对象 PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); try { //加载resource文件(也可以加载resources) Resource resources = resolver.getResource("classpath:config/alipay.properties"); PropertiesFactoryBean config = new PropertiesFactoryBean(); config.setLocation(resources); config.afterPropertiesSet(); Properties prop = config.getObject(); //循环遍历所有得键值对并且存入集合 for (String key : prop.stringPropertyNames()) { map.put(key, (String) prop.get(key)); } } catch (Exception e) { new Exception("配置文件加载失败"); } } }
第五步: 编写支付宝util
-
/* 支付宝 */ public class AlipayUtil { public static String connect(AlipayBean alipayBean) throws AlipayApiException { //1、获得初始化的AlipayClient AlipayClient alipayClient = new DefaultAlipayClient( PropertiesConfig.getKey("gatewayUrl"),//支付宝网关 PropertiesConfig.getKey("app_id"),//appid PropertiesConfig.getKey("merchant_private_key"),//商户私钥 "json", PropertiesConfig.getKey("charset"),//字符编码格式 PropertiesConfig.getKey("alipay_public_key"),//支付宝公钥 PropertiesConfig.getKey("sign_type")//签名方式 ); //2、设置请求参数 AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest(); //页面跳转同步通知页面路径 alipayRequest.setReturnUrl(PropertiesConfig.getKey("return_url")); // 服务器异步通知页面路径 alipayRequest.setNotifyUrl(PropertiesConfig.getKey("notify_url")); //封装参数 alipayRequest.setBizContent(JSON.toJSONString(alipayBean)); //3、请求支付宝进行付款,并获取支付结果 String result = alipayClient.pageExecute(alipayRequest).getBody(); //返回付款信息 return result; } }
第六步: 编写业务层
-
/*支付服务*/ public interface PayService { /*支付宝*/ String aliPay(AlipayBean alipayBean) throws AlipayApiException; }
/* 支付服务 */ @Service(value = "alipayOrderService") public class PayServiceImpl implements PayService { @Override public String aliPay(AlipayBean alipayBean) throws AlipayApiException { return AlipayUtil.connect(alipayBean); } }
第七步:编写web接口测试
-
/* 订单接口 */ @RestController() @RequestMapping("order") public class OrderController { @Resource private PayService payService;//调用支付服务 /*阿里支付*/ @PostMapping(value = "alipay") public String alipay(String out_trade_no,String subject,String total_amount,String body) throws AlipayApiException { return payService.aliPay(new AlipayBean() .setBody(body) .setOut_trade_no(out_trade_no) .setTotal_amount(new StringBuffer().append(total_amount)) .setSubject(subject)); } }
备注1:相关注意事项
1、注意下载lombok插件和依赖lombokjar
2、其中service可以不要的,直接接口调用util文件也可以,当然你也可以直接在接口写连接加载都可以
备注2:支付宝相关资料链接
备注3:个人说明
- * 沙箱版和正式流程无误,只是参数变化 和需要把应用增加到支付宝里面
-
-------------------------------------------------------------------------------------------------------------------------------------------------欢迎光临,期待您的下次光临!