springboot 支付宝支付业务网页端扫码

AlipayClient alipayClient = new DefaultAlipayClient(
    "https://openapi.alipay.com/gateway.do",
appID,//后台的appID
     privateKey, //应用私钥
     "json",
     charset,//UTF-8
     publicKey,//支付宝公钥
     signType//RSA2
);
AlipayTradePagePayRequest payRequest = new AlipayTradePagePayRequest();
payRequest.setReturnUrl("https://xxx.xxx.xxx/xxx/api");//支付宝唤起你的页面
payRequest.setNotifyUrl("https://xxx.xxx.xxx/xxx/xxx/aliCall");//回调接口
JSONObject bizContent = new JSONObject();
//自己的单号
bizContent.put("out_trade_no", itrOrderId);
//充值金额
bizContent.put("total_amount", 10);
bizContent.put("subject", "支付时候显示的标题");
bizContent.put("product_code", "FAST_INSTANT_TRADE_PAY");
payRequest.setBizContent(bizContent.toString());

AlipayTradePagePayResponse response = alipayClient.pageExecute(payRequest);




@RequestMapping("/aliCall")
public void aliCall(HttpServletRequest request, HttpServletResponse response, AliReturnPayBean returnPay) throws IOException {
response.setContentType("type=text/html;charset=UTF-8");
log.error("支付宝的的回调函数被调用");
if (!PayConfig.checkSign(request)) {
log.error("验签失败");
response.getWriter().write("failture");
return;
}
if (returnPay == null) {
log.error("支付宝的returnPay返回为空");
response.getWriter().write("success");
return;
}
log.error("支付宝的returnPay" + returnPay);
// 表示支付成功状态下的操作
if (returnPay.getTrade_status().equals("TRADE_SUCCESS")) {
log.error("支付宝的支付状态为TRADE_SUCCESS");
String out_trade_no = returnPay.getOut_trade_no();
//进行自己的业务逻辑
}
response.getWriter().write("success");
}


public class PayConfig {
// 请填写您的AppId,例如:2019091767145019(必填)
private static final String appID = "";
//应用私钥,这里修改生成的私钥即可(必填)
private static final String privateKey = "";
//支付宝公钥,而非应用公钥(必填)
public static final String publicKey = "";
//默认即可(必填)
public static final String charset = "utf-8";
//默认即可(必填)
public static final String signType = "RSA2";
// @Bean
public AlipayClient alipayClient(){
// 沙箱环境使用https://openapi.alipaydev.com/gateway.do
// 线上环境使用https://openapi.alipay.com/gateway.do
return new DefaultAlipayClient("https://openapi.alipaydev.com/gateway.do",
appID, privateKey, "json", charset, publicKey, signType);
}
/**
* 验签,是否正确
*/
public static boolean checkSign(HttpServletRequest request){
Map<String, String[]> requestMap = request.getParameterMap();
Map<String, String> paramsMap = new HashMap<>();
requestMap.forEach((key, values) -> {
String strs = "";
for(String value : values) {
strs = strs + value;
}
System.out.println(key +"===>"+strs);
paramsMap.put(key, strs);
});
System.out.println();
//调用SDK验证签名
try {
return AlipaySignature.rsaCheckV1(paramsMap, PayConfig.publicKey, PayConfig.charset, PayConfig.signType);
} catch (AlipayApiException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("*********************验签失败********************");
return false;
}
}

}



原文地址:https://www.cnblogs.com/michaelcnblogs/p/15645242.html