Skip to content

Commit

Permalink
Merge pull request #126 from winkelco/main
Browse files Browse the repository at this point in the history
Add missing parameters to webhook and set nullable fields in contact object
  • Loading branch information
aalbarca authored Aug 12, 2023
2 parents 6868a86 + c43c047 commit 08572c6
Show file tree
Hide file tree
Showing 8 changed files with 230 additions and 9 deletions.
10 changes: 5 additions & 5 deletions src/WebHook/Notification/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ public function firstName(): string

public function lastName(): string
{
return $this->name['last_name'];
return $this->name['last_name'] ?? '';
}

public function middleName(): string
{
return $this->name['middle_name'];
return $this->name['middle_name'] ?? '';
}

public function addresses(): array
Expand All @@ -88,17 +88,17 @@ public function company(): array

public function companyName(): string
{
return $this->company['company'];
return $this->company['company'] ?? '';
}

public function companyDepartment(): string
{
return $this->company['department'];
return $this->company['department'] ?? '';
}

public function companyTitle(): string
{
return $this->company['title'];
return $this->company['title'] ?? '';
}

public function phones(): array
Expand Down
18 changes: 18 additions & 0 deletions src/WebHook/Notification/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,28 @@ final class Media extends MessageNotification

private string $mime_type;

private string $sha256;

private string $filename;

private string $caption;

public function __construct(
string $id,
Support\Business $business,
string $image_id,
string $mime_type,
string $sha256,
string $filename,
string $caption,
string $received_at_timestamp
) {
parent::__construct($id, $business, $received_at_timestamp);

$this->image_id = $image_id;
$this->mime_type = $mime_type;
$this->sha256 = $sha256;
$this->filename = $filename;
$this->caption = $caption;
}

Expand All @@ -35,6 +43,16 @@ public function mimeType(): string
return $this->mime_type;
}

public function sha256(): string
{
return $this->sha256;
}

public function filename(): string
{
return $this->filename;
}

public function caption(): string
{
return $this->caption;
Expand Down
2 changes: 2 additions & 0 deletions src/WebHook/Notification/MessageNotificationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ private function buildMessageNotification(array $metadata, array $message): Mess
new Support\Business($metadata['phone_number_id'], $metadata['display_phone_number']),
$message[$message['type']]['id'],
$message[$message['type']]['mime_type'],
$message[$message['type']]['sha256'],
$message[$message['type']]['filename'] ?? '',
$message[$message['type']]['caption'] ?? '',
$message['timestamp']
);
Expand Down
36 changes: 36 additions & 0 deletions src/WebHook/Notification/StatusNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ final class StatusNotification extends Notification
{
private ?Support\Conversation $conversation = null;

private ?Support\Pricing $pricing = null;

private string $customer_id;

private Support\Status $status;
Expand All @@ -34,6 +36,13 @@ public function withConversation(Support\Conversation $conversation): self
return $this;
}

public function withPricing(Support\Pricing $pricing): self
{
$this->pricing = $pricing;

return $this;
}

public function withError(Support\Error $error): self
{
$this->error = $error;
Expand Down Expand Up @@ -100,6 +109,33 @@ public function isReferralInitiatedConversation(): ?bool
return $this->conversation->isReferralInitiated();
}

public function pricingCategory(): ?string
{
if (!$this->pricing) {
return null;
}

return (string) $this->pricing->category();
}

public function pricingModel(): ?string
{
if (!$this->pricing) {
return null;
}

return (string) $this->pricing->model();
}

public function isBillable(): ?bool
{
if (!$this->pricing) {
return null;
}

return $this->pricing->isBillable();
}

public function status(): string
{
return (string) $this->status;
Expand Down
8 changes: 8 additions & 0 deletions src/WebHook/Notification/StatusNotificationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ public function buildFromPayload(array $metadata, array $status): StatusNotifica
));
}

if (isset($status['pricing'])) {
$notification->withPricing(new Support\Pricing(
$status['pricing']['category'],
$status['pricing']['pricing_model'],
$status['pricing']['billable'],
));
}

if (isset($status['errors'])) {
$notification->withError(new Support\Error(
$status['errors'][0]['code'],
Expand Down
34 changes: 34 additions & 0 deletions src/WebHook/Notification/Support/Pricing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Netflie\WhatsAppCloudApi\WebHook\Notification\Support;

final class Pricing
{
private PricingCategory $category;

private string $model;

private bool $billable;

public function __construct(string $category, string $model, bool $billable)
{
$this->category = new PricingCategory($category);
$this->model = $model;
$this->billable = $billable;
}

public function category(): PricingCategory
{
return $this->category;
}

public function model(): string
{
return $this->model;
}

public function isBillable(): bool
{
return $this->billable;
}
}
23 changes: 23 additions & 0 deletions src/WebHook/Notification/Support/PricingCategory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Netflie\WhatsAppCloudApi\WebHook\Notification\Support;

/**
* @method static PricingCategory AUTHENTICATION()
* @method static PricingCategory MARKETING()
* @method static PricingCategory UTILITY()
* @method static PricingCategory SERVICE()
* @method static PricingCategory REFERRAL_INITIATED()
*/
final class PricingCategory extends \MyCLabs\Enum\Enum
{
private const AUTHENTICATION = 'authentication';

private const MARKETING = 'marketing';

private const UTILITY = 'utility';

private const SERVICE = 'service';

private const REFERRAL_INITIATED = 'referral_conversion';
}
108 changes: 104 additions & 4 deletions tests/Unit/WebHook/NotificationFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,10 @@ public function test_build_from_payload_can_build_an_image_notification()
"timestamp": "1669233778",
"type": "image",
"image": {
"caption": "CAPTION",
"caption": "CAPTION_TEXT",
"mime_type": "image/jpeg",
"sha256": "IMAGE_HASH",
"id": "IMAGE_ID",
"caption": "CAPTION_TEXT"
"id": "IMAGE_ID"
}
}]
},
Expand All @@ -219,10 +218,59 @@ public function test_build_from_payload_can_build_an_image_notification()

$this->assertInstanceOf(Notification\Media::class, $notification);
$this->assertEquals('IMAGE_ID', $notification->imageId());
$this->assertEquals('IMAGE_HASH', $notification->sha256());
$this->assertEquals('image/jpeg', $notification->mimeType());
$this->assertEquals('CAPTION_TEXT', $notification->caption());
}

public function test_build_from_payload_can_build_an_document_notification()
{
$payload = json_decode('{
"object": "whatsapp_business_account",
"entry": [{
"id": "WHATSAPP_BUSINESS_ACCOUNT_ID",
"changes": [{
"value": {
"messaging_product": "whatsapp",
"metadata": {
"display_phone_number": "PHONE_NUMBER",
"phone_number_id": "PHONE_NUMBER_ID"
},
"contacts": [{
"profile": {
"name": "NAME"
},
"wa_id": "WHATSAPP_ID"
}],
"messages": [{
"from": "PHONE_NUMBER",
"id": "wamid.ID",
"timestamp": "1669233778",
"type": "document",
"document": {
"caption": "CAPTION_TEXT",
"filename": "FILENAME",
"mime_type": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"sha256": "DOCUMENT_HASH",
"id": "IMAGE_ID"
}
}]
},
"field": "messages"
}]
}]
}', true);

$notification = $this->notification_factory->buildFromPayload($payload);

$this->assertInstanceOf(Notification\Media::class, $notification);
$this->assertEquals('IMAGE_ID', $notification->imageId());
$this->assertEquals('DOCUMENT_HASH', $notification->sha256());
$this->assertEquals('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', $notification->mimeType());
$this->assertEquals('CAPTION_TEXT', $notification->caption());
$this->assertEquals('FILENAME', $notification->filename());
}

public function test_build_from_payload_can_build_a_sticker_notification()
{
$payload = json_decode('{
Expand Down Expand Up @@ -254,7 +302,7 @@ public function test_build_from_payload_can_build_a_sticker_notification()
"type": "sticker",
"sticker": {
"mime_type": "image/webp",
"sha256": "HASH",
"sha256": "STICKER_HASH",
"id": "STICKER_ID"
}
}
Expand All @@ -271,6 +319,7 @@ public function test_build_from_payload_can_build_a_sticker_notification()

$this->assertInstanceOf(Notification\Media::class, $notification);
$this->assertEquals('STICKER_ID', $notification->imageId());
$this->assertEquals('STICKER_HASH', $notification->sha256());
$this->assertEquals('image/webp', $notification->mimeType());
}

Expand Down Expand Up @@ -844,6 +893,57 @@ public function test_build_from_payload_can_build_a_status_notification_without_
$this->assertNull($notification->conversationExpiresAt());
}

public function test_build_from_payload_can_build_a_status_with_pricing_notification()
{
$payload = json_decode('{
"object": "whatsapp_business_account",
"entry": [{
"id": "WHATSAPP_BUSINESS_ACCOUNT_ID",
"changes": [{
"value": {
"messaging_product": "whatsapp",
"metadata": {
"display_phone_number": "PHONE_NUMBER",
"phone_number_id": "PHONE_NUMBER_ID"
},
"statuses": [{
"id": "wamid.ID",
"recipient_id": "CUSTOMER_PHONE_NUMBER",
"status": "delivered",
"timestamp": "1690327464",
"conversation": {
"id": "CONVERSATION_ID",
"origin": {
"type": "marketing"
}
},
"pricing": {
"billable": true,
"pricing_model": "CBP",
"category": "marketing"
}
}]
},
"field": "messages"
}]
}]
}', true);

$notification = $this->notification_factory->buildFromPayload($payload);

$this->assertInstanceOf(Notification\StatusNotification::class, $notification);
$this->assertEquals('wamid.ID', $notification->id());
$this->assertEquals('CUSTOMER_PHONE_NUMBER', $notification->customerId());
$this->assertEquals('marketing', $notification->pricingCategory());
$this->assertEquals('CBP', $notification->pricingModel());
$this->assertTrue($notification->isBillable());
$this->assertEquals('CONVERSATION_ID', $notification->conversationId());
$this->assertEquals('delivered', $notification->status());
$this->assertFalse($notification->isMessageRead());
$this->assertTrue($notification->isMessageDelivered());
$this->assertTrue($notification->isMessageSent());
}

public function test_build_from_payload_can_build_a_status_notification_with_errors()
{
$payload = json_decode('{
Expand Down

0 comments on commit 08572c6

Please sign in to comment.