Add PayPal support for add-on and gift voucher checkout

This commit is contained in:
Codex Agent
2026-02-04 14:54:40 +01:00
parent 7025418d9e
commit 17025df47b
24 changed files with 1599 additions and 34 deletions

View File

@@ -28,6 +28,25 @@ class GiftVoucherLookupTest extends TestCase
->assertJsonPath('data.currency', 'EUR');
}
public function test_it_returns_voucher_by_paypal_order_id(): void
{
$voucher = GiftVoucher::factory()->create([
'code' => 'GIFT-PAYPAL',
'amount' => 59.00,
'currency' => 'EUR',
'paypal_order_id' => 'ORDER-PAYPAL-1',
'paypal_capture_id' => 'CAPTURE-PAYPAL-1',
'status' => GiftVoucher::STATUS_ISSUED,
]);
$response = $this->getJson('/api/v1/marketing/gift-vouchers/lookup?order_id=CAPTURE-PAYPAL-1');
$response->assertOk()
->assertJsonPath('data.code', $voucher->code)
->assertJsonPath('data.amount', 59)
->assertJsonPath('data.currency', 'EUR');
}
public function test_it_returns_voucher_by_code(): void
{
$voucher = GiftVoucher::factory()->create([

View File

@@ -5,6 +5,10 @@ namespace Tests\Feature;
use App\Models\CheckoutSession;
use App\Models\Coupon;
use App\Models\CouponRedemption;
use App\Models\Event;
use App\Models\EventPackage;
use App\Models\EventPackageAddon;
use App\Models\GiftVoucher;
use App\Models\IntegrationWebhookEvent;
use App\Models\Package;
use App\Models\PackagePurchase;
@@ -16,6 +20,8 @@ use App\Services\Coupons\CouponService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Notification;
use Tests\TestCase;
class PayPalWebhookControllerTest extends TestCase
@@ -140,6 +146,149 @@ class PayPalWebhookControllerTest extends TestCase
$response->assertStatus(400)->assertJson(['status' => 'invalid']);
}
public function test_capture_completed_applies_addon_purchase(): void
{
Notification::fake();
config([
'services.paypal.client_id' => 'client',
'services.paypal.secret' => 'secret',
'services.paypal.sandbox' => true,
'services.paypal.webhook_id' => 'wh_123',
]);
Http::fake([
'https://api-m.sandbox.paypal.com/v1/oauth2/token' => Http::response([
'access_token' => 'token',
'expires_in' => 3600,
]),
'https://api-m.sandbox.paypal.com/v1/notifications/verify-webhook-signature' => Http::response([
'verification_status' => 'SUCCESS',
]),
]);
$tenant = Tenant::factory()->create([
'contact_email' => 'tenant@example.com',
]);
$package = Package::factory()->endcustomer()->create([
'max_photos' => 100,
]);
$event = Event::factory()->for($tenant)->create([
'status' => 'published',
]);
$eventPackage = EventPackage::create([
'event_id' => $event->id,
'package_id' => $package->id,
'purchased_price' => $package->price,
'purchased_at' => now(),
'used_photos' => 0,
'used_guests' => 0,
'gallery_expires_at' => now()->addDays(7),
]);
$addon = EventPackageAddon::create([
'event_package_id' => $eventPackage->id,
'event_id' => $event->id,
'tenant_id' => $tenant->id,
'addon_key' => 'extra_photos_small',
'quantity' => 1,
'extra_photos' => 0,
'status' => 'pending',
'checkout_id' => 'ORDER-ADDON-1',
'metadata' => [
'increments' => ['extra_photos' => 500],
],
]);
$payload = [
'id' => 'WH-ADDON-1',
'event_type' => 'PAYMENT.CAPTURE.COMPLETED',
'resource' => [
'id' => 'CAPTURE-ADDON-1',
'status' => 'COMPLETED',
'amount' => [
'value' => '12.50',
'currency_code' => 'EUR',
],
'supplementary_data' => [
'related_ids' => [
'order_id' => 'ORDER-ADDON-1',
],
],
],
];
$response = $this->withHeaders($this->paypalHeaders())
->postJson('/paypal/webhook', $payload);
$response->assertOk()->assertJson(['status' => 'processed']);
$addon->refresh();
$eventPackage->refresh();
$this->assertSame('completed', $addon->status);
$this->assertSame('CAPTURE-ADDON-1', $addon->transaction_id);
$this->assertSame(500, $eventPackage->extra_photos);
}
public function test_capture_completed_issues_gift_voucher(): void
{
Mail::fake();
config()->set('gift-vouchers.reminder_days', 0);
config()->set('gift-vouchers.expiry_reminder_days', 0);
config([
'services.paypal.client_id' => 'client',
'services.paypal.secret' => 'secret',
'services.paypal.sandbox' => true,
'services.paypal.webhook_id' => 'wh_123',
]);
Http::fake([
'https://api-m.sandbox.paypal.com/v1/oauth2/token' => Http::response([
'access_token' => 'token',
'expires_in' => 3600,
]),
'https://api-m.sandbox.paypal.com/v1/notifications/verify-webhook-signature' => Http::response([
'verification_status' => 'SUCCESS',
]),
]);
$voucher = GiftVoucher::factory()->create([
'status' => GiftVoucher::STATUS_PENDING,
'paypal_order_id' => 'ORDER-GIFT-1',
]);
$payload = [
'id' => 'WH-GIFT-1',
'event_type' => 'PAYMENT.CAPTURE.COMPLETED',
'resource' => [
'id' => 'CAPTURE-GIFT-1',
'status' => 'COMPLETED',
'amount' => [
'value' => '29.00',
'currency_code' => 'EUR',
],
'supplementary_data' => [
'related_ids' => [
'order_id' => 'ORDER-GIFT-1',
],
],
],
];
$response = $this->withHeaders($this->paypalHeaders())
->postJson('/paypal/webhook', $payload);
$response->assertOk()->assertJson(['status' => 'processed']);
$voucher->refresh();
$this->assertSame(GiftVoucher::STATUS_ISSUED, $voucher->status);
$this->assertSame('CAPTURE-GIFT-1', $voucher->paypal_capture_id);
$this->assertNotNull($voucher->coupon_id);
}
/**
* @return array{
* 0: \App\Models\Tenant,

View File

@@ -2,12 +2,15 @@
namespace Tests\Feature\Tenant;
use App\Models\CheckoutSession;
use App\Models\Event;
use App\Models\EventPackage;
use App\Models\EventPackageAddon;
use App\Models\Package;
use App\Services\PayPal\PayPalOrderService;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Http;
use Mockery;
class EventAddonCheckoutTest extends TenantTestCase
{
@@ -20,14 +23,15 @@ class EventAddonCheckoutTest extends TenantTestCase
'variant_id' => 'var_addon_photos',
'increments' => ['extra_photos' => 500],
]);
Config::set('package-addons.provider', CheckoutSession::PROVIDER_LEMONSQUEEZY);
Config::set('lemonsqueezy.api_key', 'test_key');
Config::set('lemonsqueezy.base_url', 'https://lemonsqueezy.test');
Config::set('lemonsqueezy.base_url', 'https://api.lemonsqueezy.com/v1');
Config::set('lemonsqueezy.store_id', 'store_123');
// Fake Lemon Squeezy response
Http::fake([
'https://lemonsqueezy.test/checkouts' => Http::response([
'https://api.lemonsqueezy.com/v1/checkouts' => Http::response([
'data' => [
'id' => 'chk_addon_123',
'attributes' => [
@@ -81,4 +85,69 @@ class EventAddonCheckoutTest extends TenantTestCase
$addon = EventPackageAddon::where('event_package_id', $eventPackage->id)->latest()->first();
$this->assertSame(1000, $addon->extra_photos); // increments * quantity
}
public function test_paypal_checkout_creates_pending_addon_record(): void
{
Config::set('package-addons.provider', CheckoutSession::PROVIDER_PAYPAL);
Config::set('checkout.currency', 'EUR');
Config::set('package-addons.extra_photos_small.price', 12.50);
$package = Package::factory()->endcustomer()->create([
'max_photos' => 100,
'max_guests' => 50,
'gallery_days' => 7,
]);
$event = Event::factory()->for($this->tenant)->create([
'status' => 'published',
]);
$eventPackage = EventPackage::create([
'event_id' => $event->id,
'package_id' => $package->id,
'purchased_price' => $package->price,
'purchased_at' => now(),
'used_photos' => 0,
'used_guests' => 0,
'gallery_expires_at' => now()->addDays(7),
]);
$orders = Mockery::mock(PayPalOrderService::class);
$orders->shouldReceive('createSimpleOrder')
->once()
->andReturn([
'id' => 'ORDER-ADDON-1',
'links' => [
['rel' => 'approve', 'href' => 'https://paypal.test/approve'],
],
]);
$orders->shouldReceive('resolveApproveUrl')
->once()
->andReturn('https://paypal.test/approve');
$this->app->instance(PayPalOrderService::class, $orders);
$response = $this->authenticatedRequest('POST', "/api/v1/tenant/events/{$event->slug}/addons/checkout", [
'addon_key' => 'extra_photos_small',
'quantity' => 2,
'accepted_terms' => true,
'accepted_waiver' => true,
]);
$response->assertOk();
$response->assertJsonPath('checkout_id', 'ORDER-ADDON-1');
$response->assertJsonPath('checkout_url', 'https://paypal.test/approve');
$this->assertDatabaseHas('event_package_addons', [
'event_package_id' => $eventPackage->id,
'addon_key' => 'extra_photos_small',
'status' => 'pending',
'quantity' => 2,
'checkout_id' => 'ORDER-ADDON-1',
'amount' => 25.00,
'currency' => 'EUR',
]);
$addon = EventPackageAddon::where('event_package_id', $eventPackage->id)->latest()->first();
$this->assertSame(1000, $addon->extra_photos);
}
}

View File

@@ -2,8 +2,10 @@
namespace Tests\Unit;
use App\Models\CheckoutSession;
use App\Services\GiftVouchers\GiftVoucherCheckoutService;
use App\Services\LemonSqueezy\LemonSqueezyCheckoutService;
use App\Services\PayPal\PayPalOrderService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Mockery;
use Tests\TestCase;
@@ -14,6 +16,7 @@ class GiftVoucherCheckoutServiceTest extends TestCase
public function test_it_lists_tiers_with_checkout_flag(): void
{
config()->set('gift-vouchers.provider', CheckoutSession::PROVIDER_LEMONSQUEEZY);
config()->set('gift-vouchers.tiers', [
['key' => 'gift-a', 'label' => 'A', 'amount' => 10, 'currency' => 'EUR', 'lemonsqueezy_variant_id' => 'pri_a'],
['key' => 'gift-b', 'label' => 'B', 'amount' => 20, 'currency' => 'EUR', 'lemonsqueezy_variant_id' => null],
@@ -28,8 +31,27 @@ class GiftVoucherCheckoutServiceTest extends TestCase
$this->assertFalse($tiers[1]['can_checkout']);
}
public function test_it_lists_tiers_for_paypal_currency_only(): void
{
config()->set('gift-vouchers.provider', CheckoutSession::PROVIDER_PAYPAL);
config()->set('checkout.currency', 'EUR');
config()->set('gift-vouchers.tiers', [
['key' => 'gift-eur', 'label' => 'EUR', 'amount' => 10, 'currency' => 'EUR', 'lemonsqueezy_variant_id' => null],
['key' => 'gift-usd', 'label' => 'USD', 'amount' => 20, 'currency' => 'USD', 'lemonsqueezy_variant_id' => null],
]);
$service = $this->app->make(GiftVoucherCheckoutService::class);
$tiers = $service->tiers();
$this->assertCount(2, $tiers);
$this->assertTrue($tiers[0]['can_checkout']);
$this->assertFalse($tiers[1]['can_checkout']);
}
public function test_it_creates_checkout_link_with_metadata(): void
{
config()->set('gift-vouchers.provider', CheckoutSession::PROVIDER_LEMONSQUEEZY);
config()->set('gift-vouchers.tiers', [
['key' => 'gift-a', 'label' => 'A', 'amount' => 10, 'currency' => 'EUR', 'lemonsqueezy_variant_id' => 'pri_a'],
]);
@@ -62,4 +84,46 @@ class GiftVoucherCheckoutServiceTest extends TestCase
$this->assertSame('https://lemonsqueezy.test/checkout/123', $checkout['checkout_url']);
$this->assertSame('chk_123', $checkout['id']);
}
public function test_it_creates_paypal_checkout(): void
{
config()->set('gift-vouchers.provider', CheckoutSession::PROVIDER_PAYPAL);
config()->set('checkout.currency', 'EUR');
config()->set('gift-vouchers.tiers', [
['key' => 'gift-a', 'label' => 'A', 'amount' => 10, 'currency' => 'EUR', 'lemonsqueezy_variant_id' => null],
]);
$orders = Mockery::mock(PayPalOrderService::class);
$orders->shouldReceive('createSimpleOrder')
->once()
->andReturn([
'id' => 'ORDER-123',
'links' => [
['rel' => 'approve', 'href' => 'https://paypal.test/approve'],
],
]);
$orders->shouldReceive('resolveApproveUrl')
->once()
->andReturn('https://paypal.test/approve');
$this->app->instance(PayPalOrderService::class, $orders);
$service = $this->app->make(GiftVoucherCheckoutService::class);
$checkout = $service->create([
'tier_key' => 'gift-a',
'purchaser_email' => 'buyer@example.com',
'recipient_email' => 'friend@example.com',
'recipient_name' => 'Friend',
'message' => 'Hi',
]);
$this->assertSame('https://paypal.test/approve', $checkout['checkout_url']);
$this->assertSame('ORDER-123', $checkout['id']);
$this->assertDatabaseHas('gift_vouchers', [
'paypal_order_id' => 'ORDER-123',
'status' => \App\Models\GiftVoucher::STATUS_PENDING,
]);
}
}

View File

@@ -2,6 +2,7 @@
namespace Tests\Unit;
use App\Enums\CouponStatus;
use App\Enums\CouponType;
use App\Mail\GiftVoucherIssued;
use App\Models\Coupon;
@@ -156,4 +157,65 @@ class GiftVoucherServiceTest extends TestCase
$this->assertSame(65.00, (float) $voucher->amount);
$this->assertSame('USD', $voucher->currency);
}
public function test_it_issues_voucher_from_paypal_payload(): void
{
Mail::fake();
config()->set('gift-vouchers.reminder_days', 0);
config()->set('gift-vouchers.expiry_reminder_days', 0);
$voucher = GiftVoucher::factory()->create([
'status' => GiftVoucher::STATUS_PENDING,
'paypal_order_id' => 'ORDER-123',
]);
$payload = [
'id' => 'CAPTURE-123',
'status' => 'COMPLETED',
'purchase_units' => [
[
'payments' => [
'captures' => [
['id' => 'CAPTURE-123'],
],
],
],
],
];
$service = $this->app->make(GiftVoucherService::class);
$service->issueFromPayPal($voucher, $payload, 'ORDER-123');
$voucher->refresh();
$this->assertSame(GiftVoucher::STATUS_ISSUED, $voucher->status);
$this->assertSame('ORDER-123', $voucher->paypal_order_id);
$this->assertSame('CAPTURE-123', $voucher->paypal_capture_id);
$this->assertNotNull($voucher->coupon_id);
Mail::assertQueued(GiftVoucherIssued::class, 2);
}
public function test_it_marks_voucher_refunded_from_paypal(): void
{
$coupon = Coupon::factory()->create([
'status' => CouponStatus::ACTIVE,
'enabled_for_checkout' => true,
]);
$voucher = GiftVoucher::factory()->create([
'status' => GiftVoucher::STATUS_ISSUED,
'coupon_id' => $coupon->id,
]);
$service = $this->app->make(GiftVoucherService::class);
$service->markRefundedFromPayPal($voucher, ['id' => 'REFUND-1']);
$voucher->refresh();
$coupon->refresh();
$this->assertSame(GiftVoucher::STATUS_REFUNDED, $voucher->status);
$this->assertSame(CouponStatus::ARCHIVED, $coupon->status);
$this->assertFalse($coupon->enabled_for_checkout);
}
}

View File

@@ -17,7 +17,7 @@ class EventAddonCatalogTest extends TestCase
Config::set('package-addons', [
'extra_photos_small' => [
'label' => 'Config Photos',
'price_id' => 'pri_config',
'variant_id' => 'var_config',
'increments' => ['extra_photos' => 100],
],
]);
@@ -25,10 +25,13 @@ class EventAddonCatalogTest extends TestCase
PackageAddon::create([
'key' => 'extra_photos_small',
'label' => 'DB Photos',
'price_id' => 'pri_db',
'variant_id' => 'var_db',
'extra_photos' => 200,
'active' => true,
'sort' => 1,
'metadata' => [
'price_eur' => 12,
],
]);
$catalog = $this->app->make(EventAddonCatalog::class);
@@ -37,7 +40,9 @@ class EventAddonCatalogTest extends TestCase
$this->assertNotNull($addon);
$this->assertSame('DB Photos', $addon['label']);
$this->assertSame('pri_db', $addon['price_id']);
$this->assertSame('var_db', $addon['variant_id']);
$this->assertSame(200, $addon['increments']['extra_photos']);
$this->assertSame(12.0, $addon['price']);
$this->assertSame('EUR', $addon['currency']);
}
}