From 18dadd28c15ce7a3316f055652fbcfebcd1ee63a Mon Sep 17 00:00:00 2001 From: Immanuel Onyeka Date: Wed, 9 Jun 2021 15:59:32 -0400 Subject: [PATCH] Update user credits after a charge --- app/Http/Controllers/BillingController.php | 34 +++++++++++++++---- ...tionController.php => OrderController.php} | 2 +- app/Http/Middleware/VerifyCsrfToken.php | 2 +- ...06_08_224029_create_transactions_table.php | 1 + resources/js/panel/credits.vue | 7 ++-- resources/js/panel/panel.vue | 2 +- resources/js/panel/saved-cards.vue | 2 +- resources/js/panel/sidebar.vue | 4 +-- routes/web.php | 8 +++-- 9 files changed, 44 insertions(+), 18 deletions(-) rename app/Http/Controllers/{TransactionController.php => OrderController.php} (94%) diff --git a/app/Http/Controllers/BillingController.php b/app/Http/Controllers/BillingController.php index cae4dfd..29d2d68 100644 --- a/app/Http/Controllers/BillingController.php +++ b/app/Http/Controllers/BillingController.php @@ -19,7 +19,6 @@ class BillingController extends Controller //Should validate that all amounts are positive integers in a reasonable range public function secret(Request $request) { Stripe::setApiKey(env('STRIPE_SECRET')); - Log::debug($request->packs); $amount = $request->packs[ 'credits10' ]*1099 + $request->packs[ 'credits50' ]*5499 + $request->packs[ 'credits100' ]*10999 @@ -28,18 +27,17 @@ class BillingController extends Controller $transaction = new Transaction; $transaction->credits = $request->packs['credits10']*10 + $request->packs['credits50']*50 + - $request->packs['credits[100]']*100 + - $request->packs['credits[1000]']*1000; + $request->packs['credits100']*100 + + $request->packs['credits1000']*1000; $transaction->credits_extra = $request->packs['credits50']*5 + - $request->packs['credits[100]']*10 + - $request->packs['credits[1000]']*150; + $request->packs['credits100']*10 + + $request->packs['credits1000']*150; $transaction->user_id = Auth::user()->id; $transaction->charge = $amount; $transaction->status = 'processing'; $transaction->completed = false; - $transaction->save(); $intent = PaymentIntent::create([ 'amount' => $amount, @@ -47,15 +45,39 @@ class BillingController extends Controller 'customer' => Auth::user()->customer_id, 'metadata' => ['transaction_id' => $transaction->id] ]); + $transaction->intent_id = $intent->id; + $transaction->save(); return $intent->client_secret; } public function getCards() { Stripe::setApiKey(env('STRIPE_SECRET')); + return PaymentMethod::all([ 'customer' => Auth::user()->customer_id, 'type' => 'card' ]); } + + public function chargeEvent(Request $request) { + Stripe::setApiKey(env('STRIPE_SECRET')); + $event = \Stripe\Event::constructFrom($request->all()); + $charge = $event->data->object; + $transaction = Transaction::where('intent_id', $charge->payment_intent); + $user = $transaction->user; + + if ($event->type == 'charge.succeeded') { + Log::debug($charge->metadata['transaction_id']); + $user->credits = $user->credits + $transaction->credits + $transaction->credits_extra; + $transaction->status = 'completed'; + $transaction->completed = true; + $user->save(); + $transaction->save(); + + } else { + $transaction->status = $charge->status; + $transaction->save(); + } + } } diff --git a/app/Http/Controllers/TransactionController.php b/app/Http/Controllers/OrderController.php similarity index 94% rename from app/Http/Controllers/TransactionController.php rename to app/Http/Controllers/OrderController.php index c66f950..4057906 100644 --- a/app/Http/Controllers/TransactionController.php +++ b/app/Http/Controllers/OrderController.php @@ -8,7 +8,7 @@ use App\Models\Service; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Log; -class TransactionController extends Controller +class OrderController extends Controller { //THis should also reduce user's available credits public function newOrder(Request $request) { diff --git a/app/Http/Middleware/VerifyCsrfToken.php b/app/Http/Middleware/VerifyCsrfToken.php index 0c13b85..44944fc 100644 --- a/app/Http/Middleware/VerifyCsrfToken.php +++ b/app/Http/Middleware/VerifyCsrfToken.php @@ -12,6 +12,6 @@ class VerifyCsrfToken extends Middleware * @var array */ protected $except = [ - // + 'hooks/*' ]; } diff --git a/database/migrations/2021_06_08_224029_create_transactions_table.php b/database/migrations/2021_06_08_224029_create_transactions_table.php index 647e4d4..1240a26 100644 --- a/database/migrations/2021_06_08_224029_create_transactions_table.php +++ b/database/migrations/2021_06_08_224029_create_transactions_table.php @@ -20,6 +20,7 @@ class CreateTransactionsTable extends Migration $table->integer('credits')->unsigned(); $table->integer('credits_extra')->unsigned()->default(0); $table->string('status'); + $table->string('intent_id')->unique(); $table->boolean('completed'); $table->string('note')->nullable(); $table->timestamps(); diff --git a/resources/js/panel/credits.vue b/resources/js/panel/credits.vue index 55e8700..9b6bfa7 100644 --- a/resources/js/panel/credits.vue +++ b/resources/js/panel/credits.vue @@ -81,7 +81,6 @@ function getSecret() { //Gets key from the server then sends it with stripe //Maybe it should asl attach the user's receipt email function pay() { - console.log('paying') this.getSecret().then(secret => { if (!secret) {return} this.loading = true @@ -105,8 +104,7 @@ function pay() { document.getElementById('payment-error').textContent = result.error.message } else if (result.paymentIntent.status === 'succeeded') { - document.getElementById('payment-error').textContent = - 'Purchase completed' + this.complete() } else { document.getElementById('payment-error').textContent = 'An unknown error occured' @@ -123,11 +121,12 @@ function getCards() { 'X-XSRF-TOKEN': this.token} }).then((response) => {response.json().then(data => { this.cards = data.data - if (this.cards) { + if (this.cards && this.cards.length > 0) { this.picked = this.cards[0].id } })}) } + function ready() { return this.total != 0 && !this.loading && ((this.cardValid && this.billingName) || this.picked) diff --git a/resources/js/panel/panel.vue b/resources/js/panel/panel.vue index f03db76..f828601 100644 --- a/resources/js/panel/panel.vue +++ b/resources/js/panel/panel.vue @@ -1,6 +1,6 @@