Add PayPal support for add-on and gift voucher checkout
This commit is contained in:
@@ -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,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user