export async function toAppleSubscription_async()

in typescript/src/update-subs/apple.ts [13:63]


export async function toAppleSubscription_async(
  response: AppleValidationResponse,
): Promise<Subscription> {
  const latestReceiptInfo = response.latestReceiptInfo;

  let autoRenewStatus = false;
  if (response.latestReceiptInfo.autoRenewStatus) {
    autoRenewStatus = true;
  }

  let cancellationDate: string | undefined;
  if (latestReceiptInfo.cancellationDate) {
    cancellationDate = latestReceiptInfo.cancellationDate.toISOString();
  }

  const billingPeriod = PRODUCT_BILLING_PERIOD[latestReceiptInfo.productId];
  if (billingPeriod === undefined) {
    console.warn(
      `Unable to get the billing period, unknown product ID ${latestReceiptInfo.productId}`,
    );
  }

  var extra = '';

  // Defining the two variables we need to call for the extra data
  const transaction_id: string = response.latestReceiptInfo.originalTransactionId;
  const appBundleId: string | undefined = response.latestReceiptInfo.bundleId;
  if (appBundleId !== undefined) {
    const extra_object = await transactionIdToAppleStoreKitSubscriptionDataDerivationForExtra(appBundleId, transaction_id); 
    extra = JSON.stringify(extra_object);
  }

  const subscription =  new Subscription(
    latestReceiptInfo.originalTransactionId, // subscriptionId
    latestReceiptInfo.originalPurchaseDate.toISOString(), // startTimestamp
    latestReceiptInfo.expiresDate.toISOString(), // endTimestamp
    cancellationDate, // cancellationTimestamp
    autoRenewStatus, // autoRenewing
    latestReceiptInfo.productId, // productId
    appleBundleToPlatform(response.latestReceiptInfo.bundleId)?.toString(), // platform
    latestReceiptInfo.trialPeriod || latestReceiptInfo.inIntroOfferPeriod, // freeTrial
    billingPeriod, // billingPeriod
    null, // googlePayload
    response.latestReceipt, // receipt
    response.originalResponse, // applePayload
    dateToSecondTimestamp(thirtyMonths(latestReceiptInfo.expiresDate)), // ttl
    extra, // extra
  );

  return Promise.resolve(subscription);
}