Skip to content
Merged
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
17 changes: 17 additions & 0 deletions src/Apis/PaymentApiConfigBearerAuth.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace UnzerSDK\Apis;

use UnzerSDK\Apis\Constants\AuthorizationMethods;


/**
* Config for Payment API (PAPI) with bearer authentication.
*/
class PaymentApiConfigBearerAuth extends PaymentApiConfig
{
public static function getAuthorizationMethod(): string
{
return AuthorizationMethods::BEARER;
}
}
14 changes: 7 additions & 7 deletions src/Interfaces/ResourceServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
interface ResourceServiceInterface
{
/**
* Retrieves an Payout resource via the API using the corresponding Payment or paymentId.
* Retrieves a Payout resource via the API using the corresponding Payment or paymentId.
* The Payout resource can not be fetched using its id since they are unique only within the Payment.
* A Payment can have zero or one Payouts.
*
Expand Down Expand Up @@ -162,7 +162,7 @@ public function createBasket(Basket $basket): Basket;
public function fetchBasket($basket): Basket;

/**
* Update the a basket resource with the given basket object (id must be set).
* Update a basket resource with the given basket object (id must be set).
*
* @param Basket $basket
*
Expand Down Expand Up @@ -195,12 +195,12 @@ public function createPaymentType(BasePaymentType $paymentType): BasePaymentType
* @return BasePaymentType|AbstractUnzerResource The updated PaymentType object.
*
* @throws UnzerApiException An UnzerApiException is thrown if there is an error returned on API-request.
* @throws RuntimeException A RuntimeException is thrown when there is a error while using the SDK.
* @throws RuntimeException A RuntimeException is thrown when there is an error while using the SDK.
*/
public function updatePaymentType(BasePaymentType $paymentType): BasePaymentType;

/**
* Fetch the payment type with the given Id from the API.
* Fetch the payment type with the given ID from the API.
*
* @param string $typeId
*
Expand Down Expand Up @@ -257,7 +257,7 @@ public function fetchCustomer($customer): Customer;
* @throws UnzerApiException An UnzerApiException is thrown if there is an error returned on API-request.
* @throws RuntimeException A RuntimeException is thrown when there is an error while using the SDK.
*/
public function fetchCustomerByExtCustomerId(string $customerId): Customer;
public function fetchCustomerByExtCustomerId(string $customerId, int $version): Customer;

/**
* Update and return a Customer object via API.
Expand Down Expand Up @@ -325,8 +325,8 @@ public function fetchCharge(Charge $charge): Charge;
* Fetch a chargeback object by combination of payment id, chargeback id and charge id.
* Chargeback ids are not unique to a merchant but to the payment.
*
* @param Payment|string $payment The payment object or payment id to fetch the authorization from.
* @param string $chargebackId The id of the chargeback to fetch.
* @param string $paymentId The payment object or payment id to fetch the authorization from.
* @param string $chargebackId The id of the chargeback to fetch.
*
* @return Chargeback|AbstractUnzerResource The fetched chargeback.
*
Expand Down
27 changes: 24 additions & 3 deletions src/Resources/CustomerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use UnzerSDK\Constants\CompanyRegistrationTypes;
use UnzerSDK\Resources\EmbeddedResources\Address;
use UnzerSDK\Resources\EmbeddedResources\CompanyInfo;
use UnzerSDK\Resources\V2\Customer as CustomerV2;

/**
* Creates the different Customer objects.
Expand All @@ -15,6 +16,18 @@
*/
class CustomerFactory
{
private static int $version = 1;

public static function setVersion(int $version): void
{
self::$version = $version;
}

public static function getVersion(): int
{
return self::$version;
}

/**
* Creates a local Customer object for B2C transactions.
* Please use Unzer::createCustomer(...) to create the customer resource on the API side.
Expand All @@ -26,7 +39,7 @@ class CustomerFactory
*/
public static function createCustomer(string $firstname, string $lastname): Customer
{
return (new Customer())->setFirstname($firstname)->setLastname($lastname);
return self::getCustomer()->setFirstname($firstname)->setLastname($lastname);
}

/**
Expand Down Expand Up @@ -58,7 +71,7 @@ public static function createNotRegisteredB2bCustomer(
->setFunction('OWNER')
->setCommercialSector($commercialSector);

return (new Customer())
return self::getCustomer()
->setFirstname($firstname)
->setLastname($lastname)
->setBirthDate($birthDate)
Expand Down Expand Up @@ -89,9 +102,17 @@ public static function createRegisteredB2bCustomer(
->setCommercialRegisterNumber($commercialRegisterNumber)
->setCommercialSector($commercialSector);

return (new Customer())
return self::getCustomer()
->setCompany($company)
->setBillingAddress($billingAddress)
->setCompanyInfo($companyInfo);
}

/**
* @return Customer
*/
protected static function getCustomer(): Customer
{
return (self::$version == 2) ? new CustomerV2() : new Customer();
}
}
21 changes: 21 additions & 0 deletions src/Resources/V2/Customer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace UnzerSDK\Resources\V2;

use UnzerSDK\Apis\PaymentApiConfigBearerAuth;
use UnzerSDK\Resources\Customer as CustomerV1;

class Customer extends CustomerV1
{
public function getApiVersion(): string
{
return "v2";
}

public function getApiConfig(): string
{
return PaymentApiConfigBearerAuth::class;
}


}
13 changes: 9 additions & 4 deletions src/Services/IdService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace UnzerSDK\Services;

use RuntimeException;

use function count;

/**
Expand All @@ -22,7 +21,7 @@ class IdService
*
* @param string $url
* @param string $idString
* @param bool $onlyLast
* @param bool $onlyLast
*
* @return string
*
Expand All @@ -31,7 +30,7 @@ class IdService
public static function getResourceIdFromUrl(string $url, string $idString, bool $onlyLast = false): string
{
$matches = [];
$pattern = '/\/([s|p]{1}-' . $idString . '-[a-z\d]+)\/?' . ($onlyLast ? '$' : '') . '/';
$pattern = '/\/([s|p]{1}-' . $idString . '-[a-z\d-]+)\/?' . ($onlyLast ? '$' : '') . '/';
preg_match($pattern, $url, $matches);

if (count($matches) < 2) {
Expand Down Expand Up @@ -76,7 +75,7 @@ public static function isPaymentChargeback(string $url): string
*
* @param string $url
* @param string $idString
* @param bool $onlyLast
* @param bool $onlyLast
*
* @return string|null
*/
Expand Down Expand Up @@ -117,4 +116,10 @@ public static function getResourceTypeFromIdString(string $typeId): ?string

return $typeIdString;
}

public static function isUUDIResource(string $id): bool
{
preg_match('/^[sp]-([a-z]{3}|p24)-[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/', $id, $matches);
return count($matches) > 0;
}
}
11 changes: 7 additions & 4 deletions src/Services/ResourceService.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
use UnzerSDK\Resources\TransactionTypes\Chargeback;
use UnzerSDK\Resources\TransactionTypes\Payout;
use UnzerSDK\Resources\TransactionTypes\Shipment;
use UnzerSDK\Resources\V2\Customer as CustomerV2;
use UnzerSDK\Resources\V2\Paypage as PaypageV2;
use UnzerSDK\Traits\CanRecur;
use UnzerSDK\Unzer;
Expand Down Expand Up @@ -119,8 +120,8 @@ public function send(
string $httpMethod = HttpAdapterInterface::REQUEST_GET,
string $apiVersion = Unzer::API_VERSION
): stdClass {
$configClass = $resource->getApiConfig();
if (!$resource instanceof Token && $configClass::getAuthorizationMethod() === AuthorizationMethods::BEARER) {
$apiConfig = $resource->getApiConfig();
if (!$resource instanceof Token && $apiConfig::getAuthorizationMethod() === AuthorizationMethods::BEARER) {
$this->unzer->prepareJwtToken();
}

Expand Down Expand Up @@ -648,7 +649,9 @@ public function fetchCustomer($customer): Customer
$customerObject = $customer;

if (is_string($customer)) {
$customerObject = (new Customer())->setId($customer);
$isUUID = IdService::isUUDIResource($customer);
$customerObject = $isUUID ? new CustomerV2() : new Customer();
$customerObject->setId($customer);
}

$this->fetchResource($customerObject->setParentResource($this->unzer));
Expand All @@ -658,7 +661,7 @@ public function fetchCustomer($customer): Customer
/**
* {@inheritDoc}
*/
public function fetchCustomerByExtCustomerId(string $customerId): Customer
public function fetchCustomerByExtCustomerId(string $customerId, int $version = 1): Customer
{
$customerObject = (new Customer())->setCustomerId($customerId);
$this->fetchResource($customerObject->setParentResource($this->unzer));
Expand Down
4 changes: 2 additions & 2 deletions src/Unzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -485,9 +485,9 @@ public function fetchCustomer($customer): Customer
/**
* {@inheritDoc}
*/
public function fetchCustomerByExtCustomerId(string $customerId): Customer
public function fetchCustomerByExtCustomerId(string $customerId, int $version = 1): Customer
{
return $this->resourceService->fetchCustomerByExtCustomerId($customerId);
return $this->resourceService->fetchCustomerByExtCustomerId($customerId, $version);
}

/**
Expand Down
Loading