Actual code for processing subscriptions

master
Sven Slootweg 12 years ago
parent 45a715fad8
commit 74d01a3021

@ -0,0 +1,42 @@
<?php
/*
* ReDonate is more free software. It is licensed under the WTFPL, which
* allows you to do pretty much anything with it, without having to
* ask permission. Commercial use is allowed, and no attribution is
* required. We do politely request that you share your modifications
* to benefit other developers, but you are under no enforced
* obligation to do so :)
*
* Please read the accompanying LICENSE document for the full WTFPL
* licensing text.
*/
if(!isset($_APP)) { die("Unauthorized."); }
class LogEntry extends CPHPDatabaseRecordClass
{
public $table_name = "log_entries";
public $fill_query = "SELECT * FROM log_entries WHERE `Id` = :Id";
public $verify_query = "SELECT * FROM log_entries WHERE `Id` = :Id";
public $prototype = array(
'string' => array(
'Ip' => "Ip",
'SessionId' => "SessionId",
'Data' => "Data"
),
'numeric' => array(
'Type' => "Type",
'CampaignId' => "CampaignId"
),
'timestamp' => array(
'Date' => "Date"
),
'campaign' => array(
'Campaign' => "Campaign"
)
);
const PAGELOAD = 1;
const SUBSCRIPTION = 2;
}

@ -0,0 +1,50 @@
<?php
/*
* ReDonate is more free software. It is licensed under the WTFPL, which
* allows you to do pretty much anything with it, without having to
* ask permission. Commercial use is allowed, and no attribution is
* required. We do politely request that you share your modifications
* to benefit other developers, but you are under no enforced
* obligation to do so :)
*
* Please read the accompanying LICENSE document for the full WTFPL
* licensing text.
*/
if(!isset($_APP)) { die("Unauthorized."); }
class Subscription extends CPHPDatabaseRecordClass
{
public $table_name = "subscriptions";
public $fill_query = "SELECT * FROM subscriptions WHERE `Id` = :Id";
public $verify_query = "SELECT * FROM subscriptions WHERE `Id` = :Id";
public $prototype = array(
'string' => array(
'EmailAddress' => "EmailAddress",
'ConfirmationKey' => "ConfirmationKey",
'SettingsKey' => "SettingsKey",
'Currency' => "Currency"
),
'numeric' => array(
'CampaignId' => "CampaignId",
'Amount' => "Amount"
),
'timestamp' => array(
'SubscriptionDate' => "SubscriptionDate",
'UnsubscriptionDate' => "UnsubscriptionDate",
'LastEmailDate' => "LastEmail"
),
'boolean' => array(
'IsConfirmed' => "Confirmed"
),
'campaign' => array(
'Campaign' => "Campaign"
)
);
public static function FindByEmail($email)
{
return self::CreateFromQuery("SELECT * FROM subscriptions WHERE `EmailAddress` = :EmailAddress", array(':EmailAddress' => $email), 0);
}
}

@ -16,11 +16,26 @@ if(!isset($_APP)) { die("Unauthorized."); }
try
{
$sCampaign = Campaign::FindByUrlName($router->uParameters[1]);
$sPageTitle = "Contribute to {$sCampaign->sName}";
$sPageContents = NewTemplater::Render("landing", $locale->strings, array("can-donate-once" => true, "project-name" => $sCampaign->sName));
}
catch (NotFoundException $e)
{
$sPageContents = NewTemplater::Render("404", $locale->strings, array());
return;
}
$sLogEntry = new LogEntry(0);
$sLogEntry->uType = LogEntry::PAGELOAD;
$sLogEntry->uIp = $_SERVER['REMOTE_ADDR'];
$sLogEntry->uData = json_encode(array());
$sLogEntry->uCampaignId = $sCampaign->sId;
$sLogEntry->uDate = time();
$sLogEntry->uSessionId = session_id();
$sLogEntry->InsertIntoDatabase();
$sPageTitle = "Contribute to {$sCampaign->sName}";
$sPageContents = NewTemplater::Render("landing", $locale->strings, array(
"can-donate-once" => true,
"project-name" => $sCampaign->sName,
"urlname" => $sCampaign->sUrlName,
"error" => $sError
));

@ -0,0 +1,84 @@
<?php
/*
* ReDonate is more free software. It is licensed under the WTFPL, which
* allows you to do pretty much anything with it, without having to
* ask permission. Commercial use is allowed, and no attribution is
* required. We do politely request that you share your modifications
* to benefit other developers, but you are under no enforced
* obligation to do so :)
*
* Please read the accompanying LICENSE document for the full WTFPL
* licensing text.
*/
if(!isset($_APP)) { die("Unauthorized."); }
try
{
$sCampaign = Campaign::FindByUrlName($router->uParameters[1]);
}
catch (NotFoundException $e)
{
$sPageContents = NewTemplater::Render("404", $locale->strings, array());
return;
}
if(empty($_POST['email']) || User::CheckIfEmailValid($_POST['email']) == false)
{
$sError = "Please enter a valid e-mail address.";
require("modules/landing.php");
return;
}
if(empty($_POST['currency']))
{
$sError = "Please pick a valid currency.";
require("modules/landing.php");
return;
}
if(empty($_POST['amount']) || preg_match("([0-9]*[.,][0-9]+|[0-9]+)", $_POST['amount']) == false)
{
$sError = "Please enter a valid amount.";
require("modules/landing.php");
return;
}
try
{
Subscription::FindByEmail($_POST['email']);
$exists = true;
}
catch (NotFoundException $e)
{
$exists = false;
}
if($exists)
{
$sPageContents = NewTemplater::Render("subscription/change", $locale->strings, array());
/* TODO: Change request */
return;
}
$sLogEntry = new LogEntry(0);
$sLogEntry->uType = LogEntry::SUBSCRIPTION;
$sLogEntry->uIp = $_SERVER['REMOTE_ADDR'];
$sLogEntry->uData = json_encode(array("email" => $_POST['email']));
$sLogEntry->uCampaignId = $sCampaign->sId;
$sLogEntry->uDate = time();
$sLogEntry->uSessionId = session_id();
$sLogEntry->InsertIntoDatabase();
$sSubscription = new Subscription(0);
$sSubscription->uEmailAddress = $_POST['email'];
$sSubscription->uConfirmationKey = random_string(25);
$sSubscription->uSettingsKey = random_string(25);
$sSubscription->uCurrency = $_POST['currency'];
$sSubscription->uAmount = str_replace(",", ".", $_POST['amount']);
$sSubscription->uSubscriptionDate = time();
$sSubscription->uConfirmed = False;
$sSubscription->uCampaignId = $sCampaign->sId;
$sSubscription->InsertIntoDatabase();
$sPageContents = NewTemplater::Render("subscription/success", $locale->strings, array());

@ -26,6 +26,7 @@ function __autoload($class_name)
$sPageTitle = "";
$sPageContents = "";
$sError = "";
$router = new CPHPRouter();
$router->allow_slash = true;
@ -34,10 +35,10 @@ $router->ignore_query = true;
$router->routes = array(
0 => array(
"^/$" => "modules/index.php",
"^/register/$" => "modules/register.php",
"^/login/$" => "modules/login.php",
"^/campaign/([a-zA-Z0-9-]+)" => "modules/landing.php",
"^/campaign/([a-zA-Z0-9-]+)/subscribe" => "modules/subscribe.php",
"^/register$" => "modules/register.php",
"^/login$" => "modules/login.php",
"^/campaign/([a-zA-Z0-9-]+)$" => "modules/landing.php",
"^/campaign/([a-zA-Z0-9-]+)/subscribe$" => "modules/subscribe.php",
)
);

@ -170,6 +170,12 @@ body
font-size: 18px;
}
p.error
{
font-weight: bold;
color: #C50003;
}
/* Form */
#field_currency

@ -28,23 +28,30 @@
</div>
<div class="subscribe">
<h3>Subscribe to a recurring donation</h3>
<p>
My e-mail address is...
<input type="text" id="field_email" placeholder="you@provider.com">
</p>
<p>
... and I'd like to pledge
<select id="field_currency">
<option value="usd">$</option>
<option value="eur">€</option>
<option value="btc">BTC</option>
</select>
<input type="text" id="field_amount" value="5.00">
a month.
</p>
<p class="pledge-button">
<button class="green-button" id="button_subscribe">Pledge!</button>
</p>
{%if isempty|error == false}
<p class="error">
{%?error}
</p>
{%/if}
<form method="post" action="/campaign/{%?urlname}/subscribe">
<p>
My e-mail address is...
{%input type="text" name="email" id="field_email" placeholder="you@provider.com"}
</p>
<p>
... and I'd like to pledge
{%select name="currency" id="field_currency"}
{%option value="usd" text="$"}
{%option value="eur" text="€"}
{%option value="btc" text="BTC"}
{%/select}
{%input type="text" name="amount" id="field_amount" value="5.00"}
a month.
</p>
<p class="pledge-button">
<button type="submit" class="green-button" id="button_subscribe">Pledge!</button>
</p>
</form>
{%if can-donate-once == true}
<h3 class="section">One-off donation</h3>

@ -0,0 +1,11 @@
<h2>Hi. We've met before!</h2>
<p>
You have subscribed to this campaign in the past.
</p>
<p>
<strong>If your intention was to change your settings,</strong> please check the e-mail we just sent you.
It contains a verification link that will make the changes you requested, straight away.
</p>
<p>
Thanks for using ReDonate!
</p>

@ -0,0 +1,9 @@
<h2>Great! Only one more step...</h2>
<p>
We've sent you an e-mail to verify that you really wanted to subscribe - after all, it would
be a bad thing if someone else could subscribe you without your consent.
</p>
<p>
Please check the e-mail we've sent you, and click the verification link... and then you're
done!
</p>
Loading…
Cancel
Save