From 268e1b6a54b736c0e38c77a1433a69f27767d7f4 Mon Sep 17 00:00:00 2001 From: Olusesan Ameye Date: Wed, 1 Jun 2022 20:58:05 -0400 Subject: [PATCH] fix --- wrenchboard/src/include/payments.h | 5 +- wrenchboard/src/shared_tool/payments.cc | 86 ++++++++++++++++++++++++- 2 files changed, 88 insertions(+), 3 deletions(-) diff --git a/wrenchboard/src/include/payments.h b/wrenchboard/src/include/payments.h index ca128594..895fc6a0 100644 --- a/wrenchboard/src/include/payments.h +++ b/wrenchboard/src/include/payments.h @@ -12,8 +12,9 @@ long WrenchCanceContractPayment( CVars in, CVars &out ); long WrenchNewCardPayment( CVars in, CVars &out ); long WrenchCardRechargePayment( CVars in, CVars &out ); -long CheckWallet(long member_id,CVars in); -long DeductMemberWallet(long member_id,long wallet_id,long amount, long payment_id); +long CheckWallet(long member_id,CVars in); +long DeductMemberWallet(long member_id,long wallet_id,long amount, long payment_id); +long WrenchMemberCouponPayment( CVars in, CVars &out ); #endif diff --git a/wrenchboard/src/shared_tool/payments.cc b/wrenchboard/src/shared_tool/payments.cc index c86edfbb..8d401869 100644 --- a/wrenchboard/src/shared_tool/payments.cc +++ b/wrenchboard/src/shared_tool/payments.cc @@ -28,9 +28,91 @@ CREATE TABLE payment_types ( INSERT INTO payment_types(type_code,code,dir) VALUES('OFFER_PYM_DEPOSIT','OFDPS',0); INSERT INTO payment_types(type_code,code,dir) VALUES('OFFER_PYM_REFUND','OFRFD',1); -- // refund if offer was rejected or not accepted */ - long UpdateMemberWallet(long member_id,long wallet_id,long amount, long payment_id); +long WrenchMemberCouponPayment( CVars in, CVars &out ) +{ + logfmt(FLOG_MAX, "long WrenchMemberCouponPayment(CVars in, CVars &out)" ); + long ret = PHP_API_BAD_PARAM; + ULONG payment_id = 0; + const char * loc = getenv("REMOTE_ADDR"); + in["loc"] = loc; in["loc"].set_valid(true); + + long member_id = REQ_LONG( in, "member_id", 1, -1 ); + REQ_STRING (in, "code", 4, 15, "(.*)"); + long code_id = REQ_LONG( in, "code_id", 1, -1 ); + + // let us make sure you have the wallet + // do we have a wallet for this action + CVars vw; + vw["currency"] = "NAIRA"; // this might become a variable based on the country + vw["currency"].set_valid( true ); + long wallet_id = CheckWallet(member_id,vw); + if(!wallet_id || wallet_id == 0 ){ + return -1; // no wallet + } + in["curr_balance"] ="0"; + + long retDb = load_db_record( out, "SELECT amount AS curr_balance FROM members_wallet WHERE member_id = %lu AND currency='%s' ", member_id, vw["currency"].c_str()); + if (retDb) + { in["curr_balance"] =out["curr_balance"]; in["curr_balance"].set_valid( true ); + logfmt(FLOG_MAX, "Current balance Read ********************" ); + } + else { return -1; /* unable to read wallet*/ } + + + long retDb2 = load_db_record( out, "SELECT * FROM coupons_allocation WHERE member_id = %lu AND code ='%s' AND id = %lu AND active IS NULL", in["member_id"].Long(), in["code"].c_str(),code_id ); + if (retDb2) + { + in["coupon_amount"] =out["amount"]; in["coupon_amount"].set_valid( true ); + + CVars x; + x["member_id"] = member_id; x["member_id"].set_valid( true ); + x["code"] = "COUP"; x["code"].set_valid( true ); + x["dir"] = "1"; x["dir"].set_valid( true ); + x["loc"] = in["loc"]; x["loc"].set_valid( true ); + + x["curr_balance"] = in["curr_balance"]; x["curr_balance"].set_valid( true ); + x["amount"] = in["coupon_amount"]; x["amount"].set_valid( true ); + x["fee"] = "0"; x["fee"].set_valid( true ); + + // x["what_offer"] = offer_id; x["what_offer"].set_valid( true ); + x["what_coupon"] = code_id; x["what_coupon"].set_valid( true ); // assign coupon id + + + x["flags"] = FLAG_INIT; x["flags"].set_valid( true ); // starting the pprocess + payment_id = insert_db_record( DBS_VALID, "members_payments", "members_payments_id_seq", x ); + + if (payment_id) { + ret = PHP_CREATED_OK; + x["flags"] = FLAG_START; x["flags"].set_valid( true ); // done not completed yet + // now generate the confirmation + char confirmation[15] = ""; + Confirmation(payment_id, confirmation, sizeof (confirmation)); // this stamp the offer code directly in that call + + UpdateMemberWallet( x["member_id"].Long(), wallet_id,x["amount"].Long(), payment_id); // correct this dont send amount + + x["flags"] = FLAG_OK; x["flags"].set_valid( true ); + x["payment_id"] = payment_id; x["payment_id"].set_valid( true ); + pgsql_exec("UPDATE members_payments SET flags = %lu WHERE id = %lu",x["flags"].Long(),payment_id ); + pgsql_exec("UPDATE coupons_allocation SET active = now() WHERE id = %lu AND CODE='%s' ",code_id,in["code"].c_str() ); + load_db_record( out, "SELECT * FROM members_payments WHERE id = %lu ", payment_id ); + } else { + out["status"] = "Unable to create payment"; + } + + } + else + { out["status"] = "Invalid coupon"; + ret= PHP_API_BAD_PARAM; + } + + logfmt(FLOG_MAX, "/long WrenchMemberCouponPayment(CVars in, CVars &out)" ); + return ret; +} + + + /* call to verify the user have the wallet in place - or create it if this fail , dont continue with collecting money , no place to put it. @@ -200,6 +282,8 @@ long WrenchNewCardPayment( CVars in, CVars &out ) return payment_return; } + + long WrenchCardRechargePayment( CVars in, CVars &out ) { logfmt(FLOG_MAX, "long WrenchCardRechargePayment(CVars in, CVars &out)" );