This commit is contained in:
2020-02-05 07:15:32 -05:00
parent 5d9da78cf6
commit 67159b0132
3 changed files with 382 additions and 402 deletions
+75 -54
View File
@@ -142,27 +142,27 @@ long SessionCheck(long uid, const char *sessionid, int create) {
return -1L; // Invalif parameters return -1L; // Invalif parameters
} }
// Clean old sessions // Clean old sessions
pgsql_exec("DELETE FROM members_session WHERE uid=%ld AND updated < (now() - interval '15 minutes')", uid); pgsql_exec("DELETE FROM members_session WHERE member_id=%ld AND updated < (now() - interval '15 minutes')", uid);
// Update/check existing session // Update/check existing session
if (create == 0 && pgsql_exec("UPDATE members_session SET updated=NOW() WHERE uid=%ld AND sessionid='%s'", uid, sessionid) > 0) { if (create == 0 && pgsql_exec("UPDATE members_session SET updated=NOW() WHERE member_id=%ld AND session='%s'", uid, sessionid) > 0) {
return 1L; // Session updated return 1L; // Session updated
} }
if (create > 0) { if (create > 0) {
// Check session i? // Check session i?
const PGresult *res = pgsql_query("SELECT * FROM members_session WHERE uid=%ld AND sessionid<>'%s'", uid, sessionid); const PGresult *res = pgsql_query("SELECT * FROM members_session WHERE member_id=%ld AND session<>'%s'", uid, sessionid);
if (res != NULL && pgsql_num_rows(res) > 0) { if (res != NULL && pgsql_num_rows(res) > 0) {
return -2L; // Active sessions found return -2L; // Active sessions found
} }
CVars sess; // Do we have the same session already? CVars sess; // Do we have the same session already?
if (load_db_record(sess, "SELECT * FROM members_session WHERE uid=%lu AND sessionid='%s'", uid, sessionid) > 0) { if (load_db_record(sess, "SELECT * FROM members_session WHERE member_id=%lu AND session='%s'", uid, sessionid) > 0) {
pgsql_exec("UPDATE members_session SET updated=NOW() WHERE uid=%ld AND sessionid='%s'", uid, sessionid); pgsql_exec("UPDATE members_session SET updated=NOW() WHERE member_id=%ld AND session='%s'", uid, sessionid);
return sess["id"].Long(); return sess["id"].Long();
} }
// Create a new session // Create a new session
sess["uid"] = uid; sess["member_id"] = uid;
sess["uid"].set_valid(true); sess["member_id"].set_valid(true);
sess["sessionid"] = sessionid; sess["session"] = sessionid;
sess["sessionid"].set_valid(true); sess["session"].set_valid(true);
long sid = insert_db_record(DBS_VALID, "members_session", "members_session_id_seq", sess); long sid = insert_db_record(DBS_VALID, "members_session", "members_session_id_seq", sess);
if (sid > 0) { if (sid > 0) {
return sid; // New session created return sid; // New session created
@@ -180,10 +180,12 @@ long LoginCoreGradeAccount(CVars in, CVars &out) {
REQ_STRING(in, "password", 5, 49, "(.*)"); REQ_STRING(in, "password", 5, 49, "(.*)");
REQ_STRING(in, "sessionid", 4, 40, "(.*)"); REQ_STRING(in, "sessionid", 4, 40, "(.*)");
OPTIONAL(in, "loc") REQ_STRING(in, "loc", 3, 15, "(.*)"); OPTIONAL(in, "loc") REQ_STRING(in, "loc", 3, 15, "(.*)");
ret = load_db_record(out, "SELECT *,id AS member_id FROM members WHERE status=1 AND LOWER(username)=LOWER('%s') AND password= md5('%s')", in["username"].c_str(), in["password"].c_str()); ret = load_db_record(out, "SELECT UPPER( md5( now()::text ) ) AS sessionid, *,id AS member_id FROM members WHERE status=1 AND LOWER(username)=LOWER('%s') AND password= md5('%s')", in["username"].c_str(), in["password"].c_str());
if (ret) { if (ret) {
if (SessionCheck(out["id"].Long(), in["sessionid"].c_str(), 1) > 0) { if (SessionCheck(out["member_id"].Long(), out["sessionid"].c_str(), 1) > 0) {
out["stauts"] = "OK"; out["stauts"] = "OK";
ret = PHP_API_OK; ret = PHP_API_OK;
} else { } else {
@@ -203,21 +205,24 @@ long CreateCoreGradeAccountPending(CVars in, CVars &out) {
out = in; out = in;
/* /*
CREATE TABLE members_pending ( CREATE TABLE members_pending (
id SERIAL, id SERIAL,
username VARCHAR(15) UNIQUE NOT NULL, username VARCHAR(15) UNIQUE NOT NULL,
firstname VARCHAR(50), firstname VARCHAR(50),
lastname VARCHAR(50), lastname VARCHAR(50),
email VARCHAR(50), email VARCHAR(50),
phone VARCHAR(25), phone VARCHAR(25),
status integer DEFAULT 1, status integer DEFAULT 1,
added timestamp without time zone DEFAULT now(), added timestamp without time zone DEFAULT now(),
verify_link VARCHAR(100), verify_link VARCHAR(100),
expire timestamp without time zone DEFAULT now() +'30 days' expire timestamp without time zone DEFAULT now() +'30 days'
); );
ALTER TABLE ONLY members_pending ALTER TABLE ONLY members_pending
ADD CONSTRAINT members_pending_id_key UNIQUE (id); ADD CONSTRAINT members_pending_id_key UNIQUE (id);
*/ */
REQ_STRING(in, "username", 5, 49, "(.*)"); REQ_STRING(in, "username", 5, 49, "(.*)");
REQ_STRING(in, "firstname", 2, 49, "(.*)"); REQ_STRING(in, "firstname", 2, 49, "(.*)");
REQ_STRING(in, "lastname", 2, 49, "(.*)"); REQ_STRING(in, "lastname", 2, 49, "(.*)");
@@ -225,6 +230,9 @@ ADD CONSTRAINT members_pending_id_key UNIQUE (id);
OPTIONAL(in, "phone") REQ_STRING(in, "phone", 3, 15, "(.*)"); OPTIONAL(in, "phone") REQ_STRING(in, "phone", 3, 15, "(.*)");
OPTIONAL(in, "loc") REQ_STRING(in, "loc", 3, 15, "(.*)"); OPTIONAL(in, "loc") REQ_STRING(in, "loc", 3, 15, "(.*)");
out["pending_id"] = "0";
out["status_message"] = "Pending";
int r1, r2, r3; int r1, r2, r3;
char verify_link[100]; char verify_link[100];
srand(time(NULL)); srand(time(NULL));
@@ -238,32 +246,45 @@ ADD CONSTRAINT members_pending_id_key UNIQUE (id);
const char *loc = getenv("REMOTE_ADDR"); const char *loc = getenv("REMOTE_ADDR");
CVars x;
x["username"] = in["username"];
x["username"].set_valid(true);
x["firstname"] = in["firstname"];
x["firstname"].set_valid(true);
x["lastname"] = in["lastname"];
x["lastname"].set_valid(true);
x["email"] = in["email"];
x["email"].set_valid(true);
x["phone"] = in["phone"];
x["phone"].set_valid(true);
x["loc"] = loc;
x["loc"].set_valid(true);
x["verify_link"] = verify_link;
x["verify_link"].set_valid(true);
ret = insert_db_record(DBS_VALID, "members_pending", "members_pending_id_seq", x);
if (ret > 0) { const PGresult *res = pgsql_query("SELECT * FROM members WHERE LOWER(username) = LOWER('%s') ", in["username"].c_str());
pgsql_query("UPDATE members SET password = md5('%s') WHERE id = %lu", in["password"].c_str(), ret);
out["pending_id"] = ret; if (res != NULL && pgsql_num_rows(res) > 0) {
out["pending_id"].set_valid(true); out["status_message"] = "Unable to create account, try another username or reset password";
x["pending_id"] = ret; } else {
x["pending_id"].set_valid(true); // dulpicate condition satisfied
AccountPendingMail(x);
CVars x;
x["username"] = in["username"];
x["username"].set_valid(true);
x["firstname"] = in["firstname"];
x["firstname"].set_valid(true);
x["lastname"] = in["lastname"];
x["lastname"].set_valid(true);
x["email"] = in["email"];
x["email"].set_valid(true);
x["phone"] = in["phone"];
x["phone"].set_valid(true);
x["loc"] = loc;
x["loc"].set_valid(true);
x["verify_link"] = verify_link;
x["verify_link"].set_valid(true);
ret = insert_db_record(DBS_VALID, "members_pending", "members_pending_id_seq", x);
if (ret > 0) {
pgsql_query("UPDATE members_pending SET password = md5('%s') WHERE id = %lu", in["password"].c_str(), ret);
out["pending_id"] = ret;
out["pending_id"].set_valid(true);
x["pending_id"] = ret;
x["pending_id"].set_valid(true);
AccountPendingMail(x);
}
} }
return ret; return ret;
} }
//****************************************************************************** //******************************************************************************
@@ -272,9 +293,9 @@ long CreateCoreGradeAccount(CVars in, CVars &out) {
logfmt(logINFO, "CreateCoreGradeAccount()"); logfmt(logINFO, "CreateCoreGradeAccount()");
long ret = PHP_API_BAD_PARAM; long ret = PHP_API_BAD_PARAM;
REQ_STRING(in, "verify_link", 2, 120, "(.*)"); REQ_STRING(in, "verify_link", 2, 120, "(.*)");
// REQ_STRING(in, "pass1", 3, 15, "(.*)"); // REQ_STRING(in, "pass1", 3, 15, "(.*)");
OPTIONAL(in, "loc") REQ_STRING(in, "loc", 3, 15, "(.*)"); OPTIONAL(in, "loc") REQ_STRING(in, "loc", 3, 15, "(.*)");
const char *loc = getenv("REMOTE_ADDR"); const char *loc = getenv("REMOTE_ADDR");
try { try {
long lonkF = load_db_record(out, "SELECT *,id AS pending_id FROM members_pending WHERE verify_link ='%s' AND expire > now() LIMIT 1", in["verify_link"].c_str()); long lonkF = load_db_record(out, "SELECT *,id AS pending_id FROM members_pending WHERE verify_link ='%s' AND expire > now() LIMIT 1", in["verify_link"].c_str());
if (lonkF) { if (lonkF) {
@@ -292,7 +313,7 @@ long CreateCoreGradeAccount(CVars in, CVars &out) {
x["phone"].set_valid(true); x["phone"].set_valid(true);
x["loc"] = loc; x["loc"] = loc;
x["loc"].set_valid(true); x["loc"].set_valid(true);
x["password"] = out["password"];; x["password"] = out["password"];
x["password"].set_valid(true); x["password"].set_valid(true);
ret = insert_db_record(DBS_VALID, "members", "members_id_seq", x); ret = insert_db_record(DBS_VALID, "members", "members_id_seq", x);
@@ -302,10 +323,10 @@ long CreateCoreGradeAccount(CVars in, CVars &out) {
out["member_id"].set_valid(true); out["member_id"].set_valid(true);
x["member_id"] = ret; x["member_id"] = ret;
x["member_id"].set_valid(true); x["member_id"].set_valid(true);
// pgsql_query("UPDATE members SET password = md5('%s') WHERE id = %lu", in["pass1"].c_str(), ret); // pgsql_query("UPDATE members SET password = md5('%s') WHERE id = %lu", in["pass1"].c_str(), ret);
pgsql_query("UPDATE members_pending SET status = 5 WHERE id = %lu", out["pending_id"].Long()); pgsql_query("UPDATE members_pending SET status = 5,verified=now() WHERE id = %lu", out["pending_id"].Long());
AccountPendingMail(x); // AccountPendingMail(x); welcome email
} }
+89 -104
View File
@@ -12,119 +12,104 @@
#include "cfg.h" #include "cfg.h"
#include <curl/curl.h> #include <curl/curl.h>
long groups_calls(CVars in, CVars &out) {
logfmt(logINFO, "groups_calls()");
out["result"] = "YES I GET TO BACK END";
long action = REQ_LONG(in, "action", 0, -1);
switch (action) {
long groups_calls(CVars in, CVars &out) case COREGRADE_GROUP_ACCEPTGROUP:
{ //return LoginCoreGradeAccount( in, out);
logfmt( logINFO, "groups_calls()" ); break;
out["result"] = "YES I GET TO BACK END";
long action = REQ_LONG( in, "action", 0, -1);
switch( action )
{
case COREGRADE_GROUP_INVITEGROUP:
case COREGRADE_GROUP_ACCEPTGROUP: //return CreateCoreGradeAccountPending(in, out);
//return LoginCoreGradeAccount( in, out); break;
break;
case COREGRADE_GROUP_CREATEGROUP:
case COREGRADE_GROUP_INVITEGROUP: return CreateCoreGradeGroup(in, out);
//return CreateCoreGradeAccountPending(in, out); break;
break; }
logfmt(logINFO, "/groups_calls()");
case COREGRADE_GROUP_CREATEGROUP: return 0;
return CreateCoreGradeGroup( in, out);
break;
}
logfmt( logINFO, "/groups_calls()" );
return 0;
} }
long CreateCoreGradeGroup(CVars in, CVars &out) long CreateCoreGradeGroup(CVars in, CVars &out) {
{ long ret = PHP_API_BAD_PARAM;
long ret = PHP_API_BAD_PARAM; out = in;
out =in;
REQ_STRING (in, "group_name", 5, 99, "(.*)"); REQ_STRING(in, "group_name", 5, 99, "(.*)");
OPTIONAL( in, "description" ) REQ_STRING (in, "description", 1, 249, "(.*)"); OPTIONAL(in, "description") REQ_STRING(in, "description", 1, 249, "(.*)");
REQ_LONG( in, "contribute", 1, -1 ); REQ_LONG(in, "contribute", 1, -1);
REQ_LONG( in, "member_id", 1, -1 ); REQ_LONG(in, "member_id", 1, -1);
OPTIONAL( in, "loc" ) REQ_STRING (in, "loc", 3, 15, "(.*)"); OPTIONAL(in, "loc") REQ_STRING(in, "loc", 3, 15, "(.*)");
CVars x;
x["group_name"] = in["group_name"]; x["group_name"].set_valid( true );
x["description"] = in["description"]; x["description"].set_valid( true );
x["invite"] = "1"; x["invite"].set_valid( true );
x["member_id"] = in["member_id"]; x["member_id"].set_valid( true );
x["loc"] = in["loc"]; x["loc"].set_valid( true );
ret = insert_db_record( DBS_VALID, "members_groups", "members_groups_id_seq", x );
if ( ret > 0 ) CVars x;
{ x["group_name"] = in["group_name"];
if ( in["contribute"].Long() > 0 ) x["group_name"].set_valid(true);
{ x["description"] = in["description"];
pgsql_exec("UPDATE members_groups SET contribute=NOW() WHERE id=%lu ", ret); x["description"].set_valid(true);
} x["invite"] = "1";
out["group_id"] = ret; out["group_id"].set_valid( true ); x["invite"].set_valid(true);
x["group_id"] = ret; x["group_id"].set_valid( true ); x["member_id"] = in["member_id"];
CVars y; x["member_id"].set_valid(true);
y["member_id"] = in["member_id"]; y["member_id"].set_valid( true ); x["loc"] = in["loc"];
y["admin_status"] = in["member_id"]; y["admin_status"].set_valid( true ); x["loc"].set_valid(true);
y["group_id"] = out["group_id"]; y["group_id"].set_valid( true ); ret = insert_db_record(DBS_VALID, "members_groups", "members_groups_id_seq", x);
y["loc"] = in["loc"]; y["loc"].set_valid( true );
//ALTER TABLE group_members ADD admin_status INT REFERENCES members(id); if (ret > 0) {
CoreGradeGroupCreateMember(y,out); if (in["contribute"].Long() > 0) {
CreateCoreGradeGroupMail(y); pgsql_exec("UPDATE members_groups SET contribute=NOW() WHERE id=%lu ", ret);
} }
return ret; out["group_id"] = ret;
out["group_id"].set_valid(true);
x["group_id"] = ret;
x["group_id"].set_valid(true);
CVars y;
y["member_id"] = in["member_id"];
y["member_id"].set_valid(true);
y["admin_status"] = in["member_id"];
y["admin_status"].set_valid(true);
y["group_id"] = out["group_id"];
y["group_id"].set_valid(true);
y["loc"] = in["loc"];
y["loc"].set_valid(true);
//ALTER TABLE group_members ADD admin_status INT REFERENCES members(id);
CoreGradeGroupCreateMember(y, out);
CreateCoreGradeGroupMail(y);
}
return ret;
} }
long CoreGradeGroupCreateMember(CVars in, CVars &out) long CoreGradeGroupCreateMember(CVars in, CVars &out) {
{ long ret = PHP_API_BAD_PARAM;
long ret = PHP_API_BAD_PARAM; out = in;
out =in; REQ_LONG(in, "member_id", 1, -1);
REQ_LONG( in, "member_id", 1, -1 ); REQ_LONG(in, "group_id", 1, -1);
REQ_LONG( in, "group_id", 1, -1 ); OPTIONAL(in, "loc") REQ_STRING(in, "loc", 3, 15, "(.*)");
OPTIONAL( in, "loc" ) REQ_STRING (in, "loc", 3, 15, "(.*)");
CVars x;
x["group_id"] = in["group_id"]; x["group_id"].set_valid( true );
x["member_id"] = in["member_id"]; x["member_id"].set_valid( true );
x["loc"] = in["loc"]; x["loc"].set_valid( true );
if (in["admin_status"].Long() > 0)
{
x["admin_status"] = in["admin_status"]; x["admin_status"].set_valid( true );
}
ret = insert_db_record( DBS_VALID, "group_members", "group_members_id_seq", x );
if ( ret > 0 ) CVars x;
{ out["group_member_id"] = ret; out["group_member_id"].set_valid( true ); x["group_id"] = in["group_id"];
x["group_member_id"] = ret; x["group_member_id"].set_valid( true ); x["group_id"].set_valid(true);
GroupCreateMemberMail(x); x["member_id"] = in["member_id"];
pgsql_exec("UPDATE members_groups SET accepted = (SELECT count(*) FROM group_members WHERE group_id = %lu ) WHERE id=%lu",x["group_id"].Long(),x["group_id"].Long() ); x["member_id"].set_valid(true);
} x["loc"] = in["loc"];
return ret; x["loc"].set_valid(true);
if (in["admin_status"].Long() > 0) {
x["admin_status"] = in["admin_status"];
x["admin_status"].set_valid(true);
}
ret = insert_db_record(DBS_VALID, "group_members", "group_members_id_seq", x);
if (ret > 0) {
out["group_member_id"] = ret;
out["group_member_id"].set_valid(true);
x["group_member_id"] = ret;
x["group_member_id"].set_valid(true);
GroupCreateMemberMail(x);
pgsql_exec("UPDATE members_groups SET accepted = (SELECT count(*) FROM group_members WHERE group_id = %lu ) WHERE id=%lu", x["group_id"].Long(), x["group_id"].Long());
}
return ret;
} }
//****************************************************************************** //******************************************************************************
/*
CREATE TABLE group_members (
id SERIAL,
member_id INT REFERENCES members(id),
group_name VARCHAR(100) UNIQUE NOT NULL,
description VARCHAR(250),
added timestamp without time zone DEFAULT now(),
balance integer DEFAULT 0,
contribute timestamp without time zone,
alert integer DEFAULT 0,
status integer DEFAULT 1,
invite integer DEFAULT 0,
accepted integer DEFAULT 0,
rejected integer DEFAULT 0,
loc INET
);
ALTER TABLE ONLY members_groups
ADD CONSTRAINT members_groups_id_key UNIQUE (id);
*/
+218 -244
View File
@@ -16,263 +16,237 @@
#include "account.h" #include "account.h"
#include "storeface.h" #include "storeface.h"
long CreateBulkTopupBatch(CVars in, CVars &out) {
long ret = 0;
REQ_STRING(in, "batch_name", 1, 50, "(.*)");
REQ_LONG(in, "customer_id", 1, -1);
CVars cust;
cust["customer_id"] = in["customer_id"];
cust["customer_id"].set_valid(true);
cust["batch_name"] = in["batch_name"];
cust["batch_name"].set_valid(true);
out["bulk_id"] = insert_db_record(DBS_VALID, "bulktopup_batch", "bulktopup_batch_id_seq", cust);
if (out["bulk_id"].Long() > 0) {
ret = out["bulk_id"].Long();
}
return ret;
long CreateBulkTopupBatch(CVars in, CVars &out)
{
long ret = 0;
REQ_STRING( in, "batch_name", 1, 50, "(.*)");
REQ_LONG( in, "customer_id", 1, -1 );
CVars cust;
cust["customer_id"] = in["customer_id"]; cust["customer_id"].set_valid(true);
cust["batch_name"] = in["batch_name"]; cust["batch_name"].set_valid(true);
out["bulk_id"] = insert_db_record( DBS_VALID, "bulktopup_batch", "bulktopup_batch_id_seq", cust );
if (out["bulk_id"].Long() > 0 )
{
ret = out["bulk_id"].Long();
}
return ret;
}
long AddBulkTopupBatchItem(CVars in, CVars &out)
{
long ret = 0;
/*
CREATE TABLE bulktopup_batch_items
(
id serial NOT NULL,
bulk_id INT REFERENCES bulktopup_batch(id),
customer_id INT REFERENCES customer(id),
topup_name VARCHAR(50),
topup_number VARCHAR(15) NOT NULL,
topup_amount INT DEFAULT 0,
topup_network VARCHAR(10),
order_id VARCHAR(25),
deliver_status VARCHAR(15),
status integer DEFAULT 1,
added TIMESTAMP DEFAULT NOW()
);
*/
REQ_STRING( in, "topup_name", 1, 50, "(.*)");
REQ_STRING( in, "topup_number", 1, 15, "(.*)");
REQ_STRING( in, "topup_network", 1, 15, "(.*)");
REQ_LONG( in, "topup_amount", 1, -1 );
REQ_LONG( in, "customer_id", 1, -1 );
REQ_LONG( in, "bulk_id", 1, -1 );
CVars cust;
cust["topup_name"] = in["topup_name"]; cust["topup_name"].set_valid(true);
cust["topup_number"] = in["topup_number"]; cust["topup_number"].set_valid(true);
cust["topup_network"] = in["topup_network"]; cust["topup_network"].set_valid(true);
cust["topup_amount"] = in["topup_amount"]; cust["topup_amount"].set_valid(true);
cust["customer_id"] = in["customer_id"]; cust["customer_id"].set_valid(true);
cust["bulk_id"] = in["bulk_id"]; cust["bulk_id"].set_valid(true);
out["bulk_item_id"] = insert_db_record( DBS_VALID, "bulktopup_batch_items", "bulktopup_batch_items_id_seq", cust );
if (out["bulk_item_id"].Long() > 0 )
{
ret = out["bulk_item_id"].Long();
}
return ret;
}
long UpdateBulkTopupItem(CVars in, CVars &out)
{
long ret =0;
REQ_LONG( in, "bulk_id", 1, -1 );
REQ_LONG( in, "customer_id", 1, -1 );
if ( load_db_record( out, "SELECT id FROM bulktopup_batch WHERE id= %lu AND status <> 5 AND completed IS NULL",in["bulk_id"].Long() ) )
{
pgsql_exec( "UPDATE bulktopup_batch SET status = 2 WHERE completed IS NULL AND status <> 5 AND id=%lu AND customer_id = %lu",in["bulk_id"].Long(),in["customer_id"].Long());
ret = in["bulk_id"].Long();
}
return ret;
} }
long DeliverBulkTopupItem(CVars in, CVars &out) long AddBulkTopupBatchItem(CVars in, CVars &out) {
{ long ret = 0;
CVars xx;
CVars yy;
long ret = 0;
REQ_LONG( in, "bulk_id", 1, -1 );
REQ_LONG( in, "item_id", 1, -1 );
REQ_STRING( in, "payment_code", 1, 50, "(.*)");
if ( load_db_record( out, "SELECT gid AS paymentitemid FROM interswitch_biller_payment_items WHERE paymentcode = '%lu'",in["payment_code"].Long() ) )
{
in["paymentitemid"] = out["paymentitemid"];
xx["paymentitemid"] = out["paymentitemid"];
in["paymentitemid"].set_valid( true );
xx["paymentitemid"].set_valid( true );
}
else
{
return 0;
}
if ( load_db_record( out, "SELECT * FROM bulktopup_batch WHERE id= %lu AND status <> 5 AND completed IS NULL",in["bulk_id"].Long() ) )
{
if ( load_db_record( yy, "SELECT * FROM bulktopup_batch_items WHERE id= %lu AND status <> 5 AND completed IS NULL",in["item_id"].Long() ) )
{
xx["customer_id"]= out["customer_id"]; xx["customer_id"].set_valid( true );
xx["amount"]= yy["topup_amount"]; xx["amount"].set_valid( true );
xx["recipient_no"]= yy["topup_number"]; xx["topup_number"].set_valid( true );
xx["bulk_topup_item_id"] = yy["id"]; xx["bulk_topup_item_id"].set_valid( true );
// pgsql_exec( "UPDATE bulktopup_batch SET status = 2 WHERE completed IS NULL AND status <> 5 AND id=%lu AND customer_id = %lu",in["bulk_id"].Long(),in["customer_id"].Long());
xx["trans_not_id"] =CreateTopupOrder(xx,out);
if ( xx["trans_not_id"].Long() > 0 )
{
pgsql_exec( "UPDATE bulktopup_batch_items SET order_id = '%s', payment_code = '%s' WHERE id = %lu",out["orderid"].c_str(),in["payment_code"].c_str(),in["item_id"].Long() );
xx["customermobile"]=xx["recipient_no"]; xx["customermobile"].set_valid( true );
xx["customerid"]= xx["customer_id"]; xx["customerid"].set_valid( true ); // bad but true
xx["orderid"] =out["orderid"]; xx["orderid"].set_valid( true );
xx["order_id"] =out["orderid"]; xx["order_id"].set_valid( true );
xx["trans_not_id"].set_valid( true );
xx["payment_code"] = in["payment_code"]; xx["payment_code"].set_valid( true );
if ( DeliverTopOrderByBalance(xx,out) == 100 )
{
pgsql_exec( "UPDATE bulktopup_batch_items SET completed=now(),status=5 WHERE id = %lu",in["item_id"].Long() );
}
}
ret = in["bulk_id"].Long();
}
}
return ret; REQ_STRING(in, "topup_name", 1, 50, "(.*)");
REQ_STRING(in, "topup_number", 1, 15, "(.*)");
REQ_STRING(in, "topup_network", 1, 15, "(.*)");
REQ_LONG(in, "topup_amount", 1, -1);
REQ_LONG(in, "customer_id", 1, -1);
REQ_LONG(in, "bulk_id", 1, -1);
CVars cust;
cust["topup_name"] = in["topup_name"];
cust["topup_name"].set_valid(true);
cust["topup_number"] = in["topup_number"];
cust["topup_number"].set_valid(true);
cust["topup_network"] = in["topup_network"];
cust["topup_network"].set_valid(true);
cust["topup_amount"] = in["topup_amount"];
cust["topup_amount"].set_valid(true);
cust["customer_id"] = in["customer_id"];
cust["customer_id"].set_valid(true);
cust["bulk_id"] = in["bulk_id"];
cust["bulk_id"].set_valid(true);
out["bulk_item_id"] = insert_db_record(DBS_VALID, "bulktopup_batch_items", "bulktopup_batch_items_id_seq", cust);
if (out["bulk_item_id"].Long() > 0) {
ret = out["bulk_item_id"].Long();
}
return ret;
} }
long DeliverTopOrderByBalance(CVars in, CVars &out) long UpdateBulkTopupItem(CVars in, CVars &out) {
{ long ret = 0;
long retval=0; REQ_LONG(in, "bulk_id", 1, -1);
long process_status=0; REQ_LONG(in, "customer_id", 1, -1);
// in["amount"] - arrives as niara - send cents/kobo to interswitch if (load_db_record(out, "SELECT id FROM bulktopup_batch WHERE id= %lu AND status <> 5 AND completed IS NULL", in["bulk_id"].Long())) {
pgsql_exec("UPDATE bulktopup_batch SET status = 2 WHERE completed IS NULL AND status <> 5 AND id=%lu AND customer_id = %lu", in["bulk_id"].Long(), in["customer_id"].Long());
ret = in["bulk_id"].Long();
}
out["route_id"] = "0"; return ret;
out["route_etisalat"] = "0";
if( load_db_record( out,"SELECT id AS route_id FROM airtime_route WHERE payment_code ='%s'",in["payment_code"].c_str() ) )
{
out["route_etisalat"] = "1";
}else{ }
if ( in["pmode"].Long() == PAY_MODE_BALANCE ) // paying with balance
{
if ( load_db_record( out, "SELECT * FROM customer WHERE id = %lu",in["customer_id"].Long() ) )
{
long amount_in_cents = in["amount"].Long()*100;
if ( out["balance"].Long() < amount_in_cents )
{
out["status"] = "Insufficient Balance for Purchase";
out["status_code"] = "F0004";
}
else
{
in["amount"] = amount_in_cents;
in["amount"].set_valid( true );
//--------------------------------------------------
pgsql_exec( "UPDATE customer SET balance = balance - %lu WHERE id=%lu ",amount_in_cents,in["customer_id"].Long());
//--------------------------------------------------
//retval = INTW_sendBillPaymentAdvice( in, out );
if ( out["route_etisalat"].Long() > 0 && out["route_id"].Long() > 0 )
{
retval = DeliverMobilseEtisalat( in, out );
out["ResponseCode"] = retval;
}
else
{
retval = INTW_sendBillPaymentAdvice( in, out );
}
if (out["ResponseCode"].Long() == 90000L) {
process_status = 100; // completed
out["status"] = "Completed";
out["status_code"] = "A0001";
pgsql_exec( "UPDATE trans_notification SET completed = now() WHERE id=%lu AND customer_id = %lu ",in["trans_not_id"].Long(),in["customer_id"].Long());
out["email_alert"] = "I am sending Email of Success";
//CustomerAirSale( in);
VirtualAirSaleAlert(out);
}
else
{
//--------------------------------------------------
pgsql_exec( "UPDATE customer SET balance = balance + %lu WHERE id=%lu ",amount_in_cents,in["customer_id"].Long());
//--------------------------------------------------
VirtualAirSaleAlert(out);
out["email_alert"] = "I am sending Email of Failure";
out["status"] = "Unable to deliver topup";
out["status_code"] = "F0005";
}
}
}
else
{ // customer not found
out["status"] = "Account not found";
out["status_code"] = "F0003";
}
}
return process_status;
} }
long DeliverBulkTopupItem(CVars in, CVars &out) {
CVars xx;
CVars yy;
long ret = 0;
REQ_LONG(in, "bulk_id", 1, -1);
REQ_LONG(in, "item_id", 1, -1);
REQ_STRING(in, "payment_code", 1, 50, "(.*)");
long CreateTopupOrder(CVars in, CVars &out) if (load_db_record(out, "SELECT gid AS paymentitemid FROM interswitch_biller_payment_items WHERE paymentcode = '%lu'", in["payment_code"].Long())) {
{ in["paymentitemid"] = out["paymentitemid"];
CVars cust; xx["paymentitemid"] = out["paymentitemid"];
CVars xx; in["paymentitemid"].set_valid(true);
char xtid[12]; xx["paymentitemid"].set_valid(true);
int r; } else {
long ret =0; return 0;
}
REQ_LONG( in, "amount", 1, -1 ); if (load_db_record(out, "SELECT * FROM bulktopup_batch WHERE id= %lu AND status <> 5 AND completed IS NULL", in["bulk_id"].Long())) {
REQ_STRING( in, "recipient_no", 1, 15, "(.*)"); if (load_db_record(yy, "SELECT * FROM bulktopup_batch_items WHERE id= %lu AND status <> 5 AND completed IS NULL", in["item_id"].Long())) {
REQ_LONG( in, "customer_id", 1, -1 ); xx["customer_id"] = out["customer_id"];
xx["customer_id"].set_valid(true);
xx["amount"] = yy["topup_amount"];
xx["amount"].set_valid(true);
xx["recipient_no"] = yy["topup_number"];
xx["topup_number"].set_valid(true);
xx["bulk_topup_item_id"] = yy["id"];
xx["bulk_topup_item_id"].set_valid(true);
// pgsql_exec( "UPDATE bulktopup_batch SET status = 2 WHERE completed IS NULL AND status <> 5 AND id=%lu AND customer_id = %lu",in["bulk_id"].Long(),in["customer_id"].Long());
xx["trans_not_id"] = CreateTopupOrder(xx, out);
if (xx["trans_not_id"].Long() > 0) {
pgsql_exec("UPDATE bulktopup_batch_items SET order_id = '%s', payment_code = '%s' WHERE id = %lu", out["orderid"].c_str(), in["payment_code"].c_str(), in["item_id"].Long());
srand(time(NULL)); xx["customermobile"] = xx["recipient_no"];
r = abs( rand()*10000000); xx["customermobile"].set_valid(true);
xx["customerid"] = xx["customer_id"];
xx["customerid"].set_valid(true); // bad but true
xx["orderid"] = out["orderid"];
xx["orderid"].set_valid(true);
xx["order_id"] = out["orderid"];
xx["order_id"].set_valid(true);
xx["trans_not_id"].set_valid(true);
xx["payment_code"] = in["payment_code"];
xx["payment_code"].set_valid(true);
cust["orderid"] = r; if (DeliverTopOrderByBalance(xx, out) == 100) {
in["orderid"] = r; pgsql_exec("UPDATE bulktopup_batch_items SET completed=now(),status=5 WHERE id = %lu", in["item_id"].Long());
}
in["orderid"].set_valid(true); }
cust["amount"] = in["amount"]; cust["amount"].set_valid(true); ret = in["bulk_id"].Long();
cust["description"] = in["description"]; cust["description"].set_valid(true); }
cust["customer_id"] = in["customer_id"]; cust["customer_id"].set_valid(true);
cust["recipient_no"] = in["recipient_no"]; cust["recipient_no"].set_valid(true); }
return ret;
cust["orderid"].set_valid(true); }
out=in;
long DeliverTopOrderByBalance(CVars in, CVars &out) {
out["order_id"] = insert_db_record( DBS_VALID, "trans_notification", "trans_notification_id_seq", cust ); long retval = 0;
long process_status = 0;
// in["amount"] - arrives as niara - send cents/kobo to interswitch
if (out["order_id"].Long() > 0 )
{ out["route_id"] = "0";
out["route_etisalat"] = "0";
sprintf( xtid, "9%011lu", out["order_id"].Long() ); if (load_db_record(out, "SELECT id AS route_id FROM airtime_route WHERE payment_code ='%s'", in["payment_code"].c_str())) {
xx["orderid"] = xtid; out["route_etisalat"] = "1";
pgsql_exec( "UPDATE trans_notification SET orderid ='%s' WHERE id=%lu AND customer_id = %lu ",xtid,out["order_id"].Long(),in["customer_id"].Long()); } else {
out["orderid"] = xtid; }
ret = out["order_id"].Long();
}
else
{ if (in["pmode"].Long() == PAY_MODE_BALANCE) // paying with balance
out["orderid"]=""; {
} if (load_db_record(out, "SELECT * FROM customer WHERE id = %lu", in["customer_id"].Long())) {
return ret; long amount_in_cents = in["amount"].Long()*100;
if (out["balance"].Long() < amount_in_cents) {
out["status"] = "Insufficient Balance for Purchase";
out["status_code"] = "F0004";
} else {
in["amount"] = amount_in_cents;
in["amount"].set_valid(true);
//--------------------------------------------------
pgsql_exec("UPDATE customer SET balance = balance - %lu WHERE id=%lu ", amount_in_cents, in["customer_id"].Long());
//--------------------------------------------------
//retval = INTW_sendBillPaymentAdvice( in, out );
if (out["route_etisalat"].Long() > 0 && out["route_id"].Long() > 0) {
retval = DeliverMobilseEtisalat(in, out);
out["ResponseCode"] = retval;
}
else {
retval = INTW_sendBillPaymentAdvice(in, out);
}
if (out["ResponseCode"].Long() == 90000L) {
process_status = 100; // completed
out["status"] = "Completed";
out["status_code"] = "A0001";
pgsql_exec("UPDATE trans_notification SET completed = now() WHERE id=%lu AND customer_id = %lu ", in["trans_not_id"].Long(), in["customer_id"].Long());
out["email_alert"] = "I am sending Email of Success";
//CustomerAirSale( in);
VirtualAirSaleAlert(out);
} else {
//--------------------------------------------------
pgsql_exec("UPDATE customer SET balance = balance + %lu WHERE id=%lu ", amount_in_cents, in["customer_id"].Long());
//--------------------------------------------------
VirtualAirSaleAlert(out);
out["email_alert"] = "I am sending Email of Failure";
out["status"] = "Unable to deliver topup";
out["status_code"] = "F0005";
}
}
} else { // customer not found
out["status"] = "Account not found";
out["status_code"] = "F0003";
}
}
return process_status;
}
long CreateTopupOrder(CVars in, CVars &out) {
CVars cust;
CVars xx;
char xtid[12];
int r;
long ret = 0;
REQ_LONG(in, "amount", 1, -1);
REQ_STRING(in, "recipient_no", 1, 15, "(.*)");
REQ_LONG(in, "customer_id", 1, -1);
srand(time(NULL));
r = abs(rand()*10000000);
cust["orderid"] = r;
in["orderid"] = r;
in["orderid"].set_valid(true);
cust["amount"] = in["amount"];
cust["amount"].set_valid(true);
cust["description"] = in["description"];
cust["description"].set_valid(true);
cust["customer_id"] = in["customer_id"];
cust["customer_id"].set_valid(true);
cust["recipient_no"] = in["recipient_no"];
cust["recipient_no"].set_valid(true);
cust["orderid"].set_valid(true);
out = in;
out["order_id"] = insert_db_record(DBS_VALID, "trans_notification", "trans_notification_id_seq", cust);
if (out["order_id"].Long() > 0) {
sprintf(xtid, "9%011lu", out["order_id"].Long());
xx["orderid"] = xtid;
pgsql_exec("UPDATE trans_notification SET orderid ='%s' WHERE id=%lu AND customer_id = %lu ", xtid, out["order_id"].Long(), in["customer_id"].Long());
out["orderid"] = xtid;
ret = out["order_id"].Long();
} else {
out["orderid"] = "";
}
return ret;
} }