Skip to content

Commit

Permalink
Changes
Browse files Browse the repository at this point in the history
- Fixes for webhook signature validation
- Update Laravel.md
  • Loading branch information
Medboubazine committed Mar 13, 2024
1 parent fbf424c commit f600cc5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 24 deletions.
29 changes: 7 additions & 22 deletions docs/frameworks/Laravel.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ Route::post('chargilypay/webhook', [ChargilyPayController::class, "webhook"])->n
### 4. Create controler

```bash
php artisan make:controler ChargilyPayController
php artisan make:controller ChargilyPayController
```

- Attach the following methods to the controller
Expand Down Expand Up @@ -137,32 +137,17 @@ class ChargilyPayController extends Controller
$user = auth()->user();
$checkout_id = $request->input("checkout_id");
$checkout = $this->chargilyPayInstance()->checkouts()->get($checkout_id);
$payment = null;

if ($checkout) {
$metadata = $checkout->getMetadata();
$payment = \App\Models\ChargilyPayment::find($metadata['payment_id']);

if ($payment) {
if ($checkout->getStatus() === "paid") {
//update payment status in database
$payment->status = "paid";
$payment->update();
/////
///// Confirm your order
/////

} else if ($checkout->getStatus() === "failed" or $checkout->getStatus() === "canceled") {
//update payment status in database
$payment->status = "failed";
$payment->update();
/////
///// Cancel your order
/////

}
}
////
//// Is not recomended to process payment in back page / success or fail page
//// Doing payment processing in webhook for best practices
////
}
return redirect("redirect to your order page");
dd($checkout,$payment);
}
/**
* This action will be processed in the background
Expand Down
5 changes: 3 additions & 2 deletions src/Api/Webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ public function get(): ?WebhookElement
{
$headers = getallheaders();
$signature = isset($headers['signature']) ? $headers['signature'] : "";
$signature = (empty($signature) and isset($headers['Signature'])) ? $headers['Signature'] : "";

$payload = file_get_contents('php://input');
$computed = hash_hmac('sha256', $payload, $this->credentials->secret);

if (hash_equals($signature, $computed)) {
$event = json_decode($payload, true);

$event = json_decode($payload, true);
return $this->newElement($event);
}
return null;
Expand Down

0 comments on commit f600cc5

Please sign in to comment.