spacejewel-ipn-communication/source/defs/Hookmanager/Hookmanager.php
2018-06-08 10:56:33 +00:00

206 lines
5.5 KiB
PHP

<?php
use \Slim\Http\Request as Request;
use \Slim\Http\Response as Response;
// The hookmanager is the web-exposed class that allows you to manage a user within the billing system.
// This system takes care of CRUD operations on customers. It doesn't keep track of order but it does suspend
// you if you don't pay.
class Hookmanager
{
private $Customer;
private $Renderview;
private $APITools;
private $Mailer;
// Can be overloaded by child classes in order to add new features.W
protected $alertTypes =
[
"subscription_created",
"subscription_updated",
"subscription_cancelled",
"subscription_payment_succeeded",
"subscription_payment_failed",
"subscription_payment_refunded",
"payment_refunded"
];
private $AppBaseURL;
public function __construct()
{
$this->Customer = new Customer();
$this->Renderview = new EmailRenderer();
$this->APITools = new ApplicationAPI();
// Create mailer when needed
}
// Return POST variable list in a structured array, DRY
private function getStructuredVariableList(Request $request)
{
$PDATA = $request->getParsedBody();
$dArr = [];
foreach($PDATA as $key => $param)
{
$dArr[$key => $param]; // Turn parsed body into an array.
}
return $dArr;
}
private function keyValidate($keyFromURI)
{
// FIXME: Theoretically, this method does not work.
// TODO: Devise a new way to validate keys without user ID
if($this->APITools->keysMatch($keyFromURI))
{
return true;
}
else
{
return false;
}
}
public function detectPayloadIntent(Request $request, Response $response, $args)
{
if ($this->keyValidate($keyFromURI))
{
$data = $this->getStructuredVariableList($Request);
foreach ($this->alertTypes as $types => $value)
{
if ($types == $data['alert_name'])
{
$sVal = $value;
}
}
// Uses the response and redirect objects and the intent from the current request to redirect it
return $this->redirectAlert($request, $response, $sVal);
}
throw new LogicException("Illegal API key");
}
// The response and request method is passed by the redirecting method
public function redirectAlert(Request $Request, Response $response, $intent)
{
switch ($intent)
{
case "subscription_created":
return $response->withRedirect(, 307)
break;
case "subscription_updated":
break;
case "subscription_cancelled":
break;
case "subscription_payment_successful":
break;
case "subscription_payment_failed":
break;
case "subscription_payment_refunded":
break;
case "payment_refunded":
break;
default:
// default code here
}
}
public function EventSubscriptionCreated(Request $request, Response $response, $args)
{
return $response->write($this->getStructuredVariableList($Request))->withStatus(200);
}
public function EventSubscriptionUpdated(Request $request, Response $response)
{
// Update user information if necessary
}
public function EventSubscriptionCancelled(Request $request, Response $response)
{
// Suspend user's domain name as stated on DB
// Delete after 30 days
}
public function EventSubscriptionPaymentSuccess(Request $request, Response $response)
{
// Renew user's subscription within billing system
// Install hosting account if this is the user's first time and send out instructions email
}
// TODO: Lenient business logic
public function EventSubscriptionPaymentFailed(Request $request, Response $response)
{
// Add payment strike to user account
// After three strikes, warn user that the system has waived further attempts. Suspend their account
// Give user an ultimatum: Pay or remain suspended
// If user fails to pay during the next 7 days, permanently suspend their account (Reject further payments. Remove user from paddle first)
// If payment is successful afterwards, remove all payment strikes (Success logic should execute automatically)
}
public function EventSubscriptionPaymentRefunded(Request $request, Response $response)
{
// Ban customer from billing system
// Delete all customer data
}
}