Skip to content

Commit 9ec42ac

Browse files
committed
[CC-2016] Identify customer version based on ID.
1 parent 7090de7 commit 9ec42ac

File tree

8 files changed

+259
-179
lines changed

8 files changed

+259
-179
lines changed

src/Interfaces/ResourceServiceInterface.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
interface ResourceServiceInterface
3131
{
3232
/**
33-
* Retrieves an Payout resource via the API using the corresponding Payment or paymentId.
33+
* Retrieves a Payout resource via the API using the corresponding Payment or paymentId.
3434
* The Payout resource can not be fetched using its id since they are unique only within the Payment.
3535
* A Payment can have zero or one Payouts.
3636
*
@@ -162,7 +162,7 @@ public function createBasket(Basket $basket): Basket;
162162
public function fetchBasket($basket): Basket;
163163

164164
/**
165-
* Update the a basket resource with the given basket object (id must be set).
165+
* Update a basket resource with the given basket object (id must be set).
166166
*
167167
* @param Basket $basket
168168
*
@@ -195,12 +195,12 @@ public function createPaymentType(BasePaymentType $paymentType): BasePaymentType
195195
* @return BasePaymentType|AbstractUnzerResource The updated PaymentType object.
196196
*
197197
* @throws UnzerApiException An UnzerApiException is thrown if there is an error returned on API-request.
198-
* @throws RuntimeException A RuntimeException is thrown when there is a error while using the SDK.
198+
* @throws RuntimeException A RuntimeException is thrown when there is an error while using the SDK.
199199
*/
200200
public function updatePaymentType(BasePaymentType $paymentType): BasePaymentType;
201201

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

262262
/**
263263
* Update and return a Customer object via API.
@@ -325,8 +325,8 @@ public function fetchCharge(Charge $charge): Charge;
325325
* Fetch a chargeback object by combination of payment id, chargeback id and charge id.
326326
* Chargeback ids are not unique to a merchant but to the payment.
327327
*
328-
* @param Payment|string $payment The payment object or payment id to fetch the authorization from.
329-
* @param string $chargebackId The id of the chargeback to fetch.
328+
* @param string $paymentId The payment object or payment id to fetch the authorization from.
329+
* @param string $chargebackId The id of the chargeback to fetch.
330330
*
331331
* @return Chargeback|AbstractUnzerResource The fetched chargeback.
332332
*

src/Services/IdService.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace UnzerSDK\Services;
44

55
use RuntimeException;
6-
76
use function count;
87

98
/**
@@ -22,7 +21,7 @@ class IdService
2221
*
2322
* @param string $url
2423
* @param string $idString
25-
* @param bool $onlyLast
24+
* @param bool $onlyLast
2625
*
2726
* @return string
2827
*
@@ -31,7 +30,7 @@ class IdService
3130
public static function getResourceIdFromUrl(string $url, string $idString, bool $onlyLast = false): string
3231
{
3332
$matches = [];
34-
$pattern = '/\/([s|p]{1}-' . $idString . '-[a-z\d]+)\/?' . ($onlyLast ? '$' : '') . '/';
33+
$pattern = '/\/([s|p]{1}-' . $idString . '-[a-z\d-]+)\/?' . ($onlyLast ? '$' : '') . '/';
3534
preg_match($pattern, $url, $matches);
3635

3736
if (count($matches) < 2) {
@@ -76,7 +75,7 @@ public static function isPaymentChargeback(string $url): string
7675
*
7776
* @param string $url
7877
* @param string $idString
79-
* @param bool $onlyLast
78+
* @param bool $onlyLast
8079
*
8180
* @return string|null
8281
*/
@@ -117,4 +116,10 @@ public static function getResourceTypeFromIdString(string $typeId): ?string
117116

118117
return $typeIdString;
119118
}
119+
120+
public static function isUUDIResource(string $id): bool
121+
{
122+
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);
123+
return count($matches) > 0;
124+
}
120125
}

src/Services/ResourceService.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
use UnzerSDK\Resources\TransactionTypes\Chargeback;
5959
use UnzerSDK\Resources\TransactionTypes\Payout;
6060
use UnzerSDK\Resources\TransactionTypes\Shipment;
61+
use UnzerSDK\Resources\V2\Customer as CustomerV2;
6162
use UnzerSDK\Resources\V2\Paypage as PaypageV2;
6263
use UnzerSDK\Traits\CanRecur;
6364
use UnzerSDK\Unzer;
@@ -119,8 +120,8 @@ public function send(
119120
string $httpMethod = HttpAdapterInterface::REQUEST_GET,
120121
string $apiVersion = Unzer::API_VERSION
121122
): stdClass {
122-
$configClass = $resource->getApiConfig();
123-
if (!$resource instanceof Token && $configClass::getAuthorizationMethod() === AuthorizationMethods::BEARER) {
123+
$apiConfig = $resource->getApiConfig();
124+
if (!$resource instanceof Token && $apiConfig::getAuthorizationMethod() === AuthorizationMethods::BEARER) {
124125
$this->unzer->prepareJwtToken();
125126
}
126127

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

650651
if (is_string($customer)) {
651-
$customerObject = (new Customer())->setId($customer);
652+
$isUUID = IdService::isUUDIResource($customer);
653+
$customerObject = $isUUID ? new CustomerV2() : new Customer();
654+
$customerObject->setId($customer);
652655
}
653656

654657
$this->fetchResource($customerObject->setParentResource($this->unzer));
@@ -658,7 +661,7 @@ public function fetchCustomer($customer): Customer
658661
/**
659662
* {@inheritDoc}
660663
*/
661-
public function fetchCustomerByExtCustomerId(string $customerId): Customer
664+
public function fetchCustomerByExtCustomerId(string $customerId, int $version = 1): Customer
662665
{
663666
$customerObject = (new Customer())->setCustomerId($customerId);
664667
$this->fetchResource($customerObject->setParentResource($this->unzer));

src/Unzer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -485,9 +485,9 @@ public function fetchCustomer($customer): Customer
485485
/**
486486
* {@inheritDoc}
487487
*/
488-
public function fetchCustomerByExtCustomerId(string $customerId): Customer
488+
public function fetchCustomerByExtCustomerId(string $customerId, int $version = 1): Customer
489489
{
490-
return $this->resourceService->fetchCustomerByExtCustomerId($customerId);
490+
return $this->resourceService->fetchCustomerByExtCustomerId($customerId, $version);
491491
}
492492

493493
/**

test/Fixtures/CustomerFixtureTrait.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function getMaximumCustomer(): Customer
5757
*/
5858
public function getMaximumCustomerInclShippingAddress(): Customer
5959
{
60-
return $this->getMaximumCustomer($version)->setShippingAddress($this->getShippingAddress());
60+
return $this->getMaximumCustomer()->setShippingAddress($this->getShippingAddress());
6161
}
6262

6363
/**
@@ -74,8 +74,7 @@ public function getMinimalNotRegisteredB2bCustomer(): Customer
7474
$this->getBillingAddress(),
7575
7676
'Unzer GmbH',
77-
CompanyCommercialSectorItems::WAREHOUSING_AND_SUPPORT_ACTIVITIES_FOR_TRANSPORTATION,
78-
$version
77+
CompanyCommercialSectorItems::WAREHOUSING_AND_SUPPORT_ACTIVITIES_FOR_TRANSPORTATION
7978
);
8079
}
8180

@@ -86,7 +85,7 @@ public function getMinimalNotRegisteredB2bCustomer(): Customer
8685
*/
8786
public function getMaximalNotRegisteredB2bCustomer(): Customer
8887
{
89-
$customer = $this->getMinimalNotRegisteredB2bCustomer($version)
88+
$customer = $this->getMinimalNotRegisteredB2bCustomer()
9089
->setShippingAddress($this->getShippingAddress())
9190
->setSalutation(Salutations::MR)
9291
->setMobile('+49172123456')
@@ -112,7 +111,7 @@ public function getMaximalNotRegisteredB2bCustomer(): Customer
112111
*/
113112
public function getMinimalRegisteredB2bCustomer(): Customer
114113
{
115-
return CustomerFactory::createRegisteredB2bCustomer($this->getBillingAddress(), '123456789', 'Unzer GmbH', CompanyCommercialSectorItems::OTHER, $version);
114+
return CustomerFactory::createRegisteredB2bCustomer($this->getBillingAddress(), '123456789', 'Unzer GmbH');
116115
}
117116

118117
/**

0 commit comments

Comments
 (0)