Skip to content

Commit

Permalink
Required customers name, surname and e-mail or phone number
Browse files Browse the repository at this point in the history
  • Loading branch information
petrknap committed Jul 10, 2024
1 parent 1ae5de9 commit b1be3f2
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 86 deletions.
6 changes: 3 additions & 3 deletions doc/create-payment-recommended.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ $customer = new \ThePay\ApiClient\Model\CreatePaymentCustomer(
);

// Create payment (105.20 € with unique id uid123)
$createPayment = new \ThePay\ApiClient\Model\CreatePaymentParams(10520, 'EUR', 'uid123');
$createPayment = new \ThePay\ApiClient\Model\CreatePaymentParams(10520, 'EUR', 'uid123', $customer);
$createPayment->setOrderId('15478');
$createPayment->setDescriptionForCustomer('Payment for items on example.com');
$createPayment->setDescriptionForMerchant('Payment from VIP customer XYZ');
$createPayment->setCustomer($customer);

$payment = $thePayClient->createPayment($createPayment);

Expand All @@ -34,7 +33,8 @@ echo $payment->getPayUrl(); // https://demo.gate.thepay.cz/5aa4f4af546a74848/pay
In scenarios where you know the customer's preferred language, you can pass the language code in `CreatePaymentParams` constructor as the fourth argument. For example:

```php
$createPayment = new \ThePay\ApiClient\Model\CreatePaymentParams(10520, 'EUR', 'uid123', 'en');
/** @var \ThePay\ApiClient\Model\CreatePaymentParams $customer */
$createPayment = new \ThePay\ApiClient\Model\CreatePaymentParams(10520, 'EUR', 'uid123', $customer, 'en');
```

Possible values are described in ISO 639-1 standard. If you pass a language, that ThePay does not support, for example French (fr), then the English language will be used,
Expand Down
34 changes: 13 additions & 21 deletions src/Model/CreatePaymentCustomer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@

final class CreatePaymentCustomer
{
/** @var StringValue|null */
private $name;
/** @var StringValue|null */
private $surname;
private string $name;
private string $surname;
/** @var StringValue|null */
private $email;
/** @var PhoneNumber|null */
Expand All @@ -21,35 +19,29 @@ final class CreatePaymentCustomer
private $shippingAddress;

/**
* @param string|null $name
* @param string|null $surname
* @note At least one of $email and $phone is required.
*
* @param string|null $email
* @param string|null $phone - customer phone in international format max 15 numeric chars https://en.wikipedia.org/wiki/MSISDN
*/
public function __construct($name, $surname, $email, $phone, Address $billingAddress = null, Address $shippingAddress = null)
public function __construct(string $name, string $surname, $email, $phone, Address $billingAddress = null, Address $shippingAddress = null)
{
$this->name = $name === null ? null : new StringValue($name);
$this->surname = $surname === null ? null : new StringValue($surname);
$this->email = $email === null ? null : new StringValue($email);
$this->phone = $phone === null ? null : new PhoneNumber($phone);
$this->name = $name;
$this->surname = $surname;
$this->email = $email === null && $phone !== null ? null : new StringValue($email);
$this->phone = $phone === null && $email !== null ? null : new PhoneNumber($phone);
$this->billingAddress = $billingAddress;
$this->shippingAddress = $shippingAddress;
}

/**
* @return string|null
*/
public function getName()
public function getName(): string
{
return $this->name === null ? null : $this->name->getValue();
return $this->name;
}

/**
* @return string|null
*/
public function getSurname()
public function getSurname(): string
{
return $this->surname === null ? null : $this->surname->getValue();
return $this->surname;
}

/**
Expand Down
68 changes: 28 additions & 40 deletions src/Model/CreatePaymentParams.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ final class CreatePaymentParams implements SignableRequest
/** @var LanguageCode|null */
private $languageCode;

/** @var CreatePaymentCustomer|null */
private $customer;
private CreatePaymentCustomer $customer;

/** @var Subscription|null */
private $subscription;
Expand All @@ -66,12 +65,12 @@ final class CreatePaymentParams implements SignableRequest
* @param string $uid
* @param string $languageCode - 2 letter lowercase language code
*/
public function __construct($amount, $currencyCode, $uid, $languageCode = null)
public function __construct($amount, $currencyCode, $uid, CreatePaymentCustomer $customer, $languageCode = null)
{
$this->amount = new Amount($amount);
$this->currencyCode = new CurrencyCode($currencyCode);
$this->uid = new Identifier($uid);
$this->customer = null;
$this->customer = $customer;
if ($languageCode) {
$this->languageCode = LanguageCode::create($languageCode);
}
Expand Down Expand Up @@ -110,15 +109,6 @@ public function getCustomer()
return $this->customer;
}

/**
* @return self
*/
public function setCustomer(CreatePaymentCustomer $customer)
{
$this->customer = $customer;
return $this;
}

/**
* @return array<CreatePaymentItem>
*/
Expand Down Expand Up @@ -385,36 +375,34 @@ public function toArray()
if ($this->languageCode) {
$result['language_code'] = $this->languageCode->getValue();
}
if ($this->customer) {
$result['customer'] = [
'name' => $this->customer->getName(),
'surname' => $this->customer->getSurname(),
'email' => $this->customer->getEmail(),
'phone' => $this->customer->getPhone(),
];

$billingAddress = $this->customer->getBillingAddress();
if ($billingAddress) {
$result['customer']['billing_address'] = [
'country_code' => $billingAddress->getCountryCode(),
'city' => $billingAddress->getCity(),
'zip' => $billingAddress->getZip(),
'street' => $billingAddress->getStreet(),
];
}
$result['customer'] = [
'name' => $this->customer->getName(),
'surname' => $this->customer->getSurname(),
'email' => $this->customer->getEmail(),
'phone' => $this->customer->getPhone(),
];

$shippingAddress = $this->customer->getShippingAddress();
if ($shippingAddress) {
$result['customer']['shipping_address'] = [
'country_code' => $shippingAddress->getCountryCode(),
'city' => $shippingAddress->getCity(),
'zip' => $shippingAddress->getZip(),
'street' => $shippingAddress->getStreet(),
];
}
} else {
$result['customer'] = null;
$billingAddress = $this->customer->getBillingAddress();
if ($billingAddress) {
$result['customer']['billing_address'] = [
'country_code' => $billingAddress->getCountryCode(),
'city' => $billingAddress->getCity(),
'zip' => $billingAddress->getZip(),
'street' => $billingAddress->getStreet(),
];
}

$shippingAddress = $this->customer->getShippingAddress();
if ($shippingAddress) {
$result['customer']['shipping_address'] = [
'country_code' => $shippingAddress->getCountryCode(),
'city' => $shippingAddress->getCity(),
'zip' => $shippingAddress->getZip(),
'street' => $shippingAddress->getStreet(),
];
}

if ($this->subscription) {
$result['subscription'] = $this->subscription->toArray();
}
Expand Down
45 changes: 23 additions & 22 deletions tests/CreatePaymentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,25 +47,25 @@ public static function createButtonProvider(): array
{
return [
[
new CreatePaymentParams(100, 'CZK', '202001010001'),
'eyJhbW91bnQiOjEwMCwiY3VycmVuY3lfY29kZSI6IkNaSyIsInVpZCI6IjIwMjAwMTAxMDAwMSIsImxhbmd1YWdlX2NvZGUiOiJjcyIsImlzX2RlcG9zaXQiOnRydWUsInNhdmVfYXV0aG9yaXphdGlvbiI6ZmFsc2UsImNhbl9jdXN0b21lcl9jaGFuZ2VfbWV0aG9kIjp0cnVlLCJtZXJjaGFudF9pZCI6Ijg2YTNlZWQwLTk1YTQtMTFlYS1hYzlmLTM3MWYzNDg4ZTBmYSIsInByb2plY3RfaWQiOjF9',
'1b3e432dec35da90a62653e2cb8ce2d75e4204a32d360c6366afd24509f5178c',
new CreatePaymentParams(100, 'CZK', '202001010001', self::getCreatePaymentCustomer()),
'eyJhbW91bnQiOjEwMCwiY3VycmVuY3lfY29kZSI6IkNaSyIsInVpZCI6IjIwMjAwMTAxMDAwMSIsImxhbmd1YWdlX2NvZGUiOiJjcyIsImlzX2RlcG9zaXQiOnRydWUsInNhdmVfYXV0aG9yaXphdGlvbiI6ZmFsc2UsImNhbl9jdXN0b21lcl9jaGFuZ2VfbWV0aG9kIjp0cnVlLCJjdXN0b21lcl9uYW1lIjoiTWlrZSIsImN1c3RvbWVyX3N1cm5hbWUiOiJTbWl0aCIsImN1c3RvbWVyX2VtYWlsIjoibWlrZS5zbWl0aEBleGFtcGxlLmNvbSIsImN1c3RvbWVyX3Bob25lIjoiNDIwNTg5Njg3OTYzIiwiY3VzdG9tZXJfYmlsbGluZ19jb3VudHJ5X2NvZGUiOiJDWiIsImN1c3RvbWVyX2JpbGxpbmdfY2l0eSI6IlByYWd1ZSIsImN1c3RvbWVyX2JpbGxpbmdfemlwIjoiMTIzIDAwIiwiY3VzdG9tZXJfYmlsbGluZ19zdHJlZXQiOiJEb3duc3RyZWV0IDUiLCJtZXJjaGFudF9pZCI6Ijg2YTNlZWQwLTk1YTQtMTFlYS1hYzlmLTM3MWYzNDg4ZTBmYSIsInByb2plY3RfaWQiOjF9',
'bbad8c591d54fa52353f680363129a9949c6de23659140a36fc799d762a92a80'
],
[
new CreatePaymentParams(100, 'EUR', '202001010002'),
'eyJhbW91bnQiOjEwMCwiY3VycmVuY3lfY29kZSI6IkVVUiIsInVpZCI6IjIwMjAwMTAxMDAwMiIsImxhbmd1YWdlX2NvZGUiOiJjcyIsImlzX2RlcG9zaXQiOnRydWUsInNhdmVfYXV0aG9yaXphdGlvbiI6ZmFsc2UsImNhbl9jdXN0b21lcl9jaGFuZ2VfbWV0aG9kIjp0cnVlLCJtZXJjaGFudF9pZCI6Ijg2YTNlZWQwLTk1YTQtMTFlYS1hYzlmLTM3MWYzNDg4ZTBmYSIsInByb2plY3RfaWQiOjF9',
'b97e2a7c037c5d31a7db734d1ca84e06f53db5841fe9e56bdc701a833208f987',
new CreatePaymentParams(100, 'EUR', '202001010002', self::getCreatePaymentCustomer()),
'eyJhbW91bnQiOjEwMCwiY3VycmVuY3lfY29kZSI6IkVVUiIsInVpZCI6IjIwMjAwMTAxMDAwMiIsImxhbmd1YWdlX2NvZGUiOiJjcyIsImlzX2RlcG9zaXQiOnRydWUsInNhdmVfYXV0aG9yaXphdGlvbiI6ZmFsc2UsImNhbl9jdXN0b21lcl9jaGFuZ2VfbWV0aG9kIjp0cnVlLCJjdXN0b21lcl9uYW1lIjoiTWlrZSIsImN1c3RvbWVyX3N1cm5hbWUiOiJTbWl0aCIsImN1c3RvbWVyX2VtYWlsIjoibWlrZS5zbWl0aEBleGFtcGxlLmNvbSIsImN1c3RvbWVyX3Bob25lIjoiNDIwNTg5Njg3OTYzIiwiY3VzdG9tZXJfYmlsbGluZ19jb3VudHJ5X2NvZGUiOiJDWiIsImN1c3RvbWVyX2JpbGxpbmdfY2l0eSI6IlByYWd1ZSIsImN1c3RvbWVyX2JpbGxpbmdfemlwIjoiMTIzIDAwIiwiY3VzdG9tZXJfYmlsbGluZ19zdHJlZXQiOiJEb3duc3RyZWV0IDUiLCJtZXJjaGFudF9pZCI6Ijg2YTNlZWQwLTk1YTQtMTFlYS1hYzlmLTM3MWYzNDg4ZTBmYSIsInByb2plY3RfaWQiOjF9',
'157e1ed8af669621b3c5aafd68b0323100c924cc24b3c719f25c3321a7330cf4'
],
];
}

public function testCreateCustomButton(): void
{
$r = $this->client->getPaymentButton(new CreatePaymentParams(100, 'CZK', '202001010003'));
$r = $this->client->getPaymentButton(new CreatePaymentParams(100, 'CZK', '202001010003', self::getCreatePaymentCustomer()));
self::assertStringContainsString('Pay!', $r);
self::assertStringContainsString('class="tp-btn"', $r);
self::assertStringNotContainsString('data-payment-method', $r);
$r = $this->client->getPaymentButton(new CreatePaymentParams(100, 'CZK', '202001010004'), 'Zaplatit!', true, 'bitcoin', ['class' => 'btn btn-success']);
$r = $this->client->getPaymentButton(new CreatePaymentParams(100, 'CZK', '202001010004', self::getCreatePaymentCustomer()), 'Zaplatit!', true, 'bitcoin', ['class' => 'btn btn-success']);
self::assertStringContainsString('Zaplatit!', $r);
self::assertStringContainsString('class="tp-btn btn btn-success"', $r);
self::assertStringContainsString('data-payment-method="bitcoin"', $r);
Expand Down Expand Up @@ -109,7 +109,7 @@ public function testGetPaymentMethods(): void
]),
]));

$result = $this->client->getPaymentButtons(new CreatePaymentParams(100, 'CZK', '202001010005'));
$result = $this->client->getPaymentButtons(new CreatePaymentParams(100, 'CZK', '202001010005', self::getCreatePaymentCustomer()));

self::assertIsString($result);

Expand All @@ -132,23 +132,11 @@ public function testGetPaymentMethods(): void

public function testCreateApiPayment(): void
{
// Create entity with information about customer
$customer = new CreatePaymentCustomer(
'Mike',
'Smith',
'[email protected]',
// Phone number in international format max 15 numeric chars https://en.wikipedia.org/wiki/MSISDN
'420589687963',
// Create billing address
new Address('CZ', 'Prague', '123 00', 'Downstreet 5')
);

// Create payment (105.20 € with unique id uid123)
$createPayment = new CreatePaymentParams(100, 'CZK', 'uid123');
$createPayment = new CreatePaymentParams(100, 'CZK', 'uid123', self::getCreatePaymentCustomer());
$createPayment->setOrderId('15478');
$createPayment->setDescriptionForCustomer('Payment for items on example.com');
$createPayment->setDescriptionForMerchant('Payment from VIP customer XYZ');
$createPayment->setCustomer($customer);

$this->apiService->method('createPayment')->willReturn(
new CreatePaymentResponse(
Expand All @@ -163,4 +151,17 @@ public function testCreateApiPayment(): void

self::assertSame(CreatePaymentResponse::class, get_class($result));
}

private static function getCreatePaymentCustomer(): CreatePaymentCustomer
{
return new CreatePaymentCustomer(
'Mike',
'Smith',
'[email protected]',
// Phone number in international format max 15 numeric chars https://en.wikipedia.org/wiki/MSISDN
'420589687963',
// Create billing address
new Address('CZ', 'Prague', '123 00', 'Downstreet 5')
);
}
}

0 comments on commit b1be3f2

Please sign in to comment.