Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add refundStarPayment method #84

Merged
merged 1 commit into from
Jun 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading