支付宝支付目录
随着科技的进步,人们对于移动支付越来越依赖,今天我就来介绍一下当下主流的支付宝支付吧
前言
支付宝支付在很多平台都是支持的,在国内有不小的影响力
一、选取合适的支付场景
首先去官网看看
https://opendocs.alipay.com/open/270/105902
这里我们选择当面支付
二、下载sdk
三、使用步骤
1.引入库
引入依赖<!-- https://mvnrepository.com/artifact/com.alipay.sdk/alipay-easysdk -->
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-easysdk</artifactId>
<version>2.1.2</version>
</dependency>
server层:
public class AlipayServiceImpl implements AlipayService {
Logger logger = LoggerFactory.getLogger("alipay");
@Override
public void alipay(HttpServletResponse res, WeChatPayBo weChatPayBo){
// 1. 设置参数(全局只需设置一次)
Factory.setOptions(getOptions());
try {
double price = weChatPayBo.getTotalPrice()/100.0;
// 2. 发起API调用(以创建当面付收款二维码为例)
AlipayTradePrecreateResponse response = Factory.Payment.FaceToFace().preCreate(weChatPayBo.getProductNames(), weChatPayBo.getOrderId(), price+"");
// 3. 处理响应或异常
if (ResponseChecker.success(response)) {
System.out.println("调用成功");
String codeUrl = response.getQrCode();
int width = 300;
int height = 300;
//二维码的图片格式
String format = "JPEG";
Hashtable hints = new Hashtable();
//内容所使用编码
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
BitMatrix bitMatrix = new MultiFormatWriter().encode(codeUrl,
BarcodeFormat.QR_CODE, width, height, hints);
// response.setContentType("image/JPEG");
MatrixToImageWriter.writeToStream(bitMatrix, format, res.getOutputStream());
logger.info(response.getQrCode());
} else {
System.err.println("调用失败,原因:" + response.msg + "," + response.subMsg);
}
} catch (Exception e) {
System.err.println("调用遭遇异常,原因:" + e.getMessage());
throw new RuntimeException(e.getMessage(), e);
}
}
配置(在下面这个方法中有很多参数,这里我就不一一说明了,官方文档有)里面的参数去支付宝开放平台拿
2.异步通知
代码如下controller层(示例):
@RequestMapping(value = "alinotify")
public String aliNotify(HttpServletRequest request) throws Exception {
try {
log.info("进入支付宝回调地址");
Map<String, String> params = new HashMap<>();
Map<String, String[]> requestParams = request.getParameterMap();
log.info("支付宝验签参数:{}", JSON.toJSONString(requestParams));
for (String name : requestParams.keySet()) {
String[] values = requestParams.get(name);
String valueStr = "";
for (int i = 0; i < values.length; i++) {
valueStr = (i == values.length - 1) ? valueStr + values[i]
: valueStr + values[i] + ",";
}
params.put(name, valueStr);
}
boolean flag = AlipaySignature.rsaCheckV1(params, alipayPublicKey, "UTF-8", "RSA2");
if (flag) {
alipayService.aliNotify(params);
log.info("支付宝通知更改状态成功!");
return "success";
}
} catch (Throwable e) {
log.error("exception: ", e);
}
return "failure";
}
代码如下server层(示例):
public void aliNotify(Map<String, String> param) throws Exception {
log.info("支付宝异步回调接口数据处理");
//只有支付成功后,支付宝才会回调应用接口,可直接获取支付宝响应的参数
String order_id = param.get(AlipayConsts.AliOutTradeNo);
//出于安全考虑,通过支付宝回传的订单号查询支付宝交易信息
AlipayTradeQueryResponse aliResp = queryOrder(order_id);
if (!AlipayConsts.SuccessCode.equals(aliResp.getCode())) {
//返回值非10000
throw new ApiException(RetEnum.MachineOrderAlipayException, aliResp.getSubMsg());
}
if (!AlipayConsts.AliTradeSuccess.equals(aliResp.getTradeStatus()) && !AlipayConsts.AliTradeFinished.equals(aliResp.getTradeStatus())) {
//支付宝订单状态不是支付成功
throw new ApiException(RetEnum.MachineOrderAliUnPay);
}
//可对支付宝响应参数AlipayTradeQueryResponse进行处理
}
异步通知验签
Map<String, String> paramsMap = ... //将异步通知中收到的所有参数都存放到map中
boolean signVerified = AlipaySignature.rsaCheckV1(paramsMap, ALIPAY_PUBLIC_KEY, CHARSET, SIGN_TYPE) //调用SDK验证签名
if(signVerfied){
// TODO 验签成功后,按照支付结果异步通知中的描述,对支付结果中的业务内容进行二次校验,校验成功后在response中返回success并继续商户自身业务处理,校验失败返回failure
}else{
// TODO 验签失败则记录异常日志,并在response中返回failure.
}
总结
支付宝的简单总结到此结束。