Skip to content
Draft
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion src/Adapter/ApplepayAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function validateApplePayMerchant(
if ($this->request === null) {
throw new ApplepayMerchantValidationException('No curl adapter initiated yet. Make sure to cal init() function before.');
}
$payload = $applePaySession->jsonSerialize();
$payload = json_encode($applePaySession, JSON_UNESCAPED_SLASHES | JSON_PRESERVE_ZERO_FRACTION);
$this->setOption(CURLOPT_URL, $merchantValidationURL);
$this->setOption(CURLOPT_POSTFIELDS, $payload);

Expand Down
13 changes: 4 additions & 9 deletions src/Resources/AbstractUnzerResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
* @link https://docs.unzer.com/
*
*/
abstract class AbstractUnzerResource implements UnzerParentInterface
abstract class AbstractUnzerResource implements UnzerParentInterface, \JsonSerializable
{
/** @var string $id */
protected $id;
Expand Down Expand Up @@ -332,16 +332,11 @@ protected function fetchResource(AbstractUnzerResource $resource): void
}

/**
* Specify data which should be serialized to JSON
*
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php
*
* @return false|string data which can be serialized by <b>json_encode</b>,
* which is a value of any type other than a resource.
* {@inheritDoc}
*/
public function jsonSerialize()
public function jsonSerialize(): mixed
{
return json_encode($this->expose(), JSON_UNESCAPED_SLASHES | JSON_PRESERVE_ZERO_FRACTION);
return $this->expose();
}

/**
Expand Down
11 changes: 4 additions & 7 deletions src/Resources/ExternalResources/ApplepaySession.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @link https://docs.unzer.com/
*
*/
class ApplepaySession
class ApplepaySession implements \JsonSerializable
{
/**
* This can be found in the Apple Developer Account
Expand Down Expand Up @@ -46,14 +46,11 @@ public function __construct(string $merchantIdentifier, string $displayName, str
}

/**
* Returns the json representation of this object's properties.
*
* @return false|string
* {@inheritDoc}
*/
public function jsonSerialize()
public function jsonSerialize(): mixed
{
$properties = get_object_vars($this);
return json_encode($properties, JSON_UNESCAPED_SLASHES | JSON_PRESERVE_ZERO_FRACTION);
return get_object_vars($this);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Services/HttpService.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public function sendRequest(ApiRequest $request): string

// perform request
$requestUrl = $this->buildRequestUrl($request);
$payload = $request->getResource()->jsonSerialize();
$payload = json_encode($request->getResource(), JSON_UNESCAPED_SLASHES | JSON_PRESERVE_ZERO_FRACTION);
$headers = $this->composeHttpHeaders($unzerObj, $apiConfig::getAuthorizationMethod());
$httpMethod = $request->getHttpMethod();
$this->initRequest($requestUrl, $payload, $httpMethod, $headers);
Expand Down
6 changes: 3 additions & 3 deletions src/Services/PaymentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function performAuthorization(
): Authorization {
$payment = $this->createPayment($paymentType);
$paymentType = $payment->getPaymentType();
$authorization->setSpecialParams($paymentType !== null ? $paymentType->getTransactionParams() : []);
$authorization->setSpecialParams($paymentType?->getTransactionParams() ?? []);

$payment->setAuthorization($authorization)
->setCustomer($customer)
Expand Down Expand Up @@ -157,7 +157,7 @@ public function performCharge(Charge $charge, $paymentType, $customer = null, ?M
$paymentType = $payment->getPaymentType();

/** @var Charge $charge */
$charge->setSpecialParams($paymentType->getTransactionParams() ?? []);
$charge->setSpecialParams($paymentType?->getTransactionParams() ?? []);
$payment->addCharge($charge)->setCustomer($customer)->setMetadata($metadata)->setBasket($basket);

$this->getResourceService()->createResource($charge);
Expand Down Expand Up @@ -417,7 +417,7 @@ public function performSca(Sca $sca, $paymentType, $customer = null, ?Metadata $
$paymentType = $payment->getPaymentType();

/** @var Sca $sca */
$sca->setSpecialParams($paymentType->getTransactionParams() ?? []);
$sca->setSpecialParams($paymentType?->getTransactionParams() ?? []);
$payment->setSca($sca)->setCustomer($customer)->setMetadata($metadata)->setBasket($basket);

$this->getResourceService()->createResource($sca);
Expand Down
2 changes: 1 addition & 1 deletion test/unit/Adapter/ApplepaySessionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function testJsonSerialize(): void
$applepaySession = new ApplepaySession('merchantIdentifier', 'displayName', 'domainName');
$expectedJson = '{"merchantIdentifier": "merchantIdentifier", "displayName": "displayName", "domainName": "domainName"}';

$jsonSerialize = $applepaySession->jsonSerialize();
$jsonSerialize = json_encode($applepaySession);
$this->assertJsonStringEqualsJsonString($expectedJson, $jsonSerialize);
}
}
4 changes: 2 additions & 2 deletions test/unit/DummyResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ public function setTestFloat(float $testFloat): DummyResource
//</editor-fold>

//<editor-fold desc="Overridable methods">
public function jsonSerialize()
public function jsonSerialize(): mixed
{
return '{"dummyResource": "JsonSerialized"}';
return ['dummyResource' => 'JsonSerialized'];
}

public function getUri(bool $appendId = true, string $httpMethod = HttpAdapterInterface::REQUEST_GET): string
Expand Down
10 changes: 5 additions & 5 deletions test/unit/Resources/AbstractUnzerResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,8 @@ public function updateValuesShouldUpdateChildObjects(): void
->setCommercialSector(CompanyCommercialSectorItems::AIR_TRANSPORT);

$testResponse = new stdClass();
$testResponse->billingAddress = json_decode($address->jsonSerialize(), false);
$testResponse->companyInfo = json_decode($info->jsonSerialize(), false);
$testResponse->billingAddress = json_decode(json_encode($address), false);
$testResponse->companyInfo = json_decode(json_encode($info), false);

$customer = new Customer();
$customer->handleResponse($testResponse);
Expand Down Expand Up @@ -319,7 +319,7 @@ public function jsonSerializeShouldTranslateResourceIntoJson(): void
'"firstname":"Peter","lastname":"Universum","mobile":"+49172123456","param1":"value1","param2":"value2",' .
'"phone":"+4962216471100","salutation":"mr","shippingAddress":{"city":"Frankfurt am Main","country":"DE",' .
'"name":"Peter Universum","state":"DE-BO","street":"Hugo-Junkers-Str. 5","zip":"60386"}}';
$this->assertEquals($expectedJson, $customer->jsonSerialize());
$this->assertJsonStringEqualsJsonString($expectedJson, json_encode($customer));
}

/**
Expand Down Expand Up @@ -423,8 +423,8 @@ public function parentPrivatePropertiesShouldNotCauseAnException(): void
$metadata->setParentResource(new Unzer('s-priv-123'));
$metadata->setSpecialParams(['something' => 'special']);

$result = $metadata->jsonSerialize();
$this->assertEquals('{"something":"special"}', $result);
$result = json_encode($metadata);
$this->assertJsonStringEqualsJsonString('{"something":"special"}', $result);
}

//<editor-fold desc="Data Providers">
Expand Down
2 changes: 1 addition & 1 deletion test/unit/Resources/PaymentTypes/ApplePayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function jsonSerializationExposesOnlyRequestParameter(): void
$expectedJson = '{ "data": "data", "header": { "ephemeralPublicKey": "ephemeralPublicKey", "publicKeyHash": ' .
'"publicKeyHash", "transactionId": "transactionId" }, "signature": "sig", "version": "EC_v1" }';

$this->assertJsonStringEqualsJsonString($expectedJson, $applepay->jsonSerialize());
$this->assertJsonStringEqualsJsonString($expectedJson, json_encode($applepay));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion test/unit/Resources/PaymentTypes/ClickToPayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function jsonSerialization(): void
);

$expectedJson = JsonProvider::getJsonFromFile('clicktopay/createRequest.json');
$this->assertJsonStringEqualsJsonString($expectedJson, $clickToPayObject->jsonSerialize());
$this->assertJsonStringEqualsJsonString($expectedJson, json_encode($clickToPayObject));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion test/unit/Resources/PaymentTypes/GooglePayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function jsonSerializationExposesOnlyRequestParameter(): void
$googlepay = $this->getTestGooglepay();

$expectedJson = JsonProvider::getJsonFromFile('googlePay/createRequest.json');
$this->assertJsonStringEqualsJsonString($expectedJson, $googlepay->jsonSerialize());
$this->assertJsonStringEqualsJsonString($expectedJson, json_encode($googlepay));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion test/unit/Resources/PaymentTypes/OpenBankingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function jsonSerialization(): void
$openBankingObject = new OpenbankingPis("DE",);

$expectedJson = JsonProvider::getJsonFromFile('openBanking/createRequest.json');
$this->assertJsonStringEqualsJsonString($expectedJson, $openBankingObject->jsonSerialize());
$this->assertJsonStringEqualsJsonString($expectedJson, json_encode($openBankingObject));
}

/**
Expand Down
4 changes: 2 additions & 2 deletions test/unit/Services/HttpServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ static function ($url) {
return str_replace(['dev-api', 'stg-api'], 'sbx-api', $url) === 'https://sbx-api.unzer.com/v1/my/uri/123';
}
),
'{"dummyResource": "JsonSerialized"}',
'{"dummyResource":"JsonSerialized"}',
'GET'
);
/** @noinspection PhpParamsInspection */
Expand Down Expand Up @@ -223,7 +223,7 @@ static function ($string) {
}
)
],
['(' . (getmypid()) . ') Request: {"dummyResource": "JsonSerialized"}'],
['(' . (getmypid()) . ') Request: {"dummyResource":"JsonSerialized"}'],
['(' . (getmypid()) . ') Response: (201) {"response":"myResponseString"}']
);

Expand Down
37 changes: 37 additions & 0 deletions test/unit/Services/PaymentServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use UnzerSDK\Resources\TransactionTypes\Cancellation;
use UnzerSDK\Resources\TransactionTypes\Charge;
use UnzerSDK\Resources\TransactionTypes\Payout;
use UnzerSDK\Resources\TransactionTypes\Sca;
use UnzerSDK\Resources\TransactionTypes\Shipment;
use UnzerSDK\Services\CancelService;
use UnzerSDK\Services\PaymentService;
Expand Down Expand Up @@ -283,6 +284,42 @@ public function chargePaymentShouldSetArgumentsInNewChargeObject(): void
$this->assertEquals([$returnedCharge], $payment->getCharges());
}

/**
* Verify performCharge does not throw a fatal error when the payment type resolves to null.
*
* @test
*/
public function performChargeWithNullPaymentTypeShouldNotThrowFatalError(): void
{
$resourceSrvMock = $this->getMockBuilder(ResourceService::class)->disableOriginalConstructor()->setMethods(['createResource'])->getMock();
$paymentSrv = (new Unzer('s-priv-123'))->setResourceService($resourceSrvMock)->getPaymentService();

try {
$paymentSrv->performCharge(new Charge(), null);
$this->assertTrue(true);
} catch (\Throwable $e) {
$this->fail('performCharge threw an unexpected error: ' . $e->getMessage());
}
}

/**
* Verify performSca does not throw a fatal error when the payment type resolves to null.
*
* @test
*/
public function performScaWithNullPaymentTypeShouldNotThrowFatalError(): void
{
$resourceSrvMock = $this->getMockBuilder(ResourceService::class)->disableOriginalConstructor()->setMethods(['createResource'])->getMock();
$paymentSrv = (new Unzer('s-priv-123'))->setResourceService($resourceSrvMock)->getPaymentService();

try {
$paymentSrv->performSca(new Sca(), null);
$this->assertTrue(true);
} catch (\Throwable $e) {
$this->fail('performSca threw an unexpected error: ' . $e->getMessage());
}
}

//</editor-fold>

//<editor-fold desc="Cancel">
Expand Down
4 changes: 2 additions & 2 deletions test/unit/Traits/HasRecurrenceTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function recurrenceTypeShouldBeExposedProperly(): void
$this->assertEquals('oneclick', $exposedTransaction['additionalTransactionData']->card['recurrenceType']);
$this->assertStringContainsString(
'"additionalTransactionData":{"card":{"recurrenceType":"oneclick"}}',
$charge->jsonSerialize()
json_encode($charge)
);
}

Expand Down Expand Up @@ -108,7 +108,7 @@ public function responseShouldBeHandledProperlyWithRecurrenceType(): void
$this->assertEquals('oneclick', $charge->getRecurrenceType());
$this->assertStringContainsString(
'"additionalTransactionData":{"card":{"recurrenceType":"oneclick"}}',
$charge->jsonSerialize()
json_encode($charge)
);
}

Expand Down
Loading