Reputation: 435
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.
Upvotes: 0
Views: 588
Reputation: 746
I recommend using Cashier. In your composer require:
"laravel/cashier": "~6.0"
You'll need to add to the providers in your config/app.php as well:
Laravel\Cashier\CashierServiceProvider::class
Under config/services add your Stripe API keys and that's about it for setup. To use stripe for a single product purchase simply integrate the Stripe Purchase button:
{!! Form::open(array('url' => '/checkout')) !!}
{!! Form::hidden('product_id', $product->id) !!}
<script
src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="{{env('STRIPE_API_PUBLIC')}}"
data-name="your app"
data-billingAddress=true
data-shippingAddress=true
data-label="Buy ${{ $product->price }}"
data-description="{{ $product->name }}"
data-amount="{{ $product->priceToCents() }}">
</script>
{!! Form::close() !!}
Alternatively you could integrate a cart and pass the entire purchase to stripe from there, but that is slightly more involved. I highly recommend this, it helped me figure it all out for my shop: https://leanpub.com/easyecommerce
Once from the form route, you pass to a controller method where you grab the request:
public function index(Request $request)
{ ....
You could var_dump to see the various data that the stripe button passes.
Upvotes: 2
Reputation: 6269
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
Upvotes: 2