diff --git a/app/Http/Controllers/BillingController.php b/app/Http/Controllers/BillingController.php index 67567ce..64ccb80 100644 --- a/app/Http/Controllers/BillingController.php +++ b/app/Http/Controllers/BillingController.php @@ -13,6 +13,16 @@ use App\Models\Transaction; class BillingController extends Controller { + protected $stripe; + protected $user; + + + public function __construct() { + $this->stripe = new \Stripe\StripeClient(env('STRIPE_SECRET')); + Stripe::setApiKey(env('STRIPE_SECRET')); + $this->user = Auth::user(); + } + //Expects an array 'packs' representing the amount of each multiple of credits. //This controller should have a way of figuring out how much of each pack //was bought later. @@ -20,6 +30,7 @@ class BillingController extends Controller public function secret(Request $request) { Stripe::setApiKey(env('STRIPE_SECRET')); + $user = Auth::user(); $amount = $request->packs[ 'credits10' ]*1099 + $request->packs[ 'credits50' ]*5499 + $request->packs[ 'credits100' ]*10999 + $request->packs[ 'credits1000' ]*101000; @@ -34,7 +45,7 @@ class BillingController extends Controller $request->packs['credits100']*10 + $request->packs['credits1000']*150; - $transaction->user_id = Auth::user()->id; + $transaction->user_id = $user->id; $transaction->charge = $amount; $transaction->status = 'processing'; $transaction->completed = false; @@ -43,13 +54,18 @@ class BillingController extends Controller $intent = PaymentIntent::create([ 'amount' => $amount, 'currency' => 'usd', - 'customer' => Auth::user()->customer_id, + 'customer' => $user->customer_id, 'description' => "You have received $total_credits credits.", 'receipt_email' => Auth::user()->email, 'metadata' => ['transaction_id' => $transaction->id] ]); $transaction->intent_id = $intent->id; + //Save the card as a default if none is set and it was selected + if ($user->payment_method == null && $request->card) { + $this->changeDefaultCard($request->card); + } + $transaction->save(); return $intent->client_secret; } @@ -71,7 +87,6 @@ class BillingController extends Controller $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; @@ -83,4 +98,28 @@ class BillingController extends Controller $transaction->save(); } } + + public function changeDefaultCard(String $card) { + $user = Auth::user(); + $user->payment_method = $card; + $user->save(); + $cards = $this->getCards(); + Log::debug($this->user->customer_id); + + return PaymentMethod::all([ + 'customer' => Auth::user()->customer_id, + 'type' => 'card' + ]); + } + + public function deleteCard(Request $request) { + $this->stripe->paymentMethods->detach($request->card); + + $user = Auth::user(); + if ($request->card == $user->payment_method) { + $user->payment_method = null; + $user->save(); + } + return ($this->getCards()); + } } diff --git a/database/migrations/2014_10_12_000000_create_users_table.php b/database/migrations/2014_10_12_000000_create_users_table.php index eae54c0..1350a03 100644 --- a/database/migrations/2014_10_12_000000_create_users_table.php +++ b/database/migrations/2014_10_12_000000_create_users_table.php @@ -21,7 +21,7 @@ class CreateUsersTable extends Migration $table->timestamp('email_verified_at')->nullable(); $table->string('password'); $table->enum('role', ['client', 'guest', 'admin']); - $table->string('payment_method'); + $table->string('payment_method')->default(''); $table->boolean('active')->default(true); $table->unsignedBigInteger('credits')->default(0); $table->rememberToken(); diff --git a/resources/js/panel/credits.vue b/resources/js/panel/credits.vue index 5c98487..91d77f1 100644 --- a/resources/js/panel/credits.vue +++ b/resources/js/panel/credits.vue @@ -28,7 +28,7 @@
-