Java 调用支付宝接口
1.注册开发者中心应用 登录点击沙箱服务
https://open.alipay.com/platform/developerIndex.htm
2.创建沙箱应用获取APPID和支付宝网关地址(创建设置密钥这里就不说明了),将公共密钥和私密保存到本地。
3.创建项目结构,设置配置文件
1.下载支付宝SDK文件jar包,讲jar包导入到项目内
2.编写配置信息和Java文件
/********************************************************************************************************************************************************************************************/
* 应用启动加载文件*/
@Component public class AlipayConfig 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:/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("配置文件加载失败"); } } }
/********************************************************************************************************************************************************************************************/
4.创建支付工具类
public class PayUtil { /** * @param outTradeNo 商户订单号,商户网站订单系统中唯一订单号,必填 对应缴费记录的orderNo * @param totalAmount 付款金额,必填 * @param subject 主题 * @param body 商品描述,可空 * @return */ public static String alipay(AlipayBean alipayBean) { //1、获得初始化的AlipayClient AlipayClient alipayClient = new DefaultAlipayClient( AlipayConfig.getKey("gatewayUrl"),//支付宝网关 AlipayConfig.getKey("app_id"),//appid AlipayConfig.getKey("merchant_private_key"),//商户私钥 "json", AlipayConfig.getKey("charset"),//字符编码格式 AlipayConfig.getKey("alipay_public_key"),//支付宝公钥 AlipayConfig.getKey("sign_type")//签名方式 ); //设置请求参数 AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest(); alipayRequest.setReturnUrl(AlipayConfig.getKey("return_url")); alipayRequest.setNotifyUrl(AlipayConfig.getKey("notify_url")); try { alipayRequest.setBizContent(JSON.toJSONString(alipayBean)); //请求 String result = alipayClient.pageExecute(alipayRequest).getBody(); System.out.println("*********************\n返回结果为:" + result); return result; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } } /** * 支付宝退款接口 * * @param outTradeNo * @param tradeNo * @param refundAmount * @param refundReason * @param out_request_no 标识一次退款请求,同一笔交易多次退款需要保证唯一,如需部分退款,则此参数必传 * @return */ public static String aliRefund(AlipayBean alipayBean) { //1、获得初始化的AlipayClient AlipayClient alipayClient = new DefaultAlipayClient( AlipayConfig.getKey("gatewayUrl"),//支付宝网关 AlipayConfig.getKey("app_id"),//appid AlipayConfig.getKey("merchant_private_key"),//商户私钥 "json", AlipayConfig.getKey("charset"),//字符编码格式 AlipayConfig.getKey("alipay_public_key"),//支付宝公钥 AlipayConfig.getKey("sign_type")//签名方式 ); //设置请求参数 AlipayTradeRefundRequest alipayRequest = new AlipayTradeRefundRequest(); alipayRequest.setReturnUrl(AlipayConfig.getKey("return_url")); alipayRequest.setNotifyUrl(AlipayConfig.getKey("notify_url")); try { alipayRequest.setBizContent(JSON.toJSONString(alipayBean)); //请求 String result= alipayClient.execute(alipayRequest).getBody(); System.out.println("*********************\n返回结果为:" + result); return result; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } } /** * 支付宝的验签方法 * * @param req * @return */ public static boolean checkSign(HttpServletRequest req) { Map<String, String[]> requestMap = req.getParameterMap(); Map<String, String> paramsMap = new HashMap<>(); requestMap.forEach((key, values) -> { String strs = ""; for (String value : values) { strs = strs + value; } System.out.println(("key值为" + key + "value为:" + strs)); paramsMap.put(key, strs); }); //调用SDK验证签名 try { return AlipaySignature.rsaCheckV1(paramsMap, AlipayConfig.getKey("alipay_public_key"), AlipayConfig.getKey("charset"), AlipayConfig.getKey("sign_type")); } catch (AlipayApiException e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("*********************验签失败********************"); return false; } }
5.创建Controller,dao和service
6.创建测试html,运行项目访问测试。
over......
本文来自博客园,作者:Forever丶随风,转载请注明原文链接:https://www.cnblogs.com/Forever-wind/p/13696476.html