赞
踩
蚂蚁金服开放平台基于支付宝的海量用户,将强大的支付、营销、数据能力,通过接口等形式开放给第三方合作伙伴,帮助第三方合作伙伴创建更具竞争力的应用。
通过接入蚂蚁金服开放平台,第三方合作伙伴可以获得更多的流量、用户和收益,而第三方合作伙伴提供的服务也让用户获得了更丰富的体验,平台生态更加繁荣,最终实现多方共赢
开发文档 和介绍网站:https://docs.open.alipay.com
1注册一个沙箱号 网站https://open.alipay.com/platform/home.htm
使用真的支付宝扫描 进去 注册 沙箱支付宝
2)进去注册信息
3)注册成功就进去点击开发中心
4)接下来下载密匙生成工具
下载完毕 解压
更新秘钥工具2021-01-19
我以上传到百度网盘 下载安装即可使用
链接:https://pan.baidu.com/s/1uAujabX2F8ObgUoWDAsU2g
提取码:ephw
5)进入沙箱 把生成的公钥放进就去 就会自动生成支付宝公钥了
6)接下来创建Java项目,在我们的pom文件中添加支付宝提供的支付相关坐标
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>3.3.49.ALL</version>
</dependency>
结构很简单 一个controller 就可以
可以参考下图
SpringMVC
@Controller public class PayController { private final String APP_ID = "应用的APPID"; private final String APP_PRIVATE_KEY = "生成的应用私钥"; private final String CHARSET = "UTF-8"; private final String ALIPAY_PUBLIC_KEY = "支付宝公钥"; //这是沙箱接口路径,正式路径为https://openapi.alipay.com/gateway.do private final String GATEWAY_URL ="https://openapi.alipaydev.com/gateway.do"; private final String FORMAT = "JSON"; //签名方式 private final String SIGN_TYPE = "RSA2"; //支付宝异步通知路径,付款完毕后会异步调用本项目的方法,必须为公网地址 private final String NOTIFY_URL = "http://公网地址/notifyUrl"; //支付宝同步通知路径,也就是当付款完毕后跳转本项目的页面,可以不是公网地址 private final String RETURN_URL = "http://公网地址/returnUrl"; }
@RequestMapping("alipay") public void alipay(HttpServletResponse httpResponse) throws IOException { Random r=new Random(); //实例化客户端,填入所需参数 AlipayClient alipayClient = new DefaultAlipayClient(GATEWAY_URL, APP_ID, APP_PRIVATE_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY, SIGN_TYPE); AlipayTradePagePayRequest request = new AlipayTradePagePayRequest(); //在公共参数中设置回跳和通知地址 request.setReturnUrl(RETURN_URL); request.setNotifyUrl(NOTIFY_URL); //商户订单号,商户网站订单系统中唯一订单号,必填 //生成随机Id String out_trade_no = UUID.randomUUID().toString(); //付款金额,必填 String total_amount =Integer.toString(r.nextInt(9999999)+1000000); //订单名称,必填 String subject ="奥迪A8 2016款 A8L 60 TFSl quattro豪华型"; //商品描述,可空 String body = "尊敬的会员欢迎购买奥迪A8 2016款 A8L 60 TFSl quattro豪华型"; request.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\"," + "\"total_amount\":\""+ total_amount +"\"," + "\"subject\":\""+ subject +"\"," + "\"body\":\""+ body +"\"," + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}"); String form = ""; try { form = alipayClient.pageExecute(request).getBody(); // 调用SDK生成表单 } catch (AlipayApiException e) { e.printStackTrace(); } httpResponse.setContentType("text/html;charset=" + CHARSET); httpResponse.getWriter().write(form);// 直接将完整的表单html输出到页面 httpResponse.getWriter().flush(); httpResponse.getWriter().close(); } }
更新2021-01-19
Stringboot 基本一个套路就是注解
@RestController
package com.tang.cloud.controller; import com.alipay.api.AlipayApiException; import com.alipay.api.AlipayClient; import com.alipay.api.DefaultAlipayClient; import com.alipay.api.request.AlipayTradePagePayRequest; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.security.SecureRandom; import java.util.UUID; @RestController public class PayController { private final String APP_ID = "2016092500596842"; private final String APP_PRIVATE_KEY = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQClKNF8psCdkuNUoN/GhMXOpCeOcvhnNJ9JV2F1iXza88ebfHmtbEY8FyzereWuddV5Q9WFREyFyISDzX63i5HIW42Bu9e9n3I6U/E2UYaRw5MHT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L6lYYrxFLCPbDVIrjHzDPqlZFZjB08mLXbiswoQKBgQCAtWoMMYEWMMzr7e1arWapyCIpkUzzvNXABCyPBwodNpfZ9zpg0UHP+tuhfrzXUbncQ/OXhpWyUN17+MssGNClKEOvHL4NVuIx4b8tEE556Ku0JA4F7EZjKT3lTDWF3HBXxGgSPTJRmaf9MDdqTDm4E35E2aXJTB1UI3B3xHzZAQKBgQCfBf7/oX4gwFUAkv7mvMIkTPok+E3nWYRS2C9YWiDwRuu+Pv8HYpCiofQ0yDKKuvZyjqjSdezx29wOwzokXVYRei7I56CjUr/mgZMFJgDXrZMP6nQkYvlxj7GTHxE/7JhT1QTvd7GM9cZM2g8ocpLEWmHeMTkJimmE+llrj5o5zQ=="; private final String CHARSET = "UTF-8"; private final String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA3TWqoUi8tjCgaR5M5sx0mXrDKEjz/QJZ3QqnVpQ7MEASiWJ49T0kou4LGF6PjeqAKI9t7aKHF2wZMuIVq69qYFgpE4219H3e0sGQzXalx9SfvMeazItTJXh7obcETUwQ0F8U4DAFVYEooFjD1VnNiDtsKrJwHzqE7bz7NFR3k/Zz8ksT3boCzOoUANzfQohn5WyclgoLKBwkZRQzzEuHYStkyKJbryYptXwGo9iSJ5JcIqrlk87HJPcmbRPKJrxFg4rE3bZbJ+AYejcLYPmyz5/owWEp1xpc7HvfklU2EzfJhJxNkNSoSKRZcBRTXZQsA7hTi7YmjZeNslk62OeA5wIDAQAB"; //这是沙箱接口路径,正式路径为https://openapi.alipay.com/gateway.do private final String GATEWAY_URL ="https://openapi.alipaydev.com/gateway.do"; private final String FORMAT = "JSON"; //签名方式 private final String SIGN_TYPE = "RSA2"; //支付宝异步通知路径,付款完毕后会异步调用本项目的方法,必须为公网地址 private final String NOTIFY_URL = "http://127.0.0.1/notifyUrl"; //支付宝同步通知路径,也就是当付款完毕后跳转本项目的页面,可以不是公网地址 private final String RETURN_URL = "http://127.0.0.1/returnUrl"; @RequestMapping("aliapy") public void alipay(HttpServletResponse httpResponse) throws IOException { SecureRandom r= new SecureRandom(); //实例化客户端,填入所需参数 AlipayClient alipayClient = new DefaultAlipayClient(GATEWAY_URL, APP_ID, APP_PRIVATE_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY, SIGN_TYPE); AlipayTradePagePayRequest request = new AlipayTradePagePayRequest(); //在公共参数中设置回跳和通知地址 request.setReturnUrl(RETURN_URL); request.setNotifyUrl(NOTIFY_URL); //商户订单号,商户网站订单系统中唯一订单号,必填 //生成随机Id String out_trade_no = UUID.randomUUID().toString(); //付款金额,必填 String total_amount =Integer.toString(r.nextInt(9999999)+1000000); //订单名称,必填 String subject ="奥迪A8 2021款 A8L 60 TFSl quattro豪华型"; //商品描述,可空 String body = "尊敬的会员欢迎购买奥迪A8 2021款 A8L 60 TFSl quattro豪华型"; request.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\"," + "\"total_amount\":\""+ total_amount +"\"," + "\"subject\":\""+ subject +"\"," + "\"body\":\""+ body +"\"," + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}"); String form = ""; try { form = alipayClient.pageExecute(request).getBody(); // 调用SDK生成表单 } catch (AlipayApiException e) { e.printStackTrace(); } httpResponse.setContentType("text/html;charset=" + CHARSET); httpResponse.getWriter().write(form);// 直接将完整的表单html输出到页面 httpResponse.getWriter().flush(); httpResponse.getWriter().close(); } }
在付款成功后,浏览器会跳转到我们定义的同步回调页面,来验证是否付款成功等操作
@RequestMapping(value = "/returnUrl", method = RequestMethod.GET) public String returnUrl(HttpServletRequest request, HttpServletResponse response) throws IOException, AlipayApiException { System.out.println("=================================同步回调====================================="); // 获取支付宝GET过来反馈信息 Map<String, String> params = new HashMap<String, String>(); Map<String, String[]> requestParams = request.getParameterMap(); for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext();) { String name = (String) iter.next(); String[] values = (String[]) requestParams.get(name); String valueStr = ""; for (int i = 0; i < values.length; i++) { valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ","; } // 乱码解决,这段代码在出现乱码时使用 valueStr = new String(valueStr.getBytes("utf-8"), "utf-8"); params.put(name, valueStr); } System.out.println(params);//查看参数都有哪些 boolean signVerified = AlipaySignature.rsaCheckV1(params, ALIPAY_PUBLIC_KEY, CHARSET, SIGN_TYPE); // 调用SDK验证签名 //验证签名通过 if(signVerified){ // 商户订单号 String out_trade_no = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"), "UTF-8"); // 支付宝交易号 String trade_no = new String(request.getParameter("trade_no").getBytes("ISO-8859-1"), "UTF-8"); // 付款金额 String total_amount = new String(request.getParameter("total_amount").getBytes("ISO-8859-1"), "UTF-8"); System.out.println("商户订单号="+out_trade_no); System.out.println("支付宝交易号="+trade_no); System.out.println("付款金额="+total_amount); //支付成功,修复支付状态 payService.updateById(Integer.valueOf(out_trade_no)); return "ok";//跳转付款成功页面 }else{ return "no";//跳转付款失败页面 } }
沙箱有商家和买家 你一看就懂!!!!
又不要真给我付点
自己看着办吧
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。