java实现H5请求支付宝支付(spring boot)
这次给的任务是看一下支付宝的app支付这块。于是通过网上进行查找相关的资料,进行相关的开发,并记录下来。通过支付宝的官网进行查看:https://docs.open.alipay.com/2031.引入支付宝的SDK的依赖;<dependency><groupId>com.alipay.sdk</groupId>...
这次给的任务是看一下支付宝的app支付这块。于是通过网上进行查找相关的资料,进行相关的开发,并记录下来。通过支付宝的官网进行查看:https://docs.open.alipay.com/203
1.引入支付宝的SDK的依赖;
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>3.7.89.ALL</version>
</dependency>
2.H5(app)请求支付宝支付的必要参数:
1)appid:
2)支付网关
a.沙箱测试的话,支付宝网关为:
https://openapi.alipaydev.com/gateway.do
b.上线正式环境的话,支付宝的网关为:
https://openapi.alipay.com/gateway.do
3)商户私钥
3.1)windows环境下载秘钥的生成工具的地址:https://docs.open.alipay.com/291/105971/
3.2)用如下图选择方式生成秘钥:
3.3)通过工具生成密钥后,这里的秘钥即是商户私钥;
3.4)将应用私钥复制到代码中:
3.5)将里面的公钥复制到网页中,https://openhome.alipay.com/platform/appDaily.htm?tab=info
如下图所示:
3.6)接着查看支付宝公钥并复制,这里就是支付宝的公钥。
3.7)复制到代码中,如图所示:
4.代码模块,首先是配置参数的模块:
public class AliPayConfig {
//支付宝移动端appid
public static final String ALIPAY_APP_APPID = "";
//支付宝商户私钥
public static final String ALIPAY_PRIVATE_KEY = "";
//支付宝公钥
public static final String ALIPAY_PUBLIC_KEY = "";
//支付宝网关
public static final String ALIPAY_GATEWAY_URL = "https://openapi.alipay.com/gateway.do";
// 验证方式
public static final String SIGN_TYPE = "RSA2";
// 返回路径
public static final String ALIPAY_NOTIFYURL = "";
// 传输方式
public static final String FORMAT = "json";
// 字符串格式
public static final String CHARSET = "UTF-8";
private static AlipayClient alipayClient = null;
public static AlipayClient getAlipayClient() throws AlipayApiException {
//实例化客户端
if (alipayClient == null) {
synchronized (AliPayUtil.class) {
if (null == alipayClient) {
alipayClient = new DefaultAlipayClient(ALIPAY_GATEWAY_URL, ALIPAY_APP_APPID, ALIPAY_PRIVATE_KEY,
FORMAT, CHARSET, ALIPAY_PUBLIC_KEY, SIGN_TYPE);
}
}
}
return alipayClient;
}
}
5.工具类,里面两个方法,一个支付,一个回掉方法。
public class H5PayUtil {
@Value("${callback.url}")
private String callbackUrl;
private static ObjectMapper mapper = new ObjectMapper();
/**
* H5请求支付宝支付
* @param h5PayDto
* @return
*/
public AlipayTradeWapPayResponse aliPayH5(H5PayDto h5PayDto) {
try {
AlipayClient alipayClient = AliPayConfig.getAlipayClient();
AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
request.setNotifyUrl("");
request.setBizContent(getAliPayRequest(h5PayDto).toString());
request.setReturnUrl(h5PayDto.getReturnUrl());
AlipayTradeWapPayResponse responseStr = alipayClient.pageExecute(request);
log.info(responseStr.getBody());
return responseStr;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private ObjectNode getAliPayRequest(H5PayDto h5PayDto) throws UnsupportedEncodingException {
ObjectNode obj = mapper.createObjectNode();
//obj.put("subject", URLEncoder.encode(payRequest.getBody(), "GBK"));
obj.put("subject", h5PayDto.getSubject());
obj.put("out_trade_no", h5PayDto.getOutTradeNo());
obj.put("timeout_express", 30d);
//obj.put("time_expire", DateTimeUtils.getYmdhmFormatDate(System.currentTimeMillis() + DateUtils.DAY_IN_MILLS));
obj.put("total_amount", h5PayDto.getTotalAmount());
obj.put("quit_url", h5PayDto.getReturnUrl());
obj.put("product_code", "QUICK_WAP_PAY");
return obj;
}
/**
* 功能描述 支付回调
*
* @param request
* @return java.lang.String
*/
public String aliPayNotify(HttpServletRequest request) {
Map<String, String> params = Maps.newHashMap();
try {
//获取支付宝POST过来反馈信息
Map requestParams = request.getParameterMap();
for (Iterator 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("ISO-8859-1"), "utf-8");
params.put(name, valueStr);
}
//调用SDK验证签名
boolean flag = AlipaySignature.rsaCheckV1(params, AliPayConfig.ALIPAY_PUBLIC_KEY, AliPayConfig.CHARSET,
AliPayConfig.SIGN_TYPE);
if (flag) { // 验证成功
return "success";
}
} catch (Exception e) {
log.info("aliPayNotify exception={}", e);
return "fail";
}
return "fail";
}
}
6.需要的参数实体
@ApiModelProperty(value = "商品的标题", required = true)
@NotNull(message = "缺少请求参数")
private String subject;
@ApiModelProperty(value = "订单号", required = true)
@NotNull(message = "缺少请求参数")
private String outTradeNo;
@ApiModelProperty(value = "金额", required = true)
@NotNull(message = "缺少请求参数")
private String totalAmount;
@ApiModelProperty(value = "支付成功后回跳地址")
private String returnUrl;
7.通过接口H5请求支付宝接口
@PostMapping("/H5Pay")
@ApiOperation(value = "根据订单号进行支付", notes = "H5调用支付宝支付接口")
@SneakyThrows
public ResponseClass<String> pay(@RequestBody H5PayDto h5PayDto) {
String result = h5PayUtil.aliPayH5(h5PayDto).getBody();
log.info("支付宝返回的信息是:" + result);
return ResponseClass.ok(result);
}
8.支付回掉的controller
@Autowired
private H5PayUtil h5PayUtil;
@RequestMapping("/H5PayNotice")
public String orderNotify(HttpServletRequest request) {
String alipayNotice = h5PayUtil.aliPayNotify(request);
return alipayNotice;
}
9.进行接口的请求。如图,返回了一个from表单,即可表示H5请求支付宝已成功。接下来可以和前端进行联调
支付的代码上传至GitHub上:
https://github.com/virtuousOne/pay
更多推荐
所有评论(0)