Skip to content

Commit b311483

Browse files
authored
Merge pull request #40 from BelniakMedia/main
Adds shopper_activity v3 end points
2 parents 98a5b39 + 1a63bb2 commit b311483

3 files changed

Lines changed: 77 additions & 21 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
/vendor
33
/composer.lock
44
/.vscode
5+
/.idea
56
/.phpunit.result.cache

src/Client.php

Lines changed: 73 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Drip\Exception\InvalidAccessTokenException;
99
use Drip\Exception\InvalidAccountIdException;
1010
use Drip\Exception\UnexpectedHttpVerbException;
11+
use Exception;
1112

1213
/**
1314
* Drip API
@@ -48,7 +49,7 @@ class Client
4849
* * `account_id` e.g. "123456"
4950
* * `api_end_point` (mostly for Drip internal testing)
5051
* * `guzzle_stack_constructor` (for test suite, may break at any time, do not use)
51-
* @throws \Exception
52+
* @throws Exception
5253
*/
5354
public function __construct(...$params)
5455
{
@@ -84,7 +85,7 @@ public function __construct(...$params)
8485
* @param array{api_end_point?:string, guzzle_stack_constructor?:callable} $options
8586
* * `api_end_point` (for test suite)
8687
* * `guzzle_stack_constructor` (for test suite)
87-
* @throws \Exception
88+
* @throws Exception
8889
*/
8990
protected function deprecated_constructor($api_key, $account_id, $options = [])
9091
{
@@ -95,7 +96,7 @@ protected function deprecated_constructor($api_key, $account_id, $options = [])
9596

9697
/**
9798
* @param string $api_key
98-
* @throws \Exception
99+
* @throws Exception
99100
*/
100101
protected function basic_auth_setup($api_key)
101102
{
@@ -109,7 +110,7 @@ protected function basic_auth_setup($api_key)
109110

110111
/**
111112
* @param string $access_token
112-
* @throws \Exception
113+
* @throws Exception
113114
*/
114115
protected function bearer_auth_setup($access_token)
115116
{
@@ -123,7 +124,7 @@ protected function bearer_auth_setup($access_token)
123124

124125
/**
125126
* @param string $account_id
126-
* @throws \Exception
127+
* @throws Exception
127128
*/
128129
protected function set_account_id($account_id)
129130
{
@@ -159,7 +160,7 @@ protected function set_test_options($options)
159160
*
160161
* @param array{status?:string} $params Set of arguments
161162
* - status (optional)
162-
* @return \Drip\ResponseInterface
163+
* @return ResponseInterface
163164
* @throw \Drip\Exception\InvalidArgumentException
164165
*/
165166
public function get_campaigns($params)
@@ -176,7 +177,7 @@ public function get_campaigns($params)
176177
*
177178
* @param array{campaign_id?:string} $params Set of arguments
178179
* - campaign_id (required)
179-
* @return \Drip\ResponseInterface
180+
* @return ResponseInterface
180181
*/
181182
public function fetch_campaign($params)
182183
{
@@ -194,7 +195,7 @@ public function fetch_campaign($params)
194195
* Requests the accounts for the given account.
195196
* Parses the response JSON and returns an array which contains: id, name, created_at etc
196197
*
197-
* @return \Drip\ResponseInterface
198+
* @return ResponseInterface
198199
*/
199200
public function get_accounts()
200201
{
@@ -205,7 +206,7 @@ public function get_accounts()
205206
* Sends a request to add a subscriber and returns its record or false.
206207
*
207208
* @param array<mixed> $params
208-
* @return \Drip\ResponseInterface
209+
* @return ResponseInterface
209210
*/
210211
public function create_or_update_subscriber($params)
211212
{
@@ -221,7 +222,7 @@ public function create_or_update_subscriber($params)
221222
* Sends a request to add/update a batch (up to 1000) of subscribers.
222223
*
223224
* @param array<mixed> $params
224-
* @return \Drip\ResponseInterface
225+
* @return ResponseInterface
225226
*/
226227
public function create_or_update_subscribers($params)
227228
{
@@ -236,7 +237,7 @@ public function create_or_update_subscribers($params)
236237
* Returns info regarding a particular subscriber
237238
*
238239
* @param array<mixed> $params
239-
* @return \Drip\ResponseInterface
240+
* @return ResponseInterface
240241
* @throw \Drip\Exception\InvalidArgumentException
241242
*/
242243
public function fetch_subscriber($params)
@@ -260,7 +261,7 @@ public function fetch_subscriber($params)
260261
* Returns info regarding a particular subscriber subscriptions to campaigns.
261262
*
262263
* @param array<mixed> $params
263-
* @return \Drip\ResponseInterface
264+
* @return ResponseInterface
264265
* @throw \Drip\Exception\InvalidArgumentException
265266
*/
266267
public function fetch_subscriber_campaigns($params)
@@ -283,7 +284,7 @@ public function fetch_subscriber_campaigns($params)
283284
/**
284285
* Returns a list of subscribers
285286
*
286-
* @return \Drip\ResponseInterface
287+
* @return ResponseInterface
287288
*/
288289
public function fetch_subscribers()
289290
{
@@ -294,7 +295,7 @@ public function fetch_subscribers()
294295
* Subscribes a user to a given campaign for a given account.
295296
*
296297
* @param array<mixed> $params
297-
* @return \Drip\ResponseInterface
298+
* @return ResponseInterface
298299
* @throw \Drip\Exception\InvalidArgumentException
299300
*/
300301
public function subscribe_subscriber($params)
@@ -327,7 +328,7 @@ public function subscribe_subscriber($params)
327328
* Some keys are removed from the params so they don't get send with the other data to Drip.
328329
*
329330
* @param array<mixed> $params
330-
* @return \Drip\ResponseInterface
331+
* @return ResponseInterface
331332
* @throw \Drip\Exception\InvalidArgumentException
332333
*/
333334
public function unsubscribe_subscriber($params)
@@ -354,7 +355,7 @@ public function unsubscribe_subscriber($params)
354355
* This calls DELETE /:account_id/subscribers/:id_or_email to delete a subscriber.
355356
*
356357
* @param array<mixed> $params
357-
* @return \Drip\ResponseInterface
358+
* @return ResponseInterface
358359
* @throw \Drip\Exception\InvalidArgumentException
359360
*/
360361
public function delete_subscriber($params)
@@ -377,7 +378,7 @@ public function delete_subscriber($params)
377378
* This calls POST /:account_id/tags to add the tag. It just returns some status code no content
378379
*
379380
* @param array<mixed> $params
380-
* @return \Drip\ResponseInterface
381+
* @return ResponseInterface
381382
* @throw \Drip\Exception\InvalidArgumentException
382383
*/
383384
public function tag_subscriber($params)
@@ -400,7 +401,7 @@ public function tag_subscriber($params)
400401
* This calls DELETE /:account_id/tags to remove the tags. It just returns some status code no content
401402
*
402403
* @param array $params
403-
* @return \Drip\ResponseInterface
404+
* @return ResponseInterface
404405
* @throw \Drip\Exception\InvalidArgumentException
405406
*/
406407
public function untag_subscriber($params)
@@ -423,7 +424,7 @@ public function untag_subscriber($params)
423424
* Posts an event specified by the user.
424425
*
425426
* @param array $params
426-
* @return \Drip\ResponseInterface
427+
* @return ResponseInterface
427428
* @throw \Drip\Exception\InvalidArgumentException
428429
*/
429430
public function record_event($params)
@@ -438,6 +439,57 @@ public function record_event($params)
438439
return $this->make_request("{$this->account_id}/events", $req_params, self::POST);
439440
}
440441

442+
/**
443+
* Posts a v3 Shopper Activity event to signal a shopping cart has been created or updated.
444+
* Valid actions: created or updated
445+
* @see https://developer.drip.com/#cart-activity
446+
* @param array $params
447+
* @return ResponseInterface
448+
* @throw \Drip\Exception\InvalidArgumentException
449+
* @throws Exception
450+
*/
451+
public function create_or_update_cart($params)
452+
{
453+
if(strstr($this->api_end_point, '/v3') === false) {
454+
throw new Exception(__CLASS__ . '::' . __METHOD__ . ' only supports the APIv3 endpoint.');
455+
}
456+
return $this->make_request("$this->account_id/shopper_activity/cart", $params, self::POST);
457+
}
458+
459+
/**
460+
* Posts a v3 Shopper Activity event to signal an order has been placed or moddifed.
461+
* Valid actions: placed, updated, paid, fulfilled, refunded, or canceled
462+
* @see https://developer.drip.com/#order-activity
463+
* @param array $params
464+
* @return ResponseInterface
465+
* @throw \Drip\Exception\InvalidArgumentException
466+
* @throws Exception
467+
*/
468+
public function create_or_update_order($params)
469+
{
470+
if(strstr($this->api_end_point, '/v3') === false) {
471+
throw new Exception(__CLASS__ . '::' . __METHOD__ . ' only supports the APIv3 endpoint.');
472+
}
473+
return $this->make_request("$this->account_id/shopper_activity/order", $params, self::POST);
474+
}
475+
476+
/**
477+
* Posts a v3 Shopper Activity event to signal a product has been created, updated or deleted.
478+
* Valid actions: created, updated or deleted
479+
* @see https://developer.drip.com/#product-activity
480+
* @param array $params
481+
* @return ResponseInterface
482+
* @throw \Drip\Exception\InvalidArgumentException
483+
* @throws Exception
484+
*/
485+
public function create_or_update_product($params)
486+
{
487+
if(strstr($this->api_end_point, '/v3') === false) {
488+
throw new Exception(__CLASS__ . '::' . __METHOD__ . ' only supports the APIv3 endpoint.');
489+
}
490+
return $this->make_request("$this->account_id/shopper_activity/product", $params, self::POST);
491+
}
492+
441493
/**
442494
* @return string
443495
*/
@@ -463,8 +515,8 @@ protected function is_success_response($code)
463515
* @param string $url
464516
* @param array<mixed> $params
465517
* @param string $req_method
466-
* @return \Drip\ResponseInterface
467-
* @throws \Exception
518+
* @return ResponseInterface
519+
* @throws Exception
468520
*/
469521
protected function make_request($url, $params = [], $req_method = self::GET)
470522
{

tests/ClientTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,9 @@ public function testUntagSubscriberWithoutTag()
510510
$response = $client->untag_subscriber(['email' => 'test@example.com']);
511511
}
512512

513+
// Shopper Activity (v3)
514+
// Todo add tests for the create_or_update_cart, create_or_update_order, and create_or_update_product methods.
515+
513516
////////////////////////// E V E N T S //////////////////////////
514517

515518
// #record_event

0 commit comments

Comments
 (0)