Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
265 changes: 263 additions & 2 deletions test/integration/PaymentTypes/ApplepayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

namespace UnzerSDK\test\integration\PaymentTypes;

use UnzerSDK\Constants\ApiResponseCodes;
use UnzerSDK\Exceptions\UnzerApiException;
use UnzerSDK\Resources\PaymentTypes\Applepay;
use UnzerSDK\test\BaseIntegrationTest;

Expand All @@ -36,6 +38,8 @@ class ApplepayTest extends BaseIntegrationTest
* Verify applepay can be created and fetched.
*
* @test
*
* @throws UnzerApiException
*/
public function applepayShouldBeCreatableAndFetchable(): void
{
Expand All @@ -56,6 +60,8 @@ public function applepayShouldBeCreatableAndFetchable(): void
* Verify that applepay is chargeable
*
* @test
*
* @throws UnzerApiException
*/
public function applepayShouldBeChargeable(): void
{
Expand All @@ -70,21 +76,71 @@ public function applepayShouldBeChargeable(): void
* Verify that applepay is chargeable
*
* @test
*
* @throws UnzerApiException
*/
public function applepayCanBeAuthorized(): void
{
$applepay = $this->createApplepayObject();
$this->unzer->createPaymentType($applepay);
$charge = $applepay->authorize(100.0, 'EUR', self::RETURN_URL);
$authorization = $applepay->authorize(1.0, 'EUR', self::RETURN_URL);

// verify authorization has been created
$this->assertNotNull($authorization->getId());
$this->assertNull($authorization->getRedirectUrl());

// verify payment object has been created
$payment = $authorization->getPayment();
$this->assertNotNull($payment);
$this->assertNotNull($payment->getId());

// verify resources are linked properly
$this->assertSame($authorization, $payment->getAuthorization());
$this->assertSame($applepay, $payment->getPaymentType());

// verify the payment object has been updated properly
$this->assertAmounts($payment, 1.0, 0.0, 1.0, 0.0);
$this->assertTrue($payment->isPending());
}

/**
* Verify the applepay can perform charges and creates a payment object doing so.
*
* @test
*
* @throws UnzerApiException
*/
public function applepayCanPerformChargeAndCreatesPaymentObject(): void
{
$applepay = $this->createApplepayObject();
/** @var Applepay $applepay */
$applepay = $this->unzer->createPaymentType($applepay);

$charge = $applepay->charge(1.0, 'EUR', self::RETURN_URL, null, null, null, null, false);

// verify charge has been created
$this->assertNotNull($charge->getId());
$this->assertNull($charge->getRedirectUrl());

// verify payment object has been created
$payment = $charge->getPayment();
$this->assertNotNull($payment);
$this->assertNotNull($payment->getId());

// verify resources are linked properly
$this->assertEquals($charge->expose(), $payment->getCharge($charge->getId())->expose());
$this->assertSame($applepay, $payment->getPaymentType());

// verify the payment object has been updated properly
$this->assertAmounts($payment, 0.0, 1.0, 1.0, 0.0);
$this->assertTrue($payment->isCompleted());
}

/**
* Verify the applepay can charge the full amount of the authorization and the payment state is updated accordingly.
*
* @test
*
* @throws UnzerApiException
*/
public function fullChargeAfterAuthorize(): void
{
Expand All @@ -106,10 +162,83 @@ public function fullChargeAfterAuthorize(): void
$this->assertTrue($paymentNew->isCompleted());
}

/**
* Verify the applepay can charge part of the authorized amount and the payment state is updated accordingly.
*
* @test
*
* @throws UnzerApiException
*/
public function partialChargeAfterAuthorization(): void
{
$applepay = $this->createApplepayObject();
/** @var Applepay $applepay */
$applepay = $this->unzer->createPaymentType($applepay);
$authorization = $this->unzer->authorize(
100.0,
'EUR',
$applepay,
self::RETURN_URL,
null,
null,
null,
null,
false
);

$payment = $authorization->getPayment();
$this->assertAmounts($payment, 100.0, 0.0, 100.0, 0.0);
$this->assertTrue($payment->isPending());

$charge = $this->unzer->chargeAuthorization($payment->getId(), 20);
$payment1 = $charge->getPayment();
$this->assertAmounts($payment1, 80.0, 20.0, 100.0, 0.0);
$this->assertTrue($payment1->isPartlyPaid());

$charge = $this->unzer->chargeAuthorization($payment->getId(), 20);
$payment2 = $charge->getPayment();
$this->assertAmounts($payment2, 60.0, 40.0, 100.0, 0.0);
$this->assertTrue($payment2->isPartlyPaid());

$charge = $this->unzer->chargeAuthorization($payment->getId(), 60);
$payment3 = $charge->getPayment();
$this->assertAmounts($payment3, 00.0, 100.0, 100.0, 0.0);
$this->assertTrue($payment3->isCompleted());
}

/**
* Verify that an exception is thrown when trying to charge more than authorized.
*
* @test
*
* @throws UnzerApiException
*/
public function exceptionShouldBeThrownWhenChargingMoreThenAuthorized(): void
{
$applepay = $this->createApplepayObject();
/** @var Applepay $applepay */
$applepay = $this->unzer->createPaymentType($applepay);
$authorization = $applepay->authorize(100.0000, 'EUR', self::RETURN_URL, null, null, null, null, false);
$payment = $authorization->getPayment();
$this->assertAmounts($payment, 100.0, 0.0, 100.0, 0.0);
$this->assertTrue($payment->isPending());

$charge = $this->unzer->chargeAuthorization($payment->getId(), 50);
$payment1 = $charge->getPayment();
$this->assertAmounts($payment1, 50.0, 50.0, 100.0, 0.0);
$this->assertTrue($payment1->isPartlyPaid());

$this->expectException(UnzerApiException::class);
$this->expectExceptionCode(ApiResponseCodes::API_ERROR_CHARGED_AMOUNT_HIGHER_THAN_EXPECTED);
$this->unzer->chargeAuthorization($payment->getId(), 70);
}

/**
* Verify applepay authorize can be canceled.
*
* @test
*
* @throws UnzerApiException
*/
public function applepayAuthorizeCanBeCanceled(): void
{
Expand All @@ -123,7 +252,57 @@ public function applepayAuthorizeCanBeCanceled(): void
}

/**
* Verify the applepay payment can be charged until it is fully charged and the payment is updated accordingly.
*
* @test
*
* @throws UnzerApiException
*/
public function partialAndFullChargeAfterAuthorization(): void
{
$applepay = $this->createApplepayObject();
/** @var Applepay $applepay */
$applepay = $this->unzer->createPaymentType($applepay);
$authorization = $applepay->authorize(100.0000, 'EUR', self::RETURN_URL, null, null, null, null, false);
$payment = $authorization->getPayment();

$this->assertAmounts($payment, 100.0, 0.0, 100.0, 0.0);
$this->assertTrue($payment->isPending());

$charge = $this->unzer->chargeAuthorization($payment->getId(), 20);
$payment1 = $charge->getPayment();
$this->assertAmounts($payment1, 80.0, 20.0, 100.0, 0.0);
$this->assertTrue($payment1->isPartlyPaid());

$charge = $this->unzer->chargeAuthorization($payment->getId());
$payment2 = $charge->getPayment();
$this->assertAmounts($payment2, 0.0, 100.0, 100.0, 0.0);
$this->assertTrue($payment2->isCompleted());
}

/**
* Authorization can be fetched.
*
* @test
*
* @throws UnzerApiException
*/
public function authorizationShouldBeFetchable(): void
{
$applepay = $this->createApplepayObject();
/** @var Applepay $applepay */
$applepay = $this->unzer->createPaymentType($applepay);
$authorization = $applepay->authorize(100.0000, 'EUR', self::RETURN_URL);
$payment = $authorization->getPayment();

$fetchedAuthorization = $this->unzer->fetchAuthorization($payment->getId());
$this->assertEquals($fetchedAuthorization->getId(), $authorization->getId());
}

/**
* @test
*
* @throws UnzerApiException
*/
public function fullCancelAfterCharge(): void
{
Expand All @@ -140,6 +319,88 @@ public function fullCancelAfterCharge(): void
$this->assertTrue($payment->isCanceled());
}

/**
* Verify a applepay payment can be cancelled after being fully charged.
*
* @test
*
* @throws UnzerApiException
*/
public function fullCancelOnFullyChargedPayment(): void
{
$applepay = $this->createApplepayObject();
/** @var Applepay $applepay */
$applepay = $this->unzer->createPaymentType($applepay);

$authorization = $applepay->authorize(100.0000, 'EUR', self::RETURN_URL, null, null, null, null, false);
$payment = $authorization->getPayment();

$this->assertAmounts($payment, 100.0, 0.0, 100.0, 0.0);
$this->assertTrue($payment->isPending());

$payment->charge(10.0);
$this->assertAmounts($payment, 90.0, 10.0, 100.0, 0.0);
$this->assertTrue($payment->isPartlyPaid());

$payment->charge(90.0);
$this->assertAmounts($payment, 0.0, 100.0, 100.0, 0.0);
$this->assertTrue($payment->isCompleted());

$cancellation = $payment->cancelAmount();
$this->assertNotEmpty($cancellation);
$this->assertAmounts($payment, 0.0, 0.0, 100.0, 100.0);
$this->assertTrue($payment->isCanceled());
}

/**
* Full cancel on partly charged auth canceled charges.
*
* @test
*
* @throws UnzerApiException
*/
public function fullCancelOnPartlyPaidAuthWithCanceledCharges(): void
{
$applepay = $this->createApplepayObject();
/** @var Applepay $applepay */
$applepay = $this->unzer->createPaymentType($applepay);

$authorization = $applepay->authorize(100.0000, 'EUR', self::RETURN_URL, null, null, null, null, false);
$payment = $authorization->getPayment();

$payment->charge(10.0);
$this->assertAmounts($payment, 90.0, 10.0, 100.0, 0.0);

$charge = $payment->charge(10.0);
$this->assertAmounts($payment, 80.0, 20.0, 100.0, 0.0);
$this->assertTrue($payment->isPartlyPaid());

$charge->cancel();
$this->assertAmounts($payment, 80.0, 10.0, 100.0, 10.0);
$this->assertTrue($payment->isPartlyPaid());

$payment->cancelAmount();
$this->assertTrue($payment->isCanceled());
}

/**
* Verify applepay charge can be canceled.
*
* @test
*
* @throws UnzerApiException
*/
public function applepayChargeCanBeCanceled(): void
{
/** @var Applepay $applepay */
$applepay = $this->unzer->createPaymentType($this->createApplepayObject());
$charge = $applepay->charge(100.0, 'EUR', self::RETURN_URL, null, null, null, null, false);

$cancel = $charge->cancel();
$this->assertNotNull($cancel);
$this->assertNotEmpty($cancel->getId());
}

/**
* @return Applepay
*/
Expand Down