2727
2828namespace UnzerSDK \test \integration \PaymentTypes ;
2929
30+ use UnzerSDK \Constants \ApiResponseCodes ;
31+ use UnzerSDK \Exceptions \UnzerApiException ;
3032use UnzerSDK \Resources \PaymentTypes \Applepay ;
3133use UnzerSDK \test \BaseIntegrationTest ;
3234
@@ -36,6 +38,8 @@ class ApplepayTest extends BaseIntegrationTest
3638 * Verify applepay can be created and fetched.
3739 *
3840 * @test
41+ *
42+ * @throws UnzerApiException
3943 */
4044 public function applepayShouldBeCreatableAndFetchable (): void
4145 {
@@ -56,6 +60,8 @@ public function applepayShouldBeCreatableAndFetchable(): void
5660 * Verify that applepay is chargeable
5761 *
5862 * @test
63+ *
64+ * @throws UnzerApiException
5965 */
6066 public function applepayShouldBeChargeable (): void
6167 {
@@ -70,21 +76,71 @@ public function applepayShouldBeChargeable(): void
7076 * Verify that applepay is chargeable
7177 *
7278 * @test
79+ *
80+ * @throws UnzerApiException
7381 */
7482 public function applepayCanBeAuthorized (): void
7583 {
7684 $ applepay = $ this ->createApplepayObject ();
7785 $ this ->unzer ->createPaymentType ($ applepay );
78- $ charge = $ applepay ->authorize (100 .0 , 'EUR ' , self ::RETURN_URL );
86+ $ authorization = $ applepay ->authorize (1 .0 , 'EUR ' , self ::RETURN_URL );
7987
88+ // verify authorization has been created
89+ $ this ->assertNotNull ($ authorization ->getId ());
90+ $ this ->assertNull ($ authorization ->getRedirectUrl ());
91+
92+ // verify payment object has been created
93+ $ payment = $ authorization ->getPayment ();
94+ $ this ->assertNotNull ($ payment );
95+ $ this ->assertNotNull ($ payment ->getId ());
96+
97+ // verify resources are linked properly
98+ $ this ->assertSame ($ authorization , $ payment ->getAuthorization ());
99+ $ this ->assertSame ($ applepay , $ payment ->getPaymentType ());
100+
101+ // verify the payment object has been updated properly
102+ $ this ->assertAmounts ($ payment , 1.0 , 0.0 , 1.0 , 0.0 );
103+ $ this ->assertTrue ($ payment ->isPending ());
104+ }
105+
106+ /**
107+ * Verify the applepay can perform charges and creates a payment object doing so.
108+ *
109+ * @test
110+ *
111+ * @throws UnzerApiException
112+ */
113+ public function applepayCanPerformChargeAndCreatesPaymentObject (): void
114+ {
115+ $ applepay = $ this ->createApplepayObject ();
116+ /** @var Applepay $applepay */
117+ $ applepay = $ this ->unzer ->createPaymentType ($ applepay );
118+
119+ $ charge = $ applepay ->charge (1.0 , 'EUR ' , self ::RETURN_URL , null , null , null , null , false );
120+
121+ // verify charge has been created
80122 $ this ->assertNotNull ($ charge ->getId ());
81- $ this ->assertNull ($ charge ->getRedirectUrl ());
123+
124+ // verify payment object has been created
125+ $ payment = $ charge ->getPayment ();
126+ $ this ->assertNotNull ($ payment );
127+ $ this ->assertNotNull ($ payment ->getId ());
128+
129+ // verify resources are linked properly
130+ $ this ->assertEquals ($ charge ->expose (), $ payment ->getCharge ($ charge ->getId ())->expose ());
131+ $ this ->assertSame ($ applepay , $ payment ->getPaymentType ());
132+
133+ // verify the payment object has been updated properly
134+ $ this ->assertAmounts ($ payment , 0.0 , 1.0 , 1.0 , 0.0 );
135+ $ this ->assertTrue ($ payment ->isCompleted ());
82136 }
83137
84138 /**
85139 * Verify the applepay can charge the full amount of the authorization and the payment state is updated accordingly.
86140 *
87141 * @test
142+ *
143+ * @throws UnzerApiException
88144 */
89145 public function fullChargeAfterAuthorize (): void
90146 {
@@ -106,10 +162,83 @@ public function fullChargeAfterAuthorize(): void
106162 $ this ->assertTrue ($ paymentNew ->isCompleted ());
107163 }
108164
165+ /**
166+ * Verify the applepay can charge part of the authorized amount and the payment state is updated accordingly.
167+ *
168+ * @test
169+ *
170+ * @throws UnzerApiException
171+ */
172+ public function partialChargeAfterAuthorization (): void
173+ {
174+ $ applepay = $ this ->createApplepayObject ();
175+ /** @var Applepay $applepay */
176+ $ applepay = $ this ->unzer ->createPaymentType ($ applepay );
177+ $ authorization = $ this ->unzer ->authorize (
178+ 100.0 ,
179+ 'EUR ' ,
180+ $ applepay ,
181+ self ::RETURN_URL ,
182+ null ,
183+ null ,
184+ null ,
185+ null ,
186+ false
187+ );
188+
189+ $ payment = $ authorization ->getPayment ();
190+ $ this ->assertAmounts ($ payment , 100.0 , 0.0 , 100.0 , 0.0 );
191+ $ this ->assertTrue ($ payment ->isPending ());
192+
193+ $ charge = $ this ->unzer ->chargeAuthorization ($ payment ->getId (), 20 );
194+ $ payment1 = $ charge ->getPayment ();
195+ $ this ->assertAmounts ($ payment1 , 80.0 , 20.0 , 100.0 , 0.0 );
196+ $ this ->assertTrue ($ payment1 ->isPartlyPaid ());
197+
198+ $ charge = $ this ->unzer ->chargeAuthorization ($ payment ->getId (), 20 );
199+ $ payment2 = $ charge ->getPayment ();
200+ $ this ->assertAmounts ($ payment2 , 60.0 , 40.0 , 100.0 , 0.0 );
201+ $ this ->assertTrue ($ payment2 ->isPartlyPaid ());
202+
203+ $ charge = $ this ->unzer ->chargeAuthorization ($ payment ->getId (), 60 );
204+ $ payment3 = $ charge ->getPayment ();
205+ $ this ->assertAmounts ($ payment3 , 00.0 , 100.0 , 100.0 , 0.0 );
206+ $ this ->assertTrue ($ payment3 ->isCompleted ());
207+ }
208+
209+ /**
210+ * Verify that an exception is thrown when trying to charge more than authorized.
211+ *
212+ * @test
213+ *
214+ * @throws UnzerApiException
215+ */
216+ public function exceptionShouldBeThrownWhenChargingMoreThenAuthorized (): void
217+ {
218+ $ applepay = $ this ->createApplepayObject ();
219+ /** @var Applepay $applepay */
220+ $ applepay = $ this ->unzer ->createPaymentType ($ applepay );
221+ $ authorization = $ applepay ->authorize (100.0000 , 'EUR ' , self ::RETURN_URL , null , null , null , null , false );
222+ $ payment = $ authorization ->getPayment ();
223+ $ this ->assertAmounts ($ payment , 100.0 , 0.0 , 100.0 , 0.0 );
224+ $ this ->assertTrue ($ payment ->isPending ());
225+
226+ $ charge = $ this ->unzer ->chargeAuthorization ($ payment ->getId (), 50 );
227+ $ payment1 = $ charge ->getPayment ();
228+ $ this ->assertAmounts ($ payment1 , 50.0 , 50.0 , 100.0 , 0.0 );
229+ $ this ->assertTrue ($ payment1 ->isPartlyPaid ());
230+
231+ $ this ->expectException (UnzerApiException::class);
232+ $ this ->expectExceptionCode (ApiResponseCodes::API_ERROR_CHARGED_AMOUNT_HIGHER_THAN_EXPECTED );
233+ $ this ->unzer ->chargeAuthorization ($ payment ->getId (), 70 );
234+ }
235+
109236 /**
110237 * Verify applepay authorize can be canceled.
111238 *
112239 * @test
240+ *
241+ * @throws UnzerApiException
113242 */
114243 public function applepayAuthorizeCanBeCanceled (): void
115244 {
@@ -123,7 +252,57 @@ public function applepayAuthorizeCanBeCanceled(): void
123252 }
124253
125254 /**
255+ * Verify the applepay payment can be charged until it is fully charged and the payment is updated accordingly.
256+ *
126257 * @test
258+ *
259+ * @throws UnzerApiException
260+ */
261+ public function partialAndFullChargeAfterAuthorization (): void
262+ {
263+ $ applepay = $ this ->createApplepayObject ();
264+ /** @var Applepay $applepay */
265+ $ applepay = $ this ->unzer ->createPaymentType ($ applepay );
266+ $ authorization = $ applepay ->authorize (100.0000 , 'EUR ' , self ::RETURN_URL , null , null , null , null , false );
267+ $ payment = $ authorization ->getPayment ();
268+
269+ $ this ->assertAmounts ($ payment , 100.0 , 0.0 , 100.0 , 0.0 );
270+ $ this ->assertTrue ($ payment ->isPending ());
271+
272+ $ charge = $ this ->unzer ->chargeAuthorization ($ payment ->getId (), 20 );
273+ $ payment1 = $ charge ->getPayment ();
274+ $ this ->assertAmounts ($ payment1 , 80.0 , 20.0 , 100.0 , 0.0 );
275+ $ this ->assertTrue ($ payment1 ->isPartlyPaid ());
276+
277+ $ charge = $ this ->unzer ->chargeAuthorization ($ payment ->getId ());
278+ $ payment2 = $ charge ->getPayment ();
279+ $ this ->assertAmounts ($ payment2 , 0.0 , 100.0 , 100.0 , 0.0 );
280+ $ this ->assertTrue ($ payment2 ->isCompleted ());
281+ }
282+
283+ /**
284+ * Authorization can be fetched.
285+ *
286+ * @test
287+ *
288+ * @throws UnzerApiException
289+ */
290+ public function authorizationShouldBeFetchable (): void
291+ {
292+ $ applepay = $ this ->createApplepayObject ();
293+ /** @var Applepay $applepay */
294+ $ applepay = $ this ->unzer ->createPaymentType ($ applepay );
295+ $ authorization = $ applepay ->authorize (100.0000 , 'EUR ' , self ::RETURN_URL );
296+ $ payment = $ authorization ->getPayment ();
297+
298+ $ fetchedAuthorization = $ this ->unzer ->fetchAuthorization ($ payment ->getId ());
299+ $ this ->assertEquals ($ fetchedAuthorization ->getId (), $ authorization ->getId ());
300+ }
301+
302+ /**
303+ * @test
304+ *
305+ * @throws UnzerApiException
127306 */
128307 public function fullCancelAfterCharge (): void
129308 {
@@ -140,6 +319,88 @@ public function fullCancelAfterCharge(): void
140319 $ this ->assertTrue ($ payment ->isCanceled ());
141320 }
142321
322+ /**
323+ * Verify a applepay payment can be cancelled after being fully charged.
324+ *
325+ * @test
326+ *
327+ * @throws UnzerApiException
328+ */
329+ public function fullCancelOnFullyChargedPayment (): void
330+ {
331+ $ applepay = $ this ->createApplepayObject ();
332+ /** @var Applepay $applepay */
333+ $ applepay = $ this ->unzer ->createPaymentType ($ applepay );
334+
335+ $ authorization = $ applepay ->authorize (100.0000 , 'EUR ' , self ::RETURN_URL , null , null , null , null , false );
336+ $ payment = $ authorization ->getPayment ();
337+
338+ $ this ->assertAmounts ($ payment , 100.0 , 0.0 , 100.0 , 0.0 );
339+ $ this ->assertTrue ($ payment ->isPending ());
340+
341+ $ payment ->charge (10.0 );
342+ $ this ->assertAmounts ($ payment , 90.0 , 10.0 , 100.0 , 0.0 );
343+ $ this ->assertTrue ($ payment ->isPartlyPaid ());
344+
345+ $ payment ->charge (90.0 );
346+ $ this ->assertAmounts ($ payment , 0.0 , 100.0 , 100.0 , 0.0 );
347+ $ this ->assertTrue ($ payment ->isCompleted ());
348+
349+ $ cancellation = $ payment ->cancelAmount ();
350+ $ this ->assertNotEmpty ($ cancellation );
351+ $ this ->assertAmounts ($ payment , 0.0 , 0.0 , 100.0 , 100.0 );
352+ $ this ->assertTrue ($ payment ->isCanceled ());
353+ }
354+
355+ /**
356+ * Full cancel on partly charged auth canceled charges.
357+ *
358+ * @test
359+ *
360+ * @throws UnzerApiException
361+ */
362+ public function fullCancelOnPartlyPaidAuthWithCanceledCharges (): void
363+ {
364+ $ applepay = $ this ->createApplepayObject ();
365+ /** @var Applepay $applepay */
366+ $ applepay = $ this ->unzer ->createPaymentType ($ applepay );
367+
368+ $ authorization = $ applepay ->authorize (100.0000 , 'EUR ' , self ::RETURN_URL , null , null , null , null , false );
369+ $ payment = $ authorization ->getPayment ();
370+
371+ $ payment ->charge (10.0 );
372+ $ this ->assertAmounts ($ payment , 90.0 , 10.0 , 100.0 , 0.0 );
373+
374+ $ charge = $ payment ->charge (10.0 );
375+ $ this ->assertAmounts ($ payment , 80.0 , 20.0 , 100.0 , 0.0 );
376+ $ this ->assertTrue ($ payment ->isPartlyPaid ());
377+
378+ $ charge ->cancel ();
379+ $ this ->assertAmounts ($ payment , 80.0 , 10.0 , 100.0 , 10.0 );
380+ $ this ->assertTrue ($ payment ->isPartlyPaid ());
381+
382+ $ payment ->cancelAmount ();
383+ $ this ->assertTrue ($ payment ->isCanceled ());
384+ }
385+
386+ /**
387+ * Verify applepay charge can be canceled.
388+ *
389+ * @test
390+ *
391+ * @throws UnzerApiException
392+ */
393+ public function applepayChargeCanBeCanceled (): void
394+ {
395+ /** @var Applepay $applepay */
396+ $ applepay = $ this ->unzer ->createPaymentType ($ this ->createApplepayObject ());
397+ $ charge = $ applepay ->charge (100.0 , 'EUR ' , self ::RETURN_URL , null , null , null , null , false );
398+
399+ $ cancel = $ charge ->cancel ();
400+ $ this ->assertNotNull ($ cancel );
401+ $ this ->assertNotEmpty ($ cancel ->getId ());
402+ }
403+
143404 /**
144405 * @return Applepay
145406 */
0 commit comments