From 07619f9d4623cc66a9e3097d890f496cb1581a25 Mon Sep 17 00:00:00 2001 From: Pantelis Roditis Date: Fri, 1 Nov 2024 09:43:20 +0200 Subject: [PATCH] add check local subs against stripe fixes #1280 --- .../PlayerSubscriptionController.php | 18 ++++++++++ .../sales/models/PlayerSubscription.php | 35 +++++++++++++++++++ .../sales/views/player-subscription/index.php | 1 + 3 files changed, 54 insertions(+) diff --git a/backend/modules/sales/controllers/PlayerSubscriptionController.php b/backend/modules/sales/controllers/PlayerSubscriptionController.php index 4dadb1c88..bda339d21 100644 --- a/backend/modules/sales/controllers/PlayerSubscriptionController.php +++ b/backend/modules/sales/controllers/PlayerSubscriptionController.php @@ -174,6 +174,24 @@ public function actionFetchStripe() return $this->redirect(['index']); } + /** + * Checks existing subscriptions against stripe and updates accordingly + * @return mixed + */ + public function actionCheckStripe() + { + if (intval(Product::find()->count()) < 1) { + \Yii::$app->session->addFlash('warning', 'There are no products on the system. First fetch the stripe products and then import the subscriptions.'); + return $this->redirect(['/sales/product/index']); + } + if (intval(Player::find()->where(['IS NOT', 'stripe_customer_id', null])->count()) < 1) { + \Yii::$app->session->addFlash('warning', 'There are no customers on the system. First fetch the stripe customers and then import the subscriptions.'); + return $this->redirect(['/sales/player-customer/index']); + } + PlayerSubscription::CheckStripe(); + return $this->redirect(['index']); + } + /** * Finds the PlayerSubscription model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. diff --git a/backend/modules/sales/models/PlayerSubscription.php b/backend/modules/sales/models/PlayerSubscription.php index 571ce4172..51187512f 100644 --- a/backend/modules/sales/models/PlayerSubscription.php +++ b/backend/modules/sales/models/PlayerSubscription.php @@ -172,6 +172,41 @@ public static function FetchStripe() \Yii::$app->session->addFlash('success', sprintf('Imported subscription: %s for player %s', Html::encode($stripe_subscription->id), Html::encode($player->username))); } } + else + \Yii::$app->session->addFlash('warning', sprintf('Customer not found: %s', Html::encode($stripe_subscription->customer))); + } + } + + /** + * Checks existing subscriptions against stripe + * @return mixed + */ + public static function CheckStripe() + { + $stripe = new \Stripe\StripeClient(\Yii::$app->sys->stripe_apiKey); + foreach (PlayerSubscription::find()->where(['!=','subscription_id','sub_vip'])->all() as $ps) { + try { + $stripe->subscriptions->retrieve($ps->subscription_id, []); + } + catch(\Stripe\Exception\InvalidRequestException $e) { + if(str_starts_with($e->getMessage(),'No such subscription:')) + { + if ($ps->product) + $metadata = json_decode($ps->product->metadata); + + if (isset($metadata->network_ids)) { + foreach (explode(',', $metadata->network_ids) as $val) { + if (($np = NetworkPlayer::findOne(['network_id' => $val, 'player_id' => $ps->player_id])) !== null) { + $np->delete(); + } + } + } + if($ps->delete()) + \Yii::$app->session->addFlash('success', sprintf('Deleted subscription: %s', Html::encode($ps->subscription_id))); + else + \Yii::$app->session->addFlash('error', sprintf('Failed to delete subscription: %s', Html::encode($ps->id))); + } + } } } diff --git a/backend/modules/sales/views/player-subscription/index.php b/backend/modules/sales/views/player-subscription/index.php index 4af4ade1d..53daec9bc 100644 --- a/backend/modules/sales/views/player-subscription/index.php +++ b/backend/modules/sales/views/player-subscription/index.php @@ -18,6 +18,7 @@

'btn btn-success']) ?> 'btn btn-warning']) ?> + 'btn btn-info']) ?> 'btn btn-danger', 'data' => [