chengkun
2025-06-05 4080b5997b38ca84b3b203c7101dcadb97b76925
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package com.alipay.easysdk.util.generic;
 
import com.alipay.easysdk.TestAccount.Mini;
import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.kernel.util.ResponseChecker;
import com.alipay.easysdk.util.generic.models.AlipayOpenApiGenericResponse;
import com.alipay.easysdk.util.generic.models.AlipayOpenApiGenericSDKResponse;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
 
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
 
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;
 
public class ClientTest {
 
    @Before
    public void setUp() {
        Factory.setOptions(Mini.CONFIG);
    }
 
    @Test
    public void testSDKExecute() throws Exception {
        Map<String, Object> bizParams = new HashMap<>();
        bizParams.put("subject", "iPhone6 16G");
        bizParams.put("out_trade_no", "f4833085-0c46-4bb0-8e5f-622a02a4cffc");
        bizParams.put("total_amount", "0.10");
 
        java.util.Map<String, String> textParams = new java.util.HashMap<>();
 
        AlipayOpenApiGenericSDKResponse response = Factory.Util.Generic().sdkExecute("alipay.trade.app.pay", textParams, bizParams);
 
        Assert.assertThat(ResponseChecker.success(response), is(true));
        Assert.assertThat(response.body, containsString("app_id=2019022663440152&biz_content=%7B%22"
                + "out_trade_no%22%3A%22f4833085-0c46-4bb0-8e5f-622a02a4cffc%22%2C%22"
                + "total_amount%22%3A%220.10%22%2C%22subject%22%3A%22iPhone6+16G%22%7D&"
                + "charset=UTF-8&format=json&method=alipay.trade.app.pay"
                + "&notify_url=https%3A%2F%2Fwww.test.com%2Fcallback&sign="));
    }
 
    @Test
    public void testFileExecute() throws Exception {
        Map<String, String> textParams = new HashMap<>();
        textParams.put("image_type", "png");
        textParams.put("image_name", "海底捞");
        textParams.put("image_pid", "22088021822217233");
 
        Map<String, String> fileParams = new HashMap<>();
        fileParams.put("image_content", "src/test/resources/fixture/sample.png");
 
        AlipayOpenApiGenericResponse response = Factory.Util.Generic().fileExecute("alipay.offline.material.image.upload", textParams, null, fileParams);
 
        assertThat(ResponseChecker.success(response), is(true));
        assertThat(response.code, is("10000"));
        assertThat(response.msg, is("Success"));
        assertThat(response.subCode, is(nullValue()));
        assertThat(response.subMsg, is(nullValue()));
        assertThat(response.httpBody, not(nullValue()));
 
    }
 
 
    @Test
    public void testExecuteWithoutAppAuthToken() throws Exception {
        String outTradeNo = UUID.randomUUID().toString();
        AlipayOpenApiGenericResponse response = Factory.Util.Generic().execute(
                "alipay.trade.create", null, getBizParams(outTradeNo));
 
        assertThat(ResponseChecker.success(response), is(true));
        assertThat(response.code, is("10000"));
        assertThat(response.msg, is("Success"));
        assertThat(response.subCode, is(nullValue()));
        assertThat(response.subMsg, is(nullValue()));
        assertThat(response.httpBody, not(nullValue()));
    }
 
    @Test
    public void testExecuteWithAppAuthToken() throws Exception {
        String outTradeNo = UUID.randomUUID().toString();
        AlipayOpenApiGenericResponse response = Factory.Util.Generic().execute(
                "alipay.trade.create", getTextParams(), getBizParams(outTradeNo));
 
        assertThat(ResponseChecker.success(response), is(false));
        assertThat(response.code, is("20001"));
        assertThat(response.msg, is("Insufficient Token Permissions"));
        assertThat(response.subCode, is("aop.invalid-app-auth-token"));
        assertThat(response.subMsg, is("无效的应用授权令牌"));
        assertThat(response.httpBody, not(nullValue()));
    }
 
    private Map<String, String> getTextParams() {
        Map<String, String> bizParams = new HashMap<>();
        bizParams.put("app_auth_token", "201712BB_D0804adb2e743078d1822d536956X34");
        return bizParams;
    }
 
    private Map<String, Object> getBizParams(String outTradeNo) {
        Map<String, Object> bizParams = new HashMap<>();
        bizParams.put("subject", "Iphone6 16G");
        bizParams.put("out_trade_no", outTradeNo);
        bizParams.put("total_amount", "0.10");
        bizParams.put("buyer_id", "2088002656718920");
        bizParams.put("extend_params", getHuabeiParams());
        return bizParams;
    }
 
    private Map<String, String> getHuabeiParams() {
        Map<String, String> extendParams = new HashMap<>();
        extendParams.put("hb_fq_num", "3");
        extendParams.put("hb_fq_seller_percent", "3");
        return extendParams;
    }
}