From: Louis Jeckel Date: Wed, 14 Oct 2020 23:37:36 +0000 (+0200) Subject: add untracked files X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=6331373f4f4072321db8df7709682d5568fb61d5;p=psq.git add untracked files --- diff --git a/app/Http/Controllers/StripeController.php b/app/Http/Controllers/StripeController.php new file mode 100644 index 0000000..d1787c8 --- /dev/null +++ b/app/Http/Controllers/StripeController.php @@ -0,0 +1,46 @@ +input('model_type'); + $id = $request->input('model_id'); + + throw_unless(class_exists($class), "Class $class doesn't exist"); + + /** @var IsStripeUnlockable $model */ + $model = $class::findOrFail($id); + + throw_unless(method_exists($model, 'getUnlockPrice'), 'Could not get unlock price'); + + $price = $model->getUnlockPrice(); + + /** @var User $user */ + $user = \Auth::user(); + $customer = $user->getStripeCustomer(); + + $paymentIntent = \Stripe\PaymentIntent::create([ + "amount" => $price, + "currency" => 'eur', +// 'setup_future_usage' => 'on_session', + 'customer' => $customer->id, + 'metadata' => [ + 'model_type' => $class, + 'model_id' => $id + ] + ]); + // Send Payment Intent details to client + return response()->json($paymentIntent); + } + + + +} diff --git a/app/Http/Controllers/StripeWebhookController.php b/app/Http/Controllers/StripeWebhookController.php new file mode 100644 index 0000000..5be2d5f --- /dev/null +++ b/app/Http/Controllers/StripeWebhookController.php @@ -0,0 +1,80 @@ +getUserByStripeId($data['customer'])) { + $article->unlockedUsers()->attach($user->id, [ + 'stripe_payment' => $data + ]); + } + + + return $this->successMethod(); + + } + + /** + * @param $payload + * @return \Symfony\Component\HttpFoundation\Response + */ + protected function handlePaymentMethodAttached($payload) + { + + $data = Arr::get($payload, 'data.object'); + if ($user = $this->getUserByStripeId($data['customer'])) { + if($data['type'] !== 'card') + return $this->successMethod(); + + $user->forceFill([ + 'card_brand' => Arr::get($data, 'card.brand'), + 'card_last_four' => Arr::get($data, 'card.last4') + ])->save(); + + return $this->successMethod(); + } + + return new Response('Customer not found', 404); + } + + + + /** + * Get the billable entity instance by Stripe ID. + * + * @param string|null $stripeId + * @return User + */ + protected function getUserByStripeId($stripeId) + { + return User::where('stripe_id', $stripeId)->first(); + } + + protected function missingMethod($parameters = []) + { + return new Response('Not found', 404); + } + +} diff --git a/database/migrations/2020_10_14_032810_add_infos_to_users.php b/database/migrations/2020_10_14_032810_add_infos_to_users.php new file mode 100644 index 0000000..1ec101c --- /dev/null +++ b/database/migrations/2020_10_14_032810_add_infos_to_users.php @@ -0,0 +1,36 @@ +string('service')->nullable(); + $table->boolean('wants_pdf')->default(0); + $table->boolean('accepts_polls')->default(0); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('users', function (Blueprint $table) { + $table->dropColumn('service'); + $table->dropColumn('wants_pdf'); + $table->dropColumn('accepts_polls'); + }); + } +} diff --git a/database/migrations/2020_10_14_210857_create_unlocked_articles_tables.php b/database/migrations/2020_10_14_210857_create_unlocked_articles_tables.php new file mode 100644 index 0000000..e51658a --- /dev/null +++ b/database/migrations/2020_10_14_210857_create_unlocked_articles_tables.php @@ -0,0 +1,34 @@ +id(); + $table->unsignedBigInteger('user_id'); + $table->morphs('article'); + $table->json('stripe_payment')->nullable(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('unlocked_articles_table'); + } +}