Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions www/admin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

require_once __DIR__ . '/common.inc';

use WebPageTest\Handlers\Admin as AdminHandler;

$page = (string) filter_input(INPUT_GET, 'page', FILTER_SANITIZE_STRING);
switch ($page) {
case ('chargify-sandbox'):
$contents = AdminHandler::getChargifySandbox($request_context);
echo $contents;
exit();
default:
http_response_code(404);
die();
}
5 changes: 5 additions & 0 deletions www/chargify-sandbox.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

declare(strict_types=1);

require_once __DIR__ . '/common.inc';
4 changes: 3 additions & 1 deletion www/common.inc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use WebPageTest\User;
use WebPageTest\RequestContext;
use WebPageTest\Exception\ClientException;
use WebPageTest\Exception\ForbiddenException;
use WebPageTest\Environment;

if (Util::getSetting('php_sessions')) {
// Start session handling for this request
Expand All @@ -25,7 +26,7 @@ if (Util::getSetting('cp_auth')) {

set_exception_handler(function ($e) {
// Developer envs just get a print out
if (Util::getSetting('environment') == 'dev') {
if (Util::getSetting('environment') == Environment::$Development) {
echo '<pre>';
var_dump($e);
echo '</pre>';
Expand Down Expand Up @@ -67,6 +68,7 @@ if (GetSetting('friendly_urls') || (array_key_exists('HTTP_MOD_REWRITE', $_SERVE

// Create global request context for future use
$request_context = new RequestContext($_REQUEST, $_SERVER);
$request_context->setEnvironment(Util::getSetting('environment', 'production'));

// Disable caching by default
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0", true);
Expand Down
5 changes: 5 additions & 0 deletions www/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ rewrite ^/signup/$ /cpauth/signup.php last;
rewrite ^/signup/([a-zA-Z0-9_]+)$ /cpauth/signup.php?step=$1 last;
rewrite ^/account/([a-zA-Z0-9_]+)$ /account/$1/ permanent;
rewrite ^/account/([a-zA-Z0-9_]+)/$ /cpauth/account.php?page=$1 last;

# admin tools
rewrite ^/admin$ /admin.php permanent;
rewrite ^/admin/([a-zA-Z0-9_-]+)$ /admin/$1/ permanent;
rewrite ^/admin/([a-zA-Z0-9_-]+)/$ /admin.php?page=$1 last;

#
# result paths section
Expand Down
17 changes: 17 additions & 0 deletions www/src/Environment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace WebPageTest;

/**
* This should be an enum, but we don't have enums yet because we're on PHP 7.4.
* Soon enough we will be on 8 and then we can have enums
*
*/
class Environment
{
public static string $Development = 'development';
public static string $QA = 'qa';
public static string $Production = 'production';
}
44 changes: 44 additions & 0 deletions www/src/Handlers/Admin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace WebPageTest\Handlers;

use WebPageTest\RequestContext;
use WebPageTest\Template;
use WebPageTest\Util;
use WebPageTest\Environment;

class Admin
{
/**
* This should only be available in QA and Dev environments.
* Does not require Admin credentials.
*
* @param WebPageTest\RequestContext $request_context
*
* @return string $contents the contents of the page
*
*/
public static function getChargifySandbox(RequestContext $request_context): string
{
$environment = $request_context->getEnvironment();
if (!($environment == Environment::$Development || $environment == Environment::$QA)) {
http_response_code(404);
die();
}

$tpl = new Template('admin');
$tpl->setLayout('account');

$variables = [
'country_list' => Util::getChargifyCountryList(),
'state_list' => Util::getChargifyUSStateList(),
'country_list_json_blob' => Util::getCountryJsonBlob(),
'ch_client_token' => Util::getSetting('ch_key_public'),
'ch_site' => Util::getSetting('ch_site')
];
$contents = $tpl->render('chargify-sandbox', $variables);
return $contents;
}
}
30 changes: 29 additions & 1 deletion www/src/RequestContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use WebPageTest\CPSignupClient;
use WebPageTest\Util;
use WebPageTest\BannerMessageManager;
use WebPageTest\Environment;

class RequestContext
{
Expand All @@ -22,6 +23,8 @@ class RequestContext
private string $request_uri;
private string $host;
private ?BannerMessageManager $banner_message_manager;
// Should use an enum, TODO
private string $environment;

public function __construct(array $global_request, array $server = [], array $options = [])
{
Expand All @@ -41,6 +44,8 @@ public function __construct(array $global_request, array $server = [], array $op
$this->request_uri = $server['REQUEST_URI'] ?? '/';

$this->host = $options['host'] ?? Util::getSetting('host', "");

$this->environment = Environment::$Production;
}

public function getRaw(): array
Expand All @@ -52,7 +57,6 @@ public function getUser(): ?User
{
return $this->user;
}

public function setUser(?User $user): void
{
if (isset($user)) {
Expand Down Expand Up @@ -120,4 +124,28 @@ public function getHost(): string
{
return $this->host;
}

public function setEnvironment(?string $env = ''): void
{
// This should really be a match, but we're on 7.4
switch ($env) {
case 'development':
$this->environment = Environment::$Development;
break;
case 'qa':
$this->environment = Environment::$QA;
break;
case 'production':
$this->environment = Environment::$Production;
break;
default:
$this->environment = Environment::$Production;
break;
}
}

public function getEnvironment(): string
{
return $this->environment;
}
}
120 changes: 120 additions & 0 deletions www/templates/admin/chargify-sandbox.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<style>
label,
input {
display: block;
}

select,
input[type="text"] {
border: 1px solid black;
}
</style>
<div class="my-account-page page_content">

<h1>Chargify Sandbox</h1>

<section class="payment-details">
<div class="content">
<h1>Payment Details</h1>
<form method="POST" action="/" id="sandbox">
<div id="notification-banner-container"></div>
<?php require_once __DIR__ . '/../account/includes/chargify-payment-form.php'; ?>
<div class="form-input address">
<label for="street-address">Address</label>
<input name="street-address" type="text" data-chargify="address" required />
</div>
<div class="form-input city">
<label for="city">City</label>
<input name="city" type="text" data-chargify="city" required />
</div>
<div class="form-input state">
<label for="state">State</label>

<select name="state" data-country-selector="state-selector" data-chargify="state" required>
<?php foreach ($state_list as $state) : ?>
<option value="<?= $state['code'] ?>">
<?= $state['name']; ?>
</option>
<?php endforeach; ?>
</select>

</div>
<div class="form-input country">
<label for="country">Country</label>
<select name="country" data-country-selector="selector" data-chargify="country" required>
<?php foreach ($country_list as $country) : ?>
<option value="<?= $country["code"] ?>" <?php ($country["code"] === "US") ? 'selected' : '' ?>>
<?= $country["name"]; ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div class="form-input zip">
<label for="zipcode">Postal Code</label>
<input name="zipcode" type="text" required data-chargify="zip" />
</div>
<div class="form-input plan">
<label for="plan">Plan</label>
<input type="text" name="plan" value="ap1" />
</div>

<div class="form-input">
<button type="submit">Get Token</button>
</div>


<h3>Token:</h3>
<div id="hidden-nonce-input"></div>
</form>
</div><!-- /.content -->
</section>


<script src="/assets/js/braintree-error-parser.js"></script>

<script>
(() => {
let hiddenNonceInput = document.querySelector('#hidden-nonce-input');
const form = document.querySelector("#sandbox");

form.addEventListener('submit', function (event) {
event.preventDefault();
let button = event.target.querySelector('button[type=submit]');
button.disabled = true;
button.setAttribute('disabled', 'disabled');
button.innerText = 'Submitted';

chargify.token(
form,
function success(token) {
hiddenNonceInput.innerHTML = token;
button.disabled = false;
button.removeAttribute('disabled');
button.innerText = 'Get Token';
},
function error(err) {
button.disabled = false;
button.removeAttribute('disabled');
button.innerText = 'Get Token';
const signupError = new CustomEvent("cc-signup-error", {
bubbles: true,
detail: BraintreeErrorParser.parse(err.errors)
});
event.target.dispatchEvent(signupError);
}
);
});
})();
</script>
<script>
(() => {
document.addEventListener('cc-signup-error', e => {
const el = document.createElement('div');
el.classList.add('notification-banner', 'notification-banner__error');
el.innerHTML = `<h4>Billing Error: ${e.detail.text}</h4><p>${e.detail.implication}</p>`;
document.querySelector('#notification-banner-container').appendChild(el);

});
})();
</script>
</div>