一 支付宝手机网站支付
支付准备工作
1.支付宝正式环境创建应用 登录支付宝开放平台https://open.alipay.com/platform/developerIndex.htm
2.提交审核,然后根据提示到商家中心完成绑定appID,地址为:https://mrchportalweb.alipay.com/accountmanage/bind/appIdBindList
3.添加手机网站支付能力
4.开发设置里选择签名算法:推荐SH256
6.公钥私钥设置:下载支付宝开发工具https://opendocs.alipay.com/open/291/105971/
7.将支付宝应用公钥设置进去获得支付宝公钥
8.详细代码
//支付内容参数
AlipayTradeWapPayModel model = new AlipayTradeWapPayModel();
String outTradeNo = UUIDUtils.getRandomNumber(ORDERNUMBER);
log.info("订单号 : {}", outTradeNo);
model.setOutTradeNo(outTradeNo);//订单号 不可重复
model.setSubject("rent");//商品标题
model.setTotalAmount(aliPayVO.getAmount().toString());//商品金额
model.setBody("pay" + aliPayVO.getAmount() + "yuan");//商品描述
model.setProductCode("QUICK_WAP_WAY");//QUICK_MSECURITY_PAY QUICK_WAP_WAY
model.setTimeoutExpress(EXPIRE);//设置过期时间为30分钟
//构建请求
AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
//request.setReturnUrl(aliPayProperties.getReturnUrl());
request.setNotifyUrl(aliPayProperties.getNotyfyUrl());
request.setBizModel(model);
//发起请求
try {
String form = aliPayApiConfig.getAliPayClient(getPayConfig()).pageExecute(request).getBody();
//创建交易流水
//自己业务逻辑
return AjaxResult.success("请求成功",form);
} catch (AlipayApiException e) {
e.printStackTrace();
}
return AjaxResult.success("请求失败");
}
/**
* 支付宝异步回调
*/
@Override
public String callBlack() {
log.info("支付宝异步回调开始");
HttpServletRequest request = ServletUtils.getRequest();
//商户订单号
String outTradeNo = null;
AmTransactionDetails amTransactionDetails = null;
AmPayTenantConfigVo amTenantConfigVo = null;
try {
outTradeNo = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"), "UTF-8");
amTransactionDetails = amTransactionDetailsMapper.selectByOutTradeNo(outTradeNo);
amTenantConfigVo = payConfigMapper.selectZfPayConfig(amTransactionDetails.getTenantId());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
try {
boolean signVerified =AliVerifySign.checkV1(request,amTenantConfigVo.getPublicKey());
if (signVerified) {//验证成功
log.info("支付订单号:{}",outTradeNo);
//支付宝交易号
String tradeNo = new String(request.getParameter("trade_no").getBytes("ISO-8859-1"), "UTF-8");
//交易状态
String tradeStatus = new String(request.getParameter("trade_status").getBytes("ISO-8859-1"), "UTF-8");
if (tradeStatus.equals("TRADE_SUCCESS")) {
// 接口调用的幂等性:无论接口被调用多少次,最后所影响的结果都是一致的
if (amTransactionDetails.getState() == 1) {
// 更新订单流水状态
// 自己的业务逻辑
return "success";
} else if (tradeStatus.equals("TRADE_FINISHED")) {
//退款逻辑
return "success";
}
} else {//验证失败
log.info("验签失败");
}
} catch (IOException e) {
e.printStackTrace();
}
return "fail";
}
/**
* 验证签名
*/
public class AliVerifySign {
/**
* 校验签名
* @param request
* @return
* @throws UnsupportedEncodingException
* @throws AlipayApiException
*/
public static boolean checkV1(HttpServletRequest request,String publicKey){
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] + ",";
}
//乱码解决,这段代码在出现乱码时使用
try {
valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
params.put(name, valueStr);
}
try {
return AlipaySignature.rsaCheckV1(params, publicKey, "UTF-8","RSA2");
} catch (AlipayApiException e) {
e.printStackTrace();
}
return false;
}
}
9.小程序支付的区别在于byuserId的获取一下代码是获取小程序登录者的userID
/**
* 获取小程序的userid
*/
@Override
public String getUserId(String code,String phone) {
AmLeaseDetailed amLeaseDetailed = amLeaseDetailedMapper.selectLeaseByPhone(phone);
AmPayTenantConfigVo amTenantConfigVo = payConfigMapper.selectByTenandIdAndType(amLeaseDetailed.getTenantId(),2);
AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",amTenantConfigVo.getAppId(),amTenantConfigVo.getPrivateKey(),"json","UTF-8",amTenantConfigVo.getPublicKey(),"RSA2");
AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
request.setGrantType("authorization_code");
request.setCode(code);
request.setRefreshToken("");
AlipaySystemOauthTokenResponse response = null;
try {
response = alipayClient.execute(request);
if (response.isSuccess()) {
return response.getUserId();
}
} catch (AlipayApiException e) {
e.printStackTrace();
}
return null;
}