Skip to content
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ To install the `MoneyMan` package, follow these steps:
MONEYMAN_CHAPA_SECRET_KEY=
MONEYMAN_CHAPA_BASE_URL=
MONEYMAN_CHAPA_CALLBACK_URL=
MONEYMAN_CHAPA_WEBHOOK_SECRET=

MONEYMAN_SANTIMPAY_BASE_URL=
MONEYMAN_SANTIMPAY_PUBLIC_KEY=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ public function up()
Schema::create('webhook_events', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->string('provider');
$table->string('is_success')->default(false);
$table->boolean('is_success')->default(false);
$table->string('tx_ref')->nullable();
$table->string('provider_ref')->nullable();
$table->string('status');
$table->decimal('amount');
$table->decimal('charge')->nullable();
$table->string('currency', 10)->default('ETB');
$table->json('data')->nullable();
$table->unique(['event_type', 'tx_ref']);
$table->unique(['provider', 'tx_ref']);
$table->timestamps();
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/Actions/WebhookHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function handle(Request $request, string $provider): WebhookEvent

$event = $driver->parse($request);

return WebhookEvent::firstOrCreate([
return WebhookEvent::updateOrCreate([
'provider' => $event->provider(),
'is_success' => $event->isSuccess(),
'tx_ref' => $event->getReference(),
Expand Down
2 changes: 2 additions & 0 deletions src/Models/WebhookEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class WebhookEvent extends Model

protected $table = 'webhook_events';

protected $guarded = [];

protected $casts = [
'provider' => Provider::class,
'data' => 'array',
Expand Down
2 changes: 0 additions & 2 deletions src/MoneyManServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Vptrading\MoneyMan;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Console\AboutCommand;
use Illuminate\Support\ServiceProvider;
use Vptrading\MoneyMan\Contracts\WebhookRegistry;
Expand All @@ -17,7 +16,6 @@ class MoneyManServiceProvider extends ServiceProvider
{
public function boot(): void
{
Model::unguard();
AboutCommand::add('MoneyMan', fn () => [
'Version' => '1.1.0',
]);
Expand Down
2 changes: 1 addition & 1 deletion src/Providers/Chapa/WebhookDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class WebhookDriver implements WebhookDriverInterface
{
public function verify(Request $request): bool
{
$secret = config('chapa.webhook_secret');
$secret = config('moneyman.providers.chapa.webhook_secret');

$hash = hash_hmac('sha256', $request->getContent(), $secret);

Expand Down
2 changes: 1 addition & 1 deletion src/Providers/Telebirr/Telebirr.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function verify(string $transactionId): PaymentVerifyResponse
'biz_content' => [
'appid' => config('moneyman.providers.telebirr.merchant_app_id'),
'merch_code' => config('moneyman.providers.telebirr.short_code'),
'merch_order_id' => 'glRDRSe9U6',
'merch_order_id' => $transactionId,
],
];

Expand Down
2 changes: 1 addition & 1 deletion src/Providers/Telebirr/WebhookEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function provider(): string

public function isSuccess(): bool
{
if ($this->payload['trans_currency'] !== 'Completed') {
if ($this->payload['trade_status'] !== 'Completed') {
return false;
}

Expand Down
44 changes: 43 additions & 1 deletion tests/Feature/Http/Controllers/WebhookControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
declare(strict_types=1);

use Firebase\JWT\JWT;
use Vptrading\MoneyMan\Exceptions\InvalidSignatureException;

it('stores chapa webhook events', function (): void {
$this->withoutExceptionHandling();
config()->set('chapa.webhook_secret', 'test_secret');
config()->set('moneyman.providers.chapa.webhook_secret', 'test_secret');
$payload = [
'event' => 'charge.success',
'first_name' => 'John',
Expand Down Expand Up @@ -129,3 +130,44 @@
'currency' => 'ETB',
]);
});

it('rejects chapa webhook events with invalid signature', function (): void {
$this->withoutExceptionHandling();
config()->set('chapa.webhook_secret', 'test_secret');

$payload = [
'event' => 'charge.success',
'tx_ref' => 'TX-INVALID',
'reference' => 'REF-INVALID',
'amount' => '100.00',
'charge' => '3.00',
'currency' => 'ETB',
'status' => 'success',
];

expect(fn () => $this->postJson(route('moneyman.webhook', [
'provider' => 'chapa',
]), $payload, ['x-chapa-signature' => 'bad-signature']))
->toThrow(InvalidSignatureException::class);

$this->assertDatabaseCount('webhook_events', 0);
});

it('rejects santimpay webhook events with invalid signature', function (): void {
$this->withoutExceptionHandling();

$payload = [
'txnId' => 'txn-invalid',
'refId' => 'ref-invalid',
'amount' => '1',
'currency' => 'ETB',
'status' => 'COMPLETED',
];

expect(fn () => $this->postJson(route('moneyman.webhook', [
'provider' => 'santimpay',
]), $payload, ['signed-token' => 'bad-signature']))
->toThrow(InvalidSignatureException::class);

$this->assertDatabaseCount('webhook_events', 0);
});
20 changes: 20 additions & 0 deletions tests/Unit/MoneyManManagerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

use Vptrading\MoneyMan\Enums\Provider as ProviderEnum;
use Vptrading\MoneyMan\MoneyMan;
use Vptrading\MoneyMan\Providers\Chapa\Chapa;
use Vptrading\MoneyMan\Providers\SantimPay\SantimPay;
use Vptrading\MoneyMan\Providers\Telebirr\Telebirr;

it('resolves providers from enum', function (): void {
expect(MoneyMan::provider(ProviderEnum::Chapa))->toBeInstanceOf(Chapa::class);
expect(MoneyMan::provider(ProviderEnum::SantimPay))->toBeInstanceOf(SantimPay::class);
expect(MoneyMan::provider(ProviderEnum::Telebirr))->toBeInstanceOf(Telebirr::class);
});

it('throws for unsupported provider', function (): void {
expect(fn () => MoneyMan::provider('unsupported'))
->toThrow(InvalidArgumentException::class);
});