Skip to content

Add Webhook functions #48

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 115 additions & 0 deletions src/ConvertKit_API.php
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,121 @@ public function get_subscriber_tags(int $subscriber_id)
);
}

/**
* Returns all created webhooks.
*
* @since 1.0.0
*
* @return false|object
*/
public function get_webhooks()
{
return $this->get(
'automations/hooks',
[
'api_secret' => $this->api_secret,
],
);
}

/**
* Creates a webhook that will be called based on the chosen event types.
*
* @param string $url URL to receive event.
* @param string $event Event to subscribe to.
* @param string $parameter Optional parameter depending on the event.
*
* @since 1.0.0
*
* @see https://developers.convertkit.com/#create-a-webhook
*
* @throws \InvalidArgumentException If the event is not supported.
*
* @return false|object
*/
public function create_webhook(string $url, string $event, string $parameter = '')
{
// Depending on the event, build the required event array structure.
switch ($event) {
case 'subscriber.subscriber_activate':
case 'subscriber.subscriber_unsubscribe':
case 'purchase.purchase_create':
$eventData = ['name' => $event];
break;

case 'subscriber.form_subscribe':
$eventData = [
'name' => $event,
'form_id' => $parameter,
];
break;

case 'subscriber.course_subscribe':
case 'subscriber.course_complete':
$eventData = [
'name' => $event,
'course_id' => $parameter,
];
break;

case 'subscriber.link_click':
$eventData = [
'name' => $event,
'initiator_value' => $parameter,
];
break;

case 'subscriber.product_purchase':
$eventData = [
'name' => $event,
'product_id' => $parameter,
];
break;

case 'subscriber.tag_add':
case 'subscriber.tag_remove':
$eventData = [
'name' => $event,
'tag_id' => $parameter,
];
break;

default:
throw new \InvalidArgumentException(sprintf('The event %s is not supported', $event));
}//end switch

// Send request.
return $this->post(
'automations/hooks',
[
'api_secret' => $this->api_secret,
'target_url' => $url,
'event' => $eventData,
]
);
}

/**
* Deletes an existing webhook.
*
* @param integer $rule_id Rule ID.
*
* @since 1.0.0
*
* @see https://developers.convertkit.com/#destroy-webhook
*
* @return false|object
*/
public function destroy_webhook(int $rule_id)
{
return $this->delete(
sprintf('automations/hooks/%s', $rule_id),
[
'api_secret' => $this->api_secret,
]
);
}

/**
* List custom fields.
*
Expand Down
102 changes: 102 additions & 0 deletions tests/ConvertKitAPITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1237,6 +1237,108 @@ public function testGetSubscriberTagsWithInvalidSubscriberID()
$subscriber = $this->api->get_subscriber_tags(12345);
}

/**
* Test that get_webhooks() returns the expected data.
*
* @since 1.0.0
*
* @return void
*/
public function testGetWebhooks()
{
// Create a webhook first.
$result = $this->api->create_webhook(
'https://webhook.site/2705fef6-34ef-4252-9c78-d511c540b58d',
'subscriber.subscriber_activate',
);
$ruleID = $result->rule->id;

// List webhooks, confirming the webhook created exists in the list.
$webhooks = $this->api->get_webhooks();
$this->assertIsArray($webhooks);

// Destroy the webhook.
$result = $this->api->destroy_webhook($ruleID);
$this->assertEquals($result->success, true);
}

/**
* Test that create_webhook() and destroy_webhook() works.
*
* We do both, so we don't end up with unnecessary webhooks remaining
* on the ConvertKit account when running tests.
*
* @since 1.0.0
*
* @return void
*/
public function testCreateAndDestroyWebhook()
{
// Create a webhook first.
$result = $this->api->create_webhook(
'https://webhook.site/2705fef6-34ef-4252-9c78-d511c540b58d',
'subscriber.subscriber_activate',
);
$ruleID = $result->rule->id;

// Destroy the webhook.
$result = $this->api->destroy_webhook($ruleID);
$this->assertEquals($result->success, true);
}

/**
* Test that create_webhook() and destroy_webhook() works with an event parameter.
*
* We do both, so we don't end up with unnecessary webhooks remaining
* on the ConvertKit account when running tests.
*
* @since 1.0.0
*
* @return void
*/
public function testCreateAndDestroyWebhookWithEventParameter()
{
// Create a webhook first.
$result = $this->api->create_webhook(
'https://webhook.site/2705fef6-34ef-4252-9c78-d511c540b58d',
'subscriber.form_subscribe',
$_ENV['CONVERTKIT_API_FORM_ID']
);
$ruleID = $result->rule->id;

// Destroy the webhook.
$result = $this->api->destroy_webhook($ruleID);
$this->assertEquals($result->success, true);
}

/**
* Test that create_webhook() throws an InvalidArgumentException when an invalid
* event is specified.
*
* @since 1.0.0
*
* @return void
*/
public function testCreateWebhookWithInvalidEvent()
{
$this->expectException(\InvalidArgumentException::class);
$this->api->create_webhook('https://webhook.site/2705fef6-34ef-4252-9c78-d511c540b58d', 'invalid.event');
}

/**
* Test that destroy_webhook() throws a ClientException when an invalid
* rule ID is specified.
*
* @since 1.0.0
*
* @return void
*/
public function testDestroyWebhookWithInvalidRuleID()
{
$this->expectException(GuzzleHttp\Exception\ClientException::class);
$this->api->destroy_webhook(12345);
}

/**
* Test that get_custom_fields() returns the expected data.
*
Expand Down