Search code examples
phplaravellaravel-5laravel-cashier

Single Charge With StripeJS using Laravel 5.2


I absolutely cannot find anything online to help me integrate stripe with Laravel 5.2. It's been challenging to learn this framework because of so many deprecations between versions :(

Anyway, here's what I'm working with

JS file to capture input

$(function() {
  var $form = $('#payment-form');
  $form.submit(function(event) {
    // Disable the submit button to prevent repeated clicks:
    $form.find('.submit').prop('disabled', true);

    // Request a token from Stripe:
    Stripe.card.createToken($form, stripeResponseHandler);

    // Prevent the form from being submitted:
    return false;
  });
});
function stripeResponseHandler(status, response) {
  // Grab the form:
  var $form = $('#payment-form');
  var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');

  if (response.error) { // Problem!

    // Show the errors on the form:
    $form.find('.payment-errors').text(response.error.message);
    $form.find('.submit').prop('disabled', false); // Re-enable submission

  } else { // Token was created!

    // Get the token ID:
    var token = response.id;
    console.log(token);
    // Insert the token ID into the form so it gets submitted to the server:
    $form.append($('<input type="hidden" name="stripeToken">').val(token));

    // Submit the form:
    $form.get(0).submit();
  }
};

once the form is done, I route the user to {{ route('success') }} via the action="" attribute in the form.

Route::any('/success', ['as' => 'success', 'uses' =>'ChargeController@pay']);

here is my controller with code provided by stripe...as you can see it will not work

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Requests\CreateSongRequest;
use Illuminate\Foundation\Http\FormRequest;
use Billable;
use Input;

class ChargeController extends Controller
{
    public function pay(Request $request){
        if(Input::has('stripeToken')){
            $token = Input::get('stripeToken');
            $amount = 10;
// I cannot use this part even though it is in the stripe documentation
            $customer = Stripe_Customer::create(array(
                                'card' => $token
                            ));
            $charge = Stripe_Charge::create(array(
                        'customer' => $customer->id,
                        'amount' => ($amount*100),
                        'currency' => 'usd',
                        'description' => "test"
                    ));
            echo 'success!';
        }
    }
}

I'm looking at using the StripeJS documentation instead of cashier. Currently, I'm looking at this error

Fatal error: Class 'App\Http\Controllers\Stripe_Customer' not found

And that's where the documentation ends. Any help? I would prefer to use cashier, but I can't find any documentation for "non-subscription" based use and the laravel website is not much help.


Solution

  • kevin's answer is fine, anyway if you are still intrested on what is wrong with your code

    here is the solution

    Try this code in the controller

    First of all you need to add this package

    composer require stripe/stripe-php 
    

    Then try

    class ChargeController extends Controller
    {
    
        public function __construct(){
         \Stripe\Stripe::setApiKey('d8e8fca2dc0f896fd7cb4cb0031ba249');
        }
    
        public function pay(Request $request){
            if(Input::has('stripeToken')){
                $token = Input::get('stripeToken');
                $amount = 10;
    
                $charge = \Stripe\Charge::create(array(
                            'customer' => $customer->id,
                            'amount' => ($amount*100),
                            'currency' => 'usd',
                            'description' => "test",
                            'card' => $token
                        ));
                echo 'charged successfuly!';
            }
        }
    }
    

    if this throws an error related to customer id, you can create the customer first then charge