Skip to content

Commit

Permalink
Add refundStarPayment method (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik authored Jun 29, 2024
1 parent e2128fc commit 874edb4
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
- New #81: Add `sendInvoice` method.
- New #82: Add `createInvoiceLink` method.
- New #83: Add `answerShippingQuery` and `answerPreCheckoutQuery` methods, and `ShippingOption` type.
- New #84: Add `refundStarPayment` method.
- Chg #24: Move update methods to `Vjik\TelegramBot\Api\Method\Update` namespace, and update types to
`Vjik\TelegramBot\Api\Type\Update` namespace.
- Chg #30: Remove `TelegramRequestWithFilesInterface`.
Expand Down
45 changes: 45 additions & 0 deletions src/Method/Payment/RefundStarPayment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?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#refundstarpayment
*/
final readonly class RefundStarPayment implements TelegramRequestWithResultPreparingInterface
{
public function __construct(
private int $userId,
private string $telegramPaymentChargeId,
) {
}

public function getHttpMethod(): HttpMethod
{
return HttpMethod::POST;
}

public function getApiMethod(): string
{
return 'refundStarPayment';
}

public function getData(): array
{
return [
'user_id' => $this->userId,
'telegram_payment_charge_id' => $this->telegramPaymentChargeId,
];
}

public function prepareResult(mixed $result): true
{
ValueHelper::assertTrueResult($result);
return $result;
}
}
13 changes: 13 additions & 0 deletions src/TelegramBotApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
use Vjik\TelegramBot\Api\Method\Payment\AnswerShippingQuery;
use Vjik\TelegramBot\Api\Method\Payment\CreateInvoiceLink;
use Vjik\TelegramBot\Api\Method\Payment\GetStarTransactions;
use Vjik\TelegramBot\Api\Method\Payment\RefundStarPayment;
use Vjik\TelegramBot\Api\Method\Payment\SendInvoice;
use Vjik\TelegramBot\Api\Method\PinChatMessage;
use Vjik\TelegramBot\Api\Method\PromoteChatMember;
Expand Down Expand Up @@ -1261,6 +1262,18 @@ public function promoteChatMember(
);
}

/**
* @see https://core.telegram.org/bots/api#refundstarpayment
*
* @psalm-suppress MixedInferredReturnType,MixedReturnStatement
*/
public function refundStarPayment(int $userId, string $telegramPaymentChargeId): FailResult|true
{
return $this->send(
new RefundStarPayment($userId, $telegramPaymentChargeId)
);
}

/**
* @see https://core.telegram.org/bots/api#reopenforumtopic
*
Expand Down
36 changes: 36 additions & 0 deletions tests/Method/Payment/RefundStarPaymentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Vjik\TelegramBot\Api\Tests\Method\Payment;

use PHPUnit\Framework\TestCase;
use Vjik\TelegramBot\Api\Method\Payment\RefundStarPayment;
use Vjik\TelegramBot\Api\Request\HttpMethod;

final class RefundStarPaymentTest extends TestCase
{
public function testBase(): void
{
$method = new RefundStarPayment(1, 'test');

$this->assertSame(HttpMethod::POST, $method->getHttpMethod());
$this->assertSame('refundStarPayment', $method->getApiMethod());
$this->assertSame(
[
'user_id' => 1,
'telegram_payment_charge_id' => 'test',
],
$method->getData()
);
}

public function testPrepareResult(): void
{
$method = new RefundStarPayment(1, 'test');

$preparedResult = $method->prepareResult(true);

$this->assertTrue($preparedResult);
}
}
12 changes: 12 additions & 0 deletions tests/TelegramBotApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1130,6 +1130,18 @@ public function testPromoteChatMember(): void
$this->assertTrue($result);
}

public function testRefundStarPayment(): void
{
$api = $this->createApi([
'ok' => true,
'result' => true,
]);

$result = $api->refundStarPayment(1, 'test');

$this->assertTrue($result);
}

public function testReopenForumTopic(): void
{
$api = $this->createApi([
Expand Down

0 comments on commit 874edb4

Please sign in to comment.