参考链接:
https://docs.open.alipay.com/203/105288/
支付的相关配置配置:
1、整合支付宝jar包—参考链接
https://docs.open.alipay.com/54/103419
2、支付宝网关gatewayUrl—–
https://openapi.alipay.com/gateway.do
3、开发者私钥private_key—-自己支付宝生成
4、支付宝公钥alipay_public_key—-自己支付宝生成
5、APPID
一、支付接口–跳转到SDK接入,将订单信息表单html输入到页面
public ActionResult doDefault() throws UnsupportedEncodingException
{
String trade_no = this.getStrParameter("trade_no");//订单编号
if(StringHelper.isNotEmpty(trade_no))
{
String result = apserver.AliPay(trade_no);
if(result!=null)
{
ResponseHelper.println(getResponse(), result);
}else
{
//跳转到付款失败页面
}
}
return null;
}
二、使用SDK快速接入,参数详情可查看
https://docs.open.alipay.com/203/107090/
参数名称 | 参数说明 |
---|---|
out_trade_no | 商户订单号,需要保证不重复 |
subject | 订单标题 |
total_amount | 订单金额 |
out_trade_no | 商户网站唯一订单号 |
public String AliPay(String trade_no)
{
String gatewayUrl = Configuration.getString("alipay.gatewayUrl");//支付宝网关
String app_id = Configuration.getString("alipay.appid");//应用id
String private_key = Configuration.getString("alipay.private_key");//支付宝私钥
String charset = "gbk";//编码格式
String alipay_public_key = Configuration.getString("alipay.alipay_public_key");
//支付宝公钥
String sign_type = "RSA2";//签名类型
try
{
//获得初始化的AlipayClient
AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl, app_id, private_key, "json", charset, alipay_public_key, sign_type);
//设置请求参数
AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
alipayRequest.setReturnUrl("");//支付回调地址
alipayRequest.setNotifyUrl("");//支付异步通知地址
//获取订单信息
DataRow order = oserver.queryDetail(trade_no);
if(order!=null)
{
String total_amount = order.getString("price");
int custom_type = order.getInt("service_type");//服务类型
String title = order.getString("order_name");//商品名称
String subject = SysLibrary.getCustom_type(custom_type);
String remark = order.getString("remark");//订单描述
alipayRequest.setBizContent("{\"out_trade_no\":\""+ trade_no +"\","
+ "\"total_amount\":\""+ total_amount +"\","
+ "\"subject\":\""+ subject +"——"+title+"\","
+ "\"body\":\""+ remark +"\","
+ "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");//填充业务参数
String result = alipayClient.pageExecute(alipayRequest).getBody();
//调用SDK生成表单
System.out.println(result);
return result;
}
}catch(Exception e)
{
e.printStackTrace();
}
return null;
}
三、支付宝回调接口
public ActionResult doCallback() throws UnsupportedEncodingException
{
System.out.println("支付回调");
try
{
//获取支付宝GET过来反馈信息
Map<String,String> params = new HashMap<String,String>();
Map<String,String[]> requestParams = this.getRequest().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] + ",";
}
System.out.print(valueStr);
//乱码解决,这段代码在出现乱码时使用
valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
params.put(name, valueStr);
}
params.remove("function");
//商户订单号
String out_trade_no = getRequest().getParameter("out_trade_no");
//支付宝交易号
String trade_no = getRequest().getParameter("trade_no");
//付款金额
String total_amount = getRequest().getParameter("total_amount");
this.setAttribute("out_trade_no", out_trade_no);
this.setAttribute("trade_no", trade_no);
this.setAttribute("total_amount", total_amount);
boolean signVerified = apserver.CheckRasSing(params); //调用SDK验证签名
if(signVerified)
{
//更新数据
oserver.updateOrder(out_trade_no, trade_no, "1");
return new ActionResult("/home/paySuccess.jsp",true);
}else
{
oserver.updateOrder(out_trade_no, trade_no, "0");
return new ActionResult("/home/payFail.jsp",true);
}
}catch(Exception e)
{
e.printStackTrace();
}
return null;
}
四、调用sdk验证签名
参考链接:
https://docs.open.alipay.com/291/106074/
public boolean CheckRasSing(Map<String,String> params) throws AlipayApiException
{
String charset = "utf-8";
String alipay_public_key = Configuration.getString("alipay.alipay_public_key");
String sign_type = "RSA2";
System.out.println("系统参数-----------------"+params);
boolean signVerified = AlipaySignature.rsaCheckV1(params,
alipay_public_key,charset, sign_type); //调用SDK验证签名
return signVerified;
}
五、支付宝服务器异步通知
暂时没有写
public ActionResult doNotify()
{
System.out.println("支付异步通知");
return null;
}
ps:差不多写到这里就完了,还要准备支付成功页面和支付失败页面接收支付参数,根据情况显示给用户。
版权声明:本文为qq_36826248原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。