I have a code that is responsible for add money to account. There are tariff plans on the page of the site, when you click pay, the user switches to paying the bill. The code is as follows:
public function pay(Request $request)
{
$ticketID = $request->get('tickID');
if($ticketID = 1){
$sum = 32;
}elseif($ticketID = 2){
$sum = 64;
}elseif($ticketID = 3){
$sum = 320;
}elseif($ticketID = 4){
$sum = 640;
}elseif($ticketID = 5){
$sum = 1600;
}elseif($ticketID = 6){
$sum = 3840;
}elseif($ticketID = 7){
$sum = 6400;
}elseif($ticketID = 8){
$sum = 9600;
}
$u = $this->user->steamid64;
$merchant_id = '423';
$secret_word1 = '432';
$sign = md5($merchant_id.':'.$sum.':'.$secret_word1.':'.$u);
$url = 'https://www.free-kassa.ru/merchant/cash.php?m='.$merchant_id.'&oa='.$sum.'&o='.$u.'&s='.$sign.'&lang=ru&i=&em=';
$returnValue = [
'redirect' => $url
];
return $returnValue;
}
function getIP() {
if (isset($_SERVER['HTTP_X_REAL_IP'])) return $_SERVER['HTTP_X_REAL_IP'];
return $_SERVER['REMOTE_ADDR'];
}
public function payaccept(Request $request)
{
$merchant_id = '534';
$secret_word2 = '423423';
if (!in_array(getIP(), array('144.76.93.115', '144.76.93.119', '78.47.60.198'))) die('hacking attempt!');
$sign = md5($merchant_id.':'.$_REQUEST['AMOUNT'].':'.$merchant_secret_2.':'.$_REQUEST['MERCHANT_ORDER_ID']);
if ($sign != $_REQUEST['SIGN']) die('wrong sign');
$summ = $_REQUEST['AMOUNT'];
$user = User::Where('steamid64',$_REQUEST['MERCHANT_ORDER_ID']);
if ($summ = 32) {
$user->money += $summ;
}elseif ($summ = 64) {
$user->money += $summ;
}elseif ($summ = 320) {
$user->money += $summ;
}elseif ($summ = 640) {
$user->money += round($summ*1.03);
}elseif ($summ = 1600) {
$user->money += round($summ*1.05);
}elseif ($summ = 3840) {
$user->money += round($summ*1.1);
}elseif ($summ = 6400) {
$user->money += round($summ*1.15);
}elseif ($summ = 9600) {
$user->money += round($summ*2);
}
$user->save();
die('YES');
}
Function pay forms a link, amount and digital signature. Function payaccept the function processes the payment and must enter payment information into the database.
The problem is in line $user = User::Where('steamid64',$_REQUEST['MERCHANT_ORDER_ID']); i know it with help var_dump().
This line produces a variable steamid64, but can't fint in a DB.
How i can fix it?
Well, given that you haven't put any error message or log.. I assume the error is this line:
$user = User::Where('steamid64',$_REQUEST['MERCHANT_ORDER_ID']);
Here you are creating the query... but not executing it. To do so add ->first() at the end.
$user = User::Where('steamid64',$_REQUEST['MERCHANT_ORDER_ID'])->first();
Then you will be able to call methods on the $user object.
I am trying to get each objects points and finally I need to add those points.
I am using foreach loop, but I get only one objects points as a result.
Please help me to get all objects points as sum
MODEL:
public function getTransactionPoint($transactionId)
{
$transaction = Transaction::find($transactionId);
$transactionCoupon = TCoupon::where('transaction_id', '=', $transactionId)->first();
$transactionPoint = TPoint::where('transaction_id', '=', $transactionId)->first();
$pointType = PointType::find($this->pointUser->point_type_id);
//to get transaction details
$trans_detail = DB::table('jocom_transaction_details AS a')
->select('a.*', 'b.*')
->leftJoin('jocom_products AS b', 'a.sku', '=', 'b.sku')
->where('a.transaction_id', '=', $transactionId)->get();
//for bpoints
if($pointType->type == 'BCard' )
{
foreach ($trans_detail as $value)
{
$date1=$value->valid_from;
$date2=$value->valid_to;
$startdate=strtotime($date1);
$enddate=strtotime($date2);
$in_range=false;
$out_of_range=false;
$nodate=false;
$today1= date("Y-m-d");
$today=strtotime($today1);
if(($today >= $startdate) && ($today <=$enddate))
{
$in_range=true;
}
else
{
$in_range=false;
$out_of_range=true;
}
if($in_range)
{
if($value->multibpoints)
{
$points = $value->multibpoints *$value->price;
}
elseif($value->bpoints)
{
$points = $value->bpoints;
}
}
if($out_of_range)
{
$points = ($value->price) * $pointType->earn_rate;
}
if ($points - floor($points) > 0.99) {
return floor($points) + 1;
} else {
return floor($points);
}
}//endforeach
}else
//for jpoints
foreach ($trans_detail as $key=>$value) {
$date3=$value->valid_from;
$date4=$value->valid_to;
$startdatee=strtotime($date3);
$enddatee=strtotime($date4);
$in_rangee=false;
$out_of_rangee=false;
$nodatee=false;
$today3= date("Y-m-d");
$today4=strtotime($today3);
if(($today4 >= $startdatee) && ($today4 <=$enddatee))
{
$in_rangee=true;
}
else
{
$in_rangee=false;
$out_of_rangee=true;
}
if($in_rangee)
{
if($value->multijpoints)
{
$points = $value->multijpoints * $value->price;
}
elseif ($value->jpoints) {
$points = $value->jpoints;
}
}
if($out_of_rangee)
{
$points = ($value->price) * $pointType->earn_rate;
}
if ($points - floor($points) > 0.99) {
return floor($points) + 1;
} else {
return floor($points);
}
}
}
}
}
Suppose product A has 22 jpoints and 33 bpoints and product b has 10 jpooints and 12 bpoints, I am getting only points of A product, but I need output as sum of product a and product b points
am trying to get each objects points and finally i need to add those points .am using foreach loop .but i am getting only one objects points as a result please help me to get all objects points as sum``
If I've understood your question correctly, the reason you're only getting the results for one product is because you're using return inside the loop. Instead, you can define a variable called something like $totalPoints, add to it and then return that value after the loop e.g.
$totalPoints = 0;
foreach ($trans_detail as $value) {
//The rest of the code
if ($points - floor($points) > 0.99) {
$totalPoints += floor($points) + 1;
} else {
$totalPoints += floor($points);
}
}
return $totalPoints;
Also, you don't have to use this but it would appear you can greatly simplify your code:
public function getTransactionPoint($transactionId)
{
$pointType = PointType::find($this->pointUser->point_type_id);
$trans_detail = DB::table('jocom_transaction_details AS a')
->select('a.*', 'b.*')
->leftJoin('jocom_products AS b', 'a.sku', '=', 'b.sku')
->where('a.transaction_id', '=', $transactionId)->get();
$pointsName = $pointType->type == 'BCard' ? 'bpoints' : 'jpoints';
$totalPoints = 0;
$today = strtotime(date('Y-m-d'));
foreach ($trans_detail as $value) {
if ($today >= strtotime($value->valid_from) && $today <= strtotime($value->valid_to)) {
$points = $value->multibpoints ? $value->multibpoints * $value->price : $value->$pointsName;
} else {
$points = ($value->price) * $pointType->earn_rate;
}
$totalPoints += $points - floor($points) > 0.99 ? floor($points) + 1 : floor($points);
}
return $totalPoints;
}
I trying to develop a function allowing to calculate a specific price in function the quantity discount
Example : 10% = $discount_customer[] in function the database
$qty is add by an user
if $qty < 5 then $$discount_customer[] = 0 : price does'nt change
if $qty > 5 et qty < 10 then $$discount_customer[] = 10% : price with -10%
...
if $qty > 10 then $$discount_customer[] = 15% : price with -15%
$id : id of the product
$qty : order quantity inserted by a customer
$products_price : price of the product
public function getProductsNewPriceByDiscountQuantity($id, $qty, $products_price) {
$OSCOM_Db = Registry::get('Db');
$OSCOM_Customer = Registry::get('Customer');
$QprodutsQuantityDiscount= $OSCOM_Db->prepare('select discount_quantity,
discount_customer
from :table_products_discount_quantity
where products_id = :products_id
and customers_group_id = :customers_group_id
and discount_quantity <> 0
');
$QprodutsQuantityDiscount->bindInt(':products_id', (int)$id );
$QprodutsQuantityDiscount->bindInt(':customers_group_id', $OSCOM_Customer->getCustomersGroupID());
$QprodutsQuantityDiscount->execute();
while ($QprodutsQuantityDiscount->fetch()) {
// quantity discount
$discount_quantity[] = $QprodutsQuantityDiscount->valueInt('discount_quantity');
// Customer discount
$discount_customer[] = $QprodutsQuantityDiscount->valueDecimal('discount_customer');
}
I suppose to create a foreach but how ?
Thie element doesn't take the between condition.
foreach ($discount_quantity as $k => $quantity) {
print_r('<pre>');
var_dump($quantity );
print_r('</pre>');
foreach ($discount_customer as $c => $discount) {
if ($qty > $quantity) {
$news_price = $products_price -(($products_price * $discount) /100);
}
print_r('<pre>');
print_r($news_price);
print_r('</pre>');
}
return $newprice
}
thank you
public function getProductsNewPriceByDiscountQuantity($id, $qty, $products_price) {
$OSCOM_Db = Registry::get('Db');
$OSCOM_Customer = Registry::get('Customer');
$QprodutsQuantityDiscount= $OSCOM_Db->prepare('select discount_quantity,
discount_customer
from :table_products_discount_quantity
where products_id = :products_id
and customers_group_id = :customers_group_id
and discount_quantity <> 0
');
$QprodutsQuantityDiscount->bindInt(':products_id', (int)$id );
$QprodutsQuantityDiscount->bindInt(':customers_group_id', $OSCOM_Customer->getCustomersGroupID());
$QprodutsQuantityDiscount->execute();
while ($QprodutsQuantityDiscount->fetch()) {
$discount_quantity[] = $QprodutsQuantityDiscount->valueInt('discount_quantity');
$discount_customer[] = $QprodutsQuantityDiscount->valueDecimal('discount_customer');
}
return $this->getPrctDiscount($qty, $products_price);
}
public function getPrctDiscount($qty, $products_price)
{
$discount = 0;
if ($qty > 5 && $qty < 10) {
$discount = 10;
}elseif ($qty > 10) {
$discount = 15;
}
$newPrice = $discount > 0
? $products_price - ($products_price * ($discount/100))
: $newPrice;
return $newPrice;
}
The sales notifications is not working because this line amountArray[$idproduct] += $amount; is returning me offset. I don't know how to fix it.
My full function is this:
function saveAllSaleDetails($idsale, $sale) {
$this->conexion->startTransaction();
$amountArray = [];
try {
foreach ($sale as $detail):
$idproduct = $detail['id'];
$amount = $detail['amount'];
$price = $detail['price'];
$subtotal = $detail['subtotal'];
$iduser = 1;
$this->saveSaleDetail($idsale, $idproduct, $amount, $price, $subtotal, $iduser);
$amountArray[$idproduct] += $amount;
$stock = $this->product->getProductStock($idproduct);
$stock = $stock[0][0] - $amountArray[$idproduct];
if ($stock <= 20) {
$product = $this->product->getProductById($idproduct);
$message = $product[0][1]." stock is bellow 20.";
notification::add($message, $idproduct, 'warning', 'product.php');
}
endforeach;
$this->conexion->commit();
$this->conexion->cerrar();
return true;
} catch (Exception $e) {
$this->conexion->rollback();
$this->conexion->cerrar();
var_dump($e->getMessage());
return false;
}
}
The problem is because of this line,
$amountArray[$idproduct] += $amount;
The above statement can be expanded as
$amountArray[$idproduct] = $amountArray[$idproduct] + $amount;
Initially during the first iteration of foreach loop, $amountArray[1] and $amountArray[2] are not set, and hence you're getting these undefined offset error.
So instead of
$amountArray[$idproduct] += $amount;
Do this:
$amountArray[$idproduct] = isset($amountArray[$idproduct]) ? $amountArray[$idproduct] + $amount : $amount;
Is there any way I could rewrite this function with an array instead of all these if statements? Could i maybe use some for loop together with an array? How would that look like? Any suggestions of simpler code?
Here is my php function:
function to_next_level($point) {
/*
**********************************************************************
*
* This function check how much points user has achievents and how much procent it is until next level
*
**********************************************************************
*/
$firstlevel = "3000";
$secondlevel = "7000";
$thirdlevel = "15000";
$forthlevel = "28000";
$fifthlevel = "45000";
$sixthlevel = "80000";
if($point <= $firstlevel) {
$total = ($point/$firstlevel) * 100;
$remaining = round($total);
//echo number_format($remaining, 0, '.', ' ');
return $remaining;
} elseif ($point <= $secondlevel) {
$total = ($point/$secondlevel) * 100;
$remaining = round($total);
//echo number_format($remaining, 0, '.', ' ');
return $remaining;
} elseif ($point <= $thirdlevel) {
$total = ($point/$thirdlevel) * 100;
$remaining = round($total);
//echo number_format($remaining, 0, '.', ' ');
return $remaining;
} elseif ($point <= $forthlevel) {
$total = ($point/$forthlevel) * 100;
$remaining = round($total);
//echo number_format($remaining, 0, '.', ' ');
return $remaining;
} elseif ($point <= $fifthlevel) {
$total = ($point/$fifthlevel) * 100;
$remaining = round($total);
//echo number_format($remaining, 0, '.', ' ');
return $remaining;
} elseif ($point <= $sixthlevel) {
$total = ($point/$sixthlevel) * 100;
$remaining = round($total);
//echo number_format($remaining, 0, '.', ' ');
return $remaining;
}
}
Try this:
function to_next_level($point) {
/*
**********************************************************************
*
* This function check how much points user has achievents and how much procent it is until next level
*
**********************************************************************
*/
$levelArray = array(3000, 7000, 15000, 28000, 45000, 80000);
foreach ($levelArray as $level)
{
if ($point <= $level) {
$total = ($point/$level) * 100;
$remaining = round($total);
//echo number_format($remaining, 0, '.', ' ');
return $remaining;
}
}
}
Start using OOP programming style. This is the perfect opportunity since it is a task without much complexity. Create a class that acts as central authority. That class can receive more methods over time. That way your code stays easy to maintain since all those functions are kept inside a class.
<?php
class levelAuthority
{
public static $thresholds = [ 3000, 7000, 15000, 28000, 45000, 80000 ];
public static function getDistanceToNextlevel($points)
{
foreach (self::$thresholds as $threshold) {
if ($points <= $threshold) {
$total = ($points/$threshold) * 100;
$remaining = round($total);
return $remaining;
}
}
}
}
// in the calling scope:
$points = 5000;
echo levelAuthority::getDistanceToNextlevel($points);
lots of answers to this!!
here is mine using a while loop - single exit point outside the loop:
function to_next_level($point) {
/*
**********************************************************************
*
* This function check how much points user has achievements and how much percent it is until next level
*
**********************************************************************
*/
$arr_level = array(3000,15000,28000,45000,80000);
$remaining = false;
while (!$remaining and list($key,$level) = each($arr_level)) {
if ($point <= $level) {
$total = ($point/$level) * 100;
$remaining = round($total);
}
}
// will return false if $point is greater than highest value in $arr_level
return $remaining;
}
You could write an additional function, that does the calculations and trigger it from the if/else if/else blocks.
function calculate_remaining($points, $level) {
$total = ($point/$level) * 100;
$remaining = round($total);
return $remaining;
}
You'd trigger this like:
if($point <= $firstlevel) {
return $calculate_remaining($point, $firstlevel);
} elseif ($point <= $secondlevel) {
return $calculate_remaining($point, $secondlevel);
} etc.
What about something like this?
function to_next_level($point)
{
$levels = array(
3000,
7000,
15000,
28000,
45000,
80000
);
foreach ($levels as $level)
{
if ($point <= $level)
{
$total = ($point / $level) * 100;
$remaining = round($total);
//echo number_format($remaining, 0, '.', ' ');
return $remaining;
}
}
}
The point levels are in order in the array, so [0] is $firstlevel, and so on. You simply iterate through the array and return whenever we reach the condition where $point is <= to the the $level.
Also, since $levels is static, it can be defined outside of the function.
simple:
<?php
$point = 100;
$remaining = 0;
$data = [
'firstlevel' => 3000,
'secondlevel' => 7000,
'thirdlevel' => 15000,
'forthlevel' => 28000,
'fifthlevel' => 45000,
'sixthlevel' => 80000
];
foreach($data as $item)
{
if($point <= $item)
{
$remaining = round(($point / $item ) * 100); //or return val
}
}
How about putting your variable into array and loop it?
function to_next_level($point) {
$data[0] = "3000";
$data[1] = "7000";
$data[2] = "15000";
$data[3] = "28000";
$data[4] = "45000";
$data[5] = "80000";
foreach ($data as $key => $value) {
if($point <= $value) {
$total = ($point/$value) * 100;
$remaining = round($total);
return $remaining;
}
}
}
I haven't try it. But it might work for you.