Skip to content

Commit 06a29c1

Browse files
authored
CC-2016/v2-customer (#204)
* [CC-2016] Add customerV2 resource. * [CC-2016] Identify customer version based on ID. * [CC-2016] Identify customer version based on ID.
1 parent 274650b commit 06a29c1

File tree

10 files changed

+706
-116
lines changed

10 files changed

+706
-116
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace UnzerSDK\Apis;
4+
5+
use UnzerSDK\Apis\Constants\AuthorizationMethods;
6+
7+
8+
/**
9+
* Config for Payment API (PAPI) with bearer authentication.
10+
*/
11+
class PaymentApiConfigBearerAuth extends PaymentApiConfig
12+
{
13+
public static function getAuthorizationMethod(): string
14+
{
15+
return AuthorizationMethods::BEARER;
16+
}
17+
}

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/Resources/CustomerFactory.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use UnzerSDK\Constants\CompanyRegistrationTypes;
77
use UnzerSDK\Resources\EmbeddedResources\Address;
88
use UnzerSDK\Resources\EmbeddedResources\CompanyInfo;
9+
use UnzerSDK\Resources\V2\Customer as CustomerV2;
910

1011
/**
1112
* Creates the different Customer objects.
@@ -15,6 +16,18 @@
1516
*/
1617
class CustomerFactory
1718
{
19+
private static int $version = 1;
20+
21+
public static function setVersion(int $version): void
22+
{
23+
self::$version = $version;
24+
}
25+
26+
public static function getVersion(): int
27+
{
28+
return self::$version;
29+
}
30+
1831
/**
1932
* Creates a local Customer object for B2C transactions.
2033
* Please use Unzer::createCustomer(...) to create the customer resource on the API side.
@@ -26,7 +39,7 @@ class CustomerFactory
2639
*/
2740
public static function createCustomer(string $firstname, string $lastname): Customer
2841
{
29-
return (new Customer())->setFirstname($firstname)->setLastname($lastname);
42+
return self::getCustomer()->setFirstname($firstname)->setLastname($lastname);
3043
}
3144

3245
/**
@@ -58,7 +71,7 @@ public static function createNotRegisteredB2bCustomer(
5871
->setFunction('OWNER')
5972
->setCommercialSector($commercialSector);
6073

61-
return (new Customer())
74+
return self::getCustomer()
6275
->setFirstname($firstname)
6376
->setLastname($lastname)
6477
->setBirthDate($birthDate)
@@ -89,9 +102,17 @@ public static function createRegisteredB2bCustomer(
89102
->setCommercialRegisterNumber($commercialRegisterNumber)
90103
->setCommercialSector($commercialSector);
91104

92-
return (new Customer())
105+
return self::getCustomer()
93106
->setCompany($company)
94107
->setBillingAddress($billingAddress)
95108
->setCompanyInfo($companyInfo);
96109
}
110+
111+
/**
112+
* @return Customer
113+
*/
114+
protected static function getCustomer(): Customer
115+
{
116+
return (self::$version == 2) ? new CustomerV2() : new Customer();
117+
}
97118
}

src/Resources/V2/Customer.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace UnzerSDK\Resources\V2;
4+
5+
use UnzerSDK\Apis\PaymentApiConfigBearerAuth;
6+
use UnzerSDK\Resources\Customer as CustomerV1;
7+
8+
class Customer extends CustomerV1
9+
{
10+
public function getApiVersion(): string
11+
{
12+
return "v2";
13+
}
14+
15+
public function getApiConfig(): string
16+
{
17+
return PaymentApiConfigBearerAuth::class;
18+
}
19+
20+
21+
}

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
/**

0 commit comments

Comments
 (0)