Skip to content

Commit 812e48e

Browse files
author
jeybin
committed
Webhooks fixes
1 parent 2e85477 commit 812e48e

File tree

4 files changed

+50
-21
lines changed

4 files changed

+50
-21
lines changed

NgeniusWebhooks/Purchase/HandleNgeniusPurchaseDeclined.php

-3
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,5 @@ public function handle()
4242
* Passing the payload/response from the webhook to the
4343
* CreatedHandler function in CreateChargeController
4444
*/
45-
46-
debug(">>>> inside purchase declined job");
47-
debug($this->webhookCall);
4845
}
4946
}

src/App/Exceptions/NgeniusWebhookExceptions.php

+13-3
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@ class NgeniusWebhookExceptions extends Exception
1010
{
1111
public static function missingSignature()
1212
{
13-
return new static('The request did not contain a header named `X-NGENIUS-Webhook-Signature`.');
13+
return new static('The request did not contain a header named `'.config('ngenius-config.webhook-header').'`.');
1414
}
1515

1616
public static function invalidSignature($signature)
1717
{
18-
return new static("The signature `{$signature}` found in the header named `X-NGENIUS-Webhook-Signature` is invalid. Make sure that the `ngenius.webhookSecret` config key is set to the value you found on the ngenius dashboard. If you are caching your config try running `php artisan clear:cache` to resolve the problem.");
18+
return new static("The signature `{$signature}` found in the header named `".config('ngenius-config.webhook-header')."` is invalid. Make sure that the `ngenius.webhookSecret` config key is set to the value you found on the ngenius dashboard. If you are caching your config try running `php artisan clear:cache` to resolve the problem.");
1919
}
2020

2121
public static function sharedSecretNotSet()
2222
{
23-
return new static('The ngenius Commerce webhook shared secret is not set. Make sure that the `ngenius.webhookSecret` config key is set to the value you found on the ngenius dashboard.');
23+
return new static('The ngenius Commerce webhook shared secret is not set. Make sure that the `ngenius.webhook-secret` config key is set to the value you found on the ngenius dashboard.');
2424
}
2525

2626
public static function jobClassDoesNotExist(string $jobClass, NgeniusGatewayWehooks $webhookCall)
@@ -33,6 +33,16 @@ public static function missingType()
3333
return new static('The webhook call did not contain a type. Valid ngenius Commerce webhook calls should always contain a type.');
3434
}
3535

36+
public static function missingModel()
37+
{
38+
return new static('The model for webhook is not configured.');
39+
}
40+
41+
public static function emptyPayload()
42+
{
43+
return new static('The webhook payload is empty.');
44+
}
45+
3646
public function render($request)
3747
{
3848
return response(['code'=>400,

src/App/Http/Controllers/NgeniusWebhookController.php

+28-12
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Exception;
77
use Illuminate\Http\Request;
88
use Illuminate\Routing\Controller;
9+
use Jeybin\Networkintl\App\Exceptions\NgeniusWebhookExceptions;
910
use Jeybin\Networkintl\App\Http\Middleware\VerifyWebhookSignature;
1011

1112
class NgeniusWebhookController extends Controller
@@ -21,21 +22,31 @@ public function __invoke(Request $request){
2122
* The webhook model where the webhook
2223
* datas to be inserted
2324
*/
24-
$model = config('ngenius-config.webhook-model');
25+
26+
27+
$model = config('ngenius-config.webhook-model');
28+
29+
if(empty($model)){
30+
throw NgeniusWebhookExceptions::missingModel();
31+
}
2532

2633
$payload = $request->all();
2734

28-
if($payload){
29-
throwNgeniusPackageResponse('Empty event data',null,422);
35+
if(empty($payload)){
36+
throw NgeniusWebhookExceptions::emptyPayload();
3037
}
3138

39+
/**
40+
* Converting payload to array
41+
*/
3242
$payload = (!is_string($payload)) ? $payload : json_decode(json_encode($payload),true);
3343

3444
$currency = config('ngenius-config.merchant-currency');
3545
$reference = $email = $orderDetails = $merchantOrderReference = null;
3646
$amount = 0;
3747

3848
if(!empty($payload['order'])){
49+
3950
$orderDetails = $payload['order'];
4051

4152
if(!empty($orderDetails['reference'])){
@@ -45,27 +56,32 @@ public function __invoke(Request $request){
4556
}
4657

4758
if(!empty($orderDetails['amount'])){
48-
$currency = (string)$orderDetails['currencyCode'] ?? config('ngenius-config.merchant-currency');
49-
$amount = (integer)$orderDetails['amount']['value'] ?? 0;
59+
$currency = (string)(!empty($orderDetails['currencyCode'])) ? $orderDetails['currencyCode'] : config('ngenius-config.merchant-currency');
60+
$amount = (integer)(!empty($orderDetails['amount']['value'])) ? $orderDetails['amount']['value'] : 0;
5061
if($amount > 0){
5162
$amount = $amount/100;
5263
}
5364
}
54-
55-
5665
}
5766

67+
/**
68+
* Event id
69+
*/
70+
$eventId = !empty($payload['eventId']) ? $payload['eventId'] : null;
5871

59-
$insertData = ['event_id'=>$payload['eventId'] ?? null,
60-
'event_name'=>$payload['eventName'] ?? null,
61-
'order_data'=>json_encode($payload['order']) ?? null,
72+
/**
73+
* Event name from the order
74+
*/
75+
$eventName = !empty($payload['eventName']) ? $payload['eventName'] : null;
76+
77+
$insertData = ['event_id'=>$eventId,
78+
'event_name'=>$eventName,
6279
'order_reference'=>$reference,
6380
'merchant_order_reference'=>$merchantOrderReference,
6481
'email' => $email,
6582
'currency' => $currency,
6683
'amount' => $amount,
67-
'payload' => $payload];
68-
84+
'payload' => json_encode($payload)];
6985

7086
$ngeniusWebhookCall = $model::create($insertData);
7187

src/App/Models/NgeniusGatewayWehooks.php

+9-3
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function process(){
4444
* order response
4545
* eg : if PURCHASE_DECLINED -> event will be ngenius::PURCHASE_DECLINED
4646
*/
47-
// event("ngenius::{$this->type}", $this);
47+
// event("ngenius::{$this->event_name}", $this);
4848

4949
/**
5050
* Determining the job classes
@@ -56,7 +56,7 @@ public function process(){
5656
* return null or if the class doesnt exists it will
5757
* return exception or else dispatch the job
5858
*/
59-
$jobClass = $this->determineJobClass($this->type);
59+
$jobClass = $this->determineJobClass($this->event_name);
6060

6161
if ($jobClass === '') {
6262
return;
@@ -66,7 +66,11 @@ public function process(){
6666
throw NgeniusWebhookExceptions::jobClassDoesNotExist($jobClass, $this);
6767
}
6868

69-
dispatch(new $jobClass($this));
69+
try{
70+
dispatch(new $jobClass($this));
71+
}catch(Exception $e){
72+
$this->saveException($e);
73+
}
7074
}
7175

7276
public function saveException(Exception $exception)
@@ -85,9 +89,11 @@ public function saveException(Exception $exception)
8589

8690
protected function determineJobClass(string $eventType): string
8791
{
92+
8893
$jobConfigKey = str_replace('.', '_', $eventType);
8994

9095
return config("ngenius-config.webhook-jobs.{$jobConfigKey}", '');
96+
9197
}
9298

9399
protected function clearException()

0 commit comments

Comments
 (0)