Changeset View
Changeset View
Standalone View
Standalone View
src/tests/Feature/Controller/PaymentsMollieTest.php
<?php | <?php | ||||
namespace Tests\Feature\Controller; | namespace Tests\Feature\Controller; | ||||
use App\Http\Controllers\API\V4\PaymentsController; | use App\Http\Controllers\API\V4\PaymentsController; | ||||
use App\Payment; | use App\Payment; | ||||
use App\Plan; | |||||
use App\Providers\PaymentProvider; | use App\Providers\PaymentProvider; | ||||
use App\Transaction; | use App\Transaction; | ||||
use App\Wallet; | use App\Wallet; | ||||
use App\WalletSetting; | use App\WalletSetting; | ||||
use App\VatRate; | use App\VatRate; | ||||
use App\Utils; | use App\Utils; | ||||
use GuzzleHttp\Psr7\Response; | use GuzzleHttp\Psr7\Response; | ||||
use Illuminate\Support\Facades\Bus; | use Illuminate\Support\Facades\Bus; | ||||
Show All 27 Lines | public function setUp(): void | ||||
Wallet::where('id', $wallet->id)->update(['balance' => 0]); | Wallet::where('id', $wallet->id)->update(['balance' => 0]); | ||||
WalletSetting::where('wallet_id', $wallet->id)->delete(); | WalletSetting::where('wallet_id', $wallet->id)->delete(); | ||||
$types = [ | $types = [ | ||||
Transaction::WALLET_CREDIT, | Transaction::WALLET_CREDIT, | ||||
Transaction::WALLET_REFUND, | Transaction::WALLET_REFUND, | ||||
Transaction::WALLET_CHARGEBACK, | Transaction::WALLET_CHARGEBACK, | ||||
]; | ]; | ||||
Transaction::where('object_id', $wallet->id)->whereIn('type', $types)->delete(); | Transaction::where('object_id', $wallet->id)->whereIn('type', $types)->delete(); | ||||
Plan::withEnvTenantContext()->where('title', 'individual')->update(['mode' => 'email', 'months' => 1]); | |||||
} | } | ||||
/** | /** | ||||
* {@inheritDoc} | * {@inheritDoc} | ||||
*/ | */ | ||||
public function tearDown(): void | public function tearDown(): void | ||||
{ | { | ||||
$this->deleteTestUser('payment-test@' . \config('app.domain')); | $this->deleteTestUser('payment-test@' . \config('app.domain')); | ||||
$john = $this->getTestUser('john@kolab.org'); | $john = $this->getTestUser('john@kolab.org'); | ||||
$wallet = $john->wallets()->first(); | $wallet = $john->wallets()->first(); | ||||
Payment::query()->delete(); | Payment::query()->delete(); | ||||
VatRate::query()->delete(); | VatRate::query()->delete(); | ||||
Wallet::where('id', $wallet->id)->update(['balance' => 0]); | Wallet::where('id', $wallet->id)->update(['balance' => 0]); | ||||
WalletSetting::where('wallet_id', $wallet->id)->delete(); | WalletSetting::where('wallet_id', $wallet->id)->delete(); | ||||
$types = [ | $types = [ | ||||
Transaction::WALLET_CREDIT, | Transaction::WALLET_CREDIT, | ||||
Transaction::WALLET_REFUND, | Transaction::WALLET_REFUND, | ||||
Transaction::WALLET_CHARGEBACK, | Transaction::WALLET_CHARGEBACK, | ||||
]; | ]; | ||||
Transaction::where('object_id', $wallet->id)->whereIn('type', $types)->delete(); | Transaction::where('object_id', $wallet->id)->whereIn('type', $types)->delete(); | ||||
Plan::withEnvTenantContext()->where('title', 'individual')->update(['mode' => 'email', 'months' => 1]); | |||||
Utils::setTestExchangeRates([]); | Utils::setTestExchangeRates([]); | ||||
parent::tearDown(); | parent::tearDown(); | ||||
} | } | ||||
/** | /** | ||||
* Test creating/updating/deleting an outo-payment mandate | * Test creating/updating/deleting an outo-payment mandate | ||||
* | * | ||||
▲ Show 20 Lines • Show All 254 Lines • ▼ Show 20 Lines | public function testMandates(): void | ||||
$json = $response->json(); | $json = $response->json(); | ||||
$this->assertFalse(array_key_exists('id', $json)); | $this->assertFalse(array_key_exists('id', $json)); | ||||
$this->assertFalse(array_key_exists('method', $json)); | $this->assertFalse(array_key_exists('method', $json)); | ||||
$this->assertNull($wallet->fresh()->getSetting('mollie_mandate_id')); | $this->assertNull($wallet->fresh()->getSetting('mollie_mandate_id')); | ||||
} | } | ||||
/** | /** | ||||
* Test fetching an outo-payment mandate parameters | |||||
* | |||||
* @group mollie | |||||
*/ | |||||
public function testMandateParams(): void | |||||
{ | |||||
$plan = Plan::withEnvTenantContext()->where('title', 'individual')->first(); | |||||
$user = $this->getTestUser('payment-test@' . \config('app.domain')); | |||||
$wallet = $user->wallets()->first(); | |||||
$response = $this->actingAs($user)->get("api/v4/payments/mandate"); | |||||
$response->assertStatus(200); | |||||
$json = $response->json(); | |||||
$this->assertSame((int) ceil(Payment::MIN_AMOUNT / 100), $json['amount']); | |||||
$this->assertSame($json['amount'], $json['minAmount']); | |||||
$this->assertSame(0, $json['balance']); | |||||
$this->assertFalse($json['isValid']); | |||||
$this->assertFalse($json['isDisabled']); | |||||
$plan->months = 12; | |||||
$plan->save(); | |||||
$user->setSetting('plan_id', $plan->id); | |||||
$response = $this->actingAs($user)->get("api/v4/payments/mandate"); | |||||
$response->assertStatus(200); | |||||
$json = $response->json(); | |||||
$this->assertSame((int) ceil(Payment::MIN_AMOUNT / 100), $json['amount']); | |||||
$this->assertSame((int) ceil(($plan->cost() * $plan->months) / 100), $json['minAmount']); | |||||
// TODO: Test more cases | |||||
// TODO: Test user unrestricting if mandate is valid | |||||
} | |||||
/** | |||||
* Test creating a payment and receiving a status via webhook | * Test creating a payment and receiving a status via webhook | ||||
* | * | ||||
* @group mollie | * @group mollie | ||||
*/ | */ | ||||
public function testStoreAndWebhook(): void | public function testStoreAndWebhook(): void | ||||
{ | { | ||||
Bus::fake(); | Bus::fake(); | ||||
▲ Show 20 Lines • Show All 215 Lines • ▼ Show 20 Lines | public function testTopUp(): void | ||||
// Assert that email notification job has been dispatched | // Assert that email notification job has been dispatched | ||||
$this->assertSame(Payment::STATUS_PAID, $payment->status); | $this->assertSame(Payment::STATUS_PAID, $payment->status); | ||||
$this->assertEquals(2010, $wallet->fresh()->balance); | $this->assertEquals(2010, $wallet->fresh()->balance); | ||||
$transaction = $wallet->transactions() | $transaction = $wallet->transactions() | ||||
->where('type', Transaction::WALLET_CREDIT)->get()->last(); | ->where('type', Transaction::WALLET_CREDIT)->get()->last(); | ||||
$this->assertSame(2010, $transaction->amount); | $this->assertSame(2010, $transaction->amount); | ||||
$this->assertSame( | $this->assertSame( | ||||
"Auto-payment transaction {$payment->id} using Mastercard (**** **** **** 9399)", | "Auto-payment transaction {$payment->id} using Mastercard (**** **** **** 6787)", | ||||
$transaction->description | $transaction->description | ||||
); | ); | ||||
Bus::assertDispatchedTimes(\App\Jobs\PaymentEmail::class, 1); | Bus::assertDispatchedTimes(\App\Jobs\PaymentEmail::class, 1); | ||||
Bus::assertDispatched(\App\Jobs\PaymentEmail::class, function ($job) use ($payment) { | Bus::assertDispatched(\App\Jobs\PaymentEmail::class, function ($job) use ($payment) { | ||||
$job_payment = $this->getObjectProperty($job, 'payment'); | $job_payment = $this->getObjectProperty($job, 'payment'); | ||||
return $job_payment->id === $payment->id; | return $job_payment->id === $payment->id; | ||||
}); | }); | ||||
▲ Show 20 Lines • Show All 572 Lines • Show Last 20 Lines |