Skip to content

Commit

Permalink
Fix compatibility of RecurrentPaymentFactory (#76)
Browse files Browse the repository at this point in the history
closes #74
  • Loading branch information
PavelJurasek authored Oct 16, 2020
1 parent 49bdd37 commit ce828ec
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 25 deletions.
14 changes: 12 additions & 2 deletions src/Api/Entity/RecurrentPaymentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ class RecurrentPaymentFactory
'order_description',
'items',
'recurrence',
'callback',
];

/** @var string[] */
public static $requiredCallback = [
'return_url',
'notify_url',
];
Expand Down Expand Up @@ -65,6 +70,11 @@ public static function create($data, $validators = [])
throw new ValidationException('Missing keys "' . (implode(', ', $res)) . '""');
}

$res = Validator::validateRequired($data['callback'], self::$requiredCallback);
if ($res !== TRUE) {
throw new ValidationException('Missing keys "' . (implode(', ', $res)) . '" in callback definition');
}

// CHECK SCHEME DATA #####################

$res = Validator::validateOptional($data, array_merge(self::$required, self::$optional));
Expand Down Expand Up @@ -117,8 +127,8 @@ public static function create($data, $validators = [])
$recurrentPayment->setCurrency($data['currency']);
$recurrentPayment->setOrderNumber($data['order_number']);
$recurrentPayment->setOrderDescription($data['order_description']);
$recurrentPayment->setReturnUrl($data['return_url']);
$recurrentPayment->setNotifyUrl($data['notify_url']);
$recurrentPayment->setReturnUrl($data['callback']['return_url']);
$recurrentPayment->setNotifyUrl($data['callback']['notify_url']);

// ### ITEMS
foreach ($data['items'] as $param) {
Expand Down
78 changes: 58 additions & 20 deletions tests/cases/unit/Api/Entity/RecurrentPaymentFactory.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ test(function () {
'order_description' => 4,
'items' => 5,
'recurrence' => 6,
'return_url' => 7,
'notify_url' => 8,
'callback' => [
'return_url' => '7',
'notify_url' => '8',
],
];
$fields = [
'foo' => 9,
Expand All @@ -42,6 +44,24 @@ test(function () {
}, ValidationException::class, '%a%' . implode(', ', array_keys($fields)) . '%a%');
});

// Not allowed field
test(function () {
$required = [
'amount' => 1,
'currency' => 2,
'order_number' => 3,
'order_description' => 4,
'items' => 5,
'recurrence' => 6,
'callback' => [
'return_url' => '7',
],
];
Assert::throws(function () use ($required) {
RecurrentPaymentFactory::create($required);
}, ValidationException::class, 'Missing keys "notify_url" in callback definition');
});

// Simple payment
test(function () {
$nextYear = new DateTime('+1 year');
Expand Down Expand Up @@ -92,8 +112,10 @@ test(function () {
'additional_params' => [
['name' => 'invoicenumber', 'value' => '2015001003'],
],
'return_url' => 'http://www.eshop.cz/return',
'notify_url' => 'http://www.eshop.cz/notify',
'callback' => [
'return_url' => 'http://www.eshop.cz/return',
'notify_url' => 'http://www.eshop.cz/notify',
],
'lang' => 'cs',
];

Expand Down Expand Up @@ -123,8 +145,10 @@ test(function () {
'recurrence_period' => 7,
'recurrence_date_to' => date('Y-m-d'),
],
'return_url' => 6,
'notify_url' => 7,
'callback' => [
'return_url' => '6',
'notify_url' => '7',
],
];

Assert::throws(function () use ($data) {
Expand All @@ -147,8 +171,10 @@ test(function () {
'recurrence_period' => 7,
'recurrence_date_to' => date('Y-m-d'),
],
'return_url' => 6,
'notify_url' => 7,
'callback' => [
'return_url' => '6',
'notify_url' => '7',
],
];

Assert::throws(function () use ($data) {
Expand All @@ -173,8 +199,10 @@ test(function () {
'recurrence_period' => 7,
'recurrence_date_to' => date('Y-m-d'),
],
'return_url' => 6,
'notify_url' => 7,
'callback' => [
'return_url' => '6',
'notify_url' => '7',
],
];

try {
Expand All @@ -198,8 +226,10 @@ test(function () {
'recurrence_period' => 7,
'recurrence_date_to' => date('Y-m-d'),
],
'return_url' => 6,
'notify_url' => 7,
'callback' => [
'return_url' => '6',
'notify_url' => '7',
],
'x_unknown' => 1234,
'y_foobar' => 5678,
];
Expand Down Expand Up @@ -227,8 +257,10 @@ test(function () {
'recurrence_period' => 7,
'recurrence_date_to' => date('Y-m-d'),
],
'return_url' => 6,
'notify_url' => 7,
'callback' => [
'return_url' => '6',
'notify_url' => '7',
],
'eet' => [
'celk_trzba' => 200,
'zakl_dan1' => 80,
Expand Down Expand Up @@ -258,8 +290,10 @@ test(function () {
'recurrence_period' => 7,
'recurrence_date_to' => date('Y-m-d'),
],
'return_url' => 6,
'notify_url' => 7,
'callback' => [
'return_url' => '6',
'notify_url' => '7',
],
'eet' => [
'celk_trzba' => 110,
'zakl_dan1' => 80,
Expand Down Expand Up @@ -287,8 +321,10 @@ test(function () {
'recurrence_period' => 7,
'recurrence_date_to' => date('Y-m-d'),
],
'return_url' => 6,
'notify_url' => 7,
'callback' => [
'return_url' => '6',
'notify_url' => '7',
],
'eet' => [
'celk_trzba' => 174.0,
'zakl_dan1' => 143.80165289256,
Expand Down Expand Up @@ -318,8 +354,10 @@ test(function () {
'recurrence_period' => 7,
'recurrence_date_to' => date('Y-m-d'),
],
'return_url' => 6,
'notify_url' => 7,
'callback' => [
'return_url' => '6',
'notify_url' => '7',
],
'eet' => [
'celk_trzba' => 274.0,
'zakl_nepodl_dph' => 100.0,
Expand Down
6 changes: 3 additions & 3 deletions tests/cases/unit/Service/AbstractService.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ test(function () {
return $request;
});

$service = Mockery::mock(DummyService::class, [$client]);
$service = new DummyService($client);

/** @var Request $request */
$request = $service->makeRequest('GET', 'foobar');
Expand All @@ -76,7 +76,7 @@ test(function () {
return $request;
});

$service = Mockery::mock(DummyService::class, [$client]);
$service = new DummyService($client);
$data = ['foo' => 1, 'bar' => 2];

/** @var Request $request */
Expand All @@ -95,7 +95,7 @@ test(function () {
return $request;
});

$service = Mockery::mock(DummyService::class, [$client]);
$service = new DummyService($client);

Assert::throws(function () use ($service) {
$service->makeRequest('FUCK', 'foobar');
Expand Down

0 comments on commit ce828ec

Please sign in to comment.