mercredi 23 mars 2016

Laravel Stripe Integration Passing Variable

I have a small web application which uses both laravel and stripe.. In my view I give the ability to the logged in user to top up their account. On the view I have 5 packages.

1 Credit 10 Credits etc

I've got stripe to take the right amount from the users card which is great, however i'm struggling passing the data-quantity through to my controller which would add the credits.

View (Just for example I've included just the one package)

<div class="col-sm-6 col-lg-2">
                                <!-- Small Package -->
                                <div class="block block-bordered text-center">
                                    <div class="block-header">
                                        <h3 class="block-title">Super!</h3>
                                    </div>
                                    <div class="block-content block-content-full bg-primary-dark">
                                        <div class="h1 font-w700 text-white push-10">200</div>
                                        <div class="h5 font-w300 text-white">Credits</div>
                                    </div>
                                    <div class="block-content">
                                        <table class="table table-borderless table-condensed">
                                            <tbody>
                                                <tr>
                                                    <td><strong>£1.80</strong> each <small class="text-muted">ex VAT</small></td>
                                                </tr>
                                            </tbody>
                                        </table>
                                    </div>
                                    <div class="block-content bg-gray-lighter">
                                        <table class="table table-borderless table-condensed">
                                            <tbody>
                                                <tr>
                                                    <td>
                                                        <button id="" class="btn btn-block btn-success btn-default customButton" data-price="43200" data-credits="200" data-title="200 x Credits">Pay by Card</button>
                                                    </td>
                                                </tr>
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                                <!-- END Small Package -->
                            </div>  

Javascript handling the click event which captures the data-attr against each button.. i.e. the value, the title and quantity.

<script>

              var handler = StripeCheckout.configure({
                key: 'pk_test_code',
                image: '{{ url() }}/assets/img/stripe-icon.jpg',
                locale: 'auto',
                token: function(token) {

                    $.post('/call/topup', {token: token}, function(data) {
                            alert('TOPPED UP!');
                    });
                }
              });

              $('.customButton').on('click', function(e) {

                handler.open({
                  description: $(this).attr('data-title'),
                  currency: "gbp",
                  amount: $(this).attr('data-price')
                });
                e.preventDefault();
              });

              // Close Checkout on page navigation
              $(window).on('popstate', function() {
                handler.close();
              });

            </script>

Controller which handles the request and charges the card.

 public function topup(Request $request)
    {

        $token = $request->token['id'];

        $user = User::FindOrFail(1);

        $user->charge(6000, [
            'source' => $token,
            'currency' => 'gbp',
            'receipt_email' => $user->email,
        ]);

        return $request->token;
    }

I haven't yet included the topping up the account because I can't figure out how to get the data-quantity to the controller :( help.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire