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,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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']);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user