-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from io-digital/handle-webhooks
WIP: Handle webhooks
- Loading branch information
Showing
9 changed files
with
180 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,35 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
class CreatePaymentEventsTable extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::create('payment_events', function (Blueprint $table) { | ||
$table->bigIncrements('id'); | ||
$table->string('type'); | ||
$table->string('action')->nullable(); | ||
$table->text('payload'); | ||
$table->timestamps(); | ||
$table->softDeletes(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::dropIfExists('payment_events'); | ||
} | ||
} |
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,7 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Facades\Config; | ||
use Illuminate\Support\Facades\Route; | ||
use IoDigital\PeachPayment\Http\Controllers\WebhookController; | ||
|
||
Route::post(Config::get('peachpayment.webhook_url'), [WebhookController::class, 'index'])->name('peachpayment.index'); |
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,13 @@ | ||
<?php | ||
|
||
namespace IoDigital\PeachPayment\Http\Controllers; | ||
|
||
use Illuminate\Foundation\Auth\Access\AuthorizesRequests; | ||
use Illuminate\Foundation\Bus\DispatchesJobs; | ||
use Illuminate\Foundation\Validation\ValidatesRequests; | ||
use Illuminate\Routing\Controller as BaseController; | ||
|
||
class Controller extends BaseController | ||
{ | ||
use AuthorizesRequests, DispatchesJobs, ValidatesRequests; | ||
} |
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,54 @@ | ||
<?php | ||
|
||
namespace IoDigital\PeachPayment\Http\Controllers; | ||
|
||
use Illuminate\Http\Request; | ||
use Illuminate\Http\Response; | ||
use Illuminate\Support\Facades\Config; | ||
use IoDigital\PeachPayment\Models\PaymentEvent; | ||
|
||
class WebhookController extends Controller | ||
{ | ||
protected $model; | ||
|
||
public function __construct(PaymentEvent $model) | ||
{ | ||
$this->model = $model; | ||
} | ||
|
||
public function index(Request $request) | ||
{ | ||
$event = new PaymentEvent(); | ||
$body = $this->decryptMessage($request); | ||
$payload = $body['payload']; | ||
|
||
$excludedValues = Config::get('peachpayment.webhook_excluded'); | ||
|
||
foreach ($excludedValues as $exclude) { | ||
unset($payload[$exclude]); | ||
} | ||
|
||
$event->type = $body['type']; | ||
$event->action = $body['action']; | ||
$event->payload = $payload; | ||
$event->save(); | ||
|
||
return response([], Response::HTTP_OK); | ||
} | ||
|
||
private function decryptMessage(Request $request) | ||
{ | ||
$keyFromConfiguration = Config::get('peachpayment.webhook_secret_key'); | ||
$ivFromHeader = $request->header('X-Initialization-Vector'); | ||
$authTagFromHeader = $request->header('X-Authentication-Tag'); | ||
|
||
$key = hex2bin($keyFromConfiguration); | ||
$iv = hex2bin($ivFromHeader); | ||
$authTag = hex2bin($authTagFromHeader); | ||
$cipherText = hex2bin($request->getContent()); | ||
|
||
$result = openssl_decrypt($cipherText, 'aes-256-gcm', $key, OPENSSL_RAW_DATA, $iv, $authTag) | ||
|
||
return json_decode($result, true); | ||
} | ||
} |
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,21 @@ | ||
<?php | ||
|
||
namespace IoDigital\PeachPayment\Models; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\SoftDeletes; | ||
|
||
class PaymentEvent extends Model | ||
{ | ||
use SoftDeletes; | ||
|
||
protected $fillable = [ | ||
'type', | ||
'action', | ||
'payload', | ||
]; | ||
|
||
protected $casts = [ | ||
'payload' => 'array', | ||
]; | ||
} |
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,31 @@ | ||
<?php | ||
|
||
namespace IoDigital\PeachPayment\Tests\Unit; | ||
|
||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use IoDigital\PeachPayment\Tests\TestCase; | ||
|
||
class WebhookTest extends TestCase | ||
{ | ||
use RefreshDatabase; | ||
|
||
/** | ||
* Setup the test environment. | ||
*/ | ||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->artisan('migrate'); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_can_receive_an_event() | ||
{ | ||
$response = $this->postJson(route('peachpayment.index'), []); | ||
|
||
$response->assertOk(); | ||
} | ||
} |