Skip to content

Commit

Permalink
Merge pull request #114 from vhs/vhs/Payments
Browse files Browse the repository at this point in the history
Vhs/payments
  • Loading branch information
laftho committed Nov 11, 2015
2 parents 84fbcd2 + f7b9994 commit 463231b
Show file tree
Hide file tree
Showing 35 changed files with 528 additions and 152 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ conf/config.ini.php
backup
*.gz
*.zip
logs/*.log


#################
Expand Down
2 changes: 1 addition & 1 deletion app/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
require_once("../conf/config.ini.php");
require_once("include.php");

$serverLog = (DEBUG) ? new \vhs\loggers\FileLogger(dirname(__FILE__) . "/server.log") : new \vhs\loggers\SilentLogger();
$serverLog = (DEBUG) ? new \vhs\loggers\FileLogger(dirname(__FILE__) . "/../logs/server.log") : new \vhs\loggers\SilentLogger();

\vhs\web\HttpContext::Init(new \vhs\web\HttpServer(new \vhs\web\modules\HttpServerInfoModule("Nomos"), $serverLog));

Expand Down
19 changes: 19 additions & 0 deletions app/contracts/IPaymentService1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/**
* Created by PhpStorm.
* User: Thomas
* Date: 07/01/2015
* Time: 5:50 PM
*/

namespace app\contracts;


use vhs\services\IContract;

interface IPaymentService1 extends IContract {
/**
* @permission administrator|user
*/
public function GetPaginated($offset, $limit);
}
10 changes: 9 additions & 1 deletion app/domain/Membership.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,20 @@ public function validate(ValidationResults &$results) {

}

/**
* @param $code
* @return Membership[]
*/
public static function findByCode($code) {
return Membership::where(
Where::Equal(MembershipSchema::Columns()->code, $code)
);
}

/**
* @param $price
* @return Membership
*/
public static function findForPriceLevel($price) {
$memberships = Membership::where(
Where::_And(
Expand All @@ -41,7 +49,7 @@ public static function findForPriceLevel($price) {
), OrderBy::Descending(MembershipSchema::Columns()->price), 1
);

if (!is_null($memberships))
if (!is_null($memberships) && count($memberships) > 0)
return $memberships[0];

return null;
Expand Down
12 changes: 6 additions & 6 deletions app/domain/Metric.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

namespace app\domain;

use app\schema\PaymentSchema;
use app\schema\UserSchema;
use app\schema\PaymentsSchema;
use app\domain\User;
use vhs\database\Database;
use vhs\database\On;
Expand Down Expand Up @@ -55,19 +55,19 @@ public static function NewMemberCount($start, $end) {
public static function TotalMemberCount($start, $end) {
$where = Where::_And(
Where::Equal(UserSchema::Columns()->active,"y"),
Where::Equal(PaymentsSchema::Columns()->status, 1),
Where::Equal(PaymentSchema::Columns()->status, 1),
Where::_And(
Where::GreaterEqual(PaymentsSchema::Columns()->date, date('Y-m-d 00:00:00', $start)),
Where::Lesser(PaymentsSchema::Columns()->date, date('Y-m-d 00:00:00', $end))
Where::GreaterEqual(PaymentSchema::Columns()->date, date('Y-m-d 00:00:00', $start)),
Where::Lesser(PaymentSchema::Columns()->date, date('Y-m-d 00:00:00', $end))
)
);

$query = Query::count(UserSchema::Table(), $where);

$joinPayments = Join::Left(
PaymentsSchema::Table,
PaymentSchema::Table,
On::Where(
Where::Equal(UserSchema::Columns()->id,PaymentsSchema::Columns()->user_id)
Where::Equal(UserSchema::Columns()->id,PaymentSchema::Columns()->user_id)
)
);
$query->Join($joinPayments);
Expand Down
37 changes: 37 additions & 0 deletions app/domain/Payment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/**
* Created by PhpStorm.
* User: Thomas
* Date: 11/12/2014
* Time: 5:26 PM
*/

namespace app\domain;


use app\schema\PaymentSchema;
use vhs\database\Database;
use vhs\database\queries\Query;
use vhs\database\wheres\Where;
use vhs\domain\Domain;
use vhs\domain\validations\ValidationResults;

class Payment extends Domain {
public static function Define() {
Payment::Schema(PaymentSchema::Type());
}

public static function exists($txn_id) {
return Database::exists(
Query::select(
PaymentSchema::Table(),
PaymentSchema::Columns(),
Where::Equal(PaymentSchema::Columns()->txn_id, $txn_id)
)
);
}

public function validate(ValidationResults &$results) {

}
}
13 changes: 13 additions & 0 deletions app/domain/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,31 @@ private function validateEmail(ValidationResults &$results) {
$results->add(new ValidationFailure("Invalid e-mail address"));
}

/**
* @param $username
* @return User[]
*/
public static function findByUsername($username) {
return User::where(
Where::Equal(UserSchema::Columns()->username, $username)
);
}

/**
* @param $email
* @return User[]
*/
public static function findByEmail($email) {
return User::where(
Where::Equal(UserSchema::Columns()->email, $email)
);
}

/**
* @param string|null $username
* @param string|null $email
* @return boolean
*/
public static function exists($username = null, $email = null) {
$usernameWhere = Where::Equal(UserSchema::Columns()->username, $username);
$emailWhere = Where::Equal(UserSchema::Columns()->email, $email);
Expand Down
14 changes: 14 additions & 0 deletions app/endpoints/web/PaymentService1.svc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace app\endpoints\web;


use app\services\PaymentService;
use vhs\services\endpoints\JsonEndpoint;
use vhs\services\ServiceContext;

class PaymentService1 extends JsonEndpoint {
public function __construct() {
parent::__construct(new PaymentService(new ServiceContext($this)));
}
}
2 changes: 1 addition & 1 deletion app/include.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

define('ROOT_NAMESPACE_PATH', dirname(dirname(__FILE__)));

$sqlLog = (DEBUG) ? new \vhs\loggers\FileLogger(dirname(__FILE__) . "/sql.log") : new \vhs\loggers\SilentLogger();
$sqlLog = (DEBUG) ? new \vhs\loggers\FileLogger(dirname(__FILE__) . "/../logs/sql.log") : new \vhs\loggers\SilentLogger();

\vhs\database\Database::setLogger($sqlLog);
\vhs\database\Database::setRethrow(true);
Expand Down
148 changes: 148 additions & 0 deletions app/monitors/PaymentMonitor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<?php
/**
* Created by PhpStorm.
* User: Thomas
* Date: 16/08/2015
* Time: 12:56 AM
*/

namespace app\monitors;

use app\domain\Membership;
use app\domain\Payment;
use app\domain\User;
use app\security\PasswordUtil;
use app\services\EmailService;
use app\services\UserService;
use DateTime;
use vhs\Logger;
use vhs\monitors\Monitor;

class PaymentMonitor extends Monitor {

/** @var Logger */
private $logger;

public function Init(Logger &$logger = null)
{
$this->logger = &$logger;
Payment::onAnyCreated([$this, "paymentCreated"]);
}

private function log($message)
{
$this->logger->log("[PaymentMonitor] {$message}");
}

public function paymentCreated($args)
{
$emailService = new EmailService();

$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$domainName = $_SERVER['HTTP_HOST'].'/';

$host = $protocol.$domainName;

/** @var Payment $payment */
$payment = $args[0];

if ($payment->status == 1)
return;

/** @var User $user */
$user = null;
$users = User::findByEmail($payment->payer_email);

if (count($users) > 1) {
$this->log("Found more than one user for email '{$payment->payer_email}' unable to process payment");
return;
} elseif (count($users) == 1) {
$user = $users[0];
}

//TODO get membership type via item_name/item_number from payment record
/** @var Membership $membership */
$membership = Membership::findForPriceLevel($payment->rate_amount);
if (is_null($membership)) {
$memberships = Membership::findByCode("member");

if (!is_null($memberships) && count($memberships) == 1) {
$membership = $memberships[0];
} else {
$this->log("Missing membership type 'member'. Unable to process payment.");
return;
}
}

$userService = new UserService();

if (is_null($user)) { //new user
try {
$user = $userService->Create(
$payment->payer_email,
PasswordUtil::generate(),
$payment->payer_email,
$payment->payer_fname,
$payment->payer_lname,
$membership->id
);
} catch (\Exception $ex) {
//this shouldn't happen... we should've found the user by email otherwise...
$this->log($ex->getMessage());
return;
}

$emailService->EmailUser(
[ 'email' => NOMOS_FROM_EMAIL ],
'[Nomos] New User Created!',
'admin_newuser',
[
'email' => $payment->payer_email,
'fname' => $payment->payer_fname,
'lname' => $payment->payer_lname
]
);

} else {
if ($user->membership_id != $membership->id)
$userService->UpdateMembership($user->id, $membership->id);
}

$expiry = new DateTime($payment->date);
$expiry->add(new \DateInterval("P1M1W")); //add 1 month with a 1 week grace period

$user->mem_expire = $expiry->format("Y-m-d H:i:s");

$user->active = "y";

$user->save();

$payment->user_id = $user->id;
$payment->membership_id = $membership->id;
$payment->status = 1; //processed
$payment->save();

$emailService->EmailUser(
[ 'email' => NOMOS_FROM_EMAIL ],
'[Nomos] User payment made!',
'admin_payment',
[
'email' => $payment->payer_email,
'fname' => $payment->payer_fname,
'lname' => $payment->payer_lname,
'amount' => $payment->rate_amount,
'pp' => $payment->pp
]
);

$emailService->EmailUser(
$user,
'VHS Membership Payment Received!',
'payment',
[
'host' => $host,
'fname' => $user->fname,
]
);
}
}
Loading

0 comments on commit 463231b

Please sign in to comment.