2

I've got a project from Github and I want to modify it. I'm trying to remove the stripe payment, so the registration is done without the payment gateway. I'm working with Laravel.

For the routes,

Route::post('course/payment', ['uses' => 'CoursesController@payment', 'as' => 'courses.payment']);

For the controller method,

    public function payment(Request $request)
{
    $course = Course::findOrFail($request->get('course_id'));
    $this->createStripeCharge($request);
    $course->students()->attach(\Auth::id());
    return redirect()->back()->with('success', 'Payment completed successfully.');
}

The form action in the blade.php file is:

@if (\Auth::check())
    @if ($course->students()->where('user_id', \Auth::id())->count() == 0)
        <form action="{{ route('courses.payment') }}" method="POST">
            <input type="hidden" name="course_id" value="{{ $course->id }}" />
            <input type="hidden" name="amount" value="{{ $course->price * 100 }}" />
                <script
                    src="https://checkout.stripe.com/checkout.js" class="stripe-button"
                    data-key="{{ env('PUB_STRIPE_API_KEY') }}"
                    data-amount="{{ $course->price * 100 }}"
                    data-currency="usd"
                    data-name="LMS"
                    data-label="Buy this course (${{ $course->price }})"
                    data-description="Course: {{ $course->title }}"
                    data-image="https://stripe.com/img/documentation/checkout/marketplace.png"
                    data-locale="auto"
                    data-zip-code="false">
                </script>
                {{ csrf_field() }}
            </form>
    @endif
@else

I know I'm supposed to get it to a GET method, but I can't just seem to figure it out.

TheSoldier
  • 484
  • 1
  • 5
  • 25
  • 2
    Removing the Stripe portions would mean removing the two lines after `$course = Course::findOrFail($request->get('course_id'));` from your controller, and removing the ` – taintedzodiac Jun 24 '20 at 18:45
  • @taintedzodiac That takes away the payment button. I also want to replace it with an option to just enroll. So, I'd definitely need a form a just a simple redirect. But I'm not sure how. – TheSoldier Jun 24 '20 at 19:46
  • 1
    `I know I'm supposed to get it to a GET method` - why do you say that? Something like a registration [should always us POST](https://stackoverflow.com/questions/504947/when-should-i-use-get-or-post-method-whats-the-difference-between-them). If you're not sure how to use forms and buttons to submit them, I'd suggest reading some tutorials first, [here's a super simple eg right here on SO](https://stackoverflow.com/questions/37612280/laravel-form-post-to-controller). – Don't Panic Jun 25 '20 at 00:21
  • @Don'tPanic Yeah, that was not right. I figured it out, it was a POST method still. My initial confusion was in the `blade.php file`. But I did a `dd(request->all())` and figured it was just the `course_id` that was being passed. The solution was finding some way to pass that value after getting rid of the parts as @taintedzodiac pointed out. – TheSoldier Jun 25 '20 at 00:45

1 Answers1

0

For the form action,

@if (\Auth::check())
    @if ($course->students()->where('user_id', \Auth::id())->count() == 0)
        {{Form::open(['action'=> 'CoursesController@payment', 'method' => 'POST'])}}
            <div class="form-group">
            <input type="hidden" name="course_id" value="{{ $course->id }}"/>
            </div>
         {{Form::submit('Enroll', ['class'=>'btn btn-primary'])}}
         {{Form::close()}}
    @endif
@else

And for the controller:

public function payment(Request $request) {
    $course = Course::findOrFail($request->get('course_id'));
    $course->students()->attach(\Auth::id());
    return redirect()->back()->with('success', 'Payment completed successfully.');
}
TheSoldier
  • 484
  • 1
  • 5
  • 25