最近公司有一个需求是接入第三方支付(微信&支付宝),我接到了支付宝支付,所以本博客主要就简述我如何面像文档编程的经过,和中间遇到的小坑小洼,只要一步一步来操作,轻松接入alipay
微信支付的话,等有时间了,我向部门伙伴请教,看看套路是否差不多,到时候再写一篇关于接入微信支付的.
其实个人觉得,了解了一篇的话也一通百通了,毕竟还能差多少呢?
接入明确几点
- 是否满足申请条件(非常最要,貌似个人的话搞不了的,不过可以玩一下沙箱测试)
- 业务场景(非常重要)
- 接入流程(最重要)
- 文档开发(重要)
业务场景
首先蚂蚁金服开放平台先去看看你需要开发的场景是哪一类,然后确认无误之后,进行申请等.
接入流程
很爽的就是,我接手的时候是公司申请好了企业号,然后我根据具体业务需求
- 创建应用 : (我的业务是:手机网站支付),然后创建应用之后需要审核一个工作日,其实大概5-6小时的话就审核下来了,注意的是
- 添加功能 :
创建了应用还不具备开发的条件
你还需要添加应用的功能,这一步必须要注意呀,我一开始以为可以了,结果测试的时候会报一个错(错误代码 insufficient-isv-permissions 错误原因: ISV权限不足)
,就是没有添加应用的功能的错误。需要审核2 - 3小时(审核速度还是比较快的~~~) - 这个时候就可以看一下官方的DEMO or 相关文档了
文档开发
个人的建议,你这个时候可以先看一下相关的官方文档,以下推荐几个网址可以详细阅读
好了,如果你开发中遇到问题,可以优先查这几个文档先,你会有意想不到的收获.
步骤01:下载官方的DEMO
https://docs.open.alipay.com/203/105910/
这里有JAVA PHP.NET等
如何查看?这是一个eclpise的工程,自行想办法[奸笑]
先查看 WebContent > index.html
然后根据button的点击事件查看对应的模块,如:支付、订单查询、订单退款、订单退款查询、账单下载
步骤02:生成RSA2密钥
https://openclub.alipay.com/read.php?tid=1833&fid=46
查看这篇文章,了解一下什么是密钥什么是公钥等,然后按照步骤生成应用的RSA2密钥,私钥将放入代码中,公钥传入应用中
注: 这里有一个坑,密钥长度2048指的是RSA2,我当初不知道,然后找资料,就在这里了解到
步骤03:编写代码
<!--导入阿里支付相关依赖-->
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>3.7.4.ALL</version>
</dependency>
<!--alibaba fastjson-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.54</version>
</dependency>
/**
* @author houyu
* @createTime 2019/3/19 23:39
*/
public class AlipayConfig {
// 商户appid
public static String APPID = "20190319635xxxxx";
// 私钥 pkcs8格式的
public static String RSA_PRIVATE_KEY = "MIIEvgIBADANBxxxxx .... xxxxxxxxx+I";
// 服务器异步通知页面路径 需http://或者https://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问
public static String notify_url = "http://your hostname/alipay/notify";
// 页面跳转同步通知页面路径 需http://或者https://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问 商户可以自定义同步跳转地址
public static String return_url = "http://your hostname/alipay/return";
// 请求网关地址,沙箱是:https://openapi.alipaydev.com/gateway.do
public static String URL = "https://openapi.alipay.com/gateway.do";
// 编码
public static String CHARSET = "UTF-8";
// 返回格式
public static String FORMAT = "json";
// 支付宝公钥(在应用中可以获取)
public static String ALIPAY_PUBLIC_KEY = "MIIBIxxxxxxxx .... xxxxxxxxx";
// RSA2
public static String SIGNTYPE = "RSA2";
}
import java.io.Serializable;
/**
* @author houyu
* @createTime 2019/3/19 23:31
*/
public class AlipayVo implements Serializable {
/**
* 订单名称
*/
private String subject;
/**
* 商户网站唯一订单号
*/
private String out_trade_no;
/**
* 该笔订单允许的最晚付款时间
*/
private String timeout_express;
/**
* 付款金额
*/
private String total_amount;
/**
* 销售产品码,与支付宝签约的产品码名称
*/
private String product_code;
// getter and setter ....
}
import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.juiniot.alipaydemo.config.AlipayConfig;
import java.lang.management.ManagementFactory;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.HashMap;
import java.util.Map;
/**
* @author houyu
* @createTime 2019/3/20 8:38
*/
public class AlipayUtil {
/**
* 处理请求参数
* @param requestParams
* @return
*/
public static Map<String, String> handleParams(Map<String, String[]> requestParams){
Map<String, String> handleMap = new HashMap<>(requestParams.size());
for (Map.Entry<String, String[]> entry : requestParams.entrySet()) {
String key = entry.getKey();
String[] value = entry.getValue();
handleMap.put(key, join(value, ","));
}
return handleMap;
}
/**
* 数组转字符串 ["1", "2"] ==> "1,2"
* @param os
* @param splitString
* @return
*/
public static String join(Object[] os, String splitString){
String s = "";
if (os != null) {
StringBuilder sBuffer = new StringBuilder();
for (int i = 0; i < os.length; i++) {
sBuffer.append(os[i]).append(splitString);
}
s = sBuffer.deleteCharAt(sBuffer.length() - 1).toString();
}
return s;
}
/**
* 校验是否支付成功
* @param handleParams
* @return
*/
public static boolean rsaCheck(Map<String, String> handleParams) {
boolean checkV1 = false;
try {
checkV1 = AlipaySignature.rsaCheckV1(handleParams, AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.CHARSET, AlipayConfig.SIGNTYPE);
} catch (AlipayApiException e) {
e.printStackTrace();
}
return checkV1;
}
/** ---------------------------------------单例模式---------------------------------------*/
private static class SingletonHolder {
private static final AlipayUtil INSTANCE = new AlipayUtil();
}
public static AlipayUtil get() {
return SingletonHolder.INSTANCE;
}
/** ---------------------------------------单例模式---------------------------------------*/
/** 雪花算法生成ID,自带时间排序,一秒可以生成25万个ID左右 */
// 时间起始标记点,作为基准,一般取系统的最近时间(一旦确定不能变动)
private final static long twepoch = 1288834974657L;
// 机器标识位数
private final static long workerIdBits = 5L;
// 数据中心标识位数
private final static long datacenterIdBits = 5L;
// 机器ID最大值
private final static long maxWorkerId = -1L ^ (-1L << workerIdBits);
// 数据中心ID最大值
private final static long maxDatacenterId = -1L ^ (-1L << datacenterIdBits);
// 毫秒内自增位
private final static long sequenceBits = 12L;
// 机器ID偏左移12位
private final static long workerIdShift = sequenceBits;
// 数据中心ID左移17位
private final static long datacenterIdShift = sequenceBits + workerIdBits;
// 时间毫秒左移22位
private final static long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits;
private final static long sequenceMask = -1L ^ (-1L << sequenceBits);
/* 上次生产id时间戳 */
private static long lastTimestamp = -1L;
// 0,并发控制
private long sequence = 0L;
private final long workerId;
// 数据标识id部分
private final long datacenterId;
public AlipayUtil() {
this.datacenterId = getDatacenterId(maxDatacenterId);
this.workerId = getMaxWorkerId(datacenterId, maxWorkerId);
}
/**
* @param workerId 工作机器ID
* @param datacenterId 序列号
*/
public AlipayUtil(long workerId, long datacenterId) {
if (workerId > maxWorkerId || workerId < 0) {
throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId));
}
if (datacenterId > maxDatacenterId || datacenterId < 0) {
throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId));
}
this.workerId = workerId;
this.datacenterId = datacenterId;
}
/**
* 获取下一个ID
*
* @return
*/
public synchronized long nextId() {
long timestamp = timeGen();
if (timestamp < lastTimestamp) {
throw new RuntimeException(String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp));
}
if (lastTimestamp == timestamp) {
// 当前毫秒内,则+1
sequence = (sequence + 1) & sequenceMask;
if (sequence == 0) {
// 当前毫秒内计数满了,则等待下一秒
timestamp = tilNextMillis(lastTimestamp);
}
} else {
sequence = 0L;
}
lastTimestamp = timestamp;
// ID偏移组合生成最终的ID,并返回ID
long nextId = ((timestamp - twepoch) << timestampLeftShift)
| (datacenterId << datacenterIdShift)
| (workerId << workerIdShift) | sequence;
return nextId;
}
private long tilNextMillis(final long lastTimestamp) {
long timestamp = this.timeGen();
while (timestamp <= lastTimestamp) {
timestamp = this.timeGen();
}
return timestamp;
}
private long timeGen() {
return System.currentTimeMillis();
}
/**
* <p>
* 获取 maxWorkerId
* </p>
*/
protected static long getMaxWorkerId(long datacenterId, long maxWorkerId) {
StringBuffer mpid = new StringBuffer();
mpid.append(datacenterId);
String name = ManagementFactory.getRuntimeMXBean().getName();
if (!name.isEmpty()) {
/*
* GET jvmPid
*/
mpid.append(name.split("@")[0]);
}
/*
* MAC + PID 的 hashcode 获取16个低位
*/
return (mpid.toString().hashCode() & 0xffff) % (maxWorkerId + 1);
}
/**
* <p>
* 数据标识id部分
* </p>
*/
protected static long getDatacenterId(long maxDatacenterId) {
long id = 0L;
try {
InetAddress ip = InetAddress.getLocalHost();
NetworkInterface network = NetworkInterface.getByInetAddress(ip);
if (network == null) {
id = 1L;
} else {
byte[] mac = network.getHardwareAddress();
id = ((0x000000FF & (long) mac[mac.length - 1])
| (0x0000FF00 & (((long) mac[mac.length - 2]) << 8))) >> 6;
id = id % (maxDatacenterId + 1);
}
} catch (Exception e) {
System.out.println(" getDatacenterId: " + e.getMessage());
}
return id;
}
/** 雪花算法生成ID,自带时间排序,一秒可以生成25万个ID左右 */
}
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.juiniot.alipaydemo.config.AlipayConfig;
import com.juiniot.alipaydemo.util.AlipayUtil;
import com.juiniot.alipaydemo.vo.AlipayVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
/**
* @author houyu
* @createTime 2019/3/19 23:29
*/
@SuppressWarnings("Duplicates")
@Controller
@RequestMapping("/alipay")
public class AlipayController {
/**
* 测试代码是否跑通
* @return
*/
@RequestMapping("/hello")
@ResponseBody
public String hello() {
return "hello";
}
/**
* 支付宝完成回调页面(不可信回调)
*/
@GetMapping("/return")
@ResponseBody
private String alipayReturn(HttpServletRequest request) {
Map<String, String[]> parameterMap = request.getParameterMap();
Map<String, String> handleParams = AlipayUtil.handleParams(parameterMap);
// 这里的校验没有多大的意思,不可信,直接获取out_trade_no跳转到对应的payed controller也可
boolean rsaCheck = AlipayUtil.rsaCheck(handleParams);
if (rsaCheck){
System.out.println("验证通过");
}else {
System.out.println("验证失败");
}
// 获取订单号
String out_trade_no = handleParams.get("out_trade_no");
System.out.println("out_trade_no:" + out_trade_no);
// 这里一般都是 重定向 payed的controller, 然后携带对应的信息如:return "redirect:/alipay/success?out_trade_no=" + out_trade_no;
// payed的controller根据out_trade_no获取支付结果,并且给出页面提示
return "支付完成";
}
/**
* 支付宝完成结果异步的回调(可信回调)
* @param request
*/
@PostMapping("/notify")
@ResponseBody
private String alipayNotify(HttpServletRequest request) {
Map<String, String[]> parameterMap = request.getParameterMap();
Map<String, String> handleParams = AlipayUtil.handleParams(parameterMap);
boolean rsaCheck = AlipayUtil.rsaCheck(handleParams);
if (rsaCheck){
System.out.println("验证通过");
// 处理业务逻辑,更改支付状态等骚操作
// ...
}else {
System.out.println("验证失败");
}
return rsaCheck ? "success" : "failure";
}
@RequestMapping("/pay")
@ResponseBody
public String pay() {
AlipayVo alipayVo = new AlipayVo();
// String out_trade_no = UUID.randomUUID().toString().replace("-", "");
String out_trade_no = AlipayUtil.get().nextId() + "";
System.out.println("out_trade_no:" + out_trade_no);
// 设置订单单号,需要保证唯一性
alipayVo.setOut_trade_no(out_trade_no);
// 设置支付金额
alipayVo.setTotal_amount("0.01");
// 设置支付标题
alipayVo.setSubject("houyu-test-title");
// 设置订单有效时长(30分钟)
alipayVo.setTimeout_express("30m");
// 商品码(必须是QUICK_WAP_WAY),可以看文档 see: https://docs.open.alipay.com/203/107090/
alipayVo.setProduct_code("QUICK_WAP_WAY");
// 对象转为json字符串
String json = JSONObject.toJSONString(alipayVo);
// 建立连接
AlipayClient client = new DefaultAlipayClient(AlipayConfig.URL, AlipayConfig.APPID, AlipayConfig.RSA_PRIVATE_KEY, AlipayConfig.FORMAT, AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.SIGNTYPE);
// 创建请求
AlipayTradeWapPayRequest alipayTradeWapPayRequest = new AlipayTradeWapPayRequest();
// 设置异步通知地址
alipayTradeWapPayRequest.setNotifyUrl(AlipayConfig.notify_url);
// 设置对调地址,就是说支付成功之后回调你的页面,你可以继续进行你的业务操作,但是这个是不可信任的,需要根据notify_url这边的回调确定支付是否成功
alipayTradeWapPayRequest.setReturnUrl(AlipayConfig.return_url);
// 封装请求支付信息
alipayTradeWapPayRequest.setBizContent(json);
String pageString;
try {
pageString = client.pageExecute(alipayTradeWapPayRequest).getBody();
} catch (AlipayApiException e) {
pageString = "request aliapy has error";
e.printStackTrace();
}
return pageString;
}
}
以上代码应该算是清晰明了的了,希望对你有帮助…
建议:
对于这种开发形文档,随时可能更新,如果接入失败了,优先查看官网的文档解决,有可能接口更新了
交流:
如果接入过程有问题,可以一起交流!!!
for.houyu@qq.com
272694308@qq.com