Jump to content
Chrisj

help with charge formula

Recommended Posts

This code, that I didn't write, but am trying to modify, generates the cost of a video purchase.

One line (line 32) is a formula , I believe for setting a purchase price amount:

$amout += $video->video_play_price?$video->video_play_price:$video_cost;

Another line (line 50) is a formula for deducting to create another amount:

 $uploader_amount = $video_cost - $charge;

I think line 50 is correct except for I don't think $video_cost in the final price, but I'm no expert at reading this code. I tried this without success:

 $uploader_amount = $video_play_price - $charge;

 

Here is the full code, any help with line 50, will be appreciated.

<?php
if (IS_LOGGED == false) {
    $data = array('status' => 400, 'error' => 'Not logged in');
    echo json_encode($data);
    exit();
}

if (!empty($_POST['id'])) {

    if (!is_array($_POST['id'])) {
        $id_array[] = $_POST['id'];
    } else {
        $id_array = $_POST['id'];
    }

    // get cost video
    $db->where('name', 'video_play_price');
    $db_cost = $db->getOne('config');
    $video_cost = (float)$db_cost->value;

    $count_video = count($id_array);
    $user_id = $user->id;
    $wallet = (float)str_replace(',', '', $user->wallet);


	$amout = 0;
	foreach ($id_array as $id) {
            $video_id = (int)PT_Secure($id);

            // get video data
            $video = $db->where('id', $id)->getOne(T_VIDEOS);
			$amout += $video->video_play_price?$video->video_play_price:$video_cost;

	}

//   $amout = $video_cost * $count_video;

    $charge = ( $video_cost *0.50 );

    if ($wallet >= $amout) {

        $new_wallet = (string)($wallet - $amout);

        $db->startTransaction();

        $inserted_records = 0;
        foreach ($id_array as $id) {
            $video_id = (int)PT_Secure($id);

         $uploader_amount = $video_cost - $charge; 

            // get video data
            $video = $db->where('id', $id)->getOne(T_VIDEOS);


            // add data to paid table
            $insert_buy = $db->insert('u_paid_videos', [
                'id_user' => $user_id,
                'id_video' => $video_id,
                'session_key' => $_SESSION['session_key'],
                'video_play_price' => (string)$video_cost,
                'video_title' => $video->title,
                'user_id_uploaded' => $video->user_id,
                //'up_credit'=>$video_cost,
                'up_credit'=>$uploader_amount,
            ]);

            if ($insert_buy) { $inserted_records++; }
            //add wallet users' video
        $userwallet = $db->where('id', $video->user_id)->getOne(T_USERS);

        //$videouserwallet = $userwallet->wallet+$video_cost;
        $videouserwallet = $userwallet->wallet+$uploader_amount;
        $db->where('id', $video->user_id);
        $update_wallet = $db->update(T_USERS, [
          // 'wallet' => $videouserwallet,
            'wallet' => number_format($videouserwallet, 2, '.', ''),
        ]);
        }

        $db->where('id', $user_id);
        $update_wallet = $db->update(T_USERS, [
            'wallet' => $new_wallet,
        ]);


        if (($inserted_records == $count_video) && $update_wallet) {
            $db->commit();

            echo json_encode([
                'status' => 200
            ]);
            exit();
        } else {
            $db->rollback();

            echo json_encode([
                'status' => 400,
                'error' => 'Buy process error'
            ]);
            exit();
        }

    } else {

        echo json_encode([
            'status' => 400,
            'error_num' => 1,
            'error' => 'Not enough money'
        ]);
        exit();

    }

} else {

    echo json_encode([
        'status' => 400,
        'error' => 'Bad Request, Invalid or missing parameter'
    ]);
    exit();

}

 

Share this post


Link to post
Share on other sites
3 minutes ago, Chrisj said:

Another line (line 50) is a formula for deducting to create another amount:


 $uploader_amount = $video_cost - $charge;

I think line 50 is correct except for I don't think $video_cost in the final price, but I'm no expert at reading this code. I tried this without success:


 $uploader_amount = $video_play_price - $charge;

 

Without knowing your business model and what these terms mean and how they relate, how do you expect anyone else to tell you what is correct and what isn't? This an accounting problem, not a coding problem.

Share this post


Link to post
Share on other sites

Maybe it'll help if you can understand what the variables all represent.

* $video_cost is the default price for videos, because some videos might have a different amount
* $amout is the total cost to play the videos requested, and will use the video amount if there is one instead of the default $video_cost
* $charge is how much the site makes (?) per video
* $uploader_amount is supposed to be how much the uploader of each video to view gets for the view, and originally is the default video amount minus $charge

Like Barand said, the next question is what you want the site to do, and that's a business question not a coding question.

Share this post


Link to post
Share on other sites

Thanks for your replies.

Yes, "$uploader_amount is supposed to be how much the uploader of each video to view gets for the view, and originally is the default video amount minus $charge". The uploader can now optionally change the default price, and I believe it is this line:

$amout += $video->video_play_price?$video->video_play_price:$video_cost;

I'm trying to get it so when the default price is changed, and a purchase is made, $uploader_amount should then be how much the uploader of each video to view gets for the view, not the default video amount minus $charge, but new video price amount minus $charge.

These two attempts were unsuccessful:

$uploader_amount = $video_play_price - $charge;
$uploader_amount = $amout - $charge;

any additional help will be appreciated.

Edited by Chrisj

Share this post


Link to post
Share on other sites

Yes, if you want to use a $video_play_price variable then it has to be defined somewhere. But don't.

Remember how the "video get data" code got the price from $video if set or the default if not? You do the same thing when crediting the uploader. Which means that $uploader_amount variable isn't useful, as it doesn't account for the video price.

To be clear, either (a) your existing code has a bug that the uploader only gets half the default video price and your site keeps the rest, or (b) it's not a bug because that's what is supposed to happen... but I doubt this because that would mean the uploader is setting a price for you that doesn't affect what they get.

Anyways, if $video->video_play_price is set then it's the price set by the uploader, and if it's not set then the video amount is $video_cost. Do whatever you want with that value, like *0.5 if the uploader gets 50%.

Share this post


Link to post
Share on other sites

Yes, initially for testing purposes "the uploader only gets half the default video price and your site keeps the rest".

Regarding "that would mean the uploader is setting a price for you that doesn't affect what they get", if they raise the price they get a bigger 50%, right?

Currently, the uploader sets the price but isn't getting 50%

"do the same thing when crediting the uploader", can you give me an example, please?

 

Edited by Chrisj

Share this post


Link to post
Share on other sites
6 minutes ago, Chrisj said:

Regarding "that would mean the uploader is setting a price for you that doesn't affect what they get", if they raise the price they get a bigger 50%, right?

That's the thing. Currently that would not be the case. Because the amount they are credited is calculated as half of $video_cost which is the default price. If they set a higher amount, they still only get half of the default and the site gets the rest.

If they set their video with a custom price of half the default price, they would get 100% of the cost and the site would get nothing.

6 minutes ago, Chrisj said:

Currently, the uploader sets the price but isn't getting 50%

Again, half of the default price.

6 minutes ago, Chrisj said:

"do the same thing when crediting the uploader", can you give me an example, please?

You already have an example: the earlier code that was calculating the total for all the videos to play.

Share this post


Link to post
Share on other sites

Thank you again.

Yes, what I'm trying to change is the amount that uploaders are credited which is calculated as half of $video_cost which is the default price.

Yes, "if they set their video with a custom price of half the default price, they would get 100% of the cost and the site would get nothing", so I need to set default price as minimum somehow.

"already have an example: the earlier code that was calculating the total for all the videos to play", yes, but I don't know how or where to put that modified example, I didn't write this code, and my knowledge (as you can tell) is limited. any additional direction will be welcomed.

 

Share this post


Link to post
Share on other sites

Earlier there is code that looks up each video to play and adds the amounts (or defaults) to get the total cost to the user to play the videos. You see it and how it works? It grabs each video according to the ID.

Then you have code that credits the uploaders of the videos. It also grabs each video according to the ID. And just like how the earlier code was getting the value from it, you can get the value. And you can use that value (or the default) to calculate how much the uploader should get. Very much like how the earlier code did it.

Give it a shot and post what you came up with if you have problems.

Share this post


Link to post
Share on other sites

Thank you for your reply, but the code you describe, I don't see it and how it works. I understand, but I can't put it in code that you're suggesting

Share this post


Link to post
Share on other sites

Have you tried any other changes, more than what you had initially attempted?

Share this post


Link to post
Share on other sites

Yes, I've tried this without success:

 

$amout = 0;
	foreach ($id_array as $id) {
            $video_id = (int)PT_Secure($id);

            // get video data
            $video = $db->where('id', $id)->getOne(T_VIDEOS);
            $amout += $video->video_play_price?$video->video_play_price:$video_cost;

	}

    $charge = ( $amout *0.50 );

    if ($wallet >= $amout) {

        $new_wallet = (string)($wallet - $amout);

        $db->startTransaction();

        $inserted_records = 0;
        foreach ($id_array as $id) {
        $video_id = (int)PT_Secure($id);

        $uploader_amount = $amout - $charge; 

 

Share this post


Link to post
Share on other sites
On 12/10/2018 at 1:53 PM, Chrisj said:

These two attempts were unsuccessful:


$uploader_amount = $video_play_price - $charge;

$uploader_amount = $amout - $charge;

 

I recommend adding the following:

syslog(LOG_INFO,'$video_play_price: '.$video_play_price);
syslog(LOG_INFO,'$charge: '.$charge);
syslog(LOG_INFO,'$amout: '.$amout);
$uploader_amount = $video_play_price - $charge;
$uploader_amount = $amout - $charge;

If you don't know how to use syslog, research it.  If you don't want to (however, you should), do this:

echo('$video_play_price: '.$video_play_price.PHP_EOL);
echo('$charge: '.$charge.PHP_EOL);
echo('$amout: '.$amout.PHP_EOL);
$uploader_amount = $video_play_price - $charge;
$uploader_amount = $amout - $charge;
exit;

Then, make sure the values are what you expected.

Share this post


Link to post
Share on other sites

Thanks for your reply.

I added:

echo('$video_play_price: '.$video_play_price.PHP_EOL);
echo('$charge: '.$charge.PHP_EOL);
echo('$amout: '.$amout.PHP_EOL);
$uploader_amount = $video_play_price - $charge;
$uploader_amount = $amout - $charge;
exit;

to the end of the file. Then added this:

display_errors = on

to the php.ini file, but don't see anything displayed.

Any additional help will be welcomed.

Share this post


Link to post
Share on other sites

Did you see the echo'd results being displayed?

Did you restart your webserver after making the changes to php.ini?  Also, try the following.

error_reporting(E_ALL);
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);



xx

Share this post


Link to post
Share on other sites

I believe I have it working now:

    $charge = ( $video_cost *0.50 );

    if ($wallet >= $amout) {

        $new_wallet = (string)($wallet - $amout);

        $db->startTransaction();

        $inserted_records = 0;
        foreach ($id_array as $id) {
            $video_id = (int)PT_Secure($id);

            // get video data
            $video = $db->where('id', $id)->getOne(T_VIDEOS);

			$video_cost_new = $video->video_play_price?$video->video_play_price:$video_cost;
			$uploader_amount = ( $video_cost_new *0.50 );

Can you help me with some code that would set the default price as a minimum , so the uploader can't set a price below the default price?

 

 

 

 

Share this post


Link to post
Share on other sites

Should be simple enough: $video_cost is the default price, ->video_play_price is the uploader's price, compare them to see which is highest. Or cheat.

Share this post


Link to post
Share on other sites

Thanks again for your reply/suggestion, but I don't know how to code that in particular

Share this post


Link to post
Share on other sites

What I've been trying to get you to do this whole time is learn some PHP. If you're working with the code then you need to know how to do it. Asking people on the internet to do the work for you is not a good idea.
Don't know how to code it? That's fine. No problem. What could be a problem is you not being willing to invest some time into changing your situation so that you do know how to code it.

I linked you a function. If you pass two arguments, such as the default price and the uploader's price, then it will give you back the larger value.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.