扫码支付快速接入
接入方式
接入流程

支付流程

系统交互流程

1.
2.
3.
1.
2.
交易状态流程

1.
2.
3.
4.
5.
6.
预下单
1.
2.
3.
示例代码
package com.java.sdk.demo;
import com.alipay.v3.ApiException;
import com.alipay.v3.ApiClient;
import com.alipay.v3.util.model.AlipayConfig;
import com.alipay.v3.Configuration;
import com.alipay.v3.api.AlipayTradeApi;
import com.alipay.v3.model.*;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class AlipayTradeApiPrecreate {
public static void main(String[] args) throws ApiException {
ApiClient defaultClient = Configuration.getDefaultApiClient();
// 初始化alipay参数(全局设置一次)
AlipayConfig alipayConfig = new AlipayConfig();
alipayConfig.setServerUrl("https://openapi.alipay.com");
alipayConfig.setAppId("<-- 请填写您的AppId,例如:2019091767145019 -->");
alipayConfig.setPrivateKey("<-- 请填写您的应用私钥,例如:MIIEvQIBADANB ... ... -->");
alipayConfig.setAlipayPublicKey("<-- 请填写您的支付宝公钥,例如:MIIBIjANBg... -->");
defaultClient.setAlipayConfig(alipayConfig);
AlipayTradeApi api = new AlipayTradeApi();
AlipayTradePrecreateModel data = new AlipayTradePrecreateModel();
ExtendParams extendParams = new ExtendParams();
extendParams.setSysServiceProviderId("2088511833207846");
extendParams.setSpecifiedSellerName("XXX的跨境小铺");
extendParams.setCardType("S0JP0000");
data.setExtendParams(extendParams);
BusinessParams businessParams = new BusinessParams();
businessParams.setMcCreateTradeIp("127.0.0.1");
data.setBusinessParams(businessParams);
data.setOutTradeNo("20150320010101001");
data.setProductCode("FACE_TO_FACE_PAYMENT");
data.setDiscountableAmount("80.00");
data.setOperatorId("yx_001");
List<GoodsDetail> goodsDetail = new ArrayList<GoodsDetail>();
GoodsDetail goodsDetail0 = new GoodsDetail();
goodsDetail0.setQuantity(1);
goodsDetail0.setPrice("2000");
goodsDetail0.setGoodsCategory("34543238");
goodsDetail0.setCategoriesTree("124868003|126232002|126252004");
goodsDetail0.setGoodsId("apple-01");
goodsDetail0.setShowUrl("http://www.alipay.com/xxx.jpg");
goodsDetail0.setGoodsName("ipad");
goodsDetail.add(goodsDetail0);
data.setGoodsDetail(goodsDetail);
data.setTerminalId("NJ_T_001");
data.setMerchantOrderNo("20161008001");
data.setSellerId("2088102146225135");
data.setBody("Iphone6 16G");
data.setSubject("Iphone6 16G");
data.setTotalAmount("88.88");
data.setStoreId("NJ_001");
try {
AlipayTradePrecreateResponseModel response = api.precreate(data);
} catch (ApiException e) {
AlipayTradePrecreateDefaultResponse errorObject = (AlipayTradePrecreateDefaultResponse) e.getErrorObject();
System.out.println("调用失败:" + errorObject);
}
}
}
重要入参说明
请求参数 | 说明 |
---|---|
out_trade_no | 商户订单号,需要保证商家系统不重复。 |
total_amount | 订单金额。 |
subject | 商品的标题/交易标题/订单标题/订单关键字等。不可使用特殊字符,如 / ,= ,& 等。 |
重要出参说明
请求参数 | 说明 |
---|---|
qr_code | 订单二维码(有效时间 2 小时)以字符串的格式返回,开发者需要自己使用工具根据内容生成二维码图片。 |
查询交易
示例代码
package com.java.sdk.demo;
import com.alipay.v3.ApiException;
import com.alipay.v3.ApiClient;
import com.alipay.v3.util.model.AlipayConfig;
import com.alipay.v3.Configuration;
import com.alipay.v3.api.AlipayTradeApi;
import com.alipay.v3.model.*;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class AlipayTradeApiQuery {
public static void main(String[] args) throws ApiException {
ApiClient defaultClient = Configuration.getDefaultApiClient();
// 初始化alipay参数(全局设置一次)
AlipayConfig alipayConfig = new AlipayConfig();
alipayConfig.setServerUrl("https://openapi.alipay.com");
alipayConfig.setAppId("<-- 请填写您的AppId,例如:2019091767145019 -->");
alipayConfig.setPrivateKey("<-- 请填写您的应用私钥,例如:MIIEvQIBADANB ... ... -->");
alipayConfig.setAlipayPublicKey("<-- 请填写您的支付宝公钥,例如:MIIBIjANBg... -->");
defaultClient.setAlipayConfig(alipayConfig);
AlipayTradeApi api = new AlipayTradeApi();
AlipayTradeQueryModel data = new AlipayTradeQueryModel();
List<String> queryOptions = new ArrayList<String>();
queryOptions.add("trade_settle_info");
data.setQueryOptions(queryOptions);
data.setOutTradeNo("20150320010101001");
data.setTradeNo("2014112611001004680 073956707");
try {
AlipayTradeQueryResponseModel response = api.query(data);
} catch (ApiException e) {
AlipayTradeQueryDefaultResponse errorObject = (AlipayTradeQueryDefaultResponse) e.getErrorObject();
System.out.println("调用失败:" + errorObject);
}
}
}
重要入参说明
请求参数 | 说明 |
---|---|
out_trade_no | 支付时传入的商户订单号,与 trade_no 必填一个。 |
trade_no | 支付时返回的支付宝交易号,与 out_trade_no 必填一个。 |
撤销交易
示例代码
package com.java.sdk.demo;
import com.alipay.v3.ApiException;
import com.alipay.v3.ApiClient;
import com.alipay.v3.util.model.AlipayConfig;
import com.alipay.v3.Configuration;
import com.alipay.v3.api.AlipayTradeApi;
import com.alipay.v3.model.*;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class AlipayTradeApiCancel {
public static void main(String[] args) throws ApiException {
ApiClient defaultClient = Configuration.getDefaultApiClient();
// 初始化alipay参数(全局设置一次)
AlipayConfig alipayConfig = new AlipayConfig();
alipayConfig.setServerUrl("https://openapi.alipay.com");
alipayConfig.setAppId("<-- 请填写您的AppId,例如:2019091767145019 -->");
alipayConfig.setPrivateKey("<-- 请填写您的应用私钥,例如:MIIEvQIBADANB ... ... -->");
alipayConfig.setAlipayPublicKey("<-- 请填写您的支付宝公钥,例如:MIIBIjANBg... -->");
defaultClient.setAlipayConfig(alipayConfig);
AlipayTradeApi api = new AlipayTradeApi();
AlipayTradeCancelModel data = new AlipayTradeCancelModel();
data.setOutTradeNo("20150320010101001");
data.setTradeNo("2014112611001004680073956707");
try {
AlipayTradeCancelResponseModel response = api.cancel(data);
} catch (ApiException e) {
AlipayTradeCancelDefaultResponse errorObject = (AlipayTradeCancelDefaultResponse) e.getErrorObject();
System.out.println("调用失败:" + errorObject);
}
}
}
重要入参说明
请求参数 | 说明 |
---|---|
out_trade_no | 支付时传入的商户订单号,与 trade_no 必填一个。 |
trade_no | 支付时返回的支付宝交易号,与 out_trade_no 必填一个。 |
重要出参说明
请求参数 | 说明 |
---|---|
retry_flag | 是否需要重试,枚举支持:Y:撤销不成功,需要重新调用接口。N:撤销不成功,不需要重新调用接口。 |
action | 本次撤销触发的交易动作。close:关闭交易,无退款 。refund:产生了退款。 |
退款流程
系统交互流程

退款说明
Y
则表示退款成功。示例代码
package com.java.sdk.demo;
import com.alipay.v3.ApiException;
import com.alipay.v3.ApiClient;
import com.alipay.v3.util.model.AlipayConfig;
import com.alipay.v3.Configuration;
import com.alipay.v3.api.AlipayTradeApi;
import com.alipay.v3.model.*;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class AlipayTradeApiRefund {
public static void main(String[] args) throws ApiException {
ApiClient defaultClient = Configuration.getDefaultApiClient();
// 初始化alipay参数(全局设置一次)
AlipayConfig alipayConfig = new AlipayConfig();
alipayConfig.setServerUrl("https://openapi.alipay.com");
alipayConfig.setAppId("<-- 请填写您的AppId,例如:2019091767145019 -->");
alipayConfig.setPrivateKey("<-- 请填写您的应用私钥,例如:MIIEvQIBADANB ... ... -->");
alipayConfig.setAlipayPublicKey("<-- 请填写您的支付宝公钥,例如:MIIBIjANBg... -->");
defaultClient.setAlipayConfig(alipayConfig);
AlipayTradeApi api = new AlipayTradeApi();
AlipayTradeRefundModel data = new AlipayTradeRefundModel();
List<OpenApiRoyaltyDetailInfoPojo> refundRoyaltyParameters = new ArrayList<OpenApiRoyaltyDetailInfoPojo>();
OpenApiRoyaltyDetailInfoPojo refundRoyaltyParameters0 = new OpenApiRoyaltyDetailInfoPojo();
refundRoyaltyParameters0.setTransInType("userId");
refundRoyaltyParameters0.setRoyaltyScene("达人佣金");
refundRoyaltyParameters0.setAmount("0.1");
refundRoyaltyParameters0.setTransOut("2088101126765726");
refundRoyaltyParameters0.setRoyaltyType("transfer");
refundRoyaltyParameters0.setTransIn("2088101126708402");
refundRoyaltyParameters0.setDesc("分账给2088101126708402");
refundRoyaltyParameters0.setTransInName("张三");
refundRoyaltyParameters0.setTransOutType("userId");
refundRoyaltyParameters.add(refundRoyaltyParameters0);
data.setRefundRoyaltyParameters(refundRoyaltyParameters);
data.setOutRequestNo("HZ01RF001");
List<String> queryOptions = new ArrayList<String>();
queryOptions.add("refund_detail_item_list");
data.setQueryOptions(queryOptions);
data.setOutTradeNo("20150320010101001");
List<RefundGoodsDetail> refundGoodsDetail = new ArrayList<RefundGoodsDetail>();
RefundGoodsDetail refundGoodsDetail0 = new RefundGoodsDetail();
refundGoodsDetail0.setGoodsId("apple-01");
refundGoodsDetail0.setOutItemId("outItem_01");
refundGoodsDetail0.setOutSkuId("outSku_01");
refundGoodsDetail0.setRefundAmount("19.50");
refundGoodsDetail.add(refundGoodsDetail0);
data.setRefundGoodsDetail(refundGoodsDetail);
data.setTradeNo("2014112611001004680073956707");
data.setRefundAmount("200.12");
data.setRefundReason("正常退款");
try {
AlipayTradeRefundResponseModel response = api.refund(data);
} catch (ApiException e) {
AlipayTradeRefundDefaultResponse errorObject = (AlipayTradeRefundDefaultResponse) e.getErrorObject();
System.out.println("调用失败:" + errorObject);
}
}
}
重要入参说明
请求参数 | 说明 |
---|---|
out_trade_no | 支付时传入的商户订单号,与 trade_no 必填一个。 |
trade_no | 支付时返回的支付宝交易号,与 out_trade_no 必填一个。 |
out_request_no | 本次退款请求流水号,部分退款时必传。 |
refund_amount | 本次退款金额。 |
重要出参说明
请求参数 | 说明 |
---|---|
refund_fee | 该笔交易已退款的总金额。 |
退款到银行卡通知

消息示例
ISV_GATEWAY_URL?charset=GBK&biz_content=
{
"trade_no":"2014112611001004680073956707","out_trade_no":"20150320010101001","out_request_no":"HZ01RF001","dback_status":"S","dback_amount":"1.01","bank_ack_time":"2020-06-02 14:03:48","est_bank_receipt_time":"2020-06-02 14:03:48"
}
&msg_method=alipay.trade.refund.depositback.completed&utc_timestamp=1516797622752&version=1.1&sign_type=RSA2¬ify_id=d275fec564e62af6bedbcee73f3f05fi5x&app_id=2013121700999429&sign=I+Y/lvqYUEEc10EPdpntRhFIQ==
重要参数说明
请求参数 | 说明 |
---|---|
trade_no | 支付宝交易订单号。 |
dback_status | 银行卡冲退状态。S - 成功。F - 失败。银行卡冲退失败,资金自动转入用户支付宝余额。 |
dback_amount | 银行卡冲退金额。 |
bank_ack_time | 银行响应时间,格式为 yyyy-MM-dd HH:mm:ss。 |
est_bank_receipt_time | 预估银行入账时间,格式为 yyyy-MM-dd HH:mm:ss。 |
对账流程
系统交互流程
1.
2.
注意:该下载链接仅 30 秒,在得到链接后系统需要立刻下载账单文件。
示例代码
package com.java.sdk.demo;
import com.alipay.v3.ApiException;
import com.alipay.v3.ApiClient;
import com.alipay.v3.util.model.AlipayConfig;
import com.alipay.v3.Configuration;
import com.alipay.v3.api.AlipayDataDataserviceBillDownloadurlApi;
import com.alipay.v3.model.*;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class AlipayDataDataserviceBillDownloadurlApiQuery {
public static void main(String[] args) throws ApiException {
ApiClient defaultClient = Configuration.getDefaultApiClient();
// 初始化alipay参数(全局设置一次)
AlipayConfig alipayConfig = new AlipayConfig();
alipayConfig.setServerUrl("https://openapi.alipay.com");
alipayConfig.setAppId("<-- 请填写您的AppId,例如:2019091767145019 -->");
alipayConfig.setPrivateKey("<-- 请填写您的应用私钥,例如:MIIEvQIBADANB ... ... -->");
alipayConfig.setAlipayPublicKey("<-- 请填写您的支付宝公钥,例如:MIIBIjANBg... -->");
defaultClient.setAlipayConfig(alipayConfig);
AlipayDataDataserviceBillDownloadurlApi api = new AlipayDataDataserviceBillDownloadurlApi();
String billType = "trade";
String billDate = "2016-04-05";
String smid = "2088123412341234";
try {
AlipayDataDataserviceBillDownloadurlQueryResponseModel response = api.query(billType, billDate, smid);
} catch (ApiException e) {
AlipayDataDataserviceBillDownloadurlQueryDefaultResponse errorObject = (AlipayDataDataserviceBillDownloadurlQueryDefaultResponse) e.getErrorObject();
System.out.println("调用失败:" + errorObject);
}
}
}
重要入参说明
请求参数 | 说明 |
---|---|
bill_type | 固定传入 trade。 |
bill_date | 需要下载的账单日期,最晚是当期日期的前一天。 |
重要出参说明
请求参数 | 说明 |
---|---|
bill_download_url | 账单文件下载地址,有效时长:30 秒。 |
下载账单文件示例代码
//将接口返回的对账单下载地址传入urlStr
String urlStr = "http://dwbillcenter.alipay.com/downloadBillFile.resource?bizType=X&userId=X&fileType=X&bizDates=X&downloadFileName=X&fileId=X";
//指定希望保存的文件路径
String filePath = "/Users/fund_bill_20160405.csv.zip";
URL url = null;
HttpURLConnection httpUrlConnection = null;
InputStream fis = null;
FileOutputStream fos = null;
try {
url = new URL(urlStr);
httpUrlConnection = (HttpURLConnection) url.openConnection();
httpUrlConnection.setConnectTimeout(5 * 1000);
httpUrlConnection.setDoInput(true);
httpUrlConnection.setDoOutput(true);
httpUrlConnection.setUseCaches(false);
httpUrlConnection.setRequestMethod("GET");
httpUrlConnection.setRequestProperty("CHARSET", "UTF-8");
httpUrlConnection.connect();
fis = httpUrlConnection.getInputStream();
byte[] temp = new byte[1024];
int b;
fos = new FileOutputStream(new File(filePath));
while ((b = fis.read(temp)) != -1) {
fos.write(temp, 0, b);
fos.flush();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(fis!=null) fis.close();
if(fos!=null) fos.close();
if(httpUrlConnection!=null) httpUrlConnection.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
修改于 2023-11-20 02:34:00