Skip to content

Commit

Permalink
Merge pull request #1281 from proditis/master
Browse files Browse the repository at this point in the history
add check local subs against stripe
  • Loading branch information
proditis authored Nov 1, 2024
2 parents 2f56d8e + 07619f9 commit eb8e72f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
18 changes: 18 additions & 0 deletions backend/modules/sales/controllers/PlayerSubscriptionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
35 changes: 35 additions & 0 deletions backend/modules/sales/models/PlayerSubscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}
}
}
}

Expand Down
1 change: 1 addition & 0 deletions backend/modules/sales/views/player-subscription/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<p>
<?= Html::a(Yii::t('app', 'Create Player Subscription'), ['create'], ['class' => 'btn btn-success']) ?>
<?= Html::a(Yii::t('app', 'Fetch from Stripe'), ['fetch-stripe'], ['class' => 'btn btn-warning']) ?>
<?= Html::a(Yii::t('app', 'Check on Stripe'), ['check-stripe'], ['class' => 'btn btn-info']) ?>
<?= Html::a(Yii::t('app', 'Delete Inactive'), ['delete-inactive'], [
'class' => 'btn btn-danger',
'data' => [
Expand Down

0 comments on commit eb8e72f

Please sign in to comment.