Page MenuHomePhorge

D1102.1775385920.diff
No OneTemporary

Authored By
Unknown
Size
12 KB
Referenced Files
None
Subscribers
None

D1102.1775385920.diff

diff --git a/src/app/Domain.php b/src/app/Domain.php
--- a/src/app/Domain.php
+++ b/src/app/Domain.php
@@ -308,6 +308,16 @@
return $mod === self::HASH_TEXT ? "$cname=$hash" : $hash;
}
+ /**
+ * String representation of this user.
+ *
+ * @return string
+ */
+ public function toString()
+ {
+ return $this->namespace;
+ }
+
/**
* Verify if a domain exists in DNS
*
diff --git a/src/app/Http/Controllers/API/PaymentsController.php b/src/app/Http/Controllers/API/PaymentsController.php
--- a/src/app/Http/Controllers/API/PaymentsController.php
+++ b/src/app/Http/Controllers/API/PaymentsController.php
@@ -2,7 +2,7 @@
namespace App\Http\Controllers\API;
-use App\Payment;
+use App\Transaction;
use App\Wallet;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
@@ -91,7 +91,7 @@
*/
public function webhook(Request $request)
{
- $db_payment = Payment::find($request->id);
+ $db_payment = Transaction::where('mollie_id', $request->id)->first();
// Mollie recommends to return "200 OK" even if the payment does not exist
if (empty($db_payment)) {
@@ -109,8 +109,15 @@
if (!$payment->hasRefunds() && !$payment->hasChargebacks()) {
// The payment is paid and isn't refunded or charged back.
// Update the balance, if it wasn't already
- if ($db_payment->status != 'paid') {
+ if ($db_payment->mollie_status != 'paid') {
$db_payment->wallet->credit($db_payment->amount);
+ Transaction::create(
+ [
+ 'wallet_id' => $db_payment->wallet->id,
+ 'amount' => $db_payment->amount,
+ 'description' => "Added credit: {$db_payment->amount}"
+ ]
+ );
}
} elseif ($payment->hasRefunds()) {
// The payment has been (partially) refunded.
@@ -125,8 +132,8 @@
// This is a sanity check, just in case the payment provider api
// sent us open -> paid -> open -> paid. So, we lock the payment after it's paid.
- if ($db_payment->status != 'paid') {
- $db_payment->status = $payment->status;
+ if ($db_payment->mollie_status != 'paid') {
+ $db_payment->mollie_status = $payment->status;
$db_payment->save();
}
@@ -210,12 +217,14 @@
*/
protected static function storePayment($payment, $wallet_id, $amount): void
{
- $db_payment = new Payment();
- $db_payment->id = $payment->id;
- $db_payment->description = $payment->description;
- $db_payment->status = $payment->status;
- $db_payment->amount = $amount;
- $db_payment->wallet_id = $wallet_id;
- $db_payment->save();
+ $db_payment = Transaction::create(
+ [
+ 'wallet_id' => $wallet_id,
+ 'amount' => $amount,
+ 'description' => $payment->description,
+ 'mollie_id' => $payment->id,
+ 'mollie_status' => $payment->status
+ ]
+ );
}
}
diff --git a/src/app/Observers/TransactionObserver.php b/src/app/Observers/TransactionObserver.php
new file mode 100644
--- /dev/null
+++ b/src/app/Observers/TransactionObserver.php
@@ -0,0 +1,29 @@
+<?php
+
+namespace App\Observers;
+
+use App\Transaction;
+
+/**
+ * This is an observer for the Transaction model definition.
+ */
+class TransactionObserver
+{
+ /**
+ * Ensure the transaction ID is a custom ID (uuid).
+ *
+ * @param Transaction $transaction
+ *
+ * @return void
+ */
+ public function creating(Transaction $transaction)
+ {
+ while (true) {
+ $allegedly_unique = \App\Utils::uuidStr();
+ if (!Transaction::find($allegedly_unique)) {
+ $transaction->{$transaction->getKeyName()} = $allegedly_unique;
+ break;
+ }
+ }
+ }
+}
diff --git a/src/app/Providers/AppServiceProvider.php b/src/app/Providers/AppServiceProvider.php
--- a/src/app/Providers/AppServiceProvider.php
+++ b/src/app/Providers/AppServiceProvider.php
@@ -31,6 +31,7 @@
\App\Plan::observe(\App\Observers\PlanObserver::class);
\App\SignupCode::observe(\App\Observers\SignupCodeObserver::class);
\App\Sku::observe(\App\Observers\SkuObserver::class);
+ \App\Transaction::observe(\App\Observers\TransactionObserver::class);
\App\User::observe(\App\Observers\UserObserver::class);
\App\UserAlias::observe(\App\Observers\UserAliasObserver::class);
\App\UserSetting::observe(\App\Observers\UserSettingObserver::class);
diff --git a/src/app/Payment.php b/src/app/Transaction.php
rename from src/app/Payment.php
rename to src/app/Transaction.php
--- a/src/app/Payment.php
+++ b/src/app/Transaction.php
@@ -3,17 +3,21 @@
namespace App;
use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\SoftDeletes;
/**
- * A payment operation on a wallet.
+ * A transaction.
*
+ * @property string $wallet_id The ID of the wallet
* @property int $amount Amount of money in cents
* @property string $description Payment description
- * @property string $id Mollie's Payment ID
- * @property int $wallet_id The ID of the wallet
+ * @property string $params Description parameters
+ * @property string $mollie_id Mollie's Payment ID
*/
-class Payment extends Model
+class Transaction extends Model
{
+ use SoftDeletes;
+
public $incrementing = false;
protected $keyType = 'string';
@@ -21,6 +25,15 @@
'amount' => 'integer'
];
+ protected $fillable = [
+ 'wallet_id',
+ 'amount',
+ 'description',
+ 'params',
+ 'mollie_id',
+ 'mollie_status'
+ ];
+
/**
* The wallet to which this payment belongs.
*
diff --git a/src/app/User.php b/src/app/User.php
--- a/src/app/User.php
+++ b/src/app/User.php
@@ -572,4 +572,14 @@
$this->attributes['status'] = $new_status;
}
+
+ /**
+ * String representation of this user.
+ *
+ * @return string
+ */
+ public function toString()
+ {
+ return $this->email;
+ }
}
diff --git a/src/app/Wallet.php b/src/app/Wallet.php
--- a/src/app/Wallet.php
+++ b/src/app/Wallet.php
@@ -86,6 +86,18 @@
$entitlement->updated_at = $entitlement->updated_at->copy()->addMonthsWithoutOverflow($diff);
$entitlement->save();
+ \App\Transaction::create(
+ [
+ 'wallet_id' => $this->id,
+ 'amount' => $entitlement->cost * $diff,
+ 'description' => sprintf(
+ '%s was charged for %s',
+ $entitlement->sku->title,
+ $entitlement->entitleable->toString()
+ )
+ ]
+ );
+
$this->debit($entitlement->cost * $diff);
}
}
@@ -185,12 +197,12 @@
}
/**
- * Payments on this wallet.
+ * Transactions on this wallet.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function payments()
{
- return $this->hasMany('App\Payment');
+ return $this->hasMany('App\Transaction');
}
}
diff --git a/src/database/migrations/2020_03_16_100000_create_payments.php b/src/database/migrations/2020_03_16_100000_create_transactions_table.php
rename from src/database/migrations/2020_03_16_100000_create_payments.php
rename to src/database/migrations/2020_03_16_100000_create_transactions_table.php
--- a/src/database/migrations/2020_03_16_100000_create_payments.php
+++ b/src/database/migrations/2020_03_16_100000_create_transactions_table.php
@@ -4,7 +4,8 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
-class CreatePayments extends Migration
+// phpcs:ignore
+class CreateTransactionsTable extends Migration
{
/**
* Run the migrations.
@@ -14,14 +15,16 @@
public function up()
{
Schema::create(
- 'payments',
+ 'transactions',
function (Blueprint $table) {
- $table->string('id', 16)->primary();
+ $table->string('id', 36)->primary();
$table->string('wallet_id', 36);
- $table->string('status', 16);
$table->integer('amount');
$table->text('description');
+ $table->string('mollie_id', 16)->nullable();
+ $table->string('mollie_status', 16)->nullable();
$table->timestamps();
+ $table->softDeletes();
$table->foreign('wallet_id')->references('id')->on('wallets')->onDelete('cascade');
}
@@ -35,6 +38,6 @@
*/
public function down()
{
- Schema::dropIfExists('payments');
+ Schema::dropIfExists('transactions');
}
}
diff --git a/src/tests/Feature/Controller/PaymentsTest.php b/src/tests/Feature/Controller/PaymentsTest.php
--- a/src/tests/Feature/Controller/PaymentsTest.php
+++ b/src/tests/Feature/Controller/PaymentsTest.php
@@ -2,7 +2,7 @@
namespace Tests\Feature\Controller;
-use App\Payment;
+use App\Transaction;
use App\Wallet;
use GuzzleHttp\Psr7\Response;
use Tests\TestCase;
@@ -21,7 +21,7 @@
$john = $this->getTestUser('john@kolab.org');
$wallet = $john->wallets()->first();
$john->setSetting('mollie_id', null);
- Payment::where('wallet_id', $wallet->id)->delete();
+ Transaction::where('wallet_id', $wallet->id)->delete();
Wallet::where('id', $wallet->id)->update(['balance' => 0]);
}
@@ -33,7 +33,7 @@
$john = $this->getTestUser('john@kolab.org');
$wallet = $john->wallets()->first();
$john->setSetting('mollie_id', null);
- Payment::where('wallet_id', $wallet->id)->delete();
+ Transaction::where('wallet_id', $wallet->id)->delete();
Wallet::where('id', $wallet->id)->update(['balance' => 0]);
parent::tearDown();
@@ -72,13 +72,13 @@
$this->assertRegExp('|^https://www.mollie.com|', $json['redirectUrl']);
$wallet = $user->wallets()->first();
- $payments = Payment::where('wallet_id', $wallet->id)->get();
+ $transactions = Transaction::where('wallet_id', $wallet->id)->get();
- $this->assertCount(1, $payments);
- $payment = $payments[0];
- $this->assertSame(1234, $payment->amount);
- $this->assertSame('Kolab Now Payment', $payment->description);
- $this->assertSame('open', $payment->status);
+ $this->assertCount(1, $transactions);
+ $transaction = $transactions[0];
+ $this->assertSame(1234, $transaction->amount);
+ $this->assertSame('Kolab Now Payment', $transaction->description);
+ $this->assertSame('open', $transaction->mollie_status);
$this->assertEquals(0, $wallet->balance);
// Test the webhook
@@ -86,7 +86,7 @@
$mollie_response = [
"resource" => "payment",
- "id" => $payment->id,
+ "id" => $transaction->mollie_id,
"status" => "paid",
// Status is not enough, paidAt is used to distinguish the state
"paidAt" => date('c'),
@@ -120,11 +120,11 @@
$responseStack = $this->mockMollie();
$responseStack->append(new Response(200, [], json_encode($mollie_response)));
- $post = ['id' => $payment->id];
+ $post = ['id' => $transaction->mollie_id];
$response = $this->post("api/webhooks/payment/mollie", $post);
$response->assertStatus(200);
- $this->assertSame('paid', $payment->fresh()->status);
+ $this->assertSame('paid', $transaction->fresh()->mollie_status);
$this->assertEquals(1234, $wallet->fresh()->balance);
// Verify "paid -> open -> paid" scenario, assert that balance didn't change
@@ -135,7 +135,7 @@
$response = $this->post("api/webhooks/payment/mollie", $post);
$response->assertStatus(200);
- $this->assertSame('paid', $payment->fresh()->status);
+ $this->assertSame('paid', $transaction->fresh()->mollie_status);
$this->assertEquals(1234, $wallet->fresh()->balance);
$mollie_response['status'] = 'paid';
@@ -145,7 +145,7 @@
$response = $this->post("api/webhooks/payment/mollie", $post);
$response->assertStatus(200);
- $this->assertSame('paid', $payment->fresh()->status);
+ $this->assertSame('paid', $transaction->fresh()->mollie_status);
$this->assertEquals(1234, $wallet->fresh()->balance);
}

File Metadata

Mime Type
text/plain
Expires
Sun, Apr 5, 10:45 AM (1 d, 19 h ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
18833219
Default Alt Text
D1102.1775385920.diff (12 KB)

Event Timeline