Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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