Passed
Push — master ( 0d4398...aeb420 )
by Dmitry
14:21
created

Leads::createLeadDownloadTask()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 12
rs 10
cc 1
nc 1
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Promopult\TikTokMarketingApi\Service;
6
7
final class Leads extends \Promopult\TikTokMarketingApi\AbstractService
8
{
9
    /**
10
     * Create test lead
11
     *
12
     * After the subscription, you can use the endpoints below to perform an end-to-end testing for leads generation,
13
     * and clear the testing lead.
14
     *
15
     * @param int $advertiserId
16
     * @param int $pageId
17
     * @return array
18
     *
19
     * @throws \Throwable
20
     *
21
     * @see https://ads.tiktok.com/marketing_api/docs?id=1701890943400962
22
     */
23
    public function createTestLead(
24
        int $advertiserId,
25
        int $pageId
26
    ): array {
27
        return $this->requestApi(
28
            'POST',
29
            '/open_api/v1.2/pages/leads/mock/create/',
30
            [
31
                'advertiser_id' => $advertiserId,
32
                'page_id' => $pageId
33
            ]
34
        );
35
    }
36
37
    /**
38
     * Get test leads
39
     *
40
     * You can use this endpoint to get a test lead.
41
     *
42
     * @param int $advertiserId
43
     * @param int $pageId
44
     * @return array
45
     *
46
     * @throws \Throwable
47
     *
48
     * @see https://ads.tiktok.com/marketing_api/docs?id=1709486980307969
49
     */
50
    public function getTestLeads(
51
        int $advertiserId,
52
        int $pageId
53
    ): array {
54
        return $this->requestApi(
55
            'GET',
56
            '/open_api/v1.2/pages/leads/mock/get/',
57
            [
58
                'advertiser_id' => $advertiserId,
59
                'page_id' => $pageId
60
            ]
61
        );
62
    }
63
64
    /**
65
     * Delete a test lead
66
     *
67
     * You can use this endpoint to delete an existing test lead.
68
     *
69
     * @param int $advertiserId
70
     * @param int $leadId
71
     * @return array
72
     *
73
     * @throws \Throwable
74
     *
75
     * @see https://ads.tiktok.com/marketing_api/docs?id=1709487026425858
76
     */
77
    public function deleteTestLead(
78
        int $advertiserId,
79
        int $leadId
80
    ): array {
81
        return $this->requestApi(
82
            'POST',
83
            '/open_api/v1.2/pages/leads/mock/delete/',
84
            [
85
                'advertiser_id' => $advertiserId,
86
                'lead_id' => $leadId
87
            ]
88
        );
89
    }
90
91
    /**
92
     * Create lead download task
93
     *
94
     * @param int $advertiserId
95
     * @param int|null $adId
96
     * @param int|null $pageId
97
     * @return array
98
     *
99
     * @throws \Throwable
100
     *
101
     * @see https://ads.tiktok.com/marketing_api/docs?id=1701890942344193
102
     */
103
    public function createLeadDownloadTask(
104
        int $advertiserId,
105
        ?int $adId = null,
106
        ?int $pageId = null
107
    ): array {
108
        return $this->requestApi(
109
            'POST',
110
            '/open_api/v1.2/pages/leads/task/',
111
            [
112
                'advertiser_id' => $advertiserId,
113
                'ad_id' => $adId,
114
                'page_id' => $pageId,
115
            ]
116
        );
117
    }
118
119
    /**
120
     * Get lead download task for status check
121
     *
122
     * Please note that the leads file will expire in 10 minutes after it was generated. You will get a file not exist
123
     * error if the file has expired.
124
     *
125
     * @param int $advertiserId
126
     * @param int $taskId
127
     * @return array
128
     *
129
     * @throws \Throwable
130
     *
131
     * @see https://ads.tiktok.com/marketing_api/docs?id=1701890942344193
132
     */
133
    public function getLeadDownloadTask(
134
        int $advertiserId,
135
        int $taskId
136
    ): array {
137
        return $this->requestApi(
138
            'POST',
139
            '/open_api/v1.2/pages/leads/task/',
140
            [
141
                'advertiser_id' => $advertiserId,
142
                'task_id' => $taskId
143
            ]
144
        );
145
    }
146
147
    /**
148
     * Download leads
149
     *
150
     * @param int $advertiserId
151
     * @param int $taskId
152
     * @return array | string
153
     *
154
     * @throws \Throwable
155
     *
156
     * @see https://ads.tiktok.com/marketing_api/docs?id=1709486183758850
157
     */
158
    public function downloadLeads(
159
        int $advertiserId,
160
        int $taskId
161
    ) {
162
        $url = $this->credentials->getApiBaseUrl()
163
            . '/open_api/v1.2/pages/leads/task/download/'
164
            . '?' . $this->prepareGetParams([
165
                'advertiser_id' => $advertiserId,
166
                'task_id' => $taskId
167
            ]);
168
169
        $headers = [
170
            'Access-Token' => $this->credentials->getAccessToken(),
171
        ];
172
173
        $request = new \GuzzleHttp\Psr7\Request(
174
            'GET',
175
            $url,
176
            $headers,
177
            null
178
        );
179
180
        $response = $this->httpClient->sendRequest($request);
181
182
        $decodedJson = \json_decode(
183
            (string) $response->getBody(),
184
            true
185
        );
186
187
        if (
188
            isset($decodedJson['code'])
189
            && $decodedJson['code'] != 0
190
        ) {
191
            throw new \Promopult\TikTokMarketingApi\Exception\ErrorResponse(
192
                (int) $decodedJson['code'],
193
                $decodedJson['message'],
194
                $request,
195
                $response
196
            );
197
        }
198
199
        return $decodedJson;
200
    }
201
202
    /**
203
     * Subscribe to leads
204
     *
205
     * You can subscribe to a page (instant form) to get leads generated from the page. If you do not want to receive
206
     * leads from a page any more, you can cancel the subscription.
207
     *
208
     * You can also get all subscriptions for an advertiser.
209
     *
210
     * You can subscribe to leads that are generated from a lead ad (instant form). Subscriptions are organized based on
211
     * Subcription IDs (subscription_id), not page IDs (page_id). Subscriptions are independent. A page can be included
212
     * in multiple subscriptions. Subscription status changes of a page in one subscription do not affect the
213
     * subscription status of this page in another subscription.
214
     *
215
     * @param string $appId
216
     * @param string $secret
217
     * @param array $subscriptionDetail
218
     * @param string $url
219
     * @param string $object
220
     * @return array
221
     *
222
     * @throws \Throwable
223
     *
224
     * @see https://ads.tiktok.com/marketing_api/docs?id=1701890942854146
225
     */
226
    public function subscribeToLeads(
227
        int $appId,
228
        string $secret,
229
        array $subscriptionDetail,
230
        string $url,
231
        string $object = 'LEAD'
232
    ): array {
233
        return $this->requestApi(
234
            'POST',
235
            '/open_api/v1.2/subscription/subscribe/',
236
            [
237
                'app_id' => $appId,
238
                'secret' => $secret,
239
                'subscription_detail' => $subscriptionDetail,
240
                'object' => $object,
241
                'url' => $url
242
            ]
243
        );
244
    }
245
246
    /**
247
     * Cancel subscription
248
     *
249
     * @param int $appId
250
     * @param string $secret
251
     * @param int $subscriptionId
252
     * @return array
253
     *
254
     * @throws \Throwable
255
     *
256
     * @see https://ads.tiktok.com/marketing_api/docs?id=1709486460752897
257
     */
258
    public function cancelSubscription(
259
        int $appId,
260
        string $secret,
261
        int $subscriptionId
262
    ): array {
263
        return $this->requestApi(
264
            'POST',
265
            '/open_api/v1.2/subscription/unsubscribe/',
266
            [
267
                'app_id' => $appId,
268
                'secret' => $secret,
269
                'subscription_id' => $subscriptionId,
270
            ]
271
        );
272
    }
273
}
274