AdapterCreateSubscriptionHelpersTest.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. <?php
  2. namespace Srmklive\PayPal\Tests\Feature;
  3. use Carbon\Carbon;
  4. use PHPUnit\Framework\Attributes\Test;
  5. use PHPUnit\Framework\TestCase;
  6. use Srmklive\PayPal\Services\PayPal as PayPalClient;
  7. use Srmklive\PayPal\Tests\MockClientClasses;
  8. use Srmklive\PayPal\Tests\MockResponsePayloads;
  9. class AdapterCreateSubscriptionHelpersTest extends TestCase
  10. {
  11. use MockClientClasses;
  12. use MockResponsePayloads;
  13. /** @var string */
  14. protected static string $access_token = '';
  15. /** @var PayPalClient */
  16. protected PayPalClient $client;
  17. protected function setUp(): void
  18. {
  19. $this->client = new PayPalClient($this->getApiCredentials());
  20. $this->client->setClient(
  21. $this->mock_http_client(
  22. $this->mockAccessTokenResponse()
  23. )
  24. );
  25. $response = $this->client->getAccessToken();
  26. self::$access_token = $response['access_token'];
  27. parent::setUp();
  28. }
  29. #[Test]
  30. public function it_can_create_a_monthly_subscription(): void
  31. {
  32. $this->client->setAccessToken([
  33. 'access_token' => self::$access_token,
  34. 'token_type' => 'Bearer',
  35. ]);
  36. $this->client->setClient(
  37. $this->mock_http_client(
  38. $this->mockCreateCatalogProductsResponse()
  39. )
  40. );
  41. $start_date = Carbon::now()->addDay()->toDateString();
  42. $this->client = $this->client->addProduct('Demo Product', 'Demo Product', 'SERVICE', 'SOFTWARE');
  43. $this->client->setClient(
  44. $this->mock_http_client(
  45. $this->mockCreatePlansResponse()
  46. )
  47. );
  48. $this->client = $this->client->addPlanTrialPricing('DAY', 7)
  49. ->addMonthlyPlan('Demo Plan', 'Demo Plan', 100);
  50. $this->client->setClient(
  51. $this->mock_http_client(
  52. $this->mockCreateSubscriptionResponse()
  53. )
  54. );
  55. $response = $this->client->setReturnAndCancelUrl('https://example.com/paypal-success', 'https://example.com/paypal-cancel')
  56. ->setupSubscription('John Doe', 'john@example.com', $start_date);
  57. $this->assertNotEmpty($response);
  58. $this->assertArrayHasKey('id', $response);
  59. $this->assertArrayHasKey('plan_id', $response);
  60. }
  61. #[Test]
  62. public function it_can_create_a_daily_subscription(): void
  63. {
  64. $this->client->setAccessToken([
  65. 'access_token' => self::$access_token,
  66. 'token_type' => 'Bearer',
  67. ]);
  68. $this->client->setClient(
  69. $this->mock_http_client(
  70. $this->mockCreateCatalogProductsResponse()
  71. )
  72. );
  73. $start_date = Carbon::now()->addDay()->toDateString();
  74. $this->client = $this->client->addProduct('Demo Product', 'Demo Product', 'SERVICE', 'SOFTWARE');
  75. $this->client->setClient(
  76. $this->mock_http_client(
  77. $this->mockCreatePlansResponse()
  78. )
  79. );
  80. $this->client = $this->client->addPlanTrialPricing('DAY', 7)
  81. ->addDailyPlan('Demo Plan', 'Demo Plan', 1.50);
  82. $this->client->setClient(
  83. $this->mock_http_client(
  84. $this->mockCreateSubscriptionResponse()
  85. )
  86. );
  87. $response = $this->client->setReturnAndCancelUrl('https://example.com/paypal-success', 'https://example.com/paypal-cancel')
  88. ->setupSubscription('John Doe', 'john@example.com', $start_date);
  89. $this->assertNotEmpty($response);
  90. $this->assertArrayHasKey('id', $response);
  91. $this->assertArrayHasKey('plan_id', $response);
  92. }
  93. #[Test]
  94. public function it_can_create_a_weekly_subscription(): void
  95. {
  96. $this->client->setAccessToken([
  97. 'access_token' => self::$access_token,
  98. 'token_type' => 'Bearer',
  99. ]);
  100. $this->client->setClient(
  101. $this->mock_http_client(
  102. $this->mockCreateCatalogProductsResponse()
  103. )
  104. );
  105. $start_date = Carbon::now()->addDay()->toDateString();
  106. $this->client = $this->client->addProduct('Demo Product', 'Demo Product', 'SERVICE', 'SOFTWARE');
  107. $this->client->setClient(
  108. $this->mock_http_client(
  109. $this->mockCreatePlansResponse()
  110. )
  111. );
  112. $this->client = $this->client->addPlanTrialPricing('DAY', 7)
  113. ->addWeeklyPlan('Demo Plan', 'Demo Plan', 50);
  114. $this->client->setClient(
  115. $this->mock_http_client(
  116. $this->mockCreateSubscriptionResponse()
  117. )
  118. );
  119. $response = $this->client->setReturnAndCancelUrl('https://example.com/paypal-success', 'https://example.com/paypal-cancel')
  120. ->setupSubscription('John Doe', 'john@example.com', $start_date);
  121. $this->assertNotEmpty($response);
  122. $this->assertArrayHasKey('id', $response);
  123. $this->assertArrayHasKey('plan_id', $response);
  124. }
  125. #[Test]
  126. public function it_can_create_an_annual_subscription(): void
  127. {
  128. $this->client->setAccessToken([
  129. 'access_token' => self::$access_token,
  130. 'token_type' => 'Bearer',
  131. ]);
  132. $this->client->setClient(
  133. $this->mock_http_client(
  134. $this->mockCreateCatalogProductsResponse()
  135. )
  136. );
  137. $start_date = Carbon::now()->addDay()->toDateString();
  138. $this->client = $this->client->addProduct('Demo Product', 'Demo Product', 'SERVICE', 'SOFTWARE');
  139. $this->client->setClient(
  140. $this->mock_http_client(
  141. $this->mockCreatePlansResponse()
  142. )
  143. );
  144. $this->client = $this->client->addPlanTrialPricing('DAY', 7)
  145. ->addAnnualPlan('Demo Plan', 'Demo Plan', 100);
  146. $this->client->setClient(
  147. $this->mock_http_client(
  148. $this->mockCreateSubscriptionResponse()
  149. )
  150. );
  151. $response = $this->client->setReturnAndCancelUrl('https://example.com/paypal-success', 'https://example.com/paypal-cancel')
  152. ->setupSubscription('John Doe', 'john@example.com', $start_date);
  153. $this->assertNotEmpty($response);
  154. $this->assertArrayHasKey('id', $response);
  155. $this->assertArrayHasKey('plan_id', $response);
  156. }
  157. #[Test]
  158. public function it_can_create_a_subscription_with_custom_defined_interval(): void
  159. {
  160. $this->client->setAccessToken([
  161. 'access_token' => self::$access_token,
  162. 'token_type' => 'Bearer',
  163. ]);
  164. $this->client->setClient(
  165. $this->mock_http_client(
  166. $this->mockCreateCatalogProductsResponse()
  167. )
  168. );
  169. $start_date = Carbon::now()->addDay()->toDateString();
  170. $this->client = $this->client->addProduct('Demo Product', 'Demo Product', 'SERVICE', 'SOFTWARE');
  171. $this->client->setClient(
  172. $this->mock_http_client(
  173. $this->mockCreatePlansResponse()
  174. )
  175. );
  176. $this->client = $this->client->addPlanTrialPricing('DAY', 7)
  177. ->addCustomPlan('Demo Plan', 'Demo Plan', 100, 'MONTH', 3);
  178. $this->client->setClient(
  179. $this->mock_http_client(
  180. $this->mockCreateSubscriptionResponse()
  181. )
  182. );
  183. $response = $this->client->setReturnAndCancelUrl('https://example.com/paypal-success', 'https://example.com/paypal-cancel')
  184. ->setupSubscription('John Doe', 'john@example.com', $start_date);
  185. $this->assertNotEmpty($response);
  186. $this->assertArrayHasKey('id', $response);
  187. $this->assertArrayHasKey('plan_id', $response);
  188. }
  189. #[Test]
  190. public function it_throws_exception_when_invalid_interval_is_provided_for_creating_a_subscription(): void
  191. {
  192. $this->client->setAccessToken([
  193. 'access_token' => self::$access_token,
  194. 'token_type' => 'Bearer',
  195. ]);
  196. $this->client = $this->client->addProductById('PROD-XYAB12ABSB7868434');
  197. $this->expectException(\RuntimeException::class);
  198. $this->client = $this->client->addCustomPlan('Demo Plan', 'Demo Plan', 100, 'MONTHLY', 3);
  199. }
  200. #[Test]
  201. public function it_throws_exception_when_get_error_for_creating_a_billing_plan(): void
  202. {
  203. $this->client->setAccessToken([
  204. 'access_token' => self::$access_token,
  205. 'token_type' => 'Bearer',
  206. ]);
  207. $this->client->setClient(
  208. $this->mock_http_client(
  209. $this->mockCreateCatalogProductsResponse()
  210. )
  211. );
  212. $this->client = $this->client->addProduct('Demo Product', 'Demo Product', 'SERVICE', 'SOFTWARE');
  213. $this->client->setClient(
  214. $this->mock_http_client(
  215. $this->mockCreatePlansErrorResponse()
  216. )
  217. );
  218. $this->expectException(\RuntimeException::class);
  219. $this->client = $this->client->addMonthlyPlan('Demo Plan', 'Demo Plan', 100);
  220. }
  221. #[Test]
  222. public function it_throws_exception_when_get_error_for_creating_a_product(): void
  223. {
  224. $this->client->setAccessToken([
  225. 'access_token' => self::$access_token,
  226. 'token_type' => 'Bearer',
  227. ]);
  228. $this->client->setClient(
  229. $this->mock_http_client(
  230. $this->mockGetCatalogProductsErrorResponse()
  231. )
  232. );
  233. $this->expectException(\RuntimeException::class);
  234. $this->client = $this->client->addProduct('Demo Product', 'Demo Product', 'SERVICE', 'SOFTWARE');
  235. }
  236. #[Test]
  237. public function it_can_create_a_subscription_without_trial(): void
  238. {
  239. $this->client->setAccessToken([
  240. 'access_token' => self::$access_token,
  241. 'token_type' => 'Bearer',
  242. ]);
  243. $this->client->setClient(
  244. $this->mock_http_client(
  245. $this->mockCreateCatalogProductsResponse()
  246. )
  247. );
  248. $start_date = Carbon::now()->addDay()->toDateString();
  249. $this->client = $this->client->addProduct('Demo Product', 'Demo Product', 'SERVICE', 'SOFTWARE');
  250. $this->client->setClient(
  251. $this->mock_http_client(
  252. $this->mockCreatePlansResponse()
  253. )
  254. );
  255. $this->client = $this->client->addMonthlyPlan('Demo Plan', 'Demo Plan', 100);
  256. $this->client->setClient(
  257. $this->mock_http_client(
  258. $this->mockCreateSubscriptionResponse()
  259. )
  260. );
  261. $response = $this->client->setupSubscription('John Doe', 'john@example.com', $start_date);
  262. $this->assertNotEmpty($response);
  263. $this->assertArrayHasKey('id', $response);
  264. $this->assertArrayHasKey('plan_id', $response);
  265. }
  266. #[Test]
  267. public function it_can_create_a_subscription_by_existing_product_and_billing_plan(): void
  268. {
  269. $this->client->setAccessToken([
  270. 'access_token' => self::$access_token,
  271. 'token_type' => 'Bearer',
  272. ]);
  273. $start_date = Carbon::now()->addDay()->toDateString();
  274. $this->client->setClient(
  275. $this->mock_http_client(
  276. $this->mockCreateSubscriptionResponse()
  277. )
  278. );
  279. $response = $this->client->addProductById('PROD-XYAB12ABSB7868434')
  280. ->addBillingPlanById('P-5ML4271244454362WXNWU5NQ')
  281. ->setupSubscription('John Doe', 'john@example.com', $start_date);
  282. $this->assertNotEmpty($response);
  283. $this->assertArrayHasKey('id', $response);
  284. $this->assertArrayHasKey('plan_id', $response);
  285. }
  286. #[Test]
  287. public function it_skips_product_and_billing_plan_creation_if_already_set_when_creating_a_daily_subscription(): void
  288. {
  289. $this->client->setAccessToken([
  290. 'access_token' => self::$access_token,
  291. 'token_type' => 'Bearer',
  292. ]);
  293. $start_date = Carbon::now()->addDay()->toDateString();
  294. $this->client = $this->client->addProductById('PROD-XYAB12ABSB7868434')
  295. ->addBillingPlanById('P-5ML4271244454362WXNWU5NQ')
  296. ->addProduct('Demo Product', 'Demo Product', 'SERVICE', 'SOFTWARE')
  297. ->addDailyPlan('Demo Plan', 'Demo Plan', 1.50);
  298. $this->client->setClient(
  299. $this->mock_http_client(
  300. $this->mockCreateSubscriptionResponse()
  301. )
  302. );
  303. $response = $this->client->setupSubscription('John Doe', 'john@example.com', $start_date);
  304. $this->assertNotEmpty($response);
  305. $this->assertArrayHasKey('id', $response);
  306. $this->assertArrayHasKey('plan_id', $response);
  307. }
  308. #[Test]
  309. public function it_skips_product_and_billing_plan_creation_if_already_set_when_creating_a_weekly_subscription(): void
  310. {
  311. $this->client->setAccessToken([
  312. 'access_token' => self::$access_token,
  313. 'token_type' => 'Bearer',
  314. ]);
  315. $start_date = Carbon::now()->addDay()->toDateString();
  316. $this->client = $this->client->addProductById('PROD-XYAB12ABSB7868434')
  317. ->addBillingPlanById('P-5ML4271244454362WXNWU5NQ')
  318. ->addProduct('Demo Product', 'Demo Product', 'SERVICE', 'SOFTWARE')
  319. ->addWeeklyPlan('Demo Plan', 'Demo Plan', 100);
  320. $this->client->setClient(
  321. $this->mock_http_client(
  322. $this->mockCreateSubscriptionResponse()
  323. )
  324. );
  325. $response = $this->client->setupSubscription('John Doe', 'john@example.com', $start_date);
  326. $this->assertNotEmpty($response);
  327. $this->assertArrayHasKey('id', $response);
  328. $this->assertArrayHasKey('plan_id', $response);
  329. }
  330. #[Test]
  331. public function it_skips_product_and_billing_plan_creation_if_already_set_when_creating_a_monthly_subscription(): void
  332. {
  333. $this->client->setAccessToken([
  334. 'access_token' => self::$access_token,
  335. 'token_type' => 'Bearer',
  336. ]);
  337. $start_date = Carbon::now()->addDay()->toDateString();
  338. $this->client = $this->client->addProductById('PROD-XYAB12ABSB7868434')
  339. ->addBillingPlanById('P-5ML4271244454362WXNWU5NQ')
  340. ->addProduct('Demo Product', 'Demo Product', 'SERVICE', 'SOFTWARE')
  341. ->addMonthlyPlan('Demo Plan', 'Demo Plan', 100);
  342. $this->client->setClient(
  343. $this->mock_http_client(
  344. $this->mockCreateSubscriptionResponse()
  345. )
  346. );
  347. $response = $this->client->setupSubscription('John Doe', 'john@example.com', $start_date);
  348. $this->assertNotEmpty($response);
  349. $this->assertArrayHasKey('id', $response);
  350. $this->assertArrayHasKey('plan_id', $response);
  351. }
  352. #[Test]
  353. public function it_skips_product_and_billing_plan_creation_if_already_set_when_creating_an_annual_subscription(): void
  354. {
  355. $this->client->setAccessToken([
  356. 'access_token' => self::$access_token,
  357. 'token_type' => 'Bearer',
  358. ]);
  359. $start_date = Carbon::now()->addDay()->toDateString();
  360. $this->client = $this->client->addProductById('PROD-XYAB12ABSB7868434')
  361. ->addBillingPlanById('P-5ML4271244454362WXNWU5NQ')
  362. ->addProduct('Demo Product', 'Demo Product', 'SERVICE', 'SOFTWARE')
  363. ->addAnnualPlan('Demo Plan', 'Demo Plan', 100);
  364. $this->client->setClient(
  365. $this->mock_http_client(
  366. $this->mockCreateSubscriptionResponse()
  367. )
  368. );
  369. $response = $this->client->setupSubscription('John Doe', 'john@example.com', $start_date);
  370. $this->assertNotEmpty($response);
  371. $this->assertArrayHasKey('id', $response);
  372. $this->assertArrayHasKey('plan_id', $response);
  373. }
  374. #[Test]
  375. public function it_skips_product_and_billing_plan_creation_if_already_set_when_creating_a_subscription_with_custom_intervals(): void
  376. {
  377. $this->client->setAccessToken([
  378. 'access_token' => self::$access_token,
  379. 'token_type' => 'Bearer',
  380. ]);
  381. $start_date = Carbon::now()->addDay()->toDateString();
  382. $this->client = $this->client->addProductById('PROD-XYAB12ABSB7868434')
  383. ->addBillingPlanById('P-5ML4271244454362WXNWU5NQ')
  384. ->addProduct('Demo Product', 'Demo Product', 'SERVICE', 'SOFTWARE')
  385. ->addCustomPlan('Demo Plan', 'Demo Plan', 100, 'MONTH', 3);
  386. $this->client->setClient(
  387. $this->mock_http_client(
  388. $this->mockCreateSubscriptionResponse()
  389. )
  390. );
  391. $response = $this->client->setupSubscription('John Doe', 'john@example.com', $start_date);
  392. $this->assertNotEmpty($response);
  393. $this->assertArrayHasKey('id', $response);
  394. $this->assertArrayHasKey('plan_id', $response);
  395. }
  396. #[Test]
  397. public function it_can_add_setup_fees_when_creating_subscription(): void
  398. {
  399. $this->client->setAccessToken([
  400. 'access_token' => self::$access_token,
  401. 'token_type' => 'Bearer',
  402. ]);
  403. $start_date = Carbon::now()->addDay()->toDateString();
  404. $setup_fee = 9.99;
  405. $this->client = $this->client->addSetupFee($setup_fee)
  406. ->addProductById('PROD-XYAB12ABSB7868434')
  407. ->addBillingPlanById('P-5ML4271244454362WXNWU5NQ');
  408. $this->client->setClient(
  409. $this->mock_http_client(
  410. $this->mockCreateSubscriptionResponse()
  411. )
  412. );
  413. $response = $this->client->setupSubscription('John Doe', 'john@example.com', $start_date);
  414. $this->assertNotEmpty($response);
  415. $this->assertArrayHasKey('id', $response);
  416. $this->assertArrayHasKey('plan_id', $response);
  417. }
  418. #[Test]
  419. public function it_can_add_shipping_address_when_creating_subscription(): void
  420. {
  421. $this->client->setAccessToken([
  422. 'access_token' => self::$access_token,
  423. 'token_type' => 'Bearer',
  424. ]);
  425. $start_date = Carbon::now()->addDay()->toDateString();
  426. $this->client = $this->client->addShippingAddress('John Doe', 'House no. 123', 'Street 456', 'Test Area', 'Test Area', 10001, 'US')
  427. ->addProductById('PROD-XYAB12ABSB7868434')
  428. ->addBillingPlanById('P-5ML4271244454362WXNWU5NQ');
  429. $this->client->setClient(
  430. $this->mock_http_client(
  431. $this->mockCreateSubscriptionResponse()
  432. )
  433. );
  434. $response = $this->client->setupSubscription('John Doe', 'john@example.com', $start_date);
  435. $this->assertNotEmpty($response);
  436. $this->assertArrayHasKey('id', $response);
  437. $this->assertArrayHasKey('plan_id', $response);
  438. }
  439. #[Test]
  440. public function it_can_add_custom_payment_failure_threshold_value_when_creating_subscription(): void
  441. {
  442. $this->client->setAccessToken([
  443. 'access_token' => self::$access_token,
  444. 'token_type' => 'Bearer',
  445. ]);
  446. $start_date = Carbon::now()->addDay()->toDateString();
  447. $threshold = 5;
  448. $this->client = $this->client->addPaymentFailureThreshold($threshold)
  449. ->addProductById('PROD-XYAB12ABSB7868434')
  450. ->addBillingPlanById('P-5ML4271244454362WXNWU5NQ');
  451. $this->client->setClient(
  452. $this->mock_http_client(
  453. $this->mockCreateSubscriptionResponse()
  454. )
  455. );
  456. $response = $this->client->setupSubscription('John Doe', 'john@example.com', $start_date);
  457. $this->assertNotEmpty($response);
  458. $this->assertArrayHasKey('id', $response);
  459. $this->assertArrayHasKey('plan_id', $response);
  460. }
  461. #[Test]
  462. public function it_can_set_tax_percentage_when_creating_subscription(): void
  463. {
  464. $this->client->setAccessToken([
  465. 'access_token' => self::$access_token,
  466. 'token_type' => 'Bearer',
  467. ]);
  468. $start_date = Carbon::now()->addDay()->toDateString();
  469. $percentage = 10;
  470. $this->client = $this->client->addTaxes($percentage)
  471. ->addProductById('PROD-XYAB12ABSB7868434')
  472. ->addBillingPlanById('P-5ML4271244454362WXNWU5NQ');
  473. $this->client->setClient(
  474. $this->mock_http_client(
  475. $this->mockCreateSubscriptionResponse()
  476. )
  477. );
  478. $response = $this->client->setupSubscription('John Doe', 'john@example.com', $start_date);
  479. $this->assertNotEmpty($response);
  480. $this->assertArrayHasKey('id', $response);
  481. $this->assertArrayHasKey('plan_id', $response);
  482. }
  483. #[Test]
  484. public function it_can_create_a_subscription_with_fixed_installments(): void
  485. {
  486. $this->client->setAccessToken([
  487. 'access_token' => self::$access_token,
  488. 'token_type' => 'Bearer',
  489. ]);
  490. $this->client->setClient(
  491. $this->mock_http_client(
  492. $this->mockCreateCatalogProductsResponse()
  493. )
  494. );
  495. $start_date = Carbon::now()->addDay()->toDateString();
  496. $this->client = $this->client->addProduct('Demo Product', 'Demo Product', 'SERVICE', 'SOFTWARE');
  497. $this->client->setClient(
  498. $this->mock_http_client(
  499. $this->mockCreatePlansResponse()
  500. )
  501. );
  502. $this->client = $this->client->addPlanTrialPricing('DAY', 7)
  503. ->addMonthlyPlan('Demo Plan', 'Demo Plan', 100, 12);
  504. $this->client->setClient(
  505. $this->mock_http_client(
  506. $this->mockCreateSubscriptionResponse()
  507. )
  508. );
  509. $response = $this->client->setReturnAndCancelUrl('https://example.com/paypal-success', 'https://example.com/paypal-cancel')
  510. ->setupSubscription('John Doe', 'john@example.com', $start_date);
  511. $this->assertNotEmpty($response);
  512. $this->assertArrayHasKey('id', $response);
  513. $this->assertArrayHasKey('plan_id', $response);
  514. }
  515. }