Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions backend/modules/sales/models/PlayerSubscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public function StripeSync()
$this->created_at = new \yii\db\Expression("FROM_UNIXTIME(:ts)", [':ts' => $stripe_subscription->items->data[0]->created]);
}
$this->updated_at = new \yii\db\Expression('NOW()');
$this->price_id = $stripe_subscription->items->data[0]->plan->id;
$this->price_id = $stripe_subscription->items->data[0]->plan->id ?? $stripe_subscription->items->data[0]->price->id;
$this->active = intval(self::stripeActive($stripe_subscription));
return $this->update(false);
}
Expand Down Expand Up @@ -173,7 +173,7 @@ public static function FetchStripe()
$ps->created_at = new \yii\db\Expression("FROM_UNIXTIME(:ts)", [':ts' => $stripe_subscription->items->data[0]->created]);
}
$ps->updated_at = new \yii\db\Expression('NOW()');
$ps->price_id = $stripe_subscription->items->data[0]->plan->id;
$ps->price_id = $stripe_subscription->items->data[0]->plan->id ?? $stripe_subscription->items->data[0]->price->id;
$ps->active = intval(self::stripeActive($stripe_subscription));

if (!$ps->save(false)) {
Expand Down Expand Up @@ -218,9 +218,9 @@ public static function CheckStripe()
}

// Check if subscription price_id agrees with Stripe
if ($ps->price_id != $stripe_subscription->items->data[0]->plan->id) {
if ($ps->price_id != ($stripe_subscription->items->data[0]->plan->id ?? $stripe_subscription->items->data[0]->price->id)) {
\Yii::$app->session->addFlash('warning', \Yii::t('app', 'Syncing <kbd>price_id</kbd>, not the same with Stripe'));
$ps->price_id = $stripe_subscription->items->data[0]->plan->id;
$ps->price_id = $stripe_subscription->items->data[0]->plan->id ?? $stripe_subscription->items->data[0]->price->id;
$dosave = true;
}

Expand Down Expand Up @@ -351,7 +351,7 @@ public function afterSave($insert, $changedAttributes)
}
}

if (isset($metadata->api_bearer_enable) && boolval($metadata->api_bearer_enable) === true && \Yii::$app->sys->api_bearer_enable === true && \Yii::$app->sys->subscriptions_feature_api===true) {
if (isset($metadata->api_bearer_enable) && boolval($metadata->api_bearer_enable) === true && \Yii::$app->sys->api_bearer_enable === true && \Yii::$app->sys->subscriptions_feature_api === true) {
if ($this->player->apiToken === null) {
$pt = new \app\modules\frontend\models\PlayerToken();
$pt->player_id = $this->player_id;
Expand All @@ -362,7 +362,6 @@ public function afterSave($insert, $changedAttributes)
}
}
}

} else { // subscription expired
// Take the subscription spins back once, when it stops being active
if ($wasActive && ($spins = self::spinsFor($priceChanged ? $changedAttributes['price_id'] : $this->price_id)) > 0) {
Expand All @@ -377,10 +376,9 @@ public function afterSave($insert, $changedAttributes)
}
}

if (isset($metadata->api_bearer_enable) && boolval($metadata->api_bearer_enable) === true && \Yii::$app->sys->api_bearer_enable === true && \Yii::$app->sys->subscriptions_feature_api===true && $this->player->apiToken !== null) {
if (isset($metadata->api_bearer_enable) && boolval($metadata->api_bearer_enable) === true && \Yii::$app->sys->api_bearer_enable === true && \Yii::$app->sys->subscriptions_feature_api === true && $this->player->apiToken !== null) {
$this->player->apiToken->delete();
}

}

return parent::afterSave($insert, $changedAttributes);
Expand Down
15 changes: 9 additions & 6 deletions frontend/modules/subscription/actions/WebhookRestAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function run()
if ($object->status !== 'active') {
break;
}
// message received when subscription created, renewed or canceled
// message received when subscription created, renewed or canceled
case 'customer.subscription.updated':
$player = Player::findOne(['stripe_customer_id' => $object->customer]);
if ($player === null) {
Expand Down Expand Up @@ -96,12 +96,15 @@ public function run()

$ps->subscription_id = $object->id;
$si = $object->items->data[0];
$starting = $object->current_period_start;
$ending = $object->current_period_end;
$ps->starting = new \yii\db\Expression("from_unixtime($starting)");
$ps->ending = new \yii\db\Expression("from_unixtime($ending)");
$starting = $si->current_period_start ?? $object->current_period_start ?? null;
$ending = $si->current_period_end ?? $object->current_period_end ?? null;
if ($starting === null || $ending === null) {
throw new \yii\base\UserException('Subscription period not found in the Stripe event.');
}
$ps->starting = new \yii\db\Expression('from_unixtime(:starting)', [':starting' => intval($starting)]);
$ps->ending = new \yii\db\Expression('from_unixtime(:ending)', [':ending' => intval($ending)]);
$ps->active = $isActive ? 1 : 0;
$ps->price_id = $si->plan->id;
$ps->price_id = $si->plan->id ?? $si->price->id ?? null;
$ps->updated_at = new \yii\db\Expression("NOW()");
$ps->save();
// Only give perks to active subscriptions
Expand Down
7 changes: 5 additions & 2 deletions frontend/modules/subscription/models/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,13 @@ public static function invoicePaid($object)
// plan change doesn't add it twice. cancel() only acts on an active one.
$ps->cancel();
$paymentdata = $object->lines->data[0];
$priceId = $paymentdata->pricing->price_details->price ?? $paymentdata->price->id ?? null;
$subscriptionId = $object->parent->subscription_details->subscription ?? $object->subscription ?? null;

$ps->updateAttributes([
'price_id' => $paymentdata->price->id,
'price_id' => $priceId,
'active' => true,
'subscription_id' => $object->subscription,
'subscription_id' => $subscriptionId,
'starting' => new \yii\db\Expression('from_unixtime(:start)', ['start' => $paymentdata->period->start]),
'ending' => new \yii\db\Expression('from_unixtime(:end)', ['end' => $paymentdata->period->end]),
'updated_at' => new \yii\db\Expression('NOW()'),
Expand Down