modules/admin/controllers/CronjobController.php
/**
     * @throws \Google\Exception
     *
     *  Check subscription is valid or expire for user(shop owner).
     *
     *  It is execute at every 10 minute.
     *
     */
@throws \Google\Exception
Check subscription is valid or expire for user(shop owner).

It is execute at every 10 minute.
@throws \ Google \ Exception Check subscription is valid or expire for user ( shop owner ) . It is execute at every 10 minute .
public function actionCheckPlayStoreSubscriptionStatus()
    {
        $query = UserPurchasedSubscriptions::find();
        $query->where("`id` IN (SELECT MAX(`id`) AS `id`
             FROM `user_purchased_subscriptions` 
             GROUP BY `user_id`) 
        ORDER BY `created_at` DESC");
        $userSubscriptions = $query->all();
        if (!empty($userSubscriptions)) {
            foreach ($userSubscriptions as $key => $userSubscriptionRow) {
                if (!empty($userSubscriptionRow) && $userSubscriptionRow instanceof UserPurchasedSubscriptions) {
                    if (strtolower($userSubscriptionRow->device_platform) == UserPurchasedSubscriptions::DEVICE_PLATFORM_ANDROID) {
                        $googlePlayResponseSuccess = "";
                        $googlePlayResponseFail = "";
                        $subscriptionRespose = [];
                        $purchaseToken = $product_id = "";
                        if (!empty($userSubscriptionRow->subscription_response)) {
                            $subscriptionRespose = json_decode($userSubscriptionRow->subscription_response);
                            if (!empty($subscriptionRespose)) {
                                if (!empty($subscriptionRespose) && !empty($subscriptionRespose->purchaseToken)) {
                                    $purchaseToken = $subscriptionRespose->purchaseToken;
                                }
                                if (!empty($subscriptionRespose) && !empty($subscriptionRespose->productId)) {
                                    $product_id = $subscriptionRespose->productId;
                                }
                            }
                        }
                        if (!empty($subscriptionRespose) && !empty($purchaseToken) && !empty($product_id)) {
                            $path = Yii::getAlias('@uploadsRelativePath') . '/google-app-credentials.json';
                            $googleClient = new \Google_Client();
                            $googleClient->setScopes([\Google\Service\AndroidPublisher::ANDROIDPUBLISHER]);
                            $googleClient->setApplicationName(Yii::$app->params['google_play_store_subscription_app_name']);
                            $googleClient->setAuthConfig($path);
                            $googleAndroidPublisher = new \Google\Service\AndroidPublisher($googleClient);
                            $validator = new PlayValidator($googleAndroidPublisher);
                            try {
                                $response = $validator->setPackageName(Yii::$app->params['google_play_store_subscription_package_name'])
                                    ->setProductId($product_id)
                                    ->setPurchaseToken($purchaseToken)
                                    ->validateSubscription();
                                $googlePlayResponseSuccess = $response;
                            } catch (\Exception $e) {
                                $googlePlayResponseFail = $e->getMessage();
                                echo "Error: " . $e->getMessage();
                            }
                        }
                        if (!empty($googlePlayResponseSuccess) && (empty($googlePlayResponseFail) || $googlePlayResponseFail == "")) {
                            if ($googlePlayResponseSuccess instanceof SubscriptionResponse && !empty($googlePlayResponseSuccess->getExpiryTimeMillis())) {
                                $expireTime = $googlePlayResponseSuccess->getExpiryTimeMillis(); 
                                if (!empty($userSubscriptionRow) && !empty($userSubscriptionRow->user) && $userSubscriptionRow->user instanceof User && $userSubscriptionRow->user->is_subscribed_user == User::IS_SUBSCRIBE_USER_YES) {
                                    $timezone = (!empty($userSubscriptionRow->user->timezone) && !empty($userSubscriptionRow->user->timezone->time_zone)) ? $userSubscriptionRow->user->timezone->time_zone : "";
                                    if (!empty($timezone)) {
                                        date_default_timezone_set("$timezone");
                                    }
                                    $milliseconds = round(microtime(true) * 1000);
                                    if ($expireTime <= $milliseconds) {
                                        $userSubscriptionRow->user->is_subscribed_user = User::IS_SUBSCRIBE_USER_NO;
                                    } else {
                                        $userSubscriptionRow->user->is_subscribed_user = User::IS_SUBSCRIBE_USER_YES;
                                    }
                                    $userSubscriptionRow->user->save(false);
                                }
                            }
                        } else {
                            $userSubscriptionRow->user->is_subscribed_user = User::IS_SUBSCRIBE_USER_NO;
                            $userSubscriptionRow->user->save(false);
                        }
                    }
                    if (strtolower($userSubscriptionRow->device_platform) == UserPurchasedSubscriptions::DEVICE_PLATFORM_IOS) {
                    }
                }
            }
        }
    }
public function actionCheckPlayStoreSubscriptionStatus ( ) { $ query = UserPurchasedSubscriptions :: find ( ) ; $ query -> where ( " `id` IN (SELECT MAX(`id`) AS `id`
             FROM `user_purchased_subscriptions` 
             
             GROUP BY `user_id`) 
        ORDER BY `created_at` DESC " ) ; $ userSubscriptions = $ query -> all ( ) ; if ( ! empty ( $ userSubscriptions ) ) { foreach ( $ userSubscriptions as $ key => $ userSubscriptionRow ) { if ( ! empty ( $ userSubscriptionRow ) && $ userSubscriptionRow instanceof UserPurchasedSubscriptions ) { if ( strtolower ( $ userSubscriptionRow -> device_platform ) == UserPurchasedSubscriptions :: DEVICE_PLATFORM_ANDROID ) { $ googlePlayResponseSuccess = " " ; $ googlePlayResponseFail = " " ; $ subscriptionRespose = [ ] ; $ purchaseToken = $ product_id = " " ; if ( ! empty ( $ userSubscriptionRow -> subscription_response ) ) { $ subscriptionRespose = json_decode ( $ userSubscriptionRow -> subscription_response ) ; if ( ! empty ( $ subscriptionRespose ) ) { if ( ! empty ( $ subscriptionRespose ) && ! empty ( $ subscriptionRespose -> purchaseToken ) ) { $ purchaseToken = $ subscriptionRespose -> purchaseToken ; } if ( ! empty ( $ subscriptionRespose ) && ! empty ( $ subscriptionRespose -> productId ) ) { $ product_id = $ subscriptionRespose -> productId ; } } } if ( ! empty ( $ subscriptionRespose ) && ! empty ( $ purchaseToken ) && ! empty ( $ product_id ) ) { $ path = Yii :: getAlias ( '@uploadsRelativePath' ) . '/google-app-credentials.json' ; $ googleClient = new \ Google_Client ( ) ; $ googleClient -> setScopes ( [ \ Google \ Service \ AndroidPublisher :: ANDROIDPUBLISHER ] ) ; $ googleClient -> setApplicationName ( Yii :: $ app -> params [ 'google_play_store_subscription_app_name' ] ) ; $ googleClient -> setAuthConfig ( $ path ) ; $ googleAndroidPublisher = new \ Google \ Service \ AndroidPublisher ( $ googleClient ) ; $ validator = new PlayValidator ( $ googleAndroidPublisher ) ; try { $ response = $ validator -> setPackageName ( Yii :: $ app -> params [ 'google_play_store_subscription_package_name' ] ) -> setProductId ( $ product_id ) -> setPurchaseToken ( $ purchaseToken ) -> validateSubscription ( ) ; $ googlePlayResponseSuccess = $ response ; } catch ( \ Exception $ e ) { $ googlePlayResponseFail = $ e -> getMessage ( ) ; echo " Error:  " . $ e -> getMessage ( ) ; } } if ( ! empty ( $ googlePlayResponseSuccess ) && ( empty ( $ googlePlayResponseFail ) || $ googlePlayResponseFail == " " ) ) { if ( $ googlePlayResponseSuccess instanceof SubscriptionResponse && ! empty ( $ googlePlayResponseSuccess -> getExpiryTimeMillis ( ) ) ) { $ expireTime = $ googlePlayResponseSuccess -> getExpiryTimeMillis ( ) ; if ( ! empty ( $ userSubscriptionRow ) && ! empty ( $ userSubscriptionRow -> user ) && $ userSubscriptionRow -> user instanceof User && $ userSubscriptionRow -> user -> is_subscribed_user == User :: IS_SUBSCRIBE_USER_YES ) { $ timezone = ( ! empty ( $ userSubscriptionRow -> user -> timezone ) && ! empty ( $ userSubscriptionRow -> user -> timezone -> time_zone ) ) ? $ userSubscriptionRow -> user -> timezone -> time_zone : " " ; if ( ! empty ( $ timezone ) ) { date_default_timezone_set ( " $ timezone " ) ; } $ milliseconds = round ( microtime ( true ) * 1000 ) ; if ( $ expireTime <= $ milliseconds ) { $ userSubscriptionRow -> user -> is_subscribed_user = User :: IS_SUBSCRIBE_USER_NO ; } else { $ userSubscriptionRow -> user -> is_subscribed_user = User :: IS_SUBSCRIBE_USER_YES ; } $ userSubscriptionRow -> user -> save ( false ) ; } } } else { $ userSubscriptionRow -> user -> is_subscribed_user = User :: IS_SUBSCRIBE_USER_NO ; $ userSubscriptionRow -> user -> save ( false ) ; } } if ( strtolower ( $ userSubscriptionRow -> device_platform ) == UserPurchasedSubscriptions :: DEVICE_PLATFORM_IOS ) { } } } } }
@throws \Google\Exception
  Check subscription is valid or expire for user(shop owner).
@throws \ Google \ Exception Check subscription is valid or expire for user ( shop owner ) .
/**
         *
         *  IMPORTANT: If you add any services back in composer.json, you will need to remove the vendor/google/apiclient-services directory explicity for the change you made to have effect:
         *
         *  rm -r vendor/google/apiclient-services
         *  composer update
         *
         *  WHERE `device_platform` = 'android'
         */ // Check for Android User subscription start. // Set Purchase token data. // Set product ID data. // Start Android subscription check /**
                             *  For Google Play subscription Use
                             *
                             *  App ID / Service account Id as Below to use for google play store subscription
                             *
                             *  https://console.cloud.google.com/apis/credentials/domainverification?project=pc-api-5945952200218555652-968
                             *
                             */ // \Yii::info("\n------------Fail Subscription ----------------\n" . "userId:" . $userSubscriptionRow->user_id . "\n" . $e->getMessage(), 'notifyUserBasedOnAndroidGooglePlaySubscription'); // End Android subscription check // Update User subscription data (in our DATABASE) start. // Expire time from google play store // in miliseconds // current time get in miliseconds for user // Update User subscription data (in our DATABASE) end. // Check for Android User subscription end. // Check for Ios User subscription start. // Check for Ios User subscription end.