|
| 1 | +package alipay |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + |
| 9 | + "github.com/go-pay/gopay" |
| 10 | +) |
| 11 | + |
| 12 | +// 换取授权访问令牌 |
| 13 | +// StatusCode = 200 is success |
| 14 | +func (a *ClientV3) SystemOauthToken(ctx context.Context, bm gopay.BodyMap) (aliRsp *SystemOauthTokenRsp, err error) { |
| 15 | + err = bm.CheckEmptyError("grant_type") |
| 16 | + if err != nil { |
| 17 | + return nil, err |
| 18 | + } |
| 19 | + authorization, err := a.authorization(MethodPost, v3SystemOauthToken, bm) |
| 20 | + if err != nil { |
| 21 | + return nil, err |
| 22 | + } |
| 23 | + res, bs, err := a.doPost(ctx, bm, v3SystemOauthToken, authorization) |
| 24 | + if err != nil { |
| 25 | + return nil, err |
| 26 | + } |
| 27 | + aliRsp = &SystemOauthTokenRsp{StatusCode: res.StatusCode} |
| 28 | + if res.StatusCode != http.StatusOK { |
| 29 | + if err = json.Unmarshal(bs, &aliRsp.ErrResponse); err != nil { |
| 30 | + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) |
| 31 | + } |
| 32 | + return aliRsp, nil |
| 33 | + } |
| 34 | + if err = json.Unmarshal(bs, aliRsp); err != nil { |
| 35 | + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) |
| 36 | + } |
| 37 | + return aliRsp, a.autoVerifySignByCert(res, bs) |
| 38 | +} |
| 39 | + |
| 40 | +// 身份认证记录查询 |
| 41 | +// StatusCode = 200 is success |
| 42 | +func (a *ClientV3) UserCertifyOpenQuery(ctx context.Context, bm gopay.BodyMap) (aliRsp *UserCertifyOpenQueryRsp, err error) { |
| 43 | + err = bm.CheckEmptyError("certify_id") |
| 44 | + if err != nil { |
| 45 | + return nil, err |
| 46 | + } |
| 47 | + uri := v3UserCertifyOpenQuery + "?" + bm.EncodeURLParams() |
| 48 | + authorization, err := a.authorization(MethodGet, uri, nil) |
| 49 | + if err != nil { |
| 50 | + return nil, err |
| 51 | + } |
| 52 | + res, bs, err := a.doGet(ctx, uri, authorization) |
| 53 | + if err != nil { |
| 54 | + return nil, err |
| 55 | + } |
| 56 | + aliRsp = &UserCertifyOpenQueryRsp{StatusCode: res.StatusCode} |
| 57 | + if res.StatusCode != http.StatusOK { |
| 58 | + if err = json.Unmarshal(bs, &aliRsp.ErrResponse); err != nil { |
| 59 | + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) |
| 60 | + } |
| 61 | + return aliRsp, nil |
| 62 | + } |
| 63 | + if err = json.Unmarshal(bs, aliRsp); err != nil { |
| 64 | + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) |
| 65 | + } |
| 66 | + return aliRsp, a.autoVerifySignByCert(res, bs) |
| 67 | +} |
| 68 | + |
| 69 | +// 身份认证初始化服务 |
| 70 | +// StatusCode = 200 is success |
| 71 | +func (a *ClientV3) UserCertifyOpenInitialize(ctx context.Context, bm gopay.BodyMap) (aliRsp *UserCertifyOpenInitializeRsp, err error) { |
| 72 | + err = bm.CheckEmptyError("outer_order_no", "biz_code", "identity_param") |
| 73 | + if err != nil { |
| 74 | + return nil, err |
| 75 | + } |
| 76 | + authorization, err := a.authorization(MethodPost, v3UserCertifyOpenInitialize, bm) |
| 77 | + if err != nil { |
| 78 | + return nil, err |
| 79 | + } |
| 80 | + res, bs, err := a.doPost(ctx, bm, v3UserCertifyOpenInitialize, authorization) |
| 81 | + if err != nil { |
| 82 | + return nil, err |
| 83 | + } |
| 84 | + aliRsp = &UserCertifyOpenInitializeRsp{StatusCode: res.StatusCode} |
| 85 | + if res.StatusCode != http.StatusOK { |
| 86 | + if err = json.Unmarshal(bs, &aliRsp.ErrResponse); err != nil { |
| 87 | + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) |
| 88 | + } |
| 89 | + return aliRsp, nil |
| 90 | + } |
| 91 | + if err = json.Unmarshal(bs, aliRsp); err != nil { |
| 92 | + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) |
| 93 | + } |
| 94 | + return aliRsp, a.autoVerifySignByCert(res, bs) |
| 95 | +} |
| 96 | + |
| 97 | +// 支付宝会员授权信息查询接口 |
| 98 | +// StatusCode = 200 is success |
| 99 | +func (a *ClientV3) UserInfoShare(ctx context.Context, bm gopay.BodyMap) (aliRsp *UserInfoShareRsp, err error) { |
| 100 | + err = bm.CheckEmptyError("avatar", "city", "nick_name", "province") |
| 101 | + if err != nil { |
| 102 | + return nil, err |
| 103 | + } |
| 104 | + authorization, err := a.authorization(MethodPost, v3UserInfoShare, bm) |
| 105 | + if err != nil { |
| 106 | + return nil, err |
| 107 | + } |
| 108 | + res, bs, err := a.doPost(ctx, bm, v3UserInfoShare, authorization) |
| 109 | + if err != nil { |
| 110 | + return nil, err |
| 111 | + } |
| 112 | + aliRsp = &UserInfoShareRsp{StatusCode: res.StatusCode} |
| 113 | + if res.StatusCode != http.StatusOK { |
| 114 | + if err = json.Unmarshal(bs, &aliRsp.ErrResponse); err != nil { |
| 115 | + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) |
| 116 | + } |
| 117 | + return aliRsp, nil |
| 118 | + } |
| 119 | + if err = json.Unmarshal(bs, aliRsp); err != nil { |
| 120 | + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) |
| 121 | + } |
| 122 | + return aliRsp, a.autoVerifySignByCert(res, bs) |
| 123 | +} |
| 124 | + |
| 125 | +// 用户授权关系查询 |
| 126 | +// StatusCode = 200 is success |
| 127 | +func (a *ClientV3) UserAuthRelationshipQuery(ctx context.Context, bm gopay.BodyMap) (aliRsp *UserAuthRelationshipQueryRsp, err error) { |
| 128 | + err = bm.CheckEmptyError("scopes") |
| 129 | + if err != nil { |
| 130 | + return nil, err |
| 131 | + } |
| 132 | + uri := v3UserAuthRelationshipQuery + "?" + bm.EncodeURLParams() |
| 133 | + authorization, err := a.authorization(MethodGet, uri, nil) |
| 134 | + if err != nil { |
| 135 | + return nil, err |
| 136 | + } |
| 137 | + res, bs, err := a.doGet(ctx, uri, authorization) |
| 138 | + if err != nil { |
| 139 | + return nil, err |
| 140 | + } |
| 141 | + aliRsp = &UserAuthRelationshipQueryRsp{StatusCode: res.StatusCode} |
| 142 | + if res.StatusCode != http.StatusOK { |
| 143 | + if err = json.Unmarshal(bs, &aliRsp.ErrResponse); err != nil { |
| 144 | + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) |
| 145 | + } |
| 146 | + return aliRsp, nil |
| 147 | + } |
| 148 | + if err = json.Unmarshal(bs, aliRsp); err != nil { |
| 149 | + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) |
| 150 | + } |
| 151 | + return aliRsp, a.autoVerifySignByCert(res, bs) |
| 152 | +} |
| 153 | + |
| 154 | +// 查询解除授权明细 |
| 155 | +// StatusCode = 200 is success |
| 156 | +func (a *ClientV3) UserDelOauthDetailQuery(ctx context.Context, bm gopay.BodyMap) (aliRsp *UserDelOauthDetailQueryRsp, err error) { |
| 157 | + err = bm.CheckEmptyError("date", "limit", "offset") |
| 158 | + if err != nil { |
| 159 | + return nil, err |
| 160 | + } |
| 161 | + authorization, err := a.authorization(MethodPost, v3UserDelOauthDetailQuery, bm) |
| 162 | + if err != nil { |
| 163 | + return nil, err |
| 164 | + } |
| 165 | + res, bs, err := a.doPost(ctx, bm, v3UserDelOauthDetailQuery, authorization) |
| 166 | + if err != nil { |
| 167 | + return nil, err |
| 168 | + } |
| 169 | + aliRsp = &UserDelOauthDetailQueryRsp{StatusCode: res.StatusCode} |
| 170 | + if res.StatusCode != http.StatusOK { |
| 171 | + if err = json.Unmarshal(bs, &aliRsp.ErrResponse); err != nil { |
| 172 | + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) |
| 173 | + } |
| 174 | + return aliRsp, nil |
| 175 | + } |
| 176 | + if err = json.Unmarshal(bs, aliRsp); err != nil { |
| 177 | + return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs)) |
| 178 | + } |
| 179 | + return aliRsp, a.autoVerifySignByCert(res, bs) |
| 180 | +} |
0 commit comments