支付宝开发文档地址
(https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=11_1)
支付宝参数配置
public class AliPayConfig {
// 1.商户appid
public static String APP_ID = "yourAppId";
// 2.请求网关地址
public static String SERVER_URL = "https://openapi.alipay.com/gateway.do";
// 3.私钥
public static String APP_PRIVATE_KEY = "yourAppPrivateKey";
// 4.支付宝公钥
public static String ALIPAY_PUBLIC_KEY = "yourAlipayPublicKey";
// 5.服务器异步通知页面路径
public static String ASY_URL = "yourAsyUrl";
// 6.编码
public static String CHARSET = "UTF-8";
// 7.返回格式
public static String FORMAT = "json";
// 8.加密类型
public static String SIGNTYPE = "RSA2";
}
创建订单
public static JSONObject createPayOrder(String orderId, String cost,
String describe) {
JSONObject ret = new JSONObject();
// 实例化客户端
AlipayClient alipayClient = new DefaultAlipayClient(
AliPayConfig.SERVER_URL, AliPayConfig.APP_ID,
AliPayConfig.APP_PRIVATE_KEY, AliPayConfig.FORMAT,
AliPayConfig.CHARSET, AliPayConfig.ALIPAY_PUBLIC_KEY,
AliPayConfig.SIGNTYPE);
// 实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.pay
AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
// SDK已经封装掉了公共参数,这里只需要传入业务参数。以下方法为sdk的model入参方式(model和biz_content同时存在的情况下取biz_content)。
AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
model.setBody(describe);
model.setSubject("App支付测试Java");
model.setOutTradeNo(orderId);
model.setTimeoutExpress("20m");
model.setTotalAmount(cost);
model.setProductCode("QUICK_MSECURITY_PAY");
request.setBizModel(model);
request.setNotifyUrl(AliPayConfig.ASY_URL);
System.out.println(request);
String orderStr = "";
try {
// 这里和普通的接口调用不同,使用的是sdkExecute
AlipayTradeAppPayResponse response = alipayClient
.sdkExecute(request);
orderStr = response.getBody();
System.out.println(response.getBody());// 就是orderString
// 可以直接给客户端请求,无需再做处理。
} catch (AlipayApiException e) {
ret.put("code", ErrorCode.OTHER);
ret.put("msg", e.getMessage());
return ret;
}
ret.put("code", ErrorCode.SUCCESS);
ret.put("sign", orderStr);
return ret;
}
支付宝结果异步回调
public static JSONObject aliPayNotify(Map<String, String[]> requestParams) {
JSONObject result = new JSONObject();
System.out.println("支付宝支付结果通知" + requestParams.toString());
// 获取支付宝POST过来反馈信息
Map<String, String> params = new HashMap<String, String>();
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);
}
// 获取到返回的所有参数 先判断是否交易成功trade_status 再做签名校验
// 1、商户需要验证该通知数据中的out_trade_no是否为商户系统中创建的订单号,
// 2、判断total_amount是否确实为该订单的实际金额(即商户订单创建时的金额),
// 3、校验通知中的seller_id(或者seller_email)
// 是否为out_trade_no这笔单据的对应的操作方(有的时候,一个商户可能有多个seller_id/seller_email),
// 4、验证app_id是否为该商户本身。上述1、2、3、4有任何一个验证不通过,则表明本次通知是异常通知,务必忽略。
try {
// 验证签名
boolean flag = AlipaySignature.rsaCheckV1(params,
AliPayConfig.ALIPAY_PUBLIC_KEY, "utf-8", "RSA2");
if (flag) {
if ("TRADE_SUCCESS".equals(params.get("trade_status"))
|| "TRADE_FINISHED".equals(params.get("trade_status"))) {
String appId = params.get("app_id");
if (!appId.equals(AliPayConfig.APP_ID)) {
result.put("code", ErrorCode.OTHER);
return result;
}
// 订单金额
String total_amount = params.get("total_amount");
// 用户支付金额
String amount = params.get("buyer_pay_amount");
// 商户订单号
String out_trade_no = params.get("out_trade_no");
// 支付宝交易号
String trade_no = params.get("trade_no");
// 买家支付宝账号
String buyer_logon_id = params.get("buyer_logon_id");
// 交易状态
String trade_status = params.get("trade_status");
// 商品描述
String body = params.get("body");
// 交易创建时间
String gmt_create = params.get("gmt_create");
// 交易付款时间
String gmt_payment = params.get("gmt_payment");
JSONObject payParams = new JSONObject();
payParams.put("total_amount", total_amount);
payParams.put("amount", amount);
payParams.put("out_trade_no", out_trade_no);
payParams.put("trade_no", trade_no);
payParams.put("buyer_logon_id", buyer_logon_id);
payParams.put("trade_status", trade_status);
payParams.put("body", body);
payParams.put("gmt_create", gmt_create);
payParams.put("gmt_payment", gmt_payment);
result.put("params", payParams);
result.put("code", ErrorCode.SUCCESS);
}
} else {
result.put("code", ErrorCode.OTHER);
return result;
}
} catch (AlipayApiException e) {
result.put("code", ErrorCode.OTHER);
return result;
}
return result;
}
退款
public String AliRefund(String out_trade_no, String trade_no,
String out_request_no, String refund_amount) {
AlipayClient alipayClient = new DefaultAlipayClient(
"https://openapi.alipay.com/gateway.do", AliPayConfig.APP_ID,
AliPayConfig.APP_PRIVATE_KEY, "json", "utf-8",
AliPayConfig.ALIPAY_PUBLIC_KEY, "RSA2"); // 获得初始化的AlipayClient
AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();// 创建API对应的request类
String strResponse = null;
request.setBizContent("{" + " \"out_trade_no\":\"" + out_trade_no
+ "\"," + " \"trade_no\":\"" + trade_no + "\","
+ " \"out_request_no\":\"" + out_request_no + "\","
+ " \"refund_amount\":\"" + refund_amount + "\"" + " }");// 设置业务参数
AlipayTradeRefundResponse response;
try {
response = alipayClient.execute(request);
if (response.isSuccess()) {
strResponse = "退款成功";
} else {
strResponse = "退款失败";
}
} catch (AlipayApiException e) {
strResponse = "退款出错";
}
return strResponse;
}
订单查询
public void orderPayQuery(String trade_no, String out_trade_no) {
AlipayClient alipayClient = new DefaultAlipayClient(
"https://openapi.alipay.com/gateway.do", AliPayConfig.APP_ID,
AliPayConfig.APP_PRIVATE_KEY, "json", "utf-8",
AliPayConfig.ALIPAY_PUBLIC_KEY, "RSA2"); // 获得初始化的AlipayClient
AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();// 创建API对应的request类
request.setBizContent("{" + " \"out_trade_no\":\"" + out_trade_no
+ "\"," + " \"trade_no\":\"" + trade_no + "\"" + " }");// 设置业务参数
AlipayTradeQueryResponse response;
try {
response = alipayClient.execute(request);
System.out.print(response.getBody());
Map<String, String> restmap = new HashMap<String, String>();// 返回提交支付宝订单交易查询信息
boolean flag = false; // 查询状态
if (response.isSuccess()) {
// 调用成功,则处理业务逻辑
if ("10000".equals(response.getCode())) {
// 订单创建成功
flag = true;
restmap.put("order_no", response.getOutTradeNo());
restmap.put("trade_no", response.getTradeNo());
restmap.put("buyer_logon_id", response.getBuyerLogonId());
restmap.put("trade_status", response.getTradeStatus());
} else {
}
}
} catch (AlipayApiException e) {
e.printStackTrace();
}
}
网友评论