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], ]); $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_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'], ]); $checkoutService = Mockery::mock(LemonSqueezyCheckoutService::class); $checkoutService->shouldReceive('createVariantCheckout') ->once() ->with('pri_a', Mockery::on(function (array $customData) { return ($customData['type'] ?? null) === 'gift_voucher' && ($customData['tier_key'] ?? null) === 'gift-a' && ($customData['purchaser_email'] ?? null) === 'buyer@example.com' && ($customData['recipient_email'] ?? null) === 'friend@example.com' && ($customData['recipient_name'] ?? null) === 'Friend' && ($customData['message'] ?? null) === 'Hi'; }), Mockery::type('array')) ->andReturn(['checkout_url' => 'https://lemonsqueezy.test/checkout/123', 'id' => 'chk_123']); $this->app->instance(LemonSqueezyCheckoutService::class, $checkoutService); $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://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, ]); } }