Skip to content

Commit

Permalink
More webhook classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
judgej committed May 28, 2018
1 parent ee890c2 commit fd9b840
Show file tree
Hide file tree
Showing 9 changed files with 308 additions and 20 deletions.
34 changes: 34 additions & 0 deletions src/ServerRequest/Collections/CustomerPaymentProfiles.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Academe\AuthorizeNet\ServerRequest\Collections;

/**
* Collection of response UserFields.
*/

use Academe\AuthorizeNet\Response\HasDataTrait;
use Academe\AuthorizeNet\ServerRequest\Model\CustomerPaymentProfile;
use Academe\AuthorizeNet\AbstractCollection;

class CustomerPaymentProfiles extends AbstractCollection
{
use HasDataTrait;

public function __construct(array $data = [])
{
$this->setData($data);

// An array of PaymentProfile records.

foreach ($data as $item_data) {
$this->push(new CustomerPaymentProfile($item_data));
}
}

protected function hasExpectedStrictType($item)
{
// Make sure the item is the correct type, and is not empty.

return $item instanceof CustomerPaymentProfile && $item->hasAny();
}
}
51 changes: 51 additions & 0 deletions src/ServerRequest/Model/CustomerPaymentProfile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Academe\AuthorizeNet\ServerRequest\Model;

/**
* Single payment profile item.
*/

use Academe\AuthorizeNet\Response\HasDataTrait;
use Academe\AuthorizeNet\AbstractModel;

class CustomerPaymentProfile extends AbstractModel
{
use HasDataTrait;

protected $id;
protected $customerType;

public function __construct($data)
{
$this->setData($data);

$this->setId($this->getDataValue('id'));
$this->setCustomerType($this->getDataValue('customerType'));
}

public function jsonSerialize()
{
$data = [
'id' => $this->getId(),
'customerType' => $this->getCustomerType(),
];

return $data;
}

protected function setId($value)
{
$this->id = $value;
}

protected function setCustomerType($value)
{
$this->customerType = $value;
}

public function hasAny()
{
return $this->id !== null;
}
}
118 changes: 103 additions & 15 deletions src/ServerRequest/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,61 @@
use Academe\AuthorizeNet\Response\HasDataTrait;
use Academe\AuthorizeNet\AbstractModel;
use Academe\AuthorizeNet\ServerRequest\AbstractPayload;
use Academe\AuthorizeNet\ServerRequest\Payload\PaymentProfile;
use Academe\AuthorizeNet\ServerRequest\Payload\CustomerPaymentProfile;
use Academe\AuthorizeNet\ServerRequest\Payload\Fraud;
use Academe\AuthorizeNet\ServerRequest\Payload\Payment;
use Academe\AuthorizeNet\ServerRequest\Payload\Subscription;
use Academe\AuthorizeNet\ServerRequest\Payload\CustomerProfile;

class Notification extends AbstractModel
{
use HasDataTrait;

const EVENT_NAMESPACE = 'net.authorize';

/**
* The event name prefix indicates the payload type.
* Note that some prefixes are subsets of others, so be
* careful what order they are checked.
*/
const EVENT_PREFIX_FRAUD = 'net.authorize.payment.fraud';
const EVENT_PREFIX_PAYMENT = 'net.authorize.payment';
const EVENT_PREFIX_PAYMENT_PROFILE = 'net.authorize.customer.paymentProfile';
const EVENT_PREFIX_SUBSCRIPTION = 'net.authorize.customer.subscription';
const EVENT_PREFIX_CUSTOMER = 'net.authorize.customer';
const EVENT_TARGET_PAYMENT = 'payment';
const EVENT_TARGET_CUSTOMER = 'customer';

const EVENT_SUBTARGET_FRAUD = 'fraud';
const EVENT_SUBTARGET_AUTHORIZATION = 'authorization';
const EVENT_SUBTARGET_AUTHCAPTURE = 'authcapture';
const EVENT_SUBTARGET_CAPTURE = 'capture';
const EVENT_SUBTARGET_REFUND = 'refund';
const EVENT_SUBTARGET_PRIORAUTHCAPTURE = 'priorAuthCapture';
const EVENT_SUBTARGET_VOID = 'void';

const EVENT_SUBTARGET_PAYMENTPROFILE = 'paymentProfile';
const EVENT_SUBTARGET_SUBSCRIPTION = 'subscription';

/**
* A list of event actions we know about.
*/
const EVENT_ACTION_CREATED = 'created';
const EVENT_ACTION_UPDATED = 'updated';
const EVENT_ACTION_DELETED = 'deleted';
const EVENT_ACTION_SUSPENDED = 'suspended';
const EVENT_ACTION_TERMINATED = 'terminated';
const EVENT_ACTION_CANCELLED = 'cancelled';
const EVENT_ACTION_EXPIRING = 'expiring';
const EVENT_ACTION_HELD = 'held';
const EVENT_ACTION_APPROVED = 'approved';
const EVENT_ACTION_DECLINED = 'declined';

protected $notificationId;
protected $eventType;
protected $eventDate;
protected $webhookId;
protected $payload;

protected $eventTarget;
protected $eventSubtarget;
protected $eventAction;

// TODO: the deliberyStatus and retryLog is not a part of the
// webhook notifications, but the REST API for managing and
// reporting on the webhooks. Move it there when the management
Expand All @@ -60,17 +89,61 @@ public function __construct($data)
$this->setEventDate($this->getDataValue('eventDate'));
$this->setWebhookId($this->getDataValue('webhookId'));

// TODO: retryLog (collection needed)
$eventType = $this->eventType;

// Parse the eventType.
// We split it up into the following parts:
// {namespace}.{target}[.{sub-target}].{action}
// The namespace is discarded, and the remainder are used
// to determine the payload class.

// Strip off the namespace prefix if it is present.
// If not present, we may want to throw an exception
// or fall back to a default 'unknown' payload.

if (strpos($eventType, static::EVENT_NAMESPACE) === 0) {
$arr = explode('.', $eventType);

foreach (explode('.', static::EVENT_NAMESPACE) as $i) {
array_shift($arr);
}

// The main target is the section after the prefix.

$this->eventTarget = array_shift($arr);

// The action being notified is the last part of the event type.

$this->eventAction = array_pop($arr);

// The sub-target is what's left, which should be one or zero
// elements, i.e. optional.

$this->eventSubtarget = implode('.', $arr);
}

if ($payload = $this->getDataValue('payload')) {
if (strpos($this->eventType, static::EVENT_PREFIX_FRAUD) === 0) {
$this->setPayload(new Fraud($payload));
} elseif (strpos($this->eventType, static::EVENT_PREFIX_PAYMENT_PROFILE) === 0) {
$this->setPayload(new PaymentProfile($payload));
} elseif (strpos($this->eventType, static::EVENT_PREFIX_PAYMENT) === 0) {
$this->setPayload(new Payment($payload));
} elseif (strpos($this->eventType, static::EVENT_PREFIX_SUBSCRIPTION) === 0) {
$this->setPayload(new Subscription($payload));
if ($this->eventTarget === static::EVENT_TARGET_PAYMENT) {
if ($this->eventSubtarget === static::EVENT_SUBTARGET_FRAUD) {
// Notification of a payment fraud.
$this->setPayload(new Fraud($payload));
} else {
// Notification of a payment (any other sub-target).
$this->setPayload(new Payment($payload));
}
} elseif ($this->eventTarget === static::EVENT_TARGET_CUSTOMER) {
if ($this->eventSubtarget === static::EVENT_SUBTARGET_PAYMENTPROFILE) {
// Notification of a customer payment profile change.
$this->setPayload(new CustomerPaymentProfile($payload));
} elseif ($this->eventSubtarget === static::EVENT_SUBTARGET_SUBSCRIPTION) {
// Notification of a change to a customer subscription.
$this->setPayload(new Subscription($payload));
} else {
// Notification of a change to a customer profile.
$this->setPayload(new CustomerProfile($payload));
}
} else {
// TODO: fall back to some default payload.
}
}
}
Expand Down Expand Up @@ -118,4 +191,19 @@ protected function setPayload(AbstractPayload $value)
{
$this->payload = $value;
}

protected function getEventTarget()
{
return $this->eventTarget;
}

protected function getEventSubtarget()
{
return $this->eventSubtarget;
}

protected function getEventAction()
{
return $this->eventAction;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Academe\AuthorizeNet\AbstractModel;
use Academe\AuthorizeNet\ServerRequest\AbstractPayload;

class PaymentProfile extends AbstractPayload
class CustomerPaymentProfile extends AbstractPayload
{
protected $customerProfileId;
protected $customerType;
Expand Down
78 changes: 78 additions & 0 deletions src/ServerRequest/Payload/CustomerProfile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace Academe\AuthorizeNet\ServerRequest\Payload;

/**
* The customer profile notification payload.
*/

use Academe\AuthorizeNet\ServerRequest\Payload\Payment;
use Academe\AuthorizeNet\ServerRequest\Model\Profile;
use Academe\AuthorizeNet\ServerRequest\Collections\CustomerPaymentProfiles;
use Academe\AuthorizeNet\ServerRequest\AbstractPayload;

class CustomerProfile extends AbstractPayload
{
protected $merchantCustomerId;
protected $description;

protected $paymentProfiles;

public function __construct($data)
{
parent::__construct($data);

$this->merchantCustomerId = $this->getDataValue('merchantCustomerId');
$this->description = $this->getDataValue('description');

$paymentProfiles = $this->getDataValue('paymentProfiles');

if ($paymentProfiles) {
$this->paymentProfiles = new CustomerPaymentProfiles($paymentProfiles);
}
}

public function jsonSerialize()
{
$data = parent::jsonSerialize();

$data['merchantCustomerId'] = $this->merchantCustomerId;
$data['description'] = $this->description;

$data['paymentProfiles'] = $this->paymentProfiles;

return $data;
}

/**
* The customerProfileId is an alias for the id.
*/
public function getCustomerProfileId()
{
return $this->id;
}

/**
*
*/
protected function setMerchantCustomerId($value)
{
$this->merchantCustomerId = $value;
}

/**
*
*/
protected function setDescription($value)
{
$this->description = $value;
}

/**
*
*/
protected function setPaymentProfiles(PaymentProfiles $value)
{
$this->profile = $value;
}
}
4 changes: 3 additions & 1 deletion src/ServerRequest/Payload/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@

use Academe\AuthorizeNet\ServerRequest\Payload\Payment;
use Academe\AuthorizeNet\ServerRequest\Model\Profile;
use Academe\AuthorizeNet\ServerRequest\AbstractPayload;

class Subscription extends Payment
class Subscription extends AbstractPayload
{
protected $name;
protected $amount;
protected $status;

protected $profile;

public function __construct($data)
Expand Down
Loading

0 comments on commit fd9b840

Please sign in to comment.