Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for GooglePlay subscriptionsv2 #374

Open
flycan opened this issue Dec 8, 2023 · 4 comments
Open

Add support for GooglePlay subscriptionsv2 #374

flycan opened this issue Dec 8, 2023 · 4 comments

Comments

@flycan
Copy link

flycan commented Dec 8, 2023

Description

The purchases.subscriptions.get method is deprecated. Add support for purchases.subscriptionsv2.get is necessary

Example

No response

Copy link

github-actions bot commented Feb 7, 2024

This issue is stale because it has been open 60 days with no
activity. Remove Stale label or comment or this will be closed in 7 days

@github-actions github-actions bot added the Stale label Feb 7, 2024
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Feb 14, 2024
@willbrowningme
Copy link

Is this still planned?

@imdhemy imdhemy reopened this Oct 19, 2024
@imdhemy
Copy link
Owner

imdhemy commented Oct 19, 2024

Hey @willbrowningme
I reopened the issue. I will restart actively maintaining LIAP the next week. Thank you for hitting the sponsor button.

@willbrowningme
Copy link

That's awesome, thank you!

I have manged to get it working with subscriptionsv2 by overriding a few classes e.g. SubscriptionPurchase:

<?php

namespace App\Iap;

use Imdhemy\GooglePlay\Subscriptions\SubscriptionPurchase;
use Imdhemy\GooglePlay\ValueObjects\Time;
use JsonSerializable;

class CustomSubscriptionPurchase extends SubscriptionPurchase implements JsonSerializable
{
    /**
     * @var string|null
     */
    protected $kind;

    /**
     * @var string|null
     */
    protected $regionCode;

    /**
     * @var array|null
     */
    protected $lineItems;

    /**
     * @var string|null
     */
    protected $startTime;

    /**
     * @var string|null
     */
    protected $subscriptionState;

    /**
     * @var string|null
     */
    protected $latestOrderId;

    /**
     * @var string|null
     */
    protected $linkedPurchaseToken;

    /**
     * @var array|null
     */
    protected $pausedStateContext;

    /**
     * @var array|null
     */
    protected $canceledStateContext;

    /**
     * @var array|null
     */
    protected $testPurchase;

    /**
     * @var string|null
     */
    protected $acknowledgementState;

    /**
     * @var array|null
     */
    protected $externalAccountIdentifiers;

    /**
     * @var array|null
     */
    protected $subscribeWithGoogleInfo;

    /**
     * @var array
     */
    protected $plainResponse;

    /**
     * Subscription Purchase Constructor.
     */
    public function __construct(array $responseBody = [])
    {
        $attributes = array_keys(get_class_vars(self::class));

        foreach ($attributes as $attribute) {
            if (isset($responseBody[$attribute])) {
                $this->$attribute = $responseBody[$attribute];
            }
        }

        $this->plainResponse = $responseBody;
    }

    public static function fromArray(array $responseBody): self
    {
        return new self($responseBody);
    }

    /**
     * @ return string|null
     */
    public function getKind(): ?string
    {
        return $this->kind;
    }

    /**
     * @ return string|null
     */
    public function getRegionCode(): ?string
    {
        return $this->regionCode;
    }

    /**
     * @ return array|null
     */
    public function getLineItems(): ?array
    {
        return $this->lineItems;
    }

    // Override expiryTime
    public function getExpiryTime(): ?Time
    {
        return isset($this->lineItems[0]['expiryTime']) ? new Time($this->lineItems[0]['expiryTime']) : null;
    }

    /**
     * @ return string|null
     */
    /* public function getStartTime(): ?string
    {
        return $this->startTime;
    } */

    public function getStartTime(): ?Time
    {
        return is_null($this->startTime) ? null : new Time($this->startTime);
    }

    /**
     * @ return string|null
     */
    public function getSubscriptionState(): ?string
    {
        return $this->subscriptionState;
    }

    /**
     * @ return string|null
     */
    public function getLatestOrderId(): ?string
    {
        return $this->latestOrderId;
    }

    /**
     * @ return string|null
     */
    public function getLinkedPurchaseToken(): ?string
    {
        return $this->linkedPurchaseToken;
    }

    /**
     * @ return array|null
     */
    public function getPausedStateContext(): ?array
    {
        return $this->pausedStateContext;
    }

    /**
     * @ return array|null
     */
    public function getCanceledStateContext(): ?array
    {
        return $this->canceledStateContext;
    }

    /**
     * @ return array|null
     */
    public function getTestPurchase(): ?array
    {
        return $this->testPurchase;
    }

    /**
     * @ return string|null
     */
    public function getAcknowledgementStatev2(): ?string
    {
        return $this->acknowledgementState;
    }

    /**
     * @ return array|null
     */
    public function getExternalAccountIdentifiers(): ?array
    {
        return $this->externalAccountIdentifiers;
    }

    /**
     * @ return array|null
     */
    public function getSubscribeWithGoogleInfo(): ?array
    {
        return $this->subscribeWithGoogleInfo;
    }

    public function getPlainResponse(): array
    {
        return $this->plainResponse;
    }

    public function toArray(): array
    {
        return $this->getPlainResponse();
    }

    public function jsonSerialize(): array
    {
        return $this->toArray();
    }
}

SubscriptionClient:

<?php

namespace App\Iap;

use Imdhemy\GooglePlay\Subscriptions\SubscriptionClient as BaseSubscriptionClient;

class CustomSubscriptionClient extends BaseSubscriptionClient
{
    public const URI_GET = 'https://androidpublisher.googleapis.com/androidpublisher/v3/applications/%s/purchases/subscriptionsv2/tokens/%s';

    /**
     * @var ClientInterface
     */
    protected $client;

    /**
     * @throws GuzzleException
     */
    public function get(): CustomSubscriptionPurchase
    {
        $uri = $this->getEndpoint(self::URI_GET);
        $response = $this->client->get($uri);
        $responseBody = json_decode((string) $response->getBody(), true);

        return CustomSubscriptionPurchase::fromArray($responseBody);
    }

    private function getEndpoint(string $template): string
    {
        return sprintf($template, $this->packageName, $this->token);
    }
}

but it would be very welcome without the need for these overrides.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: 🔖 Ready
Development

No branches or pull requests

3 participants