Java支付宝APP支付-统一收单交易退款
上一章已經(jīng)講述了支付寶服務(wù)器異步通知接口的開發(fā),這一章我們講述一下支付寶統(tǒng)一收單交易退款接口的開發(fā)。
1、應(yīng)用場(chǎng)景
當(dāng)交易發(fā)生之后一段時(shí)間內(nèi),由于買家或者賣家的原因需要退款時(shí),賣家可以通過退款接口將支付款退還給買家,支付寶將在收到退款請(qǐng)求并且驗(yàn)證成功之后,按照退款規(guī)則將支付款按原路退到買家?guī)ぬ?hào)上。 交易超過約定時(shí)間(簽約時(shí)設(shè)置的可退款時(shí)間)的訂單無法進(jìn)行退款 支付寶退款支持單筆交易分多次退款,多次退款需要提交原支付訂單的商戶訂單號(hào)和設(shè)置不同的退款單號(hào)。一筆退款失敗后重新提交,要采用原來的退款單號(hào)??偼丝罱痤~不能超過用戶實(shí)際支付金額。
2、公共參數(shù)
2.1請(qǐng)求地址
| 正式環(huán)境 | https://openapi.alipay.com/gateway.do |
2.2公共請(qǐng)求參數(shù)
| app_id | String | 是 | 32 | 支付寶分配給開發(fā)者的應(yīng)用ID | 2014072300007148 |
| method | String | 是 | 128 | 接口名稱 | alipay.trade.refund |
| format | String | 否 | 40 | 僅支持JSON | JSON |
| charset | String | 是 | 10 | 請(qǐng)求使用的編碼格式,如utf-8,gbk,gb2312等 | utf-8 |
| sign_type | String | 是 | 10 | 商戶生成簽名字符串所使用的簽名算法類型,目前支持RSA2和RSA,推薦使用RSA2 | RSA2 |
| sign | String | 是 | 344 | 商戶請(qǐng)求參數(shù)的簽名串,詳見簽名 | 詳見示例 |
| timestamp | String | 是 | 19 | 發(fā)送請(qǐng)求的時(shí)間,格式"yyyy-MM-dd HH:mm:ss" | 2014-07-24 03:07:50 |
| version | String | 是 | 3 | 調(diào)用的接口版本,固定為:1.0 | 1.0 |
| app_auth_token | String | 否 | 40 | 詳見應(yīng)用授權(quán)概述 | ? |
| biz_content | String | 是 | ? | 請(qǐng)求參數(shù)的集合,最大長(zhǎng)度不限,除公共參數(shù)外所有請(qǐng)求參數(shù)都必須放在這個(gè)參數(shù)中傳遞,具體參照各產(chǎn)品快速接入文檔 | ? |
3、請(qǐng)求參數(shù)
| out_trade_no | String | 特殊可選 | 64 | 訂單支付時(shí)傳入的商戶訂單號(hào),不能和 trade_no同時(shí)為空。 | 20150320010101001 |
| trade_no | String | 特殊可選 | 64 | 支付寶交易號(hào),和商戶訂單號(hào)不能同時(shí)為空 | 2014112611001004680073956707 |
| refund_amount | Price | 必選 | 9 | 需要退款的金額,該金額不能大于訂單金額,單位為元,支持兩位小數(shù) | 200.12 |
| refund_currency | String | 可選 | 8 | 訂單退款幣種信息 | USD |
| refund_reason | String | 可選 | 256 | 退款的原因說明 | 正常退款 |
| out_request_no | String | 可選 | 64 | 標(biāo)識(shí)一次退款請(qǐng)求,同一筆交易多次退款需要保證唯一,如需部分退款,則此參數(shù)必傳。 | HZ01RF001 |
| operator_id | String | 可選 | 30 | 商戶的操作員編號(hào) | OP001 |
| store_id | String | 可選 | 32 | 商戶的門店編號(hào) | NJ_S_001 |
| terminal_id | String | 可選 | 32 | 商戶的終端編號(hào) | NJ_T_001 |
| ?goods_detail | GoodsDetail[] | 可選 | ? | 退款包含的商品列表信息,Json格式。? 其它說明詳見:“商品明細(xì)說明” | ? |
| refund_royalty_parameters | OpenApiRoyaltyDetailInfoPojo[] | 可選 | ? | 退分賬明細(xì)信息 | ? |
4、公共響應(yīng)參數(shù)
| code | String | 是 | - | 網(wǎng)關(guān)返回碼,詳見文檔 | 40004 |
| msg | String | 是 | - | 網(wǎng)關(guān)返回碼描述,詳見文檔 | Business Failed |
| sub_code | String | 否 | - | 業(yè)務(wù)返回碼,參見具體的API接口文檔 | ACQ.TRADE_HAS_SUCCESS |
| sub_msg | String | 否 | - | 業(yè)務(wù)返回碼描述,參見具體的API接口文檔 | 交易已被支付 |
| sign | String | 是 | - | 簽名,詳見文檔 | DZXh8eeTuAHoYE3w1J+POiPhfDxOYBfUNn1lkeT/V7P4zJdyojWEa6IZs6Hz0yDW5Cp/viufUb5I0/V5WENS3OYR8zRedqo6D+fUTdLHdc+EFyCkiQhBxIzgngPdPdfp1PIS7BdhhzrsZHbRqb7o4k3Dxc+AAnFauu4V6Zdwczo= |
5、響應(yīng)參數(shù)
| trade_no | String | 必填 | 64 | 2013112011001004330000121536 | 支付寶交易號(hào) |
| out_trade_no | String | 必填 | 64 | 商戶訂單號(hào) | 6823789339978248 |
| buyer_logon_id | String | 必填 | 100 | 用戶的登錄id | 159****5620 |
| fund_change | String | 必填 | 1 | 本次退款是否發(fā)生了資金變化 | Y |
| refund_fee | Price | 必填 | 11 | 退款總金額 | 88.88 |
| refund_currency | String | 選填 | 8 | 退款幣種信息 | USD |
| gmt_refund_pay | Date | 必填 | 32 | 退款支付時(shí)間 | 2014-11-27 15:45:57 |
| refund_detail_item_list | TradeFundBill | 選填 | ? | 退款使用的資金渠道 | ? |
| store_name | String | 選填 | 512 | 交易在支付時(shí)候的門店名稱 | 望湘園聯(lián)洋店 |
| buyer_user_id | String | 必填 | 28 | 買家在支付寶的用戶id | 2088101117955611 |
| present_refund_buyer_amount | String | 選填 | 11 | 本次退款金額中買家退款金額 | 88.88 |
| present_refund_discount_amount | String | 選填 | 11 | 本次退款金額中平臺(tái)優(yōu)惠退款金額 | 88.88 |
| present_refund_mdiscount_amount | String | 選填 | 11 | 本次退款金額中商家優(yōu)惠退款金額 | 88.88 |
6、請(qǐng)求示例
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(); request.setBizContent("{" + "\"out_trade_no\":\"20150320010101001\"," + "\"trade_no\":\"2014112611001004680073956707\"," + "\"refund_amount\":200.12," + "\"refund_currency\":\"USD\"," + "\"refund_reason\":\"正常退款\"," + "\"out_request_no\":\"HZ01RF001\"," + "\"operator_id\":\"OP001\"," + "\"store_id\":\"NJ_S_001\"," + "\"terminal_id\":\"NJ_T_001\"," + " \"goods_detail\":[{" + " \"goods_id\":\"apple-01\"," + "\"alipay_goods_id\":\"20010001\"," + "\"goods_name\":\"ipad\"," + "\"quantity\":1," + "\"price\":2000," + "\"goods_category\":\"34543238\"," + "\"body\":\"特價(jià)手機(jī)\"," + "\"show_url\":\"http://www.alipay.com/xxx.jpg\"" + " }]," + " \"refund_royalty_parameters\":[{" + " \"royalty_type\":\"transfer\"," + "\"trans_out\":\"2088101126765726\"," + "\"trans_out_type\":\"userId\"," + "\"trans_in_type\":\"userId\"," + "\"trans_in\":\"2088101126708402\"," + "\"amount\":0.1," + "\"amount_percentage\":100," + "\"desc\":\"分賬給2088101126708402\"" + " }]" + " }"); AlipayTradeRefundResponse response = alipayClient.execute(request); if(response.isSuccess()){ System.out.println("調(diào)用成功"); } else { System.out.println("調(diào)用失敗"); }7、響應(yīng)示例
{"alipay_trade_refund_response": {"code": "10000","msg": "Success","trade_no": "支付寶交易號(hào)","out_trade_no": "6823789339978248","buyer_logon_id": "159****5620","fund_change": "Y","refund_fee": 88.88,"refund_currency": "USD","gmt_refund_pay": "2014-11-27 15:45:57","refund_detail_item_list": [{"fund_channel": "ALIPAYACCOUNT","amount": 10,"real_amount": 11.21,"fund_type": "DEBIT_CARD"}],"store_name": "望湘園聯(lián)洋店","buyer_user_id": "2088101117955611","present_refund_buyer_amount": "88.88","present_refund_discount_amount": "88.88","present_refund_mdiscount_amount": "88.88"},"sign": "ERITJKEIJKJHKKKKKKKHJEREEEEEEEEEEE" }8、異常示例
{"alipay_trade_refund_response": {"code": "20000","msg": "Service Currently Unavailable","sub_code": "isp.unknow-error","sub_msg": "系統(tǒng)繁忙"},"sign": "ERITJKEIJKJHKKKKKKKHJEREEEEEEEEEEE" }9、代碼實(shí)現(xiàn)?
9.1基礎(chǔ)類
AlipayConfig配置類,主要包含支付寶的配置信息
package com.hisap.xql.api.common.ali;/*** @Author: QijieLiu* @Description: 支付寶配置信息* @Date: Created in 10:39 2018/8/20*/ public class AlipayConfig {public static String APP_ID = "xxxxxx";public static String APP_PRIVATE_KEY = "xxxxxx";//APP私鑰public static String APP_PUBLIC_KEY = "xxxxxx";//APP公鑰public static String ALIPAY_PUBLIC_KEY = "xxxxxx";//支付寶公鑰public static String UNIFIEDORDER_URL = "https://openapi.alipay.com/gateway.do";public static String NOTIFY_URL = "http://xxx.xxx.xxx.xxx/XqlApi/xxx/paynotify";public static String CHARSET = "UTF-8";public static String FORMAT = "json";public static String SIGNTYPE = "RSA2";public static String TIMEOUT_EXPRESS = "30m"; }AlipayRefund支付寶退款實(shí)體類
package com.hisap.xql.api.common.ali;/*** @Author: QijieLiu* @Description: 支付寶退款類* @Date: Created in 18:57 2018/8/20*/ public class AlipayRefund {private String out_trade_no;//必須 商戶訂單號(hào)private String trade_no;//必須 支付寶交易號(hào)private String refund_amount;//必須 退款金額private String refund_reason;//可選 代表 退款的原因說明private String out_request_no;//可選 標(biāo)識(shí)一次退款請(qǐng)求,同一筆交易多次退款需要保證唯一(就是out_request_no在2次退款一筆交易時(shí),要不一樣),如需部分退款,則此參數(shù)必傳private String operator_id;//可選 代表 商戶的操作員編號(hào)private String store_id;//可選 代表 商戶的門店編號(hào)private String terminal_id;//可選 代表 商戶的終端編號(hào)public String getOut_trade_no() {return out_trade_no;}public void setOut_trade_no(String out_trade_no) {this.out_trade_no = out_trade_no;}public String getTrade_no() {return trade_no;}public void setTrade_no(String trade_no) {this.trade_no = trade_no;}public String getRefund_amount() {return refund_amount;}public void setRefund_amount(String refund_amount) {this.refund_amount = refund_amount;}public String getRefund_reason() {return refund_reason;}public void setRefund_reason(String refund_reason) {this.refund_reason = refund_reason;}public String getOut_request_no() {return out_request_no;}public void setOut_request_no(String out_request_no) {this.out_request_no = out_request_no;}public String getOperator_id() {return operator_id;}public void setOperator_id(String operator_id) {this.operator_id = operator_id;}public String getStore_id() {return store_id;}public void setStore_id(String store_id) {this.store_id = store_id;}public String getTerminal_id() {return terminal_id;}public void setTerminal_id(String terminal_id) {this.terminal_id = terminal_id;} }ResponseJson基礎(chǔ)類,主要與APP前端進(jìn)行交互
package com.hisap.xql.api.common.bean;public class ResponseJson {// 結(jié)果碼private String code;// 結(jié)果說明private String message;// 內(nèi)容private Object data;public String getCode() {return code;}public void setCode(String code) {this.code = code;}public String getMessage() {return message;}public void setMessage(String message) {this.message = message;}public Object getData() {return data;}public void setData(Object data) {this.data = data;}}9.1業(yè)務(wù)類
這里我就只貼出Service層代碼,其余代碼都類似,參數(shù)這里只需要兩個(gè),一個(gè)是支付寶交易號(hào),還有一個(gè)是退款金額,注意一下這里的退款金額單位是元,而微信是分。當(dāng)然這里也可以添加對(duì)應(yīng)的業(yè)務(wù)邏輯,我們這里不需要。
package com.hisap.xql.api.service.impl;import java.math.BigDecimal; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map;import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.serializer.SerializerFeature; import com.alipay.api.AlipayApiException; import com.alipay.api.AlipayClient; import com.alipay.api.DefaultAlipayClient; import com.alipay.api.domain.AlipayTradeAppPayModel; import com.alipay.api.internal.util.AlipaySignature; import com.alipay.api.request.AlipayTradeAppPayRequest; import com.alipay.api.request.AlipayTradeRefundRequest; import com.alipay.api.response.AlipayTradeAppPayResponse; import com.alipay.api.response.AlipayTradeRefundResponse; import com.hisap.xql.api.common.ali.AlipayConfig; import com.hisap.xql.api.common.ali.AlipayRefund; import com.hisap.xql.api.common.bean.ResponseJson; import com.hisap.xql.api.common.constant.CodeMsg; import com.hisap.xql.api.common.utils.Collections3; import com.hisap.xql.api.common.utils.CommonUtil; import com.hisap.xql.api.common.utils.StringUtil; import com.hisap.xql.api.common.utils.VersionUtil; import com.hisap.xql.api.dao.XqlOrderGoodsMapper; import com.hisap.xql.api.model.XqlOrder; import com.hisap.xql.api.model.XqlOrderGoods; import com.hisap.xql.api.model.XqlOrderGoodsExample; import com.hisap.xql.api.model.XqlVersion; import com.hisap.xql.api.service.AliPayService; import com.hisap.xql.api.service.CommonService; import com.hisap.xql.api.service.ErpInterfaceService; import com.hisap.xql.api.service.WeChatPayService; import com.hisap.xql.api.service.XqlOrderService;@Service public class AliPayServiceImpl implements AliPayService {private static final Logger logger = LoggerFactory.getLogger(AliPayServiceImpl.class);@AutowiredCommonService commonService;@AutowiredXqlOrderService xqlOrderServiceImpl;@AutowiredXqlOrderGoodsMapper xqlOrderGoodsMapper;@AutowiredWeChatPayService weChatPayServiceImpl;@AutowiredErpInterfaceService erpInterfaceServiceImpl;@Overridepublic ResponseJson refund(String trade_no, BigDecimal refund_amount)throws Exception {ResponseJson responseJson = new ResponseJson();try{// 實(shí)例化客戶端AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.UNIFIEDORDER_URL, AlipayConfig.APP_ID,AlipayConfig.APP_PRIVATE_KEY, AlipayConfig.FORMAT,AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY,AlipayConfig.SIGNTYPE);AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();AlipayRefund alipayRefund = new AlipayRefund();alipayRefund.setTrade_no(trade_no);alipayRefund.setRefund_amount(refund_amount.toString());request.setBizContent(JSONObject.toJSONString(alipayRefund));AlipayTradeRefundResponse response = alipayClient.execute(request);if(response.getCode().equals("10000")){responseJson = CommonUtil.createResponseJson(CodeMsg.SUCCESS_CODE, CodeMsg.SUCCESS_MSG, response);}else{responseJson = CommonUtil.createResponseJson(response.getSubCode(), response.getSubMsg(), response);}}catch(Exception e){e.printStackTrace();logger.error("支付寶退款接口服務(wù)端異常,異常信息---" + e.getMessage(), e);return CommonUtil.createResponseJson(CodeMsg.SERVER_ERROR_CODE, CodeMsg.SERVER_ERROR_MSG, new JSONObject());}return responseJson;}public static void main(String[] args) throws Exception {String trade_no = "2018083021001004540585684766";BigDecimal refund_amount = new BigDecimal("0.01");ResponseJson responseJson = new ResponseJson();AliPayServiceImpl aa = new AliPayServiceImpl();responseJson = aa.refund(trade_no,refund_amount);System.out.println(JSON.toJSONString(responseJson, SerializerFeature.WriteNullStringAsEmpty));}}支付寶的統(tǒng)一收單交易退款就講到這里,有什么問題大家可以私我。
總結(jié)
以上是生活随笔為你收集整理的Java支付宝APP支付-统一收单交易退款的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 入职半年后,我升到了管理岗
- 下一篇: 南通大学报计算机等级考试,南通大学201