Skip to content

Commit

Permalink
Merge pull request #17 from ker0x/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
ker0x authored Jan 21, 2017
2 parents fecf7f4 + f036352 commit 29ebcba
Show file tree
Hide file tree
Showing 10 changed files with 267 additions and 7 deletions.
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ A PHP libray to send message and configure thread to [Facebook Messenger](https:
To install this plugin, run `composer require kerox/messenger` or add this snippet in your project’s composer.json.

```json
{
"require": {
"kerox/messenger": "~1.0"
}
"require": {
"kerox/messenger": "~1.0"
}
```

Expand All @@ -42,7 +40,7 @@ To be able to send messages to an user, you need to configure a webhook in your
Once your webhook is configured, you need to validate the callback URL

```php
if ($messenger->webhook()->isValidToken()) {
if (!$messenger->webhook()->isValidToken()) {
// Display an error
}

Expand All @@ -59,7 +57,7 @@ if (!$messenger->webhook()->isValidCallback()) {
header("HTTP/1.1 400 Invalid Request");
}

$events = $this->messenger->webhook()->getCallbackEvents();
$events = $messenger->webhook()->getCallbackEvents();
foreach ($events as $event) {
if ($event instance of MessageEvent) {
$message = $event->getMessage();
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "kerox/messenger",
"type": "library",
"description": "PHP Library for Facebook Messenger",
"keywords": ["facebook", "messenger", "facebook messenger", "bot messenger", "api"],
"homepage": "https://github.com/ker0x/messenger",
"license": "MIT",
"authors": [
Expand Down
60 changes: 60 additions & 0 deletions src/Event/CheckoutUpdateEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
namespace Kerox\Messenger\Event;

use Kerox\Messenger\Model\Callback\CheckoutUpdate;

class CheckoutUpdateEvent extends AbstractEvent
{

const NAME = 'checkout_update';

/**
* @var int
*/
protected $timestamp;

/**
* @var \Kerox\Messenger\Model\Callback\CheckoutUpdate
*/
protected $checkoutUpdate;

/**
* CheckoutUpdateEvent constructor.
*
* @param string $senderId
* @param string $recipientId
* @param int $timestamp
* @param \Kerox\Messenger\Model\Callback\CheckoutUpdate $checkoutUpdate
*/
public function __construct(string $senderId, string $recipientId, int $timestamp, CheckoutUpdate $checkoutUpdate)
{
parent::__construct($senderId, $recipientId);

$this->timestamp = $timestamp;
$this->checkoutUpdate = $checkoutUpdate;
}

/**
* @return int
*/
public function getTimestamp(): int
{
return $this->timestamp;
}

/**
* @return \Kerox\Messenger\Model\Callback\CheckoutUpdate
*/
public function getCheckoutUpdate(): CheckoutUpdate
{
return $this->checkoutUpdate;
}

/**
* @return string
*/
public function getName(): string
{
return self::NAME;
}
}
22 changes: 21 additions & 1 deletion src/Event/EventFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use Kerox\Messenger\Helper\UtilityTrait;
use Kerox\Messenger\Model\Callback\AccountLinking;
use Kerox\Messenger\Model\Callback\CheckoutUpdate;
use Kerox\Messenger\Model\Callback\Delivery;
use Kerox\Messenger\Model\Callback\Message;
use Kerox\Messenger\Model\Callback\MessageEcho;
Expand All @@ -24,6 +25,7 @@ class EventFactory
'delivery',
'read',
'payment',
'checkout_update',
];

/**
Expand Down Expand Up @@ -146,7 +148,11 @@ public static function createReadEvent($payload): ReadEvent
return new ReadEvent($senderId, $recipientId, $timestamp, $read);
}

public static function createPaymentEvent(array $payload)
/**
* @param array $payload
* @return \Kerox\Messenger\Event\PaymentEvent
*/
public static function createPaymentEvent(array $payload): PaymentEvent
{
$senderId = $payload['sender']['id'];
$recipientId = $payload['recipient']['id'];
Expand All @@ -155,4 +161,18 @@ public static function createPaymentEvent(array $payload)

return new PaymentEvent($senderId, $recipientId, $timestamp, $payment);
}

/**
* @param array $payload
* @return \Kerox\Messenger\Event\CheckoutUpdateEvent
*/
public static function createCheckoutUpdateEvent(array $payload)
{
$senderId = $payload['sender']['id'];
$recipientId = $payload['recipient']['id'];
$timestamp = $payload['timestamp'];
$checkoutUpdate = CheckoutUpdate::create($payload['checkout_update']);

return new CheckoutUpdateEvent($senderId, $recipientId, $timestamp, $checkoutUpdate);
}
}
57 changes: 57 additions & 0 deletions src/Model/Callback/CheckoutUpdate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
namespace Kerox\Messenger\Model\Callback;

use Kerox\Messenger\Model\Common\Address;

class CheckoutUpdate
{

/**
* @var string
*/
protected $payload;

/**
* @var \Kerox\Messenger\Model\Common\Address
*/
protected $shippingAddress;

/**
* CheckoutUpdate constructor.
*
* @param string $payload
* @param \Kerox\Messenger\Model\Common\Address $shippingAddress
*/
public function __construct(string $payload, Address $shippingAddress)
{
$this->payload = $payload;
$this->shippingAddress = $shippingAddress;
}

/**
* @return string
*/
public function getPayload(): string
{
return $this->payload;
}

/**
* @return \Kerox\Messenger\Model\Common\Address
*/
public function getShippingAddress(): Address
{
return $this->shippingAddress;
}

/**
* @param array $payload
* @return static
*/
public static function create(array $payload)
{
$shippingAddress = Address::create($payload['shipping_address']);

return new static($payload['payload'], $shippingAddress);
}
}
29 changes: 29 additions & 0 deletions src/Model/Common/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ class Address implements \JsonSerializable
*/
protected $country;

/**
* @var null|int
*/
protected $id;

/**
* Address constructor.
*
Expand Down Expand Up @@ -116,6 +121,25 @@ public function getCountry(): string
return $this->country;
}

/**
* @param int $id
* @return Address
*/
public function setId(int $id)
{
$this->id = $id;

return $this;
}

/**
* @return null|int
*/
public function getId()
{
return $this->id;
}

/**
* @return array
*/
Expand All @@ -128,6 +152,7 @@ public function jsonSerialize(): array
'postal_code' => $this->postalCode,
'state' => $this->state,
'country' => $this->country,
'id' => $this->id,
];

return array_filter($json);
Expand All @@ -145,6 +170,10 @@ public static function create(array $payload)
$address->setAdditionalStreet($payload['street_2']);
}

if (isset($payload['id'])) {
$address->setId($payload['id']);
}

return $address;
}
}
21 changes: 21 additions & 0 deletions tests/Mocks/Event/checkout_update.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"recipient": {
"id": "PAGE_ID"
},
"timestamp": 1473204787206,
"sender": {
"id": "USER_ID"
},
"checkout_update": {
"payload": "DEVELOPER_DEFINED_PAYLOAD",
"shipping_address": {
"id": 10105655000959552,
"country": "US",
"city": "MENLO PARK",
"street_1": "1 Hacker Way",
"street_2": "",
"state": "CA",
"postal_code": "94025"
}
}
}
13 changes: 13 additions & 0 deletions tests/TestCase/Event/EventFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace Kerox\Messenger\Test\TestCase\Event;

use Kerox\Messenger\Event\AccountLinkingEvent;
use Kerox\Messenger\Event\CheckoutUpdateEvent;
use Kerox\Messenger\Event\EventFactory;
use Kerox\Messenger\Event\DeliveryEvent;
use Kerox\Messenger\Event\MessageEchoEvent;
Expand All @@ -12,6 +13,7 @@
use Kerox\Messenger\Event\RawEvent;
use Kerox\Messenger\Event\ReadEvent;
use Kerox\Messenger\Model\Callback\AccountLinking;
use Kerox\Messenger\Model\Callback\CheckoutUpdate;
use Kerox\Messenger\Model\Callback\Delivery;
use Kerox\Messenger\Model\Callback\Message;
use Kerox\Messenger\Model\Callback\MessageEcho;
Expand Down Expand Up @@ -122,4 +124,15 @@ public function testPaymentEvent()

$this->assertEquals($expectedEvent, $event);
}

public function testCheckoutEvent()
{
$json = file_get_contents(__DIR__ . '/../../Mocks/Event/checkout_update.json');
$array = json_decode($json, true);

$expectedEvent = new CheckoutUpdateEvent('USER_ID', 'PAGE_ID', 1473204787206, CheckoutUpdate::create($array['checkout_update']));
$event = EventFactory::create($array);

$this->assertEquals($expectedEvent, $event);
}
}
14 changes: 14 additions & 0 deletions tests/TestCase/Event/EventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace Kerox\Messenger\Test\TestCase\Callback;

use Kerox\Messenger\Event\AccountLinkingEvent;
use Kerox\Messenger\Event\CheckoutUpdateEvent;
use Kerox\Messenger\Event\DeliveryEvent;
use Kerox\Messenger\Event\MessageEchoEvent;
use Kerox\Messenger\Event\MessageEvent;
Expand All @@ -10,6 +11,7 @@
use Kerox\Messenger\Event\PostbackEvent;
use Kerox\Messenger\Event\ReadEvent;
use Kerox\Messenger\Model\Callback\AccountLinking;
use Kerox\Messenger\Model\Callback\CheckoutUpdate;
use Kerox\Messenger\Model\Callback\Delivery;
use Kerox\Messenger\Model\Callback\Message;
use Kerox\Messenger\Model\Callback\MessageEcho;
Expand Down Expand Up @@ -116,4 +118,16 @@ public function testPaymentEvent()
$this->assertEquals($mockedPayment, $event->getPayment());
$this->assertEquals('payment', $event->getName());
}

public function testCheckoutUpdate()
{
$mockedCheckoutUpdate = $this->createMock(CheckoutUpdate::class);
$event = new CheckoutUpdateEvent('sender_id', 'recipient_id', 123456, $mockedCheckoutUpdate);

$this->assertEquals('sender_id', $event->getSenderId());
$this->assertEquals('recipient_id', $event->getRecipientId());
$this->assertEquals(123456, $event->getTimestamp());
$this->assertEquals($mockedCheckoutUpdate, $event->getCheckoutUpdate());
$this->assertEquals('checkout_update', $event->getName());
}
}
Loading

0 comments on commit 29ebcba

Please sign in to comment.