支付宝订单中心参考文档
https://opendocs.alipay.com/pre-open/022o4b?pathHash=0a0a2f12
https://opendocs.alipay.com/mini/02k6zr?pathHash=8a0dc48e
基于官方文档编写的demo
具体流程是
1.先要获取支付宝用户ID用户接受订单中心消息
2.上传图片
3.创建商品
4.调用订单中心接口 变更状态
package cn.lili.buyer.test.cart;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.FileItem;
import com.alipay.api.domain.*;
import com.alipay.api.request.AlipayMerchantItemFileUploadRequest;
import com.alipay.api.request.AlipayMerchantOrderSyncRequest;
import com.alipay.api.request.AlipaySystemOauthTokenRequest;
import com.alipay.api.request.AntMerchantExpandItemOpenCreateRequest;
import com.alipay.api.response.AlipayMerchantItemFileUploadResponse;
import com.alipay.api.response.AlipayMerchantOrderSyncResponse;
import com.alipay.api.response.AlipaySystemOauthTokenResponse;
import com.alipay.api.response.AntMerchantExpandItemOpenCreateResponse;
import lombok.SneakyThrows;
import org.junit.jupiter.api.Test;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import java.util.TimeZone;
public class AlipayMerchantItemFileUpload {
// 上传图片
@SneakyThrows
@Test
public void testExecuteFile1() {
AlipayConfig alipayConfig = new AlipayConfig();
alipayConfig.setServerUrl("https://openapi.alipay.com/gateway.do");
//填写自己的证书参数 这里我就不暴露了
alipayConfig.setAppId("");
alipayConfig.setPrivateKey("");
alipayConfig.setAppCertPath("");
alipayConfig.setAlipayPublicCertPath("");
alipayConfig.setRootCertPath("");
alipayConfig.setFormat("json");
alipayConfig.setCharset("UTF8");
alipayConfig.setSignType("RSA2");
AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
String imageBase64 = "iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAAEUlEQVR42mP4TyRgGFVIX4UAI/uOgGWVNeQAAAAASUVORK5CYII=";
AlipayMerchantItemFileUploadRequest request = new AlipayMerchantItemFileUploadRequest();
request.setScene("SYNC_ORDER");
FileItem fileContent = new FileItem("fileContent.jpg", Base64.getDecoder().decode(imageBase64));
request.setFileContent(fileContent);
AlipayMerchantItemFileUploadResponse response = alipayClient.certificateExecute(request);
System.out.println(response.getBody());
if (response.isSuccess()) {
System.out.println("素材标识............" + response.getMaterialKey());
System.out.println("调用成功");
} else {
System.out.println("调用失败");
}
}
// 创建商品
@SneakyThrows
@Test
public void testExecuteFile2() {
AlipayConfig alipayConfig = new AlipayConfig();
alipayConfig.setServerUrl("https://openapi.alipay.com/gateway.do");
alipayConfig.setAppId("");
alipayConfig.setPrivateKey("");
alipayConfig.setAppCertPath("");
alipayConfig.setAlipayPublicCertPath("");
alipayConfig.setRootCertPath("");
alipayConfig.setFormat("json");
alipayConfig.setCharset("UTF8");
alipayConfig.setSignType("RSA2");
AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
AntMerchantExpandItemOpenCreateRequest request = new AntMerchantExpandItemOpenCreateRequest();
request.setBizContent("{" +
"\"scene\":\"APP_ORDER\"," +
"\"target_id\":\"2017050307091553\"," +
"\"target_type\":\"8\"," +
"\"standard_category_id\":\"2019073100992228000000100000\"," +
"\"name\":\"西红柿炒蛋\"," +
"\"description\":\"精选西红柿与鸡蛋\"," +
"\"type\":\"STANDARD_GOODS\"," +
"\"ext_info\":[{" +
"\"ext_key\":\"DETAIL_URL\"," +
"\"ext_value\":\"pages/index/index\"" +
"}]," +
"\"sku_list\":[{" +
"\"price\":100," +
"\"original_price\":100" +
"}]," +
"\"material_list\":[{" +
"\"type\":\"ITEM_PIC\"," +
"\"content\":\"A*873fSYL0fWe0s-1CmMR7nAAAAeEuAQ\"" + // material_key
"}]" +
"}");
AntMerchantExpandItemOpenCreateResponse response = alipayClient.certificateExecute(request);
if (response.isSuccess()) {
System.out.println("商品ID...................." + response.getItemId());
System.out.println("调用成功");
} else {
System.out.println("调用失败");
}
}
//支付宝订单中心
@SneakyThrows
@Test
public void testExecuteFile3() {
AlipayConfig alipayConfig = new AlipayConfig();
alipayConfig.setServerUrl("https://openapi.alipay.com/gateway.do");
alipayConfig.setAppId("");
alipayConfig.setPrivateKey("");
alipayConfig.setAppCertPath("");
alipayConfig.setAlipayPublicCertPath("");
alipayConfig.setRootCertPath("");
alipayConfig.setFormat("json");
alipayConfig.setCharset("UTF8");
alipayConfig.setSignType("RSA2");
AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
AlipayMerchantOrderSyncRequest request = new AlipayMerchantOrderSyncRequest();
AlipayMerchantOrderSyncModel model = new AlipayMerchantOrderSyncModel();
model.setOrderModifiedTime(sdf.parse("2023-06-12 09:36:01.123"));
model.setOrderCreateTime(sdf.parse("2023-06-11 09:36:01.123"));
// 订单号
model.setOutBizNo("20210825134308699410928481");
// buyerId支付宝用户ID 用于卖家在支付宝客户端收到下单通知
model.setBuyerId("2088622935562221");
model.setOrderType("SERVICE_ORDER");
List<OrderExtInfo> extInfo = new ArrayList<>();
OrderExtInfo orderExtInfo = new OrderExtInfo();
// 支付宝订单中心状态 merchant_order_status
// 参数 创建CREATE 支付完成 PAID 已完成 FINISHED
orderExtInfo.setExtKey("merchant_order_status");
orderExtInfo.setExtValue("FINISHED");
extInfo.add(orderExtInfo);
OrderExtInfo orderExtInfo3 = new OrderExtInfo();
orderExtInfo3.setExtKey("merchant_biz_type");
orderExtInfo3.setExtValue("KX_SHOPPING");
extInfo.add(orderExtInfo3);
OrderExtInfo orderExtInfo4 = new OrderExtInfo();
orderExtInfo4.setExtKey("merchant_order_link_page");
// sn订单号 跳转小程序详情页
orderExtInfo4.setExtValue("/pages/order/orderDetail?sn=O202306121668132096968904705");
extInfo.add(orderExtInfo4);
OrderExtInfo orderExtInfo5 = new OrderExtInfo();
// appid
orderExtInfo5.setExtKey("tiny_app_id");
orderExtInfo5.setExtValue("2021003192684550");
extInfo.add(orderExtInfo5);
OrderExtInfo orderExtInfo6 = new OrderExtInfo();
orderExtInfo6.setExtKey("business_info");
orderExtInfo6.setExtValue("{\"isv_id\":\"208888888888888\",\"come_again\":\"alipays://platformapi/startapp?appId=2021002126602599page=%2fpages%2forder-details%2forder-details%3fticketOrderId%3d20210376854\"}");
extInfo.add(orderExtInfo6);
model.setExtInfo(extInfo);
List<ItemOrderInfo> itemOrderList = new ArrayList<>();
ItemOrderInfo itemOrderInfo = new ItemOrderInfo();
// 商品名称
itemOrderInfo.setItemName("西红柿炒蛋");
List<OrderExtInfo> extInfo2 = new ArrayList<>();
OrderExtInfo orderExtInfo2 = new OrderExtInfo();
orderExtInfo2.setExtKey("image_material_id");
// 商品图片素材 image_material_id
orderExtInfo2.setExtValue("2023060900502200000085941822");
extInfo2.add(orderExtInfo2);
itemOrderInfo.setExtInfo(extInfo2);
itemOrderList.add(itemOrderInfo);
model.setItemOrderList(itemOrderList);
request.setBizModel(model);
AlipayMerchantOrderSyncResponse response = alipayClient.certificateExecute(request);
System.out.println(response.getBody());
if (response.isSuccess()) {
System.out.println("调用成功");
} else {
System.out.println("调用失败");
}
}
//获取支付宝userId
@SneakyThrows
@Test
public void testExecuteFile5() {
AlipayConfig alipayConfig = new AlipayConfig();
alipayConfig.setServerUrl("");
alipayConfig.setAppId("");
alipayConfig.setPrivateKey("");
alipayConfig.setAppCertPath("");
alipayConfig.setAlipayPublicCertPath("");
alipayConfig.setRootCertPath("");
alipayConfig.setFormat("json");
alipayConfig.setCharset("UTF8");
alipayConfig.setSignType("RSA2");
AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
request.setGrantType("authorization_code");
request.setCode("3aa2812131664a60968e91cde76fBF22");
request.setRefreshToken("201208134b203fe6c11548bcabd8da5bb087a83b");
AlipaySystemOauthTokenResponse response = alipayClient.certificateExecute(request);
if (response.isSuccess()) {
System.out.println("调用成功");
} else {
System.out.println("调用失败");
}
}
}