Hey, I’m getting my butt kicked on a Stripe webhook. Can anyone give me a hand? Maybe ignore the be-bugging code. I’m not getting much in there, and not sure if I’m creating the file properly.
Edit: I realised I wasn’t including stipe lib. I’m getting error. Stripe_Event not found.
session_start();
require(__DIR__ . '/../../sqldata/sqldata.php');
require(__DIR__ . '/../../sqldata/stripe.php');
require('../lib/stripe/vendor/autoload.php');
require('classes/User.php');
$content = "(post recieved)";
if($_POST)
{
try{
$db = new PDO($dsn, $dbUsername, $dbPassword, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
}catch(\PDOException $e){}
// Set your secret key: remember to change this to your live secret key in production
// See your keys here: https://dashboard.stripe.com/account/apikeys
\Stripe\Stripe::setApiKey($API_KEY);
// This receives the request and runs json_decode, turning the JSON request into a PHP
// object.
$input = @file_get_contents("php://input");
$event_json = json_decode($input);
$event_id = $event_json->id;
$event = Stripe_Event::retrieve($event_id);
// An invoice payment failed, email the user, etc.
if ($event->type == 'invoice.payment_failed') {
// remove gold account for customer
$stmFetch = $db->prepare('SELECT `username`, `account`, `cus_token`, `sub_token` FROM `accounts_36933753` WHERE `cus_token` = ?');
$stmFetch->execute(array($event->data->object->customer));
$customerData = $stmFetch->fetch();
$stmRemoveGold = $db->prepare('UPDATE `accounts_36933753` SET `account` = ? WHERE `cus_token` = ?');
$stmRemoveGold->execute(array('', $event->data->object->customer));
// remove image
$stmRemoveImage = $db->prepare('UPDATE `profiles_36933753` SET `image` = ? WHERE `username` = ?');
$stmRemoveImage->execute(array('none', $customerData['username']));
// remove subscription for the user
try{
$subscription = \Stripe\Subscription::retrieve($customerData['sub_token']);
$subscription->cancel();
}catch(Stripe_Error $e){}
}
if($event->type == 'account.external_account.deleted')
{
}
if('charge.dispute.funds_withdrawn')
{
}
if('charge.failed')
{
}
if('charge.refunded')
{
}
}