forked from TribeHR/AppDirect-PHP-Wrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppDirectEvent.php
130 lines (110 loc) · 3.96 KB
/
AppDirectEvent.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
/**
* Define the different event types and classifications
*/
define('SUBSCRIPTION_ORDER', 'SUBSCRIPTION_ORDER');
define('SUBSCRIPTION_CHANGE', 'SUBSCRIPTION_CHANGE');
define('SUBSCRIPTION_CANCEL', 'SUBSCRIPTION_CANCEL');
define('SUBSCRIPTION_NOTICE', 'SUBSCRIPTION_NOTICE');
define('USER_ASSIGNMENT', 'USER_ASSIGNMENT');
define('USER_UNASSIGNMENT', 'USER_UNASSIGNMENT');
define('ACCOUNT_SYNC', 'ACCOUNT_SYNC');
define('ACCOUNT_UNSYNC', 'ACCOUNT_UNSYNC');
define('USER_SYNC', 'USER_SYNC');
define('USER_LIST_CHANGE', 'USER_LIST_CHANGE');
class AppDirectEvent extends AppDirectBase
{
var $type;
var $payload;
var $creator;
var $flag;
var $returnUrl;
private $connector;
// Method behaviour flags
const TRAP_STATELESS = true;
const ALLOW_STATELESS = false;
// Event definition/action constants
const FLAG_STATELESS = 'STATELESS';
const FLAG_DEVELOPMENT = 'DEVELOPMENT';
const NOTICE_DEACTIVATED = 'DEACTIVATED';
const NOTICE_REACTIVATED = 'REACTIVATED';
const NOTICE_UPCOMING_INVOICE = 'UPCOMING_INVOICE';
const NOTICE_CLOSED = 'CLOSED';
const ACCOUNT_STATUS_TRIAL_EXPIRED = 'FREE_TRIAL_EXPIRED';
const ACCOUNT_STATUS_ACTIVE = 'ACTIVE';
public function __construct(SimpleXMLElement $xml = null)
{
$this->connector = new AppDirectConnector();
parent::__construct($xml);
}
/**
* Return the name of this object - the name is used in the XML tags
*/
protected function getName()
{
return 'event';
}
// This exists only as backward compatibility with existing code
public function getByToken($token = null)
{
return $this->getEvent($token);
}
// Get the Event data from AppDirect, either by Token or EventUrl
// @statelessAction:
// - TRAP_STATELESS: getEvent will directly respond to the AppDirect request with a generic error (default)
// - ALLOW_STATELESS: getEvent will pass the event to the requester, trusting the dummy data will be handled properly
public function getEvent($eventUrl, $statelessAction = self::TRAP_STATELESS)
{
// Verify the OAuth signature of the call
if(!$this->connector->verifySignature())
{
$error = array('error' => 'The request did not validate using AppDirect OAuth signatures');
throw new AppDirectValidationException('401', $error);
}
// The given $eventUrl could, in legacy code, actually be a token instead
if (!$this->connector->isEventUrl($eventUrl))
{
// This is an old-style token. Properly path it.
$eventUrl = 'events/'. $eventUrl;
}
else
{
// The Event is using the new distributed API, and we're given an EventUrl
$eventUrl = urldecode($eventUrl);
}
// GET the event from the provided $eventUrl using a OAuth-signed request
$event = new AppDirectEvent($this->connector->get($eventUrl));
// If STATELESS events are to be trapped, detect them here and return to AppDirect appropriately.
// Do the negative condition check so that if anything invalid is passed, the default action is taken
if ($statelessAction != self::ALLOW_STATELESS)
{
// Check if the event has the STATELESS flag set. If so, handle the return and abort.
if (isset($event->flag) && $event->flag == self::FLAG_STATELESS)
die($event->xmlResponse(false, 'OPERATION_CANCELED', 'The STATELESS event was acknowledged and canceled'));
}
// Return the fully-built event definition
return $event;
}
public function signReturnUrl($params = array())
{
$url = $this->returnUrl;
foreach($params as $key => $value)
{
$url .= '&'.$key.'='.$value;
}
return $this->connector->getSignedUrl($url);
}
public function xmlResponse($success, $code, $message, $extraData = array())
{
$xmlResult = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8" standalone="yes"?><result></result>');
$xmlResult->addChild('success', ($success ? 'true' : 'false') );
if(!$success)
$xmlResult->addChild('errorCode', $code);
$xmlResult->addChild('message', $message);
foreach($extraData as $key => $value) {
$xmlResult->addChild($key, $value);
}
return $xmlResult->asXML();
}
}
?>