| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- <?php /** @noinspection PhpPossiblePolymorphicInvocationInspection */
- use Stripe\Exception\SignatureVerificationException;
- use Stripe\StripeClient;
- use Stripe\Webhook;
- $payload = @file_get_contents('php://input');
- if(!$payload) {
- exit();
- }
- $sig_header = $_SERVER['HTTP_STRIPE_SIGNATURE'] ?? "";
- $event = null;
- try {
- $stripe = new StripeClient(STRIPE_SECRET_KEY);
- $event = Webhook::constructEvent($payload, $sig_header, STRIPE_WEBHOOK_SECRET);
- $event_type_parts = explode(".", $event->type);
- $event_action = array_pop($event_type_parts);
- $event_entity = implode(".", $event_type_parts);
- // Handle the event
- switch($event_entity) {
- case "invoice": {
- /** @var \Stripe\Invoice $invoice */
- $invoice = $event->data->object;
- $stripe_customer = $stripe->customers->retrieve($invoice->customer);
- $location = Location::queryOne(["stripe_customer_id" => $stripe_customer->id]);
- if(!$location) {
- goto HTTP_OK;
- }
- switch($event_action) {
- case "payment_succeeded": {
- $test_id = $invoice->metadata->test_id ?? null;
- if($test_id) {
- $test = Test::get($test_id);
- $test->dts_paid = DB::dts();
- $test->commit();
- }
- break;
- }
- }
- break;
- }
- default:
- http_response_code(400);
- exit();
- }
- }
- catch(UnexpectedValueException $e) {
- // Invalid payload
- http_response_code(400);
- exit();
- }
- catch(SignatureVerificationException $e) {
- http_response_code(400);
- exit();
- }
- catch(Exception $e) {
- http_response_code(400);
- die($e->getMessage());
- }
- HTTP_OK:
- http_response_code(200);
- exit;
|