rubenochoa
4 months ago
498
3
Laravel

About Stripe payment issues

Posted 4 months ago by rubenochoa

Hello. First at

"source" => 'tok_mastercard', // obtained with Stripe.js

if i do this

$request->input('stripeToken');

I get "Must provide source or customer"

Second, how can I see the card name at stripe.com dushboard? I am using:

            @extends('layouts.master')

            @section('title')
            Laravel Shopping Cart
            @endsection

            @section('content') 
            <style type="text/css">
                .panel-title {
                display: inline;
                font-weight: bold;
                }
                .display-table {
                    display: table;
                }
                .display-tr {
                    display: table-row;
                }
                .display-td {
                    display: table-cell;
                    vertical-align: middle;
                    width: 61%;
                }
            </style>           
                    <h1>Checkout</h1>
                    <h4>Your Total: ${{ $total }}</h4>                    
                    <div class="row">
                        <div class="col-md-4 m-auto">
                            <div class="panel panel-default credit-card-box">
                                <div class="panel-heading display-table" >
                                    <div class="row display-tr" >
                                        <h3 class="panel-title display-td" >Payment Details</h3>
                                        <div class="display-td" >                            
                                            <img class="img-responsive pull-right" src="http://i76.imgup.net/accepted_c22e0.png">
                                        </div>
                                    </div>                    
                                </div>
                                <div class="panel-body">
                  
                                    @if (Session::has('success'))
                                        <div class="alert alert-success text-center">
                                            <a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
                                            <p>{{ Session::get('success') }}</p>
                                        </div>
                                    @endif
                  
                                    <form role="form" action="{{ route('checkout') }}" method="post" class="require-validation"
                                                                     data-cc-on-file="false"
                                                                    data-stripe-publishable-key="{{ env('') }}"
                                         
                                                                    id="payment-form">
                                        @csrf
                  
                                        <div class='form-row row'>
                                            <div class='col-xs-12 form-group required'>
                                                <label class='control-label'>Name on Card</label> <input
                                                    class='form-control' size='4' type='text'>
                                            </div>
                                        </div>
                  
                                        <div class='form-row row'>
                                            <div class='col-xs-12 form-group card required'>
                                                <label class='control-label'>Card Number</label> <input
                                                    autocomplete='off' class='form-control card-number' size='20'
                                                    type='text'>
                                            </div>
                                        </div>
                  
                                        <div class='form-row row'>
                                            <div class='col-xs-12 col-md-4 form-group cvc required'>
                                                <label class='control-label'>CVC</label> <input autocomplete='off'
                                                    class='form-control card-cvc' placeholder='ex. 311' size='4'
                                                    type='text'>
                                            </div>
                                            <div class='col-xs-12 col-md-4 form-group expiration required'>
                                                <label class='control-label'>Expiration Month</label> <input
                                                    class='form-control card-expiry-month' placeholder='MM' size='2'
                                                    type='text'>
                                            </div>
                                            <div class='col-xs-12 col-md-4 form-group expiration required'>
                                                <label class='control-label'>Expiration Year</label> <input
                                                    class='form-control card-expiry-year' placeholder='YYYY' size='4'
                                                    type='text'>
                                            </div>
                                        </div>
                  
                                        <div class='form-row row'>
                                            <div class='col-md-12 error form-group hide'>
                                                <div id="charge-error" class="alert alert-danger{{ !Session::has('error') ? 'hidden' : ''  }}">
                                                    {{ Session::get('error') }}
                                                </div>
                                            </div>
                                        </div>
                  
                                        <div class="row">
                                            <div class="col-xs-12">
                                                <button class="btn btn-primary btn-lg btn-block" type="submit">Pay Now</button>
                                            </div>
                                        </div>
                                          
                                    </form>
                                </div>
                            </div>        
                        </div>
                    </div>
                      
                </div>                                             
                @endsection
@section('scripts')   
<script type="text/javascript" src="https://js.stripe.com/v2/"></script>
<script type="text/javascript" src="{{URL::to('js/checkout.js')}}"></script>
@endsection

ProductController.php

public function postCheckout(Request $request)
    {
        if (!Session::has('cart')) {
            return redirect()->route('shop.shoppingCart');
        }
        $oldCart = Session::get('cart');
        $cart = new Cart($oldCart);
        
        \Stripe\Stripe::setApiKey('');
        \Stripe\Charge::create ([
                "amount" => $cart->totalPrice * 100,
                "name" => $request->name,
                "currency" => "usd",
                "source" => 'tok_mastercard', // obtained with Stripe.js
                "description" => "Test payment from itsolutionstuff.com." 
            ]);
        
            $order = new Order();
            $order->cart = serialize($cart);
            

            $order->payment_id = $charge->id;

            Auth::user()->orders()->save($order);
            $user = Auth::user();            ;  
                                
            Mail::send('shop.order_confirmation', [
                'user' => $user,
                'products' => $cart->items,
                'totalPrice' => $cart->totalPrice,
            ], function($message) use ($user) {
                    $message->to($user->email);
                    $message->from("[email protected]");
                    $message->subject("Your order confirmation");
                    $message->bcc("[email protected]");
                }
            );     
            
            Session::flash('success', 'Successfully purchased products!');    

         
            return redirect()->route('checkout')->with('error', $e->getMessage());
        

        Session::forget('cart');
        return redirect()->route('product.index')->with('success', 'Successfully purchased products!');
    }    

Checkout.js

$(function() {
    var $form         = $(".require-validation");
  $('form.require-validation').bind('submit', function(e) {
    var $form         = $(".require-validation"),
        inputSelector = ['input[type=email]', 'input[type=password]',
                         'input[type=text]', 'input[type=file]',
                         'textarea'].join(', '),
        $inputs       = $form.find('.required').find(inputSelector),
        $errorMessage = $form.find('div.error'),
        valid         = true;
        $errorMessage.addClass('hide');
 
        $('.has-error').removeClass('has-error');
    $inputs.each(function(i, el) {
      var $input = $(el);
      if ($input.val() === '') {
        $input.parent().addClass('has-error');
        $errorMessage.removeClass('hide');
        e.preventDefault();
      }
    });
  
    if (!$form.data('cc-on-file')) {
      e.preventDefault();
      Stripe.setPublishableKey($form.data('stripe-publishable-key'));
      Stripe.createToken({
        number: $('.card-number').val(),
        cvc: $('.card-cvc').val(),
        exp_month: $('.card-expiry-month').val(),
        exp_year: $('.card-expiry-year').val()
      }, stripeResponseHandler);
    }
  
  });
  
  function stripeResponseHandler(status, response) {
        if (response.error) {
            $('.error')
                .removeClass('hide')
                .find('.alert')
                .text(response.error.message);
        } 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.find('input[type=text]').empty();
            $form.append("<input type='hidden' name='stripeToken' value='" + token + "'/>");
            $form.get(0).submit();
        }
    }
  
});

Please sign in or create an account to participate in this conversation.