Adds support for membership_trial field
This commit is contained in:
1 parent
c9792e4b9b
commit
e9eb3dbe88
8 files changed
+126
-19
No files matched your search
@@ -71,20 +71,20 @@ if($data === NULL) {
|
||||
echo $crypt->json_encode_and_encrypt(array('error' => 'Can\'t decode body'));
|
||||
die();
|
||||
}
|
||||
/* Check type of registration */
|
||||
/* Check type of registration and set valid fields */
|
||||
$reg_type = "new";
|
||||
/* Renewal */
|
||||
if( isset($data['type']) && $data['type'] === "renewal" ) {
|
||||
/* Renewal */
|
||||
$required_keys = array('phone');
|
||||
$reg_type = $data['type'];
|
||||
} else {
|
||||
$required_keys = array('firstname', 'lastname', 'phone', 'email');
|
||||
}
|
||||
|
||||
/* Validate supplied data */
|
||||
$valid_keys = $required_keys;
|
||||
$valid_keys = array_merge($valid_keys, array('purchased', 'source', 'type'));
|
||||
$valid_keys = array_merge($valid_keys, array('purchased', 'source', 'type', 'membership_trial'));
|
||||
|
||||
/* Validate supplied data */
|
||||
foreach($required_keys as $key) {
|
||||
if(!array_key_exists($key, $data)) {
|
||||
set_response_code(400);
|
||||
@@ -183,6 +183,12 @@ if( isset($data['source']) && !in_array($data['source'], array('snapporder', 'sn
|
||||
die();
|
||||
}
|
||||
|
||||
/* Validate optional membership_trial */
|
||||
if( isset($data['membership_trial']) && $data['membership_trial'] !== "buddy") {
|
||||
set_response_code(400);
|
||||
echo $crypt->json_encode_and_encrypt(array('error' => 'Invalid value in field membership_trial: '.$data['membership_trial']));
|
||||
die();
|
||||
}
|
||||
|
||||
/* Create user */
|
||||
try {
|
||||
@@ -215,6 +221,11 @@ if($user['registration_status'] === "partial") {
|
||||
/* Add back phone number from query */
|
||||
$user['phone'] = $data['phone'];
|
||||
|
||||
if( isset($data['membership_trial']) ) {
|
||||
/* Add back membership_trial from query */
|
||||
$user['membership_trial'] = $data['membership_trial'];
|
||||
}
|
||||
|
||||
/* Return encrypted user object */
|
||||
echo $crypt->json_encode_and_encrypt($user);
|
||||
|
||||
|
||||
@@ -39,6 +39,12 @@ function add_user($data) {
|
||||
$data['expires'] = date_format(date_modify($data['purchased'], "+1 year"), "Y-m-d");
|
||||
unset($data['purchased']); // dont save purchase date
|
||||
|
||||
/* Trial membership (special case, ignored if not in autumn) */
|
||||
$in_autumn = intval(date("n")) >= 8 && intval(date("n")) <= 12;
|
||||
if( isset($data['membership_trial']) && $in_autumn) {
|
||||
$data['expires'] = date_format(date_create("first day of january next year"), "Y-m-d");
|
||||
}
|
||||
|
||||
$cols = array_keys($data);
|
||||
$values = array_values($data);
|
||||
|
||||
@@ -66,7 +72,11 @@ function add_user($data) {
|
||||
}
|
||||
|
||||
log_userupdate($user_id, "User registered."); // for legacy
|
||||
log_userupdate($user_id, "Medlemskap registrert via SnappOrder.");
|
||||
if( isset($data['membership_trial']) && $in_autumn) {
|
||||
log_userupdate($user_id, "Medlemskap registrert via SnappOrder. Prøvemedlemskap (".$data['membership_trial'].").");
|
||||
} else {
|
||||
log_userupdate($user_id, "Medlemskap registrert via SnappOrder.");
|
||||
}
|
||||
|
||||
return $user_id;
|
||||
}
|
||||
@@ -81,7 +91,7 @@ function get_user($user_id) {
|
||||
* - a date >= NOW(): Valid membership
|
||||
* - NULL: Lifelong membership
|
||||
*/
|
||||
$cols = array('id', 'firstname', 'lastname', 'email', 'expires', 'cardno', 'registration_status', 'birthdate');
|
||||
$cols = array('id', 'firstname', 'lastname', 'email', 'expires', 'cardno', 'registration_status', 'birthdate', 'membership_trial');
|
||||
$sql_group_ids = "GROUP_CONCAT(group_id) AS group_ids";
|
||||
$sql_is_member = "expires > NOW() OR expires IS NULL AS is_member";
|
||||
$sql = "SELECT ".implode($cols, ",").",$sql_group_ids,$sql_is_member FROM din_user AS users LEFT JOIN din_usergrouprelationship AS ug ON users.id=ug.user_id WHERE users.id=$user_id GROUP BY users.id";
|
||||
@@ -119,6 +129,9 @@ function get_user($user_id) {
|
||||
if($user['birthdate'] == "0000-00-00") {
|
||||
unset($user['birthdate']);
|
||||
}
|
||||
if($user['membership_trial'] === "") {
|
||||
unset($user['membership_trial']);
|
||||
}
|
||||
|
||||
/* FIXME: Double encode fields as utf-8... why? */
|
||||
foreach($user as $key => $value) {
|
||||
@@ -140,9 +153,9 @@ function renew_user($data) {
|
||||
unset($data['type']);
|
||||
|
||||
/* Our own values */
|
||||
$source = $data['source'];
|
||||
if( !isset($data['source']) ) {
|
||||
$source = "snapporder";
|
||||
$source = "snapporder";
|
||||
if( isset($data['source']) ) {
|
||||
$source = $data['source'];
|
||||
}
|
||||
unset($data['source']);
|
||||
|
||||
@@ -155,9 +168,17 @@ function renew_user($data) {
|
||||
$data['expires'] = date_format(date_modify($data['purchased'], "+1 year"), "Y-m-d");
|
||||
unset($data['purchased']); // dont save purchase date
|
||||
|
||||
/* Trial membership (special case, ignored if not in autumn) */
|
||||
$in_autumn = intval(date("n")) >= 8 && intval(date("n")) <= 12;
|
||||
if( isset($data['membership_trial']) && $in_autumn) {
|
||||
$data['expires'] = date_format(date_create("first day of january next year"), "Y-m-d");
|
||||
}
|
||||
|
||||
// FIXME: Decode UTF-8 values... why?
|
||||
foreach($data as $key=>$value) {
|
||||
$data[$key] = utf8_decode($value);
|
||||
if(is_string($value)) {
|
||||
$data[$key] = utf8_decode($value);
|
||||
}
|
||||
}
|
||||
|
||||
$res = $conn->autoExecute("din_user", $data, DB_AUTOQUERY_UPDATE, "id=$user_id");
|
||||
@@ -169,13 +190,17 @@ function renew_user($data) {
|
||||
/* Phonenumber table, set validated */
|
||||
$fields_values = array('validated' => 1);
|
||||
|
||||
$res = $conn->autoExecute("din_userphonenumber", $fields_values, DB_AUTOQUERY_UPDATE, "user_id=$user_id AND number='$number'");
|
||||
$res = $conn->autoExecute("din_userphonenumber", $fields_values, DB_AUTOQUERY_UPDATE, "user_id=$user_id AND number='$phone'");
|
||||
|
||||
if( DB::isError($res) ) {
|
||||
throw new InsideDatabaseException($res->getMessage().". DEBUG: ".$res->getDebugInfo());
|
||||
}
|
||||
|
||||
log_userupdate($user_id, "Medlemskap fornyet via SnappOrder ($source).");
|
||||
if( isset($data['membership_trial']) && $in_autumn) {
|
||||
log_userupdate($user_id, "Medlemskap registrert via SnappOrder ($source). Prøvemedlemskap (".$data['membership_trial'].").");
|
||||
} else {
|
||||
log_userupdate($user_id, "Medlemskap fornyet via SnappOrder ($source).");
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -3,3 +3,4 @@ ALTER TABLE `din_user` ADD `source` VARCHAR( 255 ) NOT NULL DEFAULT 'inside';
|
||||
ALTER TABLE `din_user` ADD `registration_status` VARCHAR( 255 ) NOT NULL DEFAULT 'full';
|
||||
ALTER TABLE `din_user` ADD `created` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP;
|
||||
ALTER TABLE `din_userphonenumber` ADD `validated` INT( 1 ) NOT NULL DEFAULT '0';
|
||||
ALTER TABLE `din_user` ADD `membership_trial` VARCHAR( 255 ) NOT NULL DEFAULT '';
|
||||
@@ -18,6 +18,7 @@ function test_get_query() {
|
||||
|
||||
if($decoded_result['phone'] === $number) {
|
||||
echo "OK";
|
||||
var_dump($decoded_result);
|
||||
} else {
|
||||
echo "FAIL";
|
||||
}
|
||||
@@ -40,7 +41,7 @@ function test_post_register() {
|
||||
$result = $crypt->decrypt(trim($result));
|
||||
$decoded_result = (array) json_decode($result);
|
||||
|
||||
if(isset($decoded_result['birthdate']) && $decoded_result['birthdate'] === "1983-01-01") {
|
||||
if(isset($decoded_result['membership_status']) && $decoded_result['membership_status'] === 1) {
|
||||
echo "OK";
|
||||
var_dump($decoded_result);
|
||||
} else {
|
||||
@@ -76,6 +77,64 @@ function test_post_register_renewal() {
|
||||
/* Register */
|
||||
curl_close ($ch);
|
||||
}
|
||||
//test_get_query();
|
||||
function test_post_register_buddy() {
|
||||
$crypt = new CryptoHelper(SNAP_IV, SNAP_KEY);
|
||||
$data = file_get_contents("./testuser_buddy.json");
|
||||
|
||||
$data = $crypt->encrypt($data);
|
||||
$ch = curl_init(API_URL.'/snapporder/api/register.php');
|
||||
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_ENCODING ,"");
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=UTF-8'));
|
||||
|
||||
$result = curl_exec($ch);
|
||||
$result = $crypt->decrypt(trim($result));
|
||||
$decoded_result = (array) json_decode($result);
|
||||
|
||||
$correct_expires = isset($decoded_result['expires']) && $decoded_result['expires'] === "2015-01-01";
|
||||
$has_param = isset($decoded_result['membership_trial']) && $decoded_result['membership_trial'] === "buddy";
|
||||
if($correct_expires && $has_param) {
|
||||
echo "OK";
|
||||
var_dump($decoded_result);
|
||||
} else {
|
||||
echo "FAIL\n";
|
||||
var_dump($decoded_result);
|
||||
}
|
||||
/* Register */
|
||||
curl_close ($ch);
|
||||
}
|
||||
function test_post_register_buddy_renewal() {
|
||||
$crypt = new CryptoHelper(SNAP_IV, SNAP_KEY);
|
||||
$data = file_get_contents("./testuser_buddy_renewal.json");
|
||||
|
||||
$data = $crypt->encrypt($data);
|
||||
$ch = curl_init(API_URL.'/snapporder/api/register.php');
|
||||
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_ENCODING ,"");
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=UTF-8'));
|
||||
|
||||
$result = curl_exec($ch);
|
||||
$result = $crypt->decrypt(trim($result));
|
||||
$decoded_result = (array) json_decode($result);
|
||||
|
||||
$correct_expires = isset($decoded_result['expires']) && $decoded_result['expires'] === "2015-01-01";
|
||||
$has_param = isset($decoded_result['membership_trial']) && $decoded_result['membership_trial'] === "buddy";
|
||||
if($correct_expires && $has_param) {
|
||||
echo "OK";
|
||||
var_dump($decoded_result);
|
||||
} else {
|
||||
echo "FAIL\n";
|
||||
var_dump($decoded_result);
|
||||
}
|
||||
/* Register */
|
||||
curl_close ($ch);
|
||||
}
|
||||
test_get_query();
|
||||
//test_post_register();
|
||||
test_post_register_renewal();
|
||||
//test_post_register_renewal();
|
||||
//test_post_register_buddy();
|
||||
//test_post_register_buddy_renewal();
|
||||
@@ -1,7 +1,6 @@
|
||||
{
|
||||
"firstname": "Üve",
|
||||
"lastname": "Bårdvigsen Røed",
|
||||
"email": "abaabazbasdfbfffaaaaaba@aaa44za.com",
|
||||
"birthdate": "1983-01-01",
|
||||
"phone": "+4744989787"
|
||||
"email": "abaab236zzazbasdfbfffaaaaaba@aaa44za.com",
|
||||
"phone": "+4749089787"
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"firstname": "Üve Buddy",
|
||||
"lastname": "Bårdvigsen Røed",
|
||||
"email": "abaaba23z5zazbasdfbfffaaaaaba@aaa44za.com",
|
||||
"phone": "+4749989787",
|
||||
"membership_trial": "buddy"
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"type": "renewal",
|
||||
"phone": "+4747052592",
|
||||
"membership_trial": "buddy"
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"type": "renewal",
|
||||
"phone": "+4744444444",
|
||||
"phone": "+4747052592",
|
||||
"purchased": "2014-05-12"
|
||||
}
|
||||
Reference in new issue
Block a user