retrograde
retrograde

Reputation: 2979

Stripe setup in laravel

I am looking to give my users the option of entering a cc at the time of sign up using Stripe. I am getting the error:

ErrorException
Undefined index: stripeToken

Any help would be greatly appreciated. Thank you in advance.

The Controller:

public function getStripe()
    {

        // get the contractor
    $arrPageData['contractor'] = Contractor::find($this->userID);   

    // show the cc form and pass the contractor
    return View::make('contractors.stripe', $arrPageData);
    }

public function postStripe()
    {
        //posting logic goes here.
        Stripe::setApiKey("sk_test_xxxx");

        // Get the credit card details submitted by the form
        $token = $_POST['stripeToken'];
        $email = Input::get('email');

        // Create a Stripe Record

        $stripe = new Stripe;
        $stripe->card = $token;
        $stripe->email = $email;
        $stripe->save();

    }

Contractor Model:

  function stripes() {
    return $this->hasOne('Stripe');
}

Stripe Model:

 public function contractor() {
    return $this->belongsTo('Contractor', 'contractor_id');
}

And my view file with the form:

//**This is edited to work now**
<script type="text/javascript">
// This identifies your website in the createToken call below
Stripe.setPublishableKey('pk_test_xxxx');

var stripeResponseHandler = function(status, response) {
  var $form = $('#payment-form');

  if (response.error) {
    // Show the errors on the form
    $form.find('.payment-errors').text(response.error.message);
    $form.find('button').prop('disabled', false);
  } else {
    // token contains id, last4, and card type
    var token = response.id;
    // Insert the token into the form so it gets submitted to the server
    $form.append($('<input type="hidden" name="stripeToken" />').val(token));
    // and re-submit
    $form.get(0).submit();
  }
};

jQuery(function($) {
  $('#payment-form').submit(function(e) {
    var $form = $(this);

    // Disable the submit button to prevent repeated clicks
    $form.find('button').prop('disabled', true);

    Stripe.card.createToken($form, stripeResponseHandler);

    // Prevent the form from submitting with the default action
    return false;
  });
});
 </script>

<form action="" method="POST" id="payment-form">
                      <span class="payment-errors"></span>

                      <div class="form-row">
                          <input type="text" size="20" placeholder="Card Number" data-stripe="number"/>
                      </div>

                      <div class="form-row">
                          <input type="text" size="4" placeholder="CVC" data-stripe="cvc"/>
                      </div>

                      <div class="form-row">
                        <input type="text" size="2" placeholder="Expiration Month (MM)" data-stripe="exp-month"/>
                        <span> / </span>
                        <input type="text" size="4" placeholder="Expiration Year (YYYY)" data-stripe="exp-year"/>
                        <input type="hidden" name="email"  value="{{$contractor->email}}" />
                      </div>

                      <input type="submit" class="btn btn-primary"/>
                    </form>

I must be missing a step. I though the form submittal was handled by the js file and all I need to do was record the token in my database.

Upvotes: 1

Views: 2522

Answers (2)

Dinh Viet Phu
Dinh Viet Phu

Reputation: 310

The following is simple stripe test. You can check to make sure it work first. Seem be you missing "customer" or "source".

try {
                $charge = Stripe::charges()->create([
                    // 'customer'       => 'cus_4EBumIjyaKooft',
                    'currency'      => 'USD',
                    'amount'        => $request['amount'],
                    "source"        => $request['token'],
                    "description" => "Example charge"
                ]);

                return response()->json([
                            'status' => true,
                            'data'   => $charge['id']
                        ]);
            } catch (Exception $e) {
                return response()->json([
                        'status' => false,
                        'data'   => "charges stripe fail!"
                    ]);
            }

Upvotes: 0

Jimmie Johansson
Jimmie Johansson

Reputation: 1962

I can't see where you post the stripeToken?

$token = $_POST['stripeToken'];

The error message you are getting indicate that stripeToken doesn't exist.

Upvotes: 3

Related Questions