coupon code system eingeführt. coupons werden vom super admin gemanaged. coupons werden mit paddle synchronisiert und dort validiert. plus: einige mobil-optimierungen im tenant admin pwa.
This commit is contained in:
72
tests/Feature/Api/Marketing/CouponPreviewTest.php
Normal file
72
tests/Feature/Api/Marketing/CouponPreviewTest.php
Normal file
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Api\Marketing;
|
||||
|
||||
use App\Models\Coupon;
|
||||
use App\Models\Package;
|
||||
use App\Services\Paddle\PaddleDiscountService;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Mockery;
|
||||
use Tests\TestCase;
|
||||
|
||||
class CouponPreviewTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
Mockery::close();
|
||||
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
public function test_guest_can_preview_coupon(): void
|
||||
{
|
||||
$package = Package::factory()->create([
|
||||
'paddle_price_id' => 'pri_test',
|
||||
'price' => 100,
|
||||
]);
|
||||
|
||||
$coupon = Coupon::factory()->create([
|
||||
'code' => 'SAVE20',
|
||||
'paddle_discount_id' => 'dsc_123',
|
||||
'per_customer_limit' => null,
|
||||
]);
|
||||
$coupon->packages()->attach($package);
|
||||
|
||||
$this->instance(PaddleDiscountService::class, Mockery::mock(PaddleDiscountService::class, function ($mock) {
|
||||
$mock->shouldReceive('previewDiscount')->andReturn([
|
||||
'totals' => [
|
||||
'currency_code' => 'EUR',
|
||||
'subtotal' => 10000,
|
||||
'discount' => 2000,
|
||||
'tax' => 0,
|
||||
'total' => 8000,
|
||||
],
|
||||
]);
|
||||
}));
|
||||
|
||||
$response = $this->postJson(route('api.v1.marketing.coupons.preview'), [
|
||||
'package_id' => $package->id,
|
||||
'code' => 'save20',
|
||||
]);
|
||||
|
||||
$response->assertOk()
|
||||
->assertJsonPath('coupon.code', 'SAVE20');
|
||||
|
||||
$this->assertEquals(80.0, (float) $response->json('pricing.total'));
|
||||
}
|
||||
|
||||
public function test_invalid_coupon_returns_validation_error(): void
|
||||
{
|
||||
$package = Package::factory()->create([
|
||||
'paddle_price_id' => 'pri_test_invalid',
|
||||
]);
|
||||
|
||||
$this->postJson(route('api.v1.marketing.coupons.preview'), [
|
||||
'package_id' => $package->id,
|
||||
'code' => 'UNKNOWN',
|
||||
])->assertUnprocessable()
|
||||
->assertJsonValidationErrors('code');
|
||||
}
|
||||
}
|
||||
48
tests/Feature/Console/CouponExportCommandTest.php
Normal file
48
tests/Feature/Console/CouponExportCommandTest.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Console;
|
||||
|
||||
use App\Models\Coupon;
|
||||
use App\Models\CouponRedemption;
|
||||
use App\Models\Package;
|
||||
use App\Models\Tenant;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Tests\TestCase;
|
||||
|
||||
class CouponExportCommandTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_export_command_creates_csv(): void
|
||||
{
|
||||
Storage::fake('local');
|
||||
|
||||
$coupon = Coupon::factory()->create([
|
||||
'code' => 'FLASH20',
|
||||
]);
|
||||
$package = Package::factory()->create();
|
||||
$tenant = Tenant::factory()->create();
|
||||
|
||||
CouponRedemption::factory()->create([
|
||||
'coupon_id' => $coupon->id,
|
||||
'package_id' => $package->id,
|
||||
'tenant_id' => $tenant->id,
|
||||
'status' => CouponRedemption::STATUS_SUCCESS,
|
||||
'amount_discounted' => 25,
|
||||
'redeemed_at' => now(),
|
||||
]);
|
||||
|
||||
$path = 'reports/test-coupons.csv';
|
||||
|
||||
$this->artisan('coupons:export', [
|
||||
'--days' => 7,
|
||||
'--path' => $path,
|
||||
])->assertExitCode(0);
|
||||
|
||||
Storage::disk('local')->assertExists($path);
|
||||
|
||||
$contents = Storage::disk('local')->get($path);
|
||||
$this->assertStringContainsString('FLASH20', $contents);
|
||||
}
|
||||
}
|
||||
93
tests/Feature/PaddleCheckoutControllerTest.php
Normal file
93
tests/Feature/PaddleCheckoutControllerTest.php
Normal file
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Models\Coupon;
|
||||
use App\Models\Package;
|
||||
use App\Models\Tenant;
|
||||
use App\Models\User;
|
||||
use App\Services\Coupons\CouponService;
|
||||
use App\Services\Paddle\PaddleCheckoutService;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Mockery;
|
||||
use Tests\TestCase;
|
||||
|
||||
class PaddleCheckoutControllerTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
Mockery::close();
|
||||
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
public function test_authenticated_user_can_create_checkout_with_coupon(): void
|
||||
{
|
||||
$tenant = Tenant::factory()->create([
|
||||
'paddle_customer_id' => 'cus_123',
|
||||
]);
|
||||
|
||||
$user = User::factory()->for($tenant)->create();
|
||||
|
||||
$package = Package::factory()->create([
|
||||
'paddle_price_id' => 'pri_123',
|
||||
'paddle_product_id' => 'pro_123',
|
||||
'price' => 120,
|
||||
]);
|
||||
|
||||
$coupon = Coupon::factory()->create([
|
||||
'code' => 'SAVE15',
|
||||
'paddle_discount_id' => 'dsc_123',
|
||||
]);
|
||||
$coupon->packages()->attach($package);
|
||||
|
||||
$couponServiceMock = Mockery::mock(CouponService::class);
|
||||
$couponServiceMock->shouldReceive('preview')
|
||||
->once()
|
||||
->andReturn([
|
||||
'coupon' => $coupon,
|
||||
'pricing' => [
|
||||
'currency' => 'EUR',
|
||||
'subtotal' => 120.0,
|
||||
'discount' => 18.0,
|
||||
'tax' => 0,
|
||||
'total' => 102.0,
|
||||
'formatted' => [
|
||||
'subtotal' => '€120.00',
|
||||
'discount' => '-€18.00',
|
||||
'tax' => '€0.00',
|
||||
'total' => '€102.00',
|
||||
],
|
||||
'breakdown' => [],
|
||||
],
|
||||
'source' => 'manual',
|
||||
]);
|
||||
$this->instance(CouponService::class, $couponServiceMock);
|
||||
|
||||
$paddleServiceMock = Mockery::mock(PaddleCheckoutService::class);
|
||||
$paddleServiceMock->shouldReceive('createCheckout')
|
||||
->once()
|
||||
->andReturn([
|
||||
'checkout_url' => 'https://example.com/checkout/test',
|
||||
'id' => 'chk_123',
|
||||
]);
|
||||
$this->instance(PaddleCheckoutService::class, $paddleServiceMock);
|
||||
|
||||
$this->be($user);
|
||||
|
||||
$response = $this->postJson(route('paddle.checkout.create'), [
|
||||
'package_id' => $package->id,
|
||||
'coupon_code' => 'SAVE15',
|
||||
]);
|
||||
|
||||
$response->assertOk()
|
||||
->assertJsonPath('checkout_url', 'https://example.com/checkout/test');
|
||||
|
||||
$this->assertDatabaseHas('checkout_sessions', [
|
||||
'package_id' => $package->id,
|
||||
'coupon_code' => 'SAVE15',
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user