| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660 |
- <?php
- namespace Srmklive\PayPal\Tests\Feature;
- use Carbon\Carbon;
- use PHPUnit\Framework\Attributes\Test;
- use PHPUnit\Framework\TestCase;
- use Srmklive\PayPal\Services\PayPal as PayPalClient;
- use Srmklive\PayPal\Tests\MockClientClasses;
- use Srmklive\PayPal\Tests\MockResponsePayloads;
- class AdapterCreateSubscriptionHelpersTest extends TestCase
- {
- use MockClientClasses;
- use MockResponsePayloads;
- /** @var string */
- protected static string $access_token = '';
- /** @var PayPalClient */
- protected PayPalClient $client;
- protected function setUp(): void
- {
- $this->client = new PayPalClient($this->getApiCredentials());
- $this->client->setClient(
- $this->mock_http_client(
- $this->mockAccessTokenResponse()
- )
- );
- $response = $this->client->getAccessToken();
- self::$access_token = $response['access_token'];
- parent::setUp();
- }
- #[Test]
- public function it_can_create_a_monthly_subscription(): void
- {
- $this->client->setAccessToken([
- 'access_token' => self::$access_token,
- 'token_type' => 'Bearer',
- ]);
- $this->client->setClient(
- $this->mock_http_client(
- $this->mockCreateCatalogProductsResponse()
- )
- );
- $start_date = Carbon::now()->addDay()->toDateString();
- $this->client = $this->client->addProduct('Demo Product', 'Demo Product', 'SERVICE', 'SOFTWARE');
- $this->client->setClient(
- $this->mock_http_client(
- $this->mockCreatePlansResponse()
- )
- );
- $this->client = $this->client->addPlanTrialPricing('DAY', 7)
- ->addMonthlyPlan('Demo Plan', 'Demo Plan', 100);
- $this->client->setClient(
- $this->mock_http_client(
- $this->mockCreateSubscriptionResponse()
- )
- );
- $response = $this->client->setReturnAndCancelUrl('https://example.com/paypal-success', 'https://example.com/paypal-cancel')
- ->setupSubscription('John Doe', 'john@example.com', $start_date);
- $this->assertNotEmpty($response);
- $this->assertArrayHasKey('id', $response);
- $this->assertArrayHasKey('plan_id', $response);
- }
- #[Test]
- public function it_can_create_a_daily_subscription(): void
- {
- $this->client->setAccessToken([
- 'access_token' => self::$access_token,
- 'token_type' => 'Bearer',
- ]);
- $this->client->setClient(
- $this->mock_http_client(
- $this->mockCreateCatalogProductsResponse()
- )
- );
- $start_date = Carbon::now()->addDay()->toDateString();
- $this->client = $this->client->addProduct('Demo Product', 'Demo Product', 'SERVICE', 'SOFTWARE');
- $this->client->setClient(
- $this->mock_http_client(
- $this->mockCreatePlansResponse()
- )
- );
- $this->client = $this->client->addPlanTrialPricing('DAY', 7)
- ->addDailyPlan('Demo Plan', 'Demo Plan', 1.50);
- $this->client->setClient(
- $this->mock_http_client(
- $this->mockCreateSubscriptionResponse()
- )
- );
- $response = $this->client->setReturnAndCancelUrl('https://example.com/paypal-success', 'https://example.com/paypal-cancel')
- ->setupSubscription('John Doe', 'john@example.com', $start_date);
- $this->assertNotEmpty($response);
- $this->assertArrayHasKey('id', $response);
- $this->assertArrayHasKey('plan_id', $response);
- }
- #[Test]
- public function it_can_create_a_weekly_subscription(): void
- {
- $this->client->setAccessToken([
- 'access_token' => self::$access_token,
- 'token_type' => 'Bearer',
- ]);
- $this->client->setClient(
- $this->mock_http_client(
- $this->mockCreateCatalogProductsResponse()
- )
- );
- $start_date = Carbon::now()->addDay()->toDateString();
- $this->client = $this->client->addProduct('Demo Product', 'Demo Product', 'SERVICE', 'SOFTWARE');
- $this->client->setClient(
- $this->mock_http_client(
- $this->mockCreatePlansResponse()
- )
- );
- $this->client = $this->client->addPlanTrialPricing('DAY', 7)
- ->addWeeklyPlan('Demo Plan', 'Demo Plan', 50);
- $this->client->setClient(
- $this->mock_http_client(
- $this->mockCreateSubscriptionResponse()
- )
- );
- $response = $this->client->setReturnAndCancelUrl('https://example.com/paypal-success', 'https://example.com/paypal-cancel')
- ->setupSubscription('John Doe', 'john@example.com', $start_date);
- $this->assertNotEmpty($response);
- $this->assertArrayHasKey('id', $response);
- $this->assertArrayHasKey('plan_id', $response);
- }
- #[Test]
- public function it_can_create_an_annual_subscription(): void
- {
- $this->client->setAccessToken([
- 'access_token' => self::$access_token,
- 'token_type' => 'Bearer',
- ]);
- $this->client->setClient(
- $this->mock_http_client(
- $this->mockCreateCatalogProductsResponse()
- )
- );
- $start_date = Carbon::now()->addDay()->toDateString();
- $this->client = $this->client->addProduct('Demo Product', 'Demo Product', 'SERVICE', 'SOFTWARE');
- $this->client->setClient(
- $this->mock_http_client(
- $this->mockCreatePlansResponse()
- )
- );
- $this->client = $this->client->addPlanTrialPricing('DAY', 7)
- ->addAnnualPlan('Demo Plan', 'Demo Plan', 100);
- $this->client->setClient(
- $this->mock_http_client(
- $this->mockCreateSubscriptionResponse()
- )
- );
- $response = $this->client->setReturnAndCancelUrl('https://example.com/paypal-success', 'https://example.com/paypal-cancel')
- ->setupSubscription('John Doe', 'john@example.com', $start_date);
- $this->assertNotEmpty($response);
- $this->assertArrayHasKey('id', $response);
- $this->assertArrayHasKey('plan_id', $response);
- }
- #[Test]
- public function it_can_create_a_subscription_with_custom_defined_interval(): void
- {
- $this->client->setAccessToken([
- 'access_token' => self::$access_token,
- 'token_type' => 'Bearer',
- ]);
- $this->client->setClient(
- $this->mock_http_client(
- $this->mockCreateCatalogProductsResponse()
- )
- );
- $start_date = Carbon::now()->addDay()->toDateString();
- $this->client = $this->client->addProduct('Demo Product', 'Demo Product', 'SERVICE', 'SOFTWARE');
- $this->client->setClient(
- $this->mock_http_client(
- $this->mockCreatePlansResponse()
- )
- );
- $this->client = $this->client->addPlanTrialPricing('DAY', 7)
- ->addCustomPlan('Demo Plan', 'Demo Plan', 100, 'MONTH', 3);
- $this->client->setClient(
- $this->mock_http_client(
- $this->mockCreateSubscriptionResponse()
- )
- );
- $response = $this->client->setReturnAndCancelUrl('https://example.com/paypal-success', 'https://example.com/paypal-cancel')
- ->setupSubscription('John Doe', 'john@example.com', $start_date);
- $this->assertNotEmpty($response);
- $this->assertArrayHasKey('id', $response);
- $this->assertArrayHasKey('plan_id', $response);
- }
- #[Test]
- public function it_throws_exception_when_invalid_interval_is_provided_for_creating_a_subscription(): void
- {
- $this->client->setAccessToken([
- 'access_token' => self::$access_token,
- 'token_type' => 'Bearer',
- ]);
- $this->client = $this->client->addProductById('PROD-XYAB12ABSB7868434');
- $this->expectException(\RuntimeException::class);
- $this->client = $this->client->addCustomPlan('Demo Plan', 'Demo Plan', 100, 'MONTHLY', 3);
- }
- #[Test]
- public function it_throws_exception_when_get_error_for_creating_a_billing_plan(): void
- {
- $this->client->setAccessToken([
- 'access_token' => self::$access_token,
- 'token_type' => 'Bearer',
- ]);
- $this->client->setClient(
- $this->mock_http_client(
- $this->mockCreateCatalogProductsResponse()
- )
- );
- $this->client = $this->client->addProduct('Demo Product', 'Demo Product', 'SERVICE', 'SOFTWARE');
- $this->client->setClient(
- $this->mock_http_client(
- $this->mockCreatePlansErrorResponse()
- )
- );
- $this->expectException(\RuntimeException::class);
- $this->client = $this->client->addMonthlyPlan('Demo Plan', 'Demo Plan', 100);
- }
- #[Test]
- public function it_throws_exception_when_get_error_for_creating_a_product(): void
- {
- $this->client->setAccessToken([
- 'access_token' => self::$access_token,
- 'token_type' => 'Bearer',
- ]);
- $this->client->setClient(
- $this->mock_http_client(
- $this->mockGetCatalogProductsErrorResponse()
- )
- );
- $this->expectException(\RuntimeException::class);
- $this->client = $this->client->addProduct('Demo Product', 'Demo Product', 'SERVICE', 'SOFTWARE');
- }
- #[Test]
- public function it_can_create_a_subscription_without_trial(): void
- {
- $this->client->setAccessToken([
- 'access_token' => self::$access_token,
- 'token_type' => 'Bearer',
- ]);
- $this->client->setClient(
- $this->mock_http_client(
- $this->mockCreateCatalogProductsResponse()
- )
- );
- $start_date = Carbon::now()->addDay()->toDateString();
- $this->client = $this->client->addProduct('Demo Product', 'Demo Product', 'SERVICE', 'SOFTWARE');
- $this->client->setClient(
- $this->mock_http_client(
- $this->mockCreatePlansResponse()
- )
- );
- $this->client = $this->client->addMonthlyPlan('Demo Plan', 'Demo Plan', 100);
- $this->client->setClient(
- $this->mock_http_client(
- $this->mockCreateSubscriptionResponse()
- )
- );
- $response = $this->client->setupSubscription('John Doe', 'john@example.com', $start_date);
- $this->assertNotEmpty($response);
- $this->assertArrayHasKey('id', $response);
- $this->assertArrayHasKey('plan_id', $response);
- }
- #[Test]
- public function it_can_create_a_subscription_by_existing_product_and_billing_plan(): void
- {
- $this->client->setAccessToken([
- 'access_token' => self::$access_token,
- 'token_type' => 'Bearer',
- ]);
- $start_date = Carbon::now()->addDay()->toDateString();
- $this->client->setClient(
- $this->mock_http_client(
- $this->mockCreateSubscriptionResponse()
- )
- );
- $response = $this->client->addProductById('PROD-XYAB12ABSB7868434')
- ->addBillingPlanById('P-5ML4271244454362WXNWU5NQ')
- ->setupSubscription('John Doe', 'john@example.com', $start_date);
- $this->assertNotEmpty($response);
- $this->assertArrayHasKey('id', $response);
- $this->assertArrayHasKey('plan_id', $response);
- }
- #[Test]
- public function it_skips_product_and_billing_plan_creation_if_already_set_when_creating_a_daily_subscription(): void
- {
- $this->client->setAccessToken([
- 'access_token' => self::$access_token,
- 'token_type' => 'Bearer',
- ]);
- $start_date = Carbon::now()->addDay()->toDateString();
- $this->client = $this->client->addProductById('PROD-XYAB12ABSB7868434')
- ->addBillingPlanById('P-5ML4271244454362WXNWU5NQ')
- ->addProduct('Demo Product', 'Demo Product', 'SERVICE', 'SOFTWARE')
- ->addDailyPlan('Demo Plan', 'Demo Plan', 1.50);
- $this->client->setClient(
- $this->mock_http_client(
- $this->mockCreateSubscriptionResponse()
- )
- );
- $response = $this->client->setupSubscription('John Doe', 'john@example.com', $start_date);
- $this->assertNotEmpty($response);
- $this->assertArrayHasKey('id', $response);
- $this->assertArrayHasKey('plan_id', $response);
- }
- #[Test]
- public function it_skips_product_and_billing_plan_creation_if_already_set_when_creating_a_weekly_subscription(): void
- {
- $this->client->setAccessToken([
- 'access_token' => self::$access_token,
- 'token_type' => 'Bearer',
- ]);
- $start_date = Carbon::now()->addDay()->toDateString();
- $this->client = $this->client->addProductById('PROD-XYAB12ABSB7868434')
- ->addBillingPlanById('P-5ML4271244454362WXNWU5NQ')
- ->addProduct('Demo Product', 'Demo Product', 'SERVICE', 'SOFTWARE')
- ->addWeeklyPlan('Demo Plan', 'Demo Plan', 100);
- $this->client->setClient(
- $this->mock_http_client(
- $this->mockCreateSubscriptionResponse()
- )
- );
- $response = $this->client->setupSubscription('John Doe', 'john@example.com', $start_date);
- $this->assertNotEmpty($response);
- $this->assertArrayHasKey('id', $response);
- $this->assertArrayHasKey('plan_id', $response);
- }
- #[Test]
- public function it_skips_product_and_billing_plan_creation_if_already_set_when_creating_a_monthly_subscription(): void
- {
- $this->client->setAccessToken([
- 'access_token' => self::$access_token,
- 'token_type' => 'Bearer',
- ]);
- $start_date = Carbon::now()->addDay()->toDateString();
- $this->client = $this->client->addProductById('PROD-XYAB12ABSB7868434')
- ->addBillingPlanById('P-5ML4271244454362WXNWU5NQ')
- ->addProduct('Demo Product', 'Demo Product', 'SERVICE', 'SOFTWARE')
- ->addMonthlyPlan('Demo Plan', 'Demo Plan', 100);
- $this->client->setClient(
- $this->mock_http_client(
- $this->mockCreateSubscriptionResponse()
- )
- );
- $response = $this->client->setupSubscription('John Doe', 'john@example.com', $start_date);
- $this->assertNotEmpty($response);
- $this->assertArrayHasKey('id', $response);
- $this->assertArrayHasKey('plan_id', $response);
- }
- #[Test]
- public function it_skips_product_and_billing_plan_creation_if_already_set_when_creating_an_annual_subscription(): void
- {
- $this->client->setAccessToken([
- 'access_token' => self::$access_token,
- 'token_type' => 'Bearer',
- ]);
- $start_date = Carbon::now()->addDay()->toDateString();
- $this->client = $this->client->addProductById('PROD-XYAB12ABSB7868434')
- ->addBillingPlanById('P-5ML4271244454362WXNWU5NQ')
- ->addProduct('Demo Product', 'Demo Product', 'SERVICE', 'SOFTWARE')
- ->addAnnualPlan('Demo Plan', 'Demo Plan', 100);
- $this->client->setClient(
- $this->mock_http_client(
- $this->mockCreateSubscriptionResponse()
- )
- );
- $response = $this->client->setupSubscription('John Doe', 'john@example.com', $start_date);
- $this->assertNotEmpty($response);
- $this->assertArrayHasKey('id', $response);
- $this->assertArrayHasKey('plan_id', $response);
- }
- #[Test]
- public function it_skips_product_and_billing_plan_creation_if_already_set_when_creating_a_subscription_with_custom_intervals(): void
- {
- $this->client->setAccessToken([
- 'access_token' => self::$access_token,
- 'token_type' => 'Bearer',
- ]);
- $start_date = Carbon::now()->addDay()->toDateString();
- $this->client = $this->client->addProductById('PROD-XYAB12ABSB7868434')
- ->addBillingPlanById('P-5ML4271244454362WXNWU5NQ')
- ->addProduct('Demo Product', 'Demo Product', 'SERVICE', 'SOFTWARE')
- ->addCustomPlan('Demo Plan', 'Demo Plan', 100, 'MONTH', 3);
- $this->client->setClient(
- $this->mock_http_client(
- $this->mockCreateSubscriptionResponse()
- )
- );
- $response = $this->client->setupSubscription('John Doe', 'john@example.com', $start_date);
- $this->assertNotEmpty($response);
- $this->assertArrayHasKey('id', $response);
- $this->assertArrayHasKey('plan_id', $response);
- }
- #[Test]
- public function it_can_add_setup_fees_when_creating_subscription(): void
- {
- $this->client->setAccessToken([
- 'access_token' => self::$access_token,
- 'token_type' => 'Bearer',
- ]);
- $start_date = Carbon::now()->addDay()->toDateString();
- $setup_fee = 9.99;
- $this->client = $this->client->addSetupFee($setup_fee)
- ->addProductById('PROD-XYAB12ABSB7868434')
- ->addBillingPlanById('P-5ML4271244454362WXNWU5NQ');
- $this->client->setClient(
- $this->mock_http_client(
- $this->mockCreateSubscriptionResponse()
- )
- );
- $response = $this->client->setupSubscription('John Doe', 'john@example.com', $start_date);
- $this->assertNotEmpty($response);
- $this->assertArrayHasKey('id', $response);
- $this->assertArrayHasKey('plan_id', $response);
- }
- #[Test]
- public function it_can_add_shipping_address_when_creating_subscription(): void
- {
- $this->client->setAccessToken([
- 'access_token' => self::$access_token,
- 'token_type' => 'Bearer',
- ]);
- $start_date = Carbon::now()->addDay()->toDateString();
- $this->client = $this->client->addShippingAddress('John Doe', 'House no. 123', 'Street 456', 'Test Area', 'Test Area', 10001, 'US')
- ->addProductById('PROD-XYAB12ABSB7868434')
- ->addBillingPlanById('P-5ML4271244454362WXNWU5NQ');
- $this->client->setClient(
- $this->mock_http_client(
- $this->mockCreateSubscriptionResponse()
- )
- );
- $response = $this->client->setupSubscription('John Doe', 'john@example.com', $start_date);
- $this->assertNotEmpty($response);
- $this->assertArrayHasKey('id', $response);
- $this->assertArrayHasKey('plan_id', $response);
- }
- #[Test]
- public function it_can_add_custom_payment_failure_threshold_value_when_creating_subscription(): void
- {
- $this->client->setAccessToken([
- 'access_token' => self::$access_token,
- 'token_type' => 'Bearer',
- ]);
- $start_date = Carbon::now()->addDay()->toDateString();
- $threshold = 5;
- $this->client = $this->client->addPaymentFailureThreshold($threshold)
- ->addProductById('PROD-XYAB12ABSB7868434')
- ->addBillingPlanById('P-5ML4271244454362WXNWU5NQ');
- $this->client->setClient(
- $this->mock_http_client(
- $this->mockCreateSubscriptionResponse()
- )
- );
- $response = $this->client->setupSubscription('John Doe', 'john@example.com', $start_date);
- $this->assertNotEmpty($response);
- $this->assertArrayHasKey('id', $response);
- $this->assertArrayHasKey('plan_id', $response);
- }
- #[Test]
- public function it_can_set_tax_percentage_when_creating_subscription(): void
- {
- $this->client->setAccessToken([
- 'access_token' => self::$access_token,
- 'token_type' => 'Bearer',
- ]);
- $start_date = Carbon::now()->addDay()->toDateString();
- $percentage = 10;
- $this->client = $this->client->addTaxes($percentage)
- ->addProductById('PROD-XYAB12ABSB7868434')
- ->addBillingPlanById('P-5ML4271244454362WXNWU5NQ');
- $this->client->setClient(
- $this->mock_http_client(
- $this->mockCreateSubscriptionResponse()
- )
- );
- $response = $this->client->setupSubscription('John Doe', 'john@example.com', $start_date);
- $this->assertNotEmpty($response);
- $this->assertArrayHasKey('id', $response);
- $this->assertArrayHasKey('plan_id', $response);
- }
- #[Test]
- public function it_can_create_a_subscription_with_fixed_installments(): void
- {
- $this->client->setAccessToken([
- 'access_token' => self::$access_token,
- 'token_type' => 'Bearer',
- ]);
- $this->client->setClient(
- $this->mock_http_client(
- $this->mockCreateCatalogProductsResponse()
- )
- );
- $start_date = Carbon::now()->addDay()->toDateString();
- $this->client = $this->client->addProduct('Demo Product', 'Demo Product', 'SERVICE', 'SOFTWARE');
- $this->client->setClient(
- $this->mock_http_client(
- $this->mockCreatePlansResponse()
- )
- );
- $this->client = $this->client->addPlanTrialPricing('DAY', 7)
- ->addMonthlyPlan('Demo Plan', 'Demo Plan', 100, 12);
- $this->client->setClient(
- $this->mock_http_client(
- $this->mockCreateSubscriptionResponse()
- )
- );
- $response = $this->client->setReturnAndCancelUrl('https://example.com/paypal-success', 'https://example.com/paypal-cancel')
- ->setupSubscription('John Doe', 'john@example.com', $start_date);
- $this->assertNotEmpty($response);
- $this->assertArrayHasKey('id', $response);
- $this->assertArrayHasKey('plan_id', $response);
- }
- }
|