Ready for first test

Spinning up webserver
Test URL (theoretically): https://billing.spacejewel.ga/paddle/[key]/detectIntent should output all vars in this commit
In production, it should redirect the logic and execute different code (Instead of using an url redirect)
This commit is contained in:
Miguel Nogueira 2018-06-08 14:42:10 +00:00
parent d38ce4fe15
commit 25f604fcb1
2 changed files with 112 additions and 94 deletions

View File

@ -9,6 +9,7 @@ return [
"appname" => "SG-Gateway", "appname" => "SG-Gateway",
"appversion" => "alpha 1.0", "appversion" => "alpha 1.0",
"operationmode" => "development" "operationmode" => "development"
], ],
"core" => "core" =>

View File

@ -2,6 +2,8 @@
use \Slim\Http\Request as Request; use \Slim\Http\Request as Request;
use \Slim\Http\Response as Response; use \Slim\Http\Response as Response;
use Yadakhov\Json as Json;
// The hookmanager is the web-exposed class that allows you to manage a user within the billing system. // 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 // This system takes care of CRUD operations on customers. It doesn't keep track of order but it does suspend
@ -10,15 +12,21 @@ use \Slim\Http\Response as Response;
class Hookmanager class Hookmanager
{ {
private $Customer; private $Customer;
private $Renderview; private $Renderview;
private $APITools; private $APITools;
private $Mailer; private $Mailer;
// Can be overloaded by child classes in order to add new features.W // Can be overloaded by child classes in order to add new features.W
protected $alertTypes = protected $alertTypes =
[ [
@ -43,12 +51,110 @@ class Hookmanager
$this->Customer = new Customer(); $this->Customer = new Customer();
$this->Renderview = new EmailRenderer(); $this->Renderview = new EmailRenderer();
$this->APITools = new ApplicationAPI(); $this->APITools = new ApplicationAPI();
$this->JsonHelper = new Json();
// Create mailer when needed // Create mailer when needed
} }
/**
*
* This function detects the intent within the payload.
* After doing so, it returns the intent back to you so that you can perfom the correct redirect
*
*/
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
$this->redirectAlert($request, $response, $sVal);
}
throw new LogicException("Illegal API key");
}
// The response and request method is passed by the redirecting method
private function redirectAlert(Request $Request, Response $response, $intent)
{
switch ($intent)
{
// This serves as a proxy to all other methods
case "subscription_created":
$this->EventSubscriptionCreated($Request, $response);
break;
case "subscription_updated":
$this->EventSubscriptionUpdated($Request, $response)
break;
case "subscription_cancelled":
$this->EventSubscriptionCancelled($Request, $response);
break;
case "subscription_payment_successful":
$this->EventSubscriptionPaymentSuccess($Request, $response);
break;
case "subscription_payment_failed":
$this->EventSubscriptionPaymentFailed($Request, $response);
break;
case "subscription_payment_refunded":
$this->EventSubscriptionPaymentRefunded($Request, $response);
break;
default:
$this->JsonHelper->set
([
"status" => "fail",
"message" => "Error: Invalid alert type (Or middleman attack in-progress)",
"code" => 500
]);
// Writing JsonHelper while it's not a string but an object will cause it to fire it's __toString method, which
// assembles the JSON defined in the array notation as written above in the form of an array.
// Calling Slim's withStatus method gives us the ability to tell the client something went wrong.
return $response->write($this->JsonHelper)->withStatus(500);
}
}
// Return POST variable list in a structured array, DRY // Return POST variable list in a structured array, DRY
private function getStructuredVariableList(Request $request) private function getStructuredVariableList(Request $request)
{ {
@ -65,96 +171,7 @@ class Hookmanager
} }
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) public function EventSubscriptionCreated(Request $request, Response $response, $args)
{ {
@ -163,12 +180,12 @@ class Hookmanager
} }
public function EventSubscriptionUpdated(Request $request, Response $response) public function EventSubscriptionUpdated(Request $request, Response $response, $args)
{ {
// Update user information if necessary // Update user information if necessary
} }
public function EventSubscriptionCancelled(Request $request, Response $response) public function EventSubscriptionCancelled(Request $request, Response $response, $args)
{ {
// Suspend user's domain name as stated on DB // Suspend user's domain name as stated on DB
@ -176,7 +193,7 @@ class Hookmanager
} }
public function EventSubscriptionPaymentSuccess(Request $request, Response $response) public function EventSubscriptionPaymentSuccess(Request $request, Response $response, $args)
{ {
// Renew user's subscription within billing system // Renew user's subscription within billing system
@ -184,7 +201,7 @@ class Hookmanager
} }
// TODO: Lenient business logic // TODO: Lenient business logic
public function EventSubscriptionPaymentFailed(Request $request, Response $response) public function EventSubscriptionPaymentFailed(Request $request, Response $response, $args)
{ {
// Add payment strike to user account // Add payment strike to user account
@ -195,7 +212,7 @@ class Hookmanager
} }
public function EventSubscriptionPaymentRefunded(Request $request, Response $response) public function EventSubscriptionPaymentRefunded(Request $request, Response $response, $args)
{ {
// Ban customer from billing system // Ban customer from billing system
// Delete all customer data // Delete all customer data