aGandrass
6 months ago
842
3
Laravel

SendGrid Webhooks Setup

Posted 6 months ago by aGandrass

Hi, I am trying to set up my SendGrid Webhooks for receiving all responses. With SendGrid sometimes you get two or more events per Webhook sent:

[ { "email": "[email protected]", "event": "processed", "send_at": 0, "sg_event_id": "cHJvY2Vzc2VkLTU3NzAyODgtNEJqQWpUb2lTc3V0Y21Ta0hsS0pCQS0w", "sg_message_id": "4BjAjToiSsutcmSkHlKJBA.filterdrecv-p3iad2-8ddf98858-sn8p2-20-5E8CE244-F.0", "smtp-id": "[email protected]", "timestamp": 1586291268 }, { "email": "[email protected]", "event": "delivered", "ip": "167.89.100.176", "response": "250 2.0.0 OK 1586291271 o59si2549636eda.39 - gsmtp", "sg_event_id": "ZGVsaXZlcmVkLTAtNTc3MDI4OC00QmpBalRvaVNzdXRjbVNrSGxLSkJBLTA", "sg_message_id": "4BjAjToiSsutcmSkHlKJBA.filterdrecv-p3iad2-8ddf98858-sn8p2-20-5E8CE244-F.0", "smtp-id": "[email protected]", "timestamp": 1586291271, "tls": 1 } ]

I have tried to use the following code, but now it saves 7 times (lines) the first array into my database:

public function handleSendgrid(Request $request) { foreach ($request as $value) { if(isset($request[0]["paymentuniqid"])) { $sg_payment_id = $request[0]["paymentuniqid"]; } else { $sg_payment_id = NULL; } if(isset($request[0]["sg_message_id"])) { $sg_message_id = $request[0]["sg_message_id"]; } else { $sg_message_id = NULL; } if(isset($request[0]["event"])) { $sg_event = $request[0]["event"]; } else { $sg_event = NULL; } if(isset($request[0]["category"])) { $sg_category = $request[0]["category"]; } else { $sg_category = NULL; } if(isset($request[0]["sg_event_id"])) { $sg_event_id = $request[0]["sg_event_id"]; } else { $sg_event_id = NULL; } if(isset($request[0]["response"])) { $sg_response = $request[0]["response"]; } else { $sg_response = NULL; } if(isset($request[0]["reason"])) { $sg_reason = $request[0]["reason"]; } else { $sg_reason = NULL; }

  $logbook = Logbook::create([
    '_paymentuniqid' => $sg_payment_id,
    'sg_event'       => $sg_event,
    'sg_category'    => $sg_category,
    'sg_event_id'    => $sg_event_id,
    'sg_response'    => $sg_response,
    'sg_reason'      => $sg_reason
  ]);
}

  \DB::table('offsessionpayments')
      ->where('paymentuniqid', $sg_payment_id)
      ->update([
        'paymentstatus' => $sg_event,
      ]);

http_response_code(200);

}

Does anybody know how to get every line from each array? Thank you Andre

Please sign in or create an account to participate in this conversation.