Skip to content

Instantly share code, notes, and snippets.

@NandoKstroNet
Created April 29, 2022 17:38
Show Gist options
  • Save NandoKstroNet/1d217a3ee99d7f8f78c6ec3723dbe7e4 to your computer and use it in GitHub Desktop.
Save NandoKstroNet/1d217a3ee99d7f8f78c6ec3723dbe7e4 to your computer and use it in GitHub Desktop.
Trechos de códigos para uso do Cashier + Stripe em nossa aplicação Laravel do curso Laravel Mastery - https://laravelmastery.com.br
<div class="flex justify-center items-center w-full h-full absolute">
<div class="checkoutForm w-96 p-10 border border-gray-400 rounded">
<div id="message" class="my-5 p-4 hidden w-full border border-red-600 bg-red-200 text-red-600 rounded flex gap-4 items-center">
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 text-red-600" viewBox="0 0 20 20" fill="currentColor">
<path fill-rule="evenodd" d="M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-7 4a1 1 0 11-2 0 1 1 0 012 0zm-1-9a1 1 0 00-1 1v4a1 1 0 102 0V6a1 1 0 00-1-1z" clip-rule="evenodd" />
</svg> <span id="message-feedback"></span>
</div>
<h2 class="pb-2 border-b border-gray-300 font-bold text-xl mb-10">Dados Pagamento Assinatura</h2>
<input id="card-holder-name" type="text" placeholder="Nome no Cartão" class="w-full mb-10 rounded px-6 py-2">
<!-- Stripe Elements Placeholder -->
<div id="card-element" class="border border-gray-500 px-8 py-4 rounded"></div>
<button id="card-button" class="px-4 py-2 border border-green-800 bg-green-600 rounded text-white font-bold mt-10">
Realizar Assinatura
</button>
</div>
@push('scripts')
<script src="https://js.stripe.com/v3/"></script>
<script>
const stripe = Stripe(
'{{config('cashier.key')}}'
);
const elements = stripe.elements();
const cardElement = elements.create('card');
cardElement.mount('#card-element');
const cardHolderName = document.getElementById('card-holder-name');
const cardButton = document.getElementById('card-button');
cardButton.addEventListener('click', async (e) => {
const { paymentMethod, error } = await stripe.createPaymentMethod(
'card', cardElement, {
billing_details: { name: cardHolderName.value }
}
);
if (error) {
document.querySelector('div#message').classList.remove('hidden')
document.querySelector('span#message-feedback').textContent = error.message
} else {
Livewire.emit('charge',paymentMethod.id)
}
});
</script>
@endpush
</div>
<?php
protected $listeners = ['charge'];
public function charge($paymentMethodId)
{
//dd($paymentMethodId);
$user = User::find(1);
if(!$user->subscribed('default'))
$user->newSubscription('default', 'price_1KtfNmHcatQAL5p6yd085XPr')->create($paymentMethodId);
return redirect()->route('dashboard');
}
<?php
if ($request->user() && ! $request->user()->subscribed('default')) {
return redirect()->route('subscription.checkout');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment