一:支付宝退款
示例代码:
AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do","app_id","your private_key","json","GBK","alipay_public_key","RSA2");
AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
JSONObject bizContent = new JSONObject();
bizContent.put("trade_no", "2021081722001419121412730660");
bizContent.put("refund_amount", 0.01);
bizContent.put("out_request_no", "HZ01RF001");
返回参数选项,按需传入
//JSONArray queryOptions = new JSONArray();
//queryOptions.add("refund_detail_item_list");
//bizContent.put("query_options", queryOptions);
request.setBizContent(bizContent.toString());
AlipayTradeRefundResponse response = alipayClient.execute(request);
if(response.isSuccess()){
System.out.println("调用成功");
} else {
System.out.println("调用失败");
}
二:代码示例
@RequestMapping("/refund") public AjaxResult refund(String orderid) { System.out.println( "退款订单号" + orderid ); SysOrders sysOrders = sysOrdersService.selectOne( orderid ); try { AlipayClient alipayClient = new DefaultAlipayClient( constant.serverUrl, constant.appId, constant.privateKey, constant.format, constant.charset, constant.PublicKey, "RSA2" ); AlipayTradeRefundRequest request = new AlipayTradeRefundRequest(); JSONObject bizContent = new JSONObject(); bizContent.put( "out_trade_no", sysOrders.getOrderId() ); bizContent.put( "refund_amount", 1 ); bizContent.put( "out_request_no", System.currentTimeMillis() + "" ); request.setBizContent( bizContent.toString() ); AlipayTradeRefundResponse response = alipayClient.execute( request ); if (response.isSuccess()) { System.out.println( "调用成功" ); return AjaxResult.success( "退款调用成功" ); } else { System.out.println( "退款调用失败" ); } } catch (AlipayApiException e) { e.printStackTrace(); } return AjaxResult.error( "调用失败" ); }
三:测试
数据库新增订单信息
成功支付
控制台输出
数据库订单状态
调用退款接口
控制台输出
注意:如果是全额退款支付宝不会调用异步返回的接口,如果退款金额比订单金额小,支付宝会调用异步返回的的接口,可以获取退款状态,去修改订单的状态,修改金额等等