0

Trying to include a php file in a script and even though the page loads without showing or logging errors, the included file is not executed.

I have created a test file that looks like this:

<?php

$sale_amount = '10.00';
$product = 'Drive-Plan';
include('partners/controller/record-sale.php');
echo "commission counted"
?>

opening this file in browser completes the expected function.

Trying to include this same code in my script does not complete the function.

original script works good:

    /**
     * Swap current users plan to a new one.
     *
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     */
    public function swapPlan() {
        if ($this->user->subscribed() && Input::get('plan')) {
            $this->user->subscription(Input::get('plan'))->swap();

            return response(trans('app.planSwapSuccess', ['plan' => Input::get('plan')]), 200);

        }
    }

Including my file in this script does not have the expected effect.

    /**
     * Swap current users plan to a new one.
     *
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     */
    public function swapPlan() {
        if ($this->user->subscribed() && Input::get('plan')) {
            $this->user->subscription(Input::get('plan'))->swap();

            return response(trans('app.planSwapSuccess', ['plan' => Input::get('plan')]), 200);


$sale_amount = '10.00';
$product = 'Drive-Plan';
include('partners/controller/record-sale.php');
        }
    }

The page will complete loading without errors, however the function that should be completed by the included file is not executed. Any Ideas on what I may have done wrong are appreciated.

I have spent a few days trying to figure this out. Since my test file works I'm guessing I'm missing something obvious in the full script.

Thanks for looking.

Comeplete file:

<?php namespace App\Http\Controllers;

use App;
use Auth;
use Input;
use Stripe\Stripe;
use Stripe\Plan;

class PaymentsController extends Controller {

    public function __construct() {
        $this->middleware('loggedIn');
        $this->middleware('paymentsEnabled');

        $this->user = Auth::user();
        $this->settings = App::make('App\Services\Settings');

        Stripe::setApiKey($this->settings->get('stripe_secret_key'));
    }

    /**
     * Subscribe user to a plan or swap him to a different plan.
     *
     * @return response
     */
    public function upgrade() {
        if ($this->user->subscribed()) {
            $this->user->subscription(Input::get('plan'))->swap();
        } else {
            $this->user->subscription(Input::get('plan'))->create(Input::get('stripe_token'), ['email' => $this->user->email]);
        }

        return response(trans('app.upgradeSuccess'), 200);


$sale_amount = '10.00';
$product = 'Drive-Plan';
include('partners/controller/record-sale.php');

    }

    /**
     * Swap current users plan to a new one.
     *
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     */
    public function swapPlan() {
        if ($this->user->subscribed() && Input::get('plan')) {
            $this->user->subscription(Input::get('plan'))->swap();

            return response(trans('app.planSwapSuccess', ['plan' => Input::get('plan')]), 200);


$sale_amount = '10.00';
$product = 'Drive-Plan';
include('partners/controller/record-sale.php');
        }
    }

    /**
     * Attach new credit card to user.
     *
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     */
    public function addNewCard() {
        $this->user->updateCard(Input::get('stripe_token'));

        return response(trans('app.cardAddSuccess'), 200);
    }

    /**
     * Resume a canceled subscription.
     */
    public function resumeSubscription() {
        $this->user->subscription(Input::get('plan'))->resume(Input::get('token'));

        return $this->user;


$sale_amount = '10.00';
$product = 'Drive-Plan';
include('partners/controller/record-sale.php');

    }

    /**
     * Cancel users subscription.
     *
     * @return \App\User
     */
    public function unsubscribe() {
        $this->user->subscription()->cancel();

        return $this->user;
    }

    /**
     * Return current users invoices.
     *
     * @return array
     */
    public function getInvoices() {
        return view('invoices')->with('invoices', $this->user->invoices())->with('settings', $this->settings);
    }

    /**
     * Download invoice with given id.
     *
     * @param {int|string} $id
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function downloadInvoice($id) {
        return $this->user->downloadInvoice($id, [
            'vendor'  => $this->settings->get('invoiceVendor'),
            'product' => $this->settings->get('invoiceProduct'),
        ]);
    }

    /**
     * Return all created plans.
     *
     * @return array
     */
    public function getPlans() {
        $plans     = Plan::all();
        $formatted = [];

        foreach($plans->data as $plan) {
            $formatted[] = [
                'interval' => $plan['interval'],
                'name' => $plan['name'],
                'amount' => $plan['amount'] / 100,
                'currency' => $plan['currency'],
                'id' => $plan['id'],
                'created' => $plan['created'],
            ];
        }

        usort($formatted, function($a1, $a2) {
            if ($a1['created'] == $a2['created']) return 0;
            return ($a1['created'] < $a2['created']) ? -1 : 1;
        });

        return $formatted;
    }
}
cam
  • 13
  • 5
  • any change if you use `require` ? –  Feb 14 '16 at 21:07
  • no require_once. if this file including before you get error php – Naumov Feb 14 '16 at 21:15
  • @Dagon using require does not change anything. no fatal error. – cam Feb 14 '16 at 21:15
  • Does an exit end the script? – Anees Saban Feb 14 '16 at 21:16
  • @Naumov Don't quite understand your comment. are you suggesting require_once instead of require? – cam Feb 14 '16 at 21:17
  • @valdorous do you mean the script I am trying to include this to, or the file I am trying to include? – cam Feb 14 '16 at 21:18
  • The script you are including, in fact. For such a small thing, that is repeated, should it not be a function? EDIT: Ignore the last comment, I misread what you were including. – Anees Saban Feb 14 '16 at 21:19
  • Wait... After going through your code, I am confused as to what it is you are doing. You have variables being set and echo'd after a return in a method? Code after a return is ignored. – Anees Saban Feb 14 '16 at 21:22
  • $sale_amount = '10.00'; $product = 'Drive-Plan'; require('partners/controller/record-sale.php'); or exit("unable to open commission"); – cam Feb 14 '16 at 21:22
  • or exit seems to break it. @valdorous yes, the cpde after return is ignored. – cam Feb 14 '16 at 21:23
  • That makes sense, is what I am telling you. It makes sense for it to be ignored. You need to move it before the return. – Anees Saban Feb 14 '16 at 21:24
  • I have also tried include testfile.php since browsing testfile.php completes the required action, but still it seems like the code after return is ignored – cam Feb 14 '16 at 21:25
  • No, it should be ignored. Your code should be before the return, it is a functionality provided by return. – Anees Saban Feb 14 '16 at 21:25
  • @valdorous I will try to move it directly before the return and see what it does. back in a minute – cam Feb 14 '16 at 21:25
  • you have to return from function before the included file can be actually run... :-/ ... – Wizard Feb 14 '16 at 21:31

1 Answers1

0

Your method ends on return, meaning your include in the methods and variables being set are never reached.

An example:

Moving said include and variables to before the return should solve the issue.

 /**
 * Resume a canceled subscription.
 */
 public function resumeSubscription() {
    $this->user->subscription(Input::get('plan'))->resume(Input::get('token'));

    // Moved to before return
    $sale_amount = '10.00';
    $product = 'Drive-Plan';
    include('partners/controller/record-sale.php');

    return $this->user;

    // UNREACHABLE
    //$sale_amount = '10.00';
    //$product = 'Drive-Plan';
    //include('partners/controller/record-sale.php');
}
Anees Saban
  • 607
  • 6
  • 11
  • yes. Thank you very much. this worked. Thank you so much for your time. I really need to learn php and javascript properly. how did you learn php? – cam Feb 14 '16 at 21:51
  • Learnt the basics at college, but true object-oriented programming I learnt myself via various YouTube tutorials and filled the gaps with what I learnt at college. Contributing / reading on StackOverflow has been a great help. too. ;) – Anees Saban Feb 14 '16 at 22:41
  • Do note that it is rather strange and in most cases incorrect to have an echo in a method. In fact such a small include should be a function, maybe even a static one. But I won't go into detail on that. Just had to let you know. – Anees Saban Feb 14 '16 at 22:43