Skip to content

Commit ff97b0c

Browse files
authored
Merge pull request #4 from getsolaris/feat/webhook
webhook
2 parents 3e98878 + 30a1eba commit ff97b0c

8 files changed

+113
-5
lines changed

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,29 @@ Basic 인증 방식은 `{SECRET_KEY}:` 를 Base64 인코딩 한 값을 사용합
7676

7777
[예제 보기](examples/PROMOTION.md)
7878

79+
## [웹훅 (Webhook) 연동하기](https://docs.tosspayments.com/guides/webhook#%EC%9B%B9%ED%9B%85webhook-%EC%97%B0%EB%8F%99%ED%95%98%EA%B8%B0)
80+
81+
웹훅을 사용하기 전에 토스페이먼츠 개발자센터 웹훅 페이지에서 웹훅을 등록해주세요.
82+
83+
웹훅을 이용하기 전에 `config/tosspayments.php` 파일에서 `webhook` 설정을 확인해주세요.
84+
85+
```
86+
'webhook' => [
87+
'handler' => [
88+
'controller' => \App\Http\Controllers\WebhookController::class,
89+
'method' => '__invoke',
90+
],
91+
],
92+
```
93+
94+
`handler` 설정을 변경하여 웹훅을 처리할 컨트롤러와 메소드를 지정할 수 있습니다.
95+
96+
또한 아래의 명령어를 실행하여 기본 라우트 설정값인 `url/webhooks/tosspayments` 를 변경할 수 있습니다.
97+
98+
```bash
99+
php artisan vendor:publish --provider="Getsolaris\LaravelTossPayments\TossPaymentsServiceProvider" --tag="webhook"
100+
```
101+
79102

80103
## [테스트 코드 사용하기](https://docs.tosspayments.com/reference/error-codes#%EC%97%90%EB%9F%AC-%EC%BD%94%EB%93%9C)
81104

config/toss-payments.php renamed to config/tosspayments.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,11 @@
77
'secret_key' => env('TOSS_PAYMENTS_SECRET_KEY'),
88
'content_type' => 'application/json',
99
'accept' => 'application/json',
10+
11+
'webhook' => [
12+
'handler' => [
13+
'controller' => \App\Http\Controllers\WebhookController::class,
14+
'method' => '__invoke',
15+
],
16+
],
1017
];

routes/tosspayments.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
use Getsolaris\LaravelTossPayments\Handlers\TossPaymentsWebhookHandler;
4+
5+
Route::post('webhooks/tosspayments', [TossPaymentsWebhookHandler::class, '__invoke']);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Getsolaris\LaravelTossPayments\Exceptions;
4+
5+
class WebhookPayloadException extends \Exception
6+
{
7+
protected $message = 'Webhook 의 Payload 정보가 올바르지 않습니다.';
8+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Getsolaris\LaravelTossPayments\Handlers;
4+
5+
use Getsolaris\LaravelTossPayments\Exceptions\WebhookPayloadException;
6+
7+
class TossPaymentsWebhookHandler
8+
{
9+
/**
10+
* @throws WebhookPayloadException
11+
* @throws \ReflectionException
12+
*/
13+
public function __invoke()
14+
{
15+
$payload = json_decode(request()->getContent(), true);
16+
17+
if (! $payload) {
18+
throw new WebhookPayloadException();
19+
}
20+
21+
$target = new \ReflectionClass(config('tosspayments.webhook.handler.controller'));
22+
$method = config('tosspayments.webhook.handler.method');
23+
24+
return $target->newInstance()->$method($payload);
25+
}
26+
}

src/TossPayments.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ public function __call($name, $arguments)
9191
*/
9292
protected function initializeApiUrl(): static
9393
{
94-
$this->endpoint = config('toss-payments.endpoint');
95-
$this->version = config('toss-payments.version');
94+
$this->endpoint = config('tosspayments.endpoint');
95+
$this->version = config('tosspayments.version');
9696
$this->url = $this->endpoint.'/'.$this->version;
9797

9898
return $this;
@@ -103,8 +103,8 @@ protected function initializeApiUrl(): static
103103
*/
104104
protected function initializeKeys(): static
105105
{
106-
$this->clientKey = config('toss-payments.client_key');
107-
$this->secretKey = config('toss-payments.secret_key');
106+
$this->clientKey = config('tosspayments.client_key');
107+
$this->secretKey = config('tosspayments.secret_key');
108108

109109
return $this;
110110
}

src/TossPaymentsServiceProvider.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class TossPaymentsServiceProvider extends ServiceProvider
99
/**
1010
* @var string
1111
*/
12-
private string $name = 'toss-payments';
12+
private string $name = 'tosspayments';
1313

1414
/**
1515
* @return void
@@ -31,5 +31,11 @@ public function boot(): void
3131
$this->publishes([
3232
__DIR__."/../config/{$this->name}.php" => config_path($this->name.'.php'),
3333
], 'config');
34+
35+
$this->publishes([
36+
__DIR__."/../routes/{$this->name}.php" => base_path('routes/'.$this->name.'.php'),
37+
], 'webhook');
38+
39+
$this->loadRoutesFrom(__DIR__."/../routes/{$this->name}.php");
3440
}
3541
}

tests/WebhookTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Getsolaris\LaravelTossPayments\tests;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
class WebhookTest extends TestCase
8+
{
9+
public function testWebhook(): void
10+
{
11+
$payload = $this->getData();
12+
13+
$this->assertIsArray($payload);
14+
$this->assertArrayHasKey('eventType', $payload);
15+
$this->assertArrayHasKey('createdAt', $payload);
16+
$this->assertArrayHasKey('data', $payload);
17+
$this->assertIsArray($payload['data']);
18+
}
19+
20+
/**
21+
* @return array
22+
*/
23+
private function getData(): array
24+
{
25+
$payload = '{
26+
"eventType": "PAYMENT_STATUS_CHANGED",
27+
"createdAt": "2022-01-01T00:00:00.000000",
28+
"data": {}
29+
}';
30+
31+
return json_decode($payload, true);
32+
}
33+
}

0 commit comments

Comments
 (0)