create([ 'type' => 'endcustomer', 'paddle_price_id' => 'pri_pkg_001', 'price' => 59, ]); $payload = [ 'id' => 'txn_123', 'event_type' => 'transaction.completed', 'currency_code' => 'EUR', 'totals' => [ 'grand_total' => [ 'amount' => 5900, ], ], 'metadata' => [ 'type' => 'gift_card', 'purchaser_email' => 'buyer@example.com', 'recipient_email' => 'friend@example.com', 'recipient_name' => 'Friend', 'message' => 'Happy Day', ], 'checkout_id' => 'chk_abc', ]; Bus::fake([SyncCouponToPaddle::class]); $service = $this->app->make(GiftVoucherService::class); $voucher = $service->issueFromPaddle($payload); $this->assertInstanceOf(GiftVoucher::class, $voucher); $this->assertSame(59.00, (float) $voucher->amount); $this->assertNotNull($voucher->coupon); $this->assertSame($voucher->code, $voucher->coupon->code); $this->assertTrue($voucher->expires_at->greaterThan(now()->addYears(4))); $this->assertTrue($voucher->coupon->packages()->whereKey($package->id)->exists()); Bus::assertDispatched(SyncCouponToPaddle::class); } public function test_redeeming_coupon_marks_voucher_redeemed(): void { $voucher = GiftVoucher::factory()->create([ 'status' => GiftVoucher::STATUS_ISSUED, 'amount' => 29, ]); $coupon = Coupon::factory()->create([ 'code' => $voucher->code, 'type' => CouponType::FLAT, 'amount' => 29, 'currency' => 'EUR', 'paddle_discount_id' => null, ]); $voucher->coupon()->associate($coupon)->save(); $service = $this->app->make(GiftVoucherService::class); $service->markRedeemed($coupon, 'txn_999'); $this->assertSame(GiftVoucher::STATUS_REDEEMED, $voucher->refresh()->status); $this->assertNotNull($voucher->redeemed_at); } }