diff --git a/wrenchboard/src/shared_tool/account.cc b/wrenchboard/src/shared_tool/account.cc index 8922ff68..2c02242d 100644 --- a/wrenchboard/src/shared_tool/account.cc +++ b/wrenchboard/src/shared_tool/account.cc @@ -377,7 +377,7 @@ long WrenchBoardMobileSendMoney(CVars in, CVars &out){ sprintf(send_trxid, "T%04lu", r1); logfmt(logINFO, "Recipient ID =====() %lu",in["bankid"].Long() ); -out["sendmoney_message"] ="Processing..."; + out["sendmoney_message"] ="Processing..."; REQ_LONG(in, "bankid", 1, -1); diff --git a/wrenchboard/src/shared_tool/smoney.cc b/wrenchboard/src/shared_tool/smoney.cc index 4a5c8b64..266fa9e8 100644 --- a/wrenchboard/src/shared_tool/smoney.cc +++ b/wrenchboard/src/shared_tool/smoney.cc @@ -10,6 +10,9 @@ #include "pgsql.h" #include "pgsql_wrapper.h" #include +#include "email.h" +#include "payments.h" +#include "common_tool.h" long smoney_calls(CVars in, CVars &out) { logfmt(logINFO, "smoney_calls()"); @@ -130,10 +133,14 @@ long member_sendmoney_fee(CVars in, CVars &out) { return out["processing_fee"].Long(); } +long WRB_doServiceTransferComplete(CVars in, CVars &out); +long do_transferPayment(CVars in) ; +long INTW_configure(CVars in, CVars &out) ; + long member_sendmoney(CVars in, CVars &out) { //plog->SetFileName( PREFIX"/logs/member_sendmoney.log" ); //return INTW_doServiceTransfer(in, out); - return -1L; + return WRB_doServiceTransferComplete(in, out); //return 0; } @@ -233,3 +240,365 @@ Referenced by: +long INTW_configure(CVars in, CVars &out) { + logfmt(logINFO, "long INTW_configure(CVars in, CVars &out)"); + long ret = PHP_API_BAD_PARAM; + try { + // Configure Interswitch SSL + + + out["interswitch_ver"] = CfgReadLong("interswitch.version"); + out["interswitch_sep"] = CfgReadChar("interswitch.soap_endpoint"); + out["interswitch_tid"] = CfgReadChar("interswitch.terminal_id"); + out["interswitch_code"] = CfgReadChar("interswitch.entity_code"); + out["interswitch_mkey"] = CfgReadChar("interswitch.master_key"); + out["interswitch_pin"] = CfgReadChar("interswitch.pin"); + out["interswitch_pfix"] = CfgReadChar("interswitch.prefix"); + out["interswitch_keyf"] = CfgReadChar("interswitch.keyfile"); + out["interswitch_pass"] = CfgReadChar("interswitch.password"); + out["interswitch_cert"] = CfgReadChar("interswitch.cacert"); + + ret = PHP_API_OK; + + } catch (bad_parameter) { + + out["status"] = "Bad parameter"; + + } catch (...) { + + out["status"] = "Unhandled exception in INTW_configure"; + + } + logfmt(logINFO, "/long INTW_configure(CVars in, CVars &out)"); + return ret; +} + + + + + +long do_transferPayment(CVars in) { + long ret = PHP_API_BAD_PARAM; + CVars out; + + long customer_id = REQ_LONG(in, "customer_id", 1, -1); + long transfer_id = REQ_LONG(in, "transfer_id", 1, -1); + long payMode = REQ_LONG(in, "mode", 1, -1); + long payment_id = 0; + long amount = 0; + long fee = 0; + long total = 0; + + + /* + CREATE TABLE sendmoney_payment + ( + id serial NOT NULL, + transfer_mode integer NOT NULL, + customer_id INT REFERENCES customer(id), + transfer_id INT REFERENCES interswitch_money_transfer(id), + amount integer DEFAULT 0, + fee integer DEFAULT 0, + total integer DEFAULT 0, + current_balance integer DEFAULT 0, + new_balance integer DEFAULT 0, + status integer DEFAULT 1, + added TIMESTAMP DEFAULT NOW() + ); + */ + + ret = load_db_record(out, "SELECT balance FROM members WHERE id = %lu AND status = 1", customer_id); + if (ret < 1L) { + throw new bad_parameter(out, "senderid"); + } + + if (payMode == PAYMENT_MODE) { + amount = REQ_LONG(in, "amount", 1, -1); + fee = REQ_LONG(in, "fee", 1, -1); + total = amount + fee; // make sure you are cent here + + if (out["balance"].Long() >= total) // do you have this money + { + CVars x; + x["transfer_mode"] = PAYMENT_MODE; + x["transfer_mode"].set_valid(true); + x["member_id"] = customer_id; + x["member_id"].set_valid(true); + x["transfer_id"] = transfer_id; + x["transfer_id"].set_valid(true); + x["amount"] = amount; + x["amount"].set_valid(true); + x["fee"] = fee; + x["fee"].set_valid(true); + x["total"] = total; + x["total"].set_valid(true); + x["current_balance"] = out["balance"]; + x["current_balance"].set_valid(true); + payment_id = 0; + + out["payment_id"] = insert_db_record(DBS_VALID, "sendmoney_payment", "sendmoney_payment_id_seq", x); + char confirmation[20] = ""; + Confirmation(payment_id, confirmation, sizeof (confirmation)); + + + + + ret = PHP_API_BAD_PARAM; // it is still a failure if we get here and did not take the money out of the balance + + CVars y; + y["member_id"] = x["member_id"]; + y["member_id"].set_valid(true); + y["sendmoney_id"] = x["transfer_id"]; + y["sendmoney_id"].set_valid(true); + y["code"] = "SMPAY"; + y["code"].set_valid(true); + y["dir"] = DIR_TARGET; + y["dir"].set_valid(true); + //y["payment_id"] = out["payment_id"]; y["payment_id"].set_valid( true ); + + ret = WrenchSendMoneyPayment(y, out); + + if (ret == PHP_CREATED_OK) { + load_db_record(out, "SELECT balance AS new_balance FROM members WHERE id = %lu", customer_id); + pgsql_exec("UPDATE sendmoney_payment SET new_balance=%lu WHERE id=%lu", out["new_balance"].Long(), out["payment_id"].Long()); + ret = PHP_API_OK; + } + } + } + + if (payMode == REFUND_MODE) { + if (load_db_record(out, "SELECT * FROM sendmoney_payment WHERE transfer_id = %lu AND member_id = %lu", transfer_id, customer_id)) { + logfmt(logINFO, "*****WE RE DO VERIFICATION and NO DUPLICATES)"); + logfmt(logINFO, "**)"); + logfmt(logINFO, "**)"); + logfmt(logINFO, "**)"); + logfmt(logINFO, "**)"); + logfmt(logINFO, "**)"); + logfmt(logINFO, "**)"); + logfmt(logINFO, "**)"); + logfmt(logINFO, "**)"); + CVars out; + /*REQ_STRING( in, "TransferCode", 3, 30, "(.*)" ); + REQ_STRING( in, "requestReference", 3, 30, "(.*)" );*/ + + //int vRet = INTW_querySendMoneyTransaction(in, out); + ret = PHP_API_OK; + } + + + } + + //PAYMENT_MODE + //REFUND_MODE + + return ret; +} +//*****************************************************************************/ + +long WRB_doServiceTransferComplete(CVars in, CVars &out) { + //plog->SetFileName( PREFIX"/logs/send_money.log" ); + + logfmt(logINFO, "long WRB_doServiceTransferComplete(CVars in, CVars &out)"); + long ret = PHP_API_BAD_PARAM; + try { + + out = in; + // Initiate interswitch session + CVars setup; + INTW_configure(in, setup); + + + in["action"].set_valid(false); + REQ_STRING(in, "InitiatingEntityCode", 3, 3, "(.*)"); + REQ_LONG(in, "InitiatingAmount", 1, -1); + REQ_LONG(in, "Fee", 1, -1); + REQ_LONG(in, "InitiatingChannel", 1, -1); + REQ_STRING(in, "InitiatingPaymentMethodCode", 2, 2, "(.*)"); + REQ_STRING(in, "InitiatingCurrencyCode", 3, 3, "(.*)"); + REQ_STRING(in, "TerminatingPaymentMethodCode", 2, 2, "(.*)"); + REQ_LONG(in, "TerminatingAmount", 1, -1); + REQ_STRING(in, "TerminatingCurrencyCode", 3, 3, "(.*)"); + REQ_STRING(in, "TerminatingCountryCode", 2, 2, "(.*)"); + + //REQ_STRING( in, "TerminatingEntityCode", 1, 5, "(.*)" ); + //REQ_STRING( in, "TerminatingAccountNumber", 1, 50, "(.*)" ); + //REQ_STRING( in, "TerminatingAccountType", 2, 2, "(.*)" ); + + long senderid = REQ_LONG(in, "senderid", 1, -1); + long recipientid = REQ_LONG(in, "recipientid", 1, -1); + + in["InitiatingEntityCode"] = "MBA"; + in["InitiatingEntityCode"].set_valid(true); + + + CVars sender; // Load sender + ret = load_db_record(sender, "SELECT firstname,lastname,balance FROM members WHERE id = %lu AND status = 1", senderid); + if (ret < 1L) { + throw new bad_parameter(out, "senderid"); + } + + long LongTotalCost = in["Fee"].Long() + in["TerminatingAmount"].Long() + 0; + + if (sender["balance"].Long() < LongTotalCost) { + throw new bad_parameter(out, "insufficient balance for transaction"); + } + + + + CVars recipient; // Load recipient + /*recipient["firstname"] = in["firstname"]; recipient["firstname"].set_valid( true ); + recipient["lastname"] = in["lastname"]; recipient["lastname"].set_valid( true ); + recipient["account_no"] = in["account_no"]; recipient["account_no"].set_valid( true ); + recipient["bank_code"] = in["bank_code"]; recipient["bank_code"].set_valid( true ); + recipient["account_type"] = in["account_type"]; recipient["account_type"].set_valid( true );*/ + ret = load_db_record(recipient, "SELECT firstname,lastname,account_no,bank_code,account_type FROM sendmoney_recipient WHERE id = %lu AND member_id = %lu AND status = 1", recipientid, senderid); + if (ret < 1L) { + throw new bad_parameter(out, "recipientid"); + } + + in["TerminatingEntityCode"] = recipient["bank_code"]; + in["TerminatingEntityCode"].set_valid(true); + in["TerminatingAccountNumber"] = recipient["account_no"]; + in["TerminatingAccountNumber"].set_valid(true); + in["transactionID"] = in["transaction_id"]; + in["transactionID"].set_valid(true); + + CVars inx; + inx["Fee"] = in["Fee"]; + inx["Fee"].set_valid(true); + inx["InitiatingAmount"] = in["InitiatingAmount"]; + inx["InitiatingAmount"].set_valid(true); + + inx["InitiatingChannel"] = in["InitiatingChannel"]; + inx["InitiatingChannel"].set_valid(true); + inx["InitiatingCurrencyCode"] = in["InitiatingCurrencyCode"]; + inx["InitiatingCurrencyCode"].set_valid(true); + + inx["InitiatingEntityCode"] = in["InitiatingEntityCode"]; + inx["InitiatingEntityCode"].set_valid(true); + inx["InitiatingPaymentMethodCode"] = in["InitiatingPaymentMethodCode"]; + inx["InitiatingPaymentMethodCode"].set_valid(true); + + inx["TerminatingAccountNumber"] = in["TerminatingAccountNumber"]; + inx["TerminatingAccountNumber"].set_valid(true); + inx["TerminatingAmount"] = in["TerminatingAmount"]; + inx["TerminatingAmount"].set_valid(true); + + inx["TerminatingCountryCode"] = in["TerminatingCountryCode"]; + inx["TerminatingCountryCode"].set_valid(true); + inx["TerminatingCurrencyCode"] = in["TerminatingCurrencyCode"]; + inx["TerminatingCurrencyCode"].set_valid(true); + + inx["TerminatingEntityCode"] = in["TerminatingEntityCode"]; + inx["TerminatingEntityCode"].set_valid(true); + inx["TerminatingPaymentMethodCode"] = in["TerminatingPaymentMethodCode"]; + inx["TerminatingPaymentMethodCode"].set_valid(true); + + inx["recipientid"] = in["recipientid"]; + inx["recipientid"].set_valid(true); + inx["member_id"] = in["senderid"]; + inx["member_id"].set_valid(true); + inx["transactionID"] = in["transactionID"]; + inx["transactionID"].set_valid(true); + + inx["transfer_code"] = ""; + inx["transfer_code"].set_valid(true); + + + out["money_transfer_id"] = insert_db_record(DBS_VALID, "money_transfer", " money_transfer_id_seq", inx); + if (out["money_transfer_id"].Long() < 1L) { + throw new runtime_error("Failed to create money transfer record"); + } + + const PGresult *res = pgsql_query("SELECT * FROM money_transfer WHERE id=%lu AND member_id = %lu", out["money_transfer_id"].Long(), in["member_id"].Long()); + if (res != NULL && pgsql_num_rows(res) > 0) { + + } else { + throw new runtime_error("Failed to create money_transfer record"); + } + + CVars transaction; + transaction["detail_id"] = out["money_transfer_id"]; + transaction["detail_id"].set_valid(true); + transaction["type"] = 2L; + /* DoTransfer */ transaction["type"].set_valid(true); + out["transaction_id"] = insert_db_record(DBS_VALID, "money_transfer_transaction", "money_transfer_transaction_id_seq", transaction); + if (out["transaction_id"].Long() < 1L) { + throw new runtime_error("Failed to create money_transfer_transaction record"); + } + CVars transaction_final; + + char xtid[12], uniqueRef[20]; + memset(uniqueRef, 0, 20); + // TransferCode - RequestReference - UniqueRef => request prefix for terminal owner + ID + sprintf(uniqueRef, "%s%s000000", setup["interswitch_pfix"].c_str(), out["transaction_id"].c_str()); + //sprintf( uniqueRef, "%s%-09lu", setup["interswitch_pfix"].c_str(), out["transaction_id"].Long() ); + + transaction["transfer_code"] = uniqueRef; + transaction["transfer_code"].set_valid(true); + transaction_final["transfer_code"] = transaction["transfer_code"]; + transaction_final["transfer_code"].set_valid(true); + + pgsql_exec("UPDATE money_transfer SET transfer_code='%s' WHERE id=%lu", transaction_final["transfer_code"].c_str(), out["money_transfer_id"].Long()); + + //sprintf( xtid, "%s", setup["interswitch_tid"].c_str() ); + sprintf(xtid, "%s", setup["interswitch_tid"].c_str()); + + + //in["SenderEmail"] = sender["email"]; in["SenderEmail"].set_valid(true); + //in["BeneficiaryEmail"] = recipient["email"]; in["BeneficiaryEmail"].set_valid(true); + + //*TAKE THE MONEY NOW + CVars pm; + pm["customer_id"] = senderid; + pm["customer_id"].set_valid(true); + pm["transfer_id"] = out["money_transfer_id"]; + pm["transfer_id"].set_valid(true); + pm["amount"] = in["InitiatingAmount"]; + pm["amount"].set_valid(true); + pm["fee"] = in["Fee"]; + pm["fee"].set_valid(true); + pm["mode"] = PAYMENT_MODE; + pm["mode"].set_valid(true); + + /* + #define SM_PENDING 1 + #define SM_CANCEL 3 + #define SM_FAILED 4 + #define SM_COMPLETED 5 + */ + if (do_transferPayment(pm) == PHP_API_OK) { + out["sendmoney_status"] = SM_PENDING; + in["sendmoney_id"] = out["money_transfer_id"]; + ret = PHP_API_OK; + sendmoney_email(SM_PENDING, in, out); + // all is good on money side + logfmt(logINFO, "*** ALL Good ------ ****** "); + } else { + out["sendmoney_status"] = SM_FAILED; + throw new runtime_error("Failed to create payment record"); + } + + load_db_record(out, "SELECT m.*,mp.confirmation,r.firstname||' '||r.lastname AS Recitient,r.account_no,b.name AS bank_name FROM money_transfer m LEFT JOIN members_payments mp ON mp.what_sendmoney = m.id LEFT JOIN sendmoney_recipient r ON r.id = m.recipientid LEFT JOIN bank_entity_codes b ON b.code = r.bank_code WHERE m.id=%lu", out["money_transfer_id"].Long()); + + } catch (runtime_error& ex) { + + out["status"] = ex.what(); + ret = PHP_API_BAD_PARAM; + + } catch (bad_parameter) { + + out["status"] = "Invalid input"; + ret = PHP_API_BAD_PARAM; + + } catch (...) { + + out["status"] = "Unhandled exception in INTW_doTransfer"; + ret = PHP_API_BAD_PARAM; + + } + + logfmt(logINFO, "/long INTW_doTransfer(CVars in, CVars &out)"); + return ret; + +}