Save donations with related user, show any error

master
Uğur Arıcı 5 years ago
parent 4653cf74a2
commit 03d41d55a3
  1. 10
      app/Donation.php
  2. 67
      app/Http/Controllers/DonationController.php
  3. 48
      database/migrations/2020_05_15_225602_create_donations_table.php
  4. 2
      public/js/app.js
  5. 2
      public/mix-manifest.json
  6. 1
      resources/js/components/DonationCard.vue
  7. 9
      resources/views/donate.blade.php

@ -0,0 +1,10 @@
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Donation extends Model
{
//
}

@ -2,7 +2,11 @@
namespace App\Http\Controllers;
use App\Donation;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
class DonationController extends Controller
{
@ -47,7 +51,7 @@ class DonationController extends Controller
$paymentRequest = new \Iyzipay\Request\CreatePaymentRequest();
$paymentRequest->setLocale(\Iyzipay\Model\Locale::EN);
$paymentRequest->setConversationId(\Str::random(6));
$paymentRequest->setConversationId(Str::random(6));
$paymentRequest->setPrice($request->amount);
$paymentRequest->setPaidPrice($request->amount);
$paymentRequest->setCurrency($iyzipayCurrencies[$request->currency]);
@ -59,7 +63,7 @@ class DonationController extends Controller
$paymentCard->setCardHolderName($request->card_holder_name);
$paymentCard->setCardNumber($request->card_number);
$paymentCard->setExpireMonth($request->card_expire_month);
$paymentCard->setExpireYear("20" . $request->card_expire_year);
$paymentCard->setExpireYear("120" . $request->card_expire_year);
$paymentCard->setCvc($request->card_cvc);
if ($request->frequency !== "once") {
$paymentCard->setRegisterCard(1);
@ -105,7 +109,64 @@ class DonationController extends Controller
$payment = \Iyzipay\Model\Payment::create($paymentRequest, $options);
dd($payment);
// if payment is not success
if ($payment->getStatus() !== "success") {
return redirect()->back()->withErrors([
'payment_error' => "Payment Error: " . $payment->getErrorMessage() . " Code: " . $payment->getErrorCode(),
])->withInput();
}
// if payment succedded
$user = User::where('email', $request->email)->first();
if (!$user) {
$user = new User;
$user->name = $request->first_name . " " . $request->last_name;
$user->email = $request->email;
$randomPassword = Str::random(8);
$user->password = Hash::make($randomPassword);
$user->save();
// @todo: send new user e-mail
}
$donation = new Donation;
$donation->user_id = $user->id;
$donation->conversation_id = $payment->getConversationId();
$donation->first_name = $request->first_name;
$donation->last_name = $request->last_name;
if ($request->show_name) {
$donation->show_name_on_list = $request->show_name;
}
$donation->email = $request->email;
$donation->amount = $request->amount;
$donation->currency = $request->currency;
$donation->frequency = $request->frequency;
$donation->billing_info = json_encode([
"contact_name" => $request->billing_contact_name,
"address" => $request->billing_address,
"city" => $request->billing_city,
"country" => $request->billing_country,
"zipcode" => $request->billing_zipcode,
]);
$donation->payment_result = $payment->getRawResult();
if ($request->frequency != "once" && $payment->getCardToken() && $payment->getCardUserKey()) {
$donation->card_token = $payment->getCardToken();
$donation->card_user_key = $payment->getCardUserKey();
$donation->card_cvc = $request->card_cvc;
if ($request->frequency == "monthly") {
$donation->next_payment_at = now()->addMonth()->format("Y-m-d");
}
if ($request->frequency == "annually") {
$donation->next_payment_at = now()->addYear()->format("Y-m-d");
}
}
$donation->save();
dd($donation);
}
}

@ -0,0 +1,48 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateDonationsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('donations', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->nullable()->default(null);
$table->string('conversation_id');
$table->string('first_name');
$table->string('last_name');
$table->boolean('show_name_on_list')->default(false);
$table->string('email');
$table->float("amount")->unsigned();
$table->enum("currency", ["USD", "EUR", "TL"]);
$table->enum("frequency", ["once", "monthly", "annually"]);
$table->json("billing_info");
$table->json("payment_result");
$table->string('card_token')->nullable()->default(null);
$table->string('card_user_key')->nullable()->default(null);
$table->string('card_cvc')->nullable()->default(null);
$table->date('next_payment_at')->nullable()->default(null);
$table->integer('next_payment_tried')->default(0);
$table->foreignId('next_payment_id')->nullable()->default(null);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('donations');
}
}

2
public/js/app.js vendored

File diff suppressed because one or more lines are too long

@ -1,4 +1,4 @@
{
"/js/app.js": "/js/app.js?id=d309ac63e39e34e21835",
"/js/app.js": "/js/app.js?id=8c7cd7cf79e301166cc3",
"/css/app.css": "/css/app.css?id=36412c9f34a08f26af3c"
}

@ -4,6 +4,7 @@
<div class="form-group row mb-0">
<div class="col-12">
<h1>Donate</h1>
<p>Özgür Yazılım Derneği is a registered association in İstanbul, Turkey with the number 34-242-113</p>
</div>
</div>
<form method="POST" action="/make-donation">

@ -4,6 +4,15 @@
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
@if ($errors->any())
<div class="alert alert-danger">
<ul class="mt-3">
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<donation-card></donation-card>
</div>
</div>

Loading…
Cancel
Save