Skip to content

Instantly share code, notes, and snippets.

@nrctkno
Last active January 22, 2024 16:08
Show Gist options
  • Save nrctkno/f8513100c05d05956ade510ccc30082b to your computer and use it in GitHub Desktop.
Save nrctkno/f8513100c05d05956ade510ccc30082b to your computer and use it in GitHub Desktop.
Appstore connect API client method to get prices from group ID and provider ID (which are provided by Appstore server API subscriptionStatuses's response).
<?php
class AppStoreConnectAPIClient
{
...
/**
* Return all prices indexed by currency code, given a subscription group ID and a product ID.
* @return array<int|string, array<string,mixed>>
*/
public function pricesByGroupAndProductID(string $groupID, string $productID): array
{
//get subscription ID
$subscriptions = $this->subscriptionsByGroup($groupID);
$subscription = array_filter((array)$subscriptions->data, function ($e) use ($productID) {
return $e->attributes->productId === $productID;
});
if (empty($subscription)) {
$this->logger->error('Appstore subscription identifier not found', compact('groupID', 'productID'));
throw new AppStoreClientException('Appstore subscription identifier not found');
}
$subscriptionID = reset($subscription)->id;
//get all pricing and territory info that subscription ID
$url = "v1/subscriptions/{$subscriptionID}/prices?limit=200&include=subscriptionPricePoint,territory";
$data = [];
$included = [];
$nextPageCursor = '';
while (!is_null($nextPageCursor)) {
$response = $this->request($url . ($nextPageCursor ? '&cursor=' . $nextPageCursor : ''), 'GET');
array_push($data, ...$response->data);
array_push($included, ...$response->included);
$nextPageUrl = property_exists($response->links, 'next')
? AnyUrl::fromText($response->links->next)
: null;
if (is_null($nextPageUrl)) {
$nextPageCursor = null;
} else {
parse_str($nextPageUrl->query(), $queryParams);
$nextPageCursor = is_string($queryParams['cursor']) ? $queryParams['cursor'] : null;
}
}
//split and reindex price points and territories
$pricePoints = [];
$territories = [];
foreach ($included as $pricePointOrTerritory) {
if ($pricePointOrTerritory->type === 'subscriptionPricePoints') {
$pricePoints[$pricePointOrTerritory->id] = $pricePointOrTerritory->attributes;
} elseif ($pricePointOrTerritory->type === 'territories') {
$territories[$pricePointOrTerritory->id] = $pricePointOrTerritory->attributes;
}
}
//build prices by country
$pricesByCountry = [];
foreach ($data as $priceRef) {
$pricePointID = $priceRef->relationships->subscriptionPricePoint->data->id;
$territoryID = $priceRef->relationships->territory->data->id;
$pricesByCountry[$territoryID] = array_merge(
(array)$pricePoints[$pricePointID],
(array)$territories[$territoryID]
);
}
return $pricesByCountry;
}
...
}
{
"pricesByGroupAndProductID": {
"ARE": {
"customerPrice": "699.99",
"proceeds": "566.66",
"proceedsYear2": "566.66",
"currency": "AED"
},
"AFG": {
"customerPrice": "179.0",
"proceeds": "152.15",
"proceedsYear2": "152.15",
"currency": "USD"
},
"ATG": {
"customerPrice": "179.0",
"proceeds": "152.15",
"proceedsYear2": "152.15",
"currency": "USD"
},
"AIA": {
"customerPrice": "179.0",
"proceeds": "152.15",
"proceedsYear2": "152.15",
"currency": "USD"
},
"ALB": {
"customerPrice": "199.0",
"proceeds": "140.96",
"proceedsYear2": "140.96",
"currency": "USD"
},
"ARM": {
"customerPrice": "199.0",
"proceeds": "140.96",
"proceedsYear2": "140.96",
"currency": "USD"
},
"AGO": {
"customerPrice": "179.0",
"proceeds": "152.15",
"proceedsYear2": "152.15",
"currency": "USD"
},
"ARG": {
"customerPrice": "179.0",
"proceeds": "152.15",
"proceedsYear2": "152.15",
"currency": "USD"
},
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment