-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
answerShippingQuery
, answerPreCheckoutQuery
and `ShippingOpti…
…on` (#83)
- Loading branch information
Showing
9 changed files
with
362 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Vjik\TelegramBot\Api\Method\Payment; | ||
|
||
use Vjik\TelegramBot\Api\ParseResult\ValueHelper; | ||
use Vjik\TelegramBot\Api\Request\HttpMethod; | ||
use Vjik\TelegramBot\Api\Request\TelegramRequestWithResultPreparingInterface; | ||
|
||
/** | ||
* @see https://core.telegram.org/bots/api#answerprecheckoutquery | ||
*/ | ||
final readonly class AnswerPreCheckoutQuery implements TelegramRequestWithResultPreparingInterface | ||
{ | ||
public function __construct( | ||
private string $preCheckoutQueryId, | ||
private bool $ok, | ||
private ?string $errorMessage = null, | ||
) { | ||
} | ||
|
||
public function getHttpMethod(): HttpMethod | ||
{ | ||
return HttpMethod::POST; | ||
} | ||
|
||
public function getApiMethod(): string | ||
{ | ||
return 'answerPreCheckoutQuery'; | ||
} | ||
|
||
public function getData(): array | ||
{ | ||
return array_filter( | ||
[ | ||
'pre_checkout_query_id' => $this->preCheckoutQueryId, | ||
'ok' => $this->ok, | ||
'error_message' => $this->errorMessage, | ||
], | ||
static fn(mixed $value): bool => $value !== null, | ||
); | ||
} | ||
|
||
public function prepareResult(mixed $result): true | ||
{ | ||
ValueHelper::assertTrueResult($result); | ||
return $result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Vjik\TelegramBot\Api\Method\Payment; | ||
|
||
use Vjik\TelegramBot\Api\ParseResult\ValueHelper; | ||
use Vjik\TelegramBot\Api\Request\HttpMethod; | ||
use Vjik\TelegramBot\Api\Request\TelegramRequestWithResultPreparingInterface; | ||
use Vjik\TelegramBot\Api\Type\Payment\ShippingOption; | ||
|
||
/** | ||
* @see https://core.telegram.org/bots/api#answershippingquery | ||
*/ | ||
final readonly class AnswerShippingQuery implements TelegramRequestWithResultPreparingInterface | ||
{ | ||
/** | ||
* @param ShippingOption[]|null $shippingOptions | ||
*/ | ||
public function __construct( | ||
private string $shippingQueryId, | ||
private bool $ok, | ||
private ?array $shippingOptions = null, | ||
private ?string $errorMessage = null, | ||
) { | ||
} | ||
|
||
public function getHttpMethod(): HttpMethod | ||
{ | ||
return HttpMethod::POST; | ||
} | ||
|
||
public function getApiMethod(): string | ||
{ | ||
return 'answerShippingQuery'; | ||
} | ||
|
||
public function getData(): array | ||
{ | ||
return array_filter( | ||
[ | ||
'shipping_query_id' => $this->shippingQueryId, | ||
'ok' => $this->ok, | ||
'shipping_options' => $this->shippingOptions === null ? null : array_map( | ||
static fn(ShippingOption $option) => $option->toRequestArray(), | ||
$this->shippingOptions, | ||
), | ||
'error_message' => $this->errorMessage, | ||
], | ||
static fn(mixed $value): bool => $value !== null, | ||
); | ||
} | ||
|
||
public function prepareResult(mixed $result): true | ||
{ | ||
ValueHelper::assertTrueResult($result); | ||
return $result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Vjik\TelegramBot\Api\Type\Payment; | ||
|
||
/** | ||
* @see https://core.telegram.org/bots/api#shippingoption | ||
*/ | ||
final readonly class ShippingOption | ||
{ | ||
/** | ||
* @param LabeledPrice[] $prices | ||
*/ | ||
public function __construct( | ||
public string $id, | ||
public string $title, | ||
public array $prices | ||
) { | ||
} | ||
|
||
public function toRequestArray(): array | ||
{ | ||
return [ | ||
'id' => $this->id, | ||
'title' => $this->title, | ||
'prices' => array_map( | ||
static fn(LabeledPrice $price) => $price->toRequestArray(), | ||
$this->prices | ||
), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Vjik\TelegramBot\Api\Tests\Method\Payment; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Vjik\TelegramBot\Api\Method\Payment\AnswerPreCheckoutQuery; | ||
use Vjik\TelegramBot\Api\Request\HttpMethod; | ||
|
||
final class AnswerPreCheckoutQueryTest extends TestCase | ||
{ | ||
public function testBase(): void | ||
{ | ||
$method = new AnswerPreCheckoutQuery( | ||
'qid', | ||
true, | ||
); | ||
|
||
$this->assertSame(HttpMethod::POST, $method->getHttpMethod()); | ||
$this->assertSame('answerPreCheckoutQuery', $method->getApiMethod()); | ||
$this->assertSame( | ||
[ | ||
'pre_checkout_query_id' => 'qid', | ||
'ok' => true, | ||
], | ||
$method->getData(), | ||
); | ||
} | ||
|
||
public function testFull(): void | ||
{ | ||
$method = new AnswerPreCheckoutQuery( | ||
'qid', | ||
true, | ||
'error message', | ||
); | ||
|
||
$this->assertSame(HttpMethod::POST, $method->getHttpMethod()); | ||
$this->assertSame('answerPreCheckoutQuery', $method->getApiMethod()); | ||
$this->assertSame( | ||
[ | ||
'pre_checkout_query_id' => 'qid', | ||
'ok' => true, | ||
'error_message' => 'error message', | ||
], | ||
$method->getData(), | ||
); | ||
} | ||
|
||
public function testPrepareResult(): void | ||
{ | ||
$method = new AnswerPreCheckoutQuery( | ||
'qid', | ||
true, | ||
); | ||
|
||
$preparedResult = $method->prepareResult(true); | ||
|
||
$this->assertTrue($preparedResult); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Vjik\TelegramBot\Api\Tests\Method\Payment; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Vjik\TelegramBot\Api\Method\Payment\AnswerShippingQuery; | ||
use Vjik\TelegramBot\Api\Request\HttpMethod; | ||
use Vjik\TelegramBot\Api\Type\Payment\ShippingOption; | ||
|
||
final class AnswerShippingQueryTest extends TestCase | ||
{ | ||
public function testBase(): void | ||
{ | ||
$method = new AnswerShippingQuery( | ||
'qid', | ||
true, | ||
); | ||
|
||
$this->assertSame(HttpMethod::POST, $method->getHttpMethod()); | ||
$this->assertSame('answerShippingQuery', $method->getApiMethod()); | ||
$this->assertSame( | ||
[ | ||
'shipping_query_id' => 'qid', | ||
'ok' => true, | ||
], | ||
$method->getData(), | ||
); | ||
} | ||
|
||
public function testFull(): void | ||
{ | ||
$option = new ShippingOption('', '', []); | ||
$method = new AnswerShippingQuery( | ||
'qid', | ||
true, | ||
[$option], | ||
'error message', | ||
); | ||
|
||
$this->assertSame(HttpMethod::POST, $method->getHttpMethod()); | ||
$this->assertSame('answerShippingQuery', $method->getApiMethod()); | ||
$this->assertSame( | ||
[ | ||
'shipping_query_id' => 'qid', | ||
'ok' => true, | ||
'shipping_options' => [$option->toRequestArray()], | ||
'error_message' => 'error message', | ||
], | ||
$method->getData(), | ||
); | ||
} | ||
|
||
public function testPrepareResult(): void | ||
{ | ||
$method = new AnswerShippingQuery( | ||
'qid', | ||
true, | ||
); | ||
|
||
$preparedResult = $method->prepareResult(true); | ||
|
||
$this->assertTrue($preparedResult); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.