3

I am trying to update database.

I have set up the webhook in stripe in test mode and send a "invoice.payment_succeeded" test webhook to file.but it shows response "none" in stripe output.

What have i done wrong, here is the webhook file, please someone help me, i am very stuck at this. any help will be appreciate...

 <?php 

include '../admin/include/functions.php';

require_once('Stripe/lib/Stripe.php');
require_once 'stripe_secret.php';

// Retrieve the request's body and parse it as JSON
$input = @file_get_contents("php://input");
$event_json = json_decode($input);


$event_id = $event_json->id;

    if(isset($event_json->id)) {

        try {
                Stripe::setApiKey($stripe['secretkey']);

                $event = Stripe_Event::retrieve($event_id);

                var_dump($event);

                $invoice = $event->data->object;

                if($event->type == 'invoice.payment_succeeded') {

                    $customer = Stripe_Customer::retrieve($invoice->customer);

                    $email  = $customer->email;

                    $customerid = $customer->id;

                    /*$amount = $invoice->amount / 100;*/

                    $expiry = $invoice->period->end;
                    $expiredate = date('Y-d-m', $expiry);

                    $userup = $obj->run_query("UPDATE users SET Expiry_Date = '$expiredate' WHERE user_stripe_id = '$customerid' ");

                    if ($userup) {
                        echo "User Date extended";
                    }
                    // send a invoice notice email here
                }
                if($event->type == 'invoice.payment_failed') {

                    $obj->run_query("UPDATE users SET Status = '0' WHERE user_stripe_id = '$customerid' ");

                    echo "User membership expired";
                }

            } 


        catch(Stripe_CardError $e) {

        }
        catch (Stripe_InvalidRequestError $e) {
        // Invalid parameters were supplied to Stripe's API

        } catch (Stripe_AuthenticationError $e) {
        // Authentication with Stripe's API failed
        // (maybe you changed API keys recently)

        } catch (Stripe_ApiConnectionError $e) {
        // Network communication with Stripe failed
        } catch (Stripe_Error $e) {

        // Display a very generic error to the user, and maybe send
        // yourself an email
        } catch (Exception $e) {

        // Something else happened, completely unrelated to Stripe
        }

    }

http_response_code(200);


 ?>
mshoaibdev
  • 123
  • 2
  • 12

1 Answers1

1

The test webhooks from the test webhook button sends a webhook with the right format but all the values are null / zero / etc. Thus your line that does $obj->run_query("UPDATE users SET Expiry_Date = '$expiredate' WHERE user_stripe_id = '$customerid' "); will return a falsey result. This means you don't echo anything and just send back an empty 200 response.

Matthew Arkin
  • 4,460
  • 2
  • 27
  • 28
  • Thanks matthew Arkin for your rpy, i've created 2 plans for customer monthy and yearly,i just want to make sure when the plan expire stripe send invoice to customer email, if the customer dont respond, stripe sends me webhook invoice.payment_failed so i updated my database. then user will not access application. i cant test this code, if this work or not.what should i do? please help me – mshoaibdev Feb 05 '16 at 17:39
  • create a subscription with a 1 day trial – Matthew Arkin Feb 05 '16 at 20:51