34
use smbcloud_ascapi_frontend::release_controls;
35
use smbcloud_ascapi_frontend::review;
36
use smbcloud_ascapi_frontend::upload::{self, upload_package, validate_app, AltoolAuth, AltoolOp};
37
+use smbcloud_ascapi_iap::in_app_purchase::{
38
+ InAppPurchaseCreateAttributes, InAppPurchaseType, InAppPurchaseUpdateAttributes,
39
+};
40
+use smbcloud_ascapi_iap::localization::LocalizationUpdate;
41
+use smbcloud_ascapi_iap::prelude::*;
42
use smbcloud_ascapi_msstore::ListingFields as MsStoreListingFields;
43
use smbcloud_ascapi_play::{ImageType as PlayImageType, ListingFields as PlayListingFields};
44
use smbcloud_ascapi_pricing::app_price::PriceKind;
820
#[command(subcommand)]
821
command: AppPricesCommand,
822
},
823
+ /// In-app purchases (one-time products, not subscriptions): create
824
+ /// one, localize it, price it, choose its territories and upload the
825
+ /// App Review screenshot.
826
+ InAppPurchases {
827
+ #[command(subcommand)]
828
+ command: InAppPurchasesCommand,
829
+ },
830
/// Signing certificates: list what the team holds, issue a new one, or
831
/// revoke an old one.
832
Certificates {
1842
},
1843
}
1844
1845
+#[derive(Clone, Copy, ValueEnum)]
1846
+enum CliInAppPurchaseType {
1847
+ Consumable,
1848
+ NonConsumable,
1849
+ NonRenewingSubscription,
1850
+}
1851
+
1852
+impl From<CliInAppPurchaseType> for InAppPurchaseType {
1853
+ fn from(value: CliInAppPurchaseType) -> Self {
1854
+ match value {
1855
+ CliInAppPurchaseType::Consumable => InAppPurchaseType::Consumable,
1856
+ CliInAppPurchaseType::NonConsumable => InAppPurchaseType::NonConsumable,
1857
+ CliInAppPurchaseType::NonRenewingSubscription => {
1858
+ InAppPurchaseType::NonRenewingSubscription
1859
+ }
1860
+ }
1861
+ }
1862
+}
1863
+
1864
+#[derive(Subcommand)]
1865
+enum InAppPurchasesCommand {
1866
+ /// Every in-app purchase of an app, with its state.
1867
+ List { app_id: String },
1868
+ /// One product with everything that decides whether it can be
1869
+ /// submitted: localizations, manual price, territories, screenshot.
1870
+ Get { id: String },
1871
+ /// Create a product. The product id is permanent and can never be
1872
+ /// reused by this account, even after the product is deleted.
1873
+ Create {
1874
+ app_id: String,
1875
+ /// The id the app asks the store for, e.g. `onde.unlock`.
1876
+ #[arg(long)]
1877
+ product_id: String,
1878
+ /// Reference name, seen only in App Store Connect (max 64).
1879
+ #[arg(long)]
1880
+ name: String,
1881
+ #[arg(long, value_enum)]
1882
+ r#type: CliInAppPurchaseType,
1883
+ /// Let Family Sharing members use the purchase. Can only be
1884
+ /// turned on later, never off, so it is off unless given.
1885
+ #[arg(long)]
1886
+ family_sharable: bool,
1887
+ #[arg(long, conflicts_with = "review_note_file")]
1888
+ review_note: Option<String>,
1889
+ #[arg(long)]
1890
+ review_note_file: Option<PathBuf>,
1891
+ },
1892
+ /// Change the reference name or the review note.
1893
+ Update {
1894
+ id: String,
1895
+ #[arg(long)]
1896
+ name: Option<String>,
1897
+ #[arg(long, conflicts_with = "review_note_file")]
1898
+ review_note: Option<String>,
1899
+ #[arg(long)]
1900
+ review_note_file: Option<PathBuf>,
1901
+ },
1902
+ /// Set the display name and description for one locale, creating the
1903
+ /// localization or updating the one that exists.
1904
+ Localize {
1905
+ id: String,
1906
+ /// e.g. `en-US`.
1907
+ #[arg(long)]
1908
+ locale: String,
1909
+ /// Display name on the purchase sheet (max 30).
1910
+ #[arg(long)]
1911
+ name: String,
1912
+ /// Description on the purchase sheet (max 45).
1913
+ #[arg(long)]
1914
+ description: Option<String>,
1915
+ },
1916
+ /// The price points Apple offers in a territory.
1917
+ PricePoints {
1918
+ id: String,
1919
+ #[arg(long, default_value = "USA")]
1920
+ territory: String,
1921
+ },
1922
+ /// Set one manual price in a base territory; Apple derives the rest.
1923
+ SetPrice {
1924
+ id: String,
1925
+ #[arg(long, default_value = "USA")]
1926
+ territory: String,
1927
+ /// The customer price exactly as Apple writes it, e.g. `4.99`.
1928
+ #[arg(long)]
1929
+ price: String,
1930
+ },
1931
+ /// Choose the storefronts the product is sold in. Replaces the set.
1932
+ SetAvailability {
1933
+ id: String,
1934
+ /// Every storefront App Store Connect lists.
1935
+ #[arg(
1936
+ long,
1937
+ conflicts_with = "territory",
1938
+ required_unless_present = "territory"
1939
+ )]
1940
+ all_territories: bool,
1941
+ /// One storefront code; repeat for more.
1942
+ #[arg(long)]
1943
+ territory: Vec<String>,
1944
+ /// Don't add storefronts Apple opens later.
1945
+ #[arg(long)]
1946
+ no_new_territories: bool,
1947
+ },
1948
+ /// Upload the screenshot App Review asks for (the purchase as it
1949
+ /// appears in the app). Replaces an existing one only with --replace.
1950
+ ReviewScreenshot {
1951
+ id: String,
1952
+ path: PathBuf,
1953
+ #[arg(long)]
1954
+ replace: bool,
1955
+ },
1956
+}
1957
+
1958
#[derive(Subcommand)]
1959
enum AppsCommand {
1960
/// `GET /v1/apps`.
2770
run_app_screenshots(&client, command, cli.dry_run).await
2771
}
2772
Command::AppPrices { command } => run_app_prices(&client, command).await,
2773
+ Command::InAppPurchases { command } => {
2774
+ run_in_app_purchases(&client, command, cli.dry_run).await
2775
+ }
2776
Command::Certificates { command } => run_certificates(&client, command, cli.dry_run).await,
2777
Command::Profiles { command } => run_profiles(&client, command, cli.dry_run).await,
2778
Command::ReviewSubmissions { command } => {
3736
}
3737
}
3738
3739
+fn check_len(field: &str, value: &str, max: usize) -> Result<()> {
3740
+ let len = value.chars().count();
3741
+ if len > max {
3742
+ anyhow::bail!("{field} is {len} characters; App Store Connect allows {max}");
3743
+ }
3744
+ Ok(())
3745
+}
3746
+
3747
+async fn run_in_app_purchases(
3748
+ client: &Client,
3749
+ command: InAppPurchasesCommand,
3750
+ dry_run: bool,
3751
+) -> Result<()> {
3752
+ use smbcloud_ascapi_iap::{availability, in_app_purchase, localization, price};
3753
+ match command {
3754
+ InAppPurchasesCommand::List { app_id } => {
3755
+ print_json(&client.list_in_app_purchases(&app_id).await?)
3756
+ }
3757
+ InAppPurchasesCommand::Get { id } => {
3758
+ let product = client.get_in_app_purchase(&id).await?;
3759
+ let localizations = client.list_in_app_purchase_localizations(&id).await?;
3760
+ let manual_prices = client.list_manual_prices(&id).await?;
3761
+ let availability = client.get_in_app_purchase_availability(&id).await?;
3762
+ let screenshot = client.get_review_screenshot(&id).await?;
3763
+ print_json(&serde_json::json!({
3764
+ "product": product,
3765
+ "localizations": localizations,
3766
+ "manualPrices": manual_prices,
3767
+ "availability": availability,
3768
+ "reviewScreenshot": screenshot,
3769
+ }))
3770
+ }
3771
+ InAppPurchasesCommand::Create {
3772
+ app_id,
3773
+ product_id,
3774
+ name,
3775
+ r#type,
3776
+ family_sharable,
3777
+ review_note,
3778
+ review_note_file,
3779
+ } => {
3780
+ check_len("--name", &name, in_app_purchase::NAME_MAX_LEN)?;
3781
+ let review_note = text_arg(review_note, review_note_file, "--review-note-file")?;
3782
+ if let Some(note) = &review_note {
3783
+ check_len("--review-note", note, in_app_purchase::REVIEW_NOTE_MAX_LEN)?;
3784
+ }
3785
+ let existing = client.list_in_app_purchases(&app_id).await?;
3786
+ if let Some(found) = existing
3787
+ .iter()
3788
+ .find(|p| p.attributes.product_id.as_deref() == Some(product_id.as_str()))
3789
+ {
3790
+ anyhow::bail!(
3791
+ "{product_id} already exists on this app (id {}); use `update`",
3792
+ found.id
3793
+ );
3794
+ }
3795
+ let attributes = InAppPurchaseCreateAttributes {
3796
+ name,
3797
+ product_id,
3798
+ in_app_purchase_type: r#type.into(),
3799
+ review_note,
3800
+ family_sharable: Some(family_sharable),
3801
+ };
3802
+ let body = in_app_purchase::create_body(&app_id, attributes.clone());
3803
+ if dry_run_guard(dry_run, &body)? {
3804
+ return Ok(());
3805
+ }
3806
+ let created = client
3807
+ .create_in_app_purchase(&app_id, attributes.clone())
3808
+ .await?;
3809
+ let back = client.get_in_app_purchase(&created.id).await?;
3810
+ if back.attributes.product_id.as_deref() != Some(attributes.product_id.as_str()) {
3811
+ anyhow::bail!(
3812
+ "created {} but reading it back shows a different product id",
3813
+ created.id
3814
+ );
3815
+ }
3816
+ print_json(&back)
3817
+ }
3818
+ InAppPurchasesCommand::Update {
3819
+ id,
3820
+ name,
3821
+ review_note,
3822
+ review_note_file,
3823
+ } => {
3824
+ let review_note = text_arg(review_note, review_note_file, "--review-note-file")?;
3825
+ if let Some(name) = &name {
3826
+ check_len("--name", name, in_app_purchase::NAME_MAX_LEN)?;
3827
+ }
3828
+ if let Some(note) = &review_note {
3829
+ check_len("--review-note", note, in_app_purchase::REVIEW_NOTE_MAX_LEN)?;
3830
+ }
3831
+ if name.is_none() && review_note.is_none() {
3832
+ anyhow::bail!("nothing to change: pass --name or --review-note");
3833
+ }
3834
+ let update = InAppPurchaseUpdateAttributes {
3835
+ name,
3836
+ review_note,
3837
+ family_sharable: None,
3838
+ };
3839
+ let body = in_app_purchase::update_body(&id, update.clone());
3840
+ if dry_run_guard(dry_run, &body)? {
3841
+ return Ok(());
3842
+ }
3843
+ client.update_in_app_purchase(&id, update.clone()).await?;
3844
+ let back = client.get_in_app_purchase(&id).await?;
3845
+ if update.name.is_some() && back.attributes.name != update.name
3846
+ || update.review_note.is_some() && back.attributes.review_note != update.review_note
3847
+ {
3848
+ anyhow::bail!("updated {id} but reading it back does not match what was sent");
3849
+ }
3850
+ print_json(&back)
3851
+ }
3852
+ InAppPurchasesCommand::Localize {
3853
+ id,
3854
+ locale,
3855
+ name,
3856
+ description,
3857
+ } => {
3858
+ check_len("--name", &name, localization::NAME_MAX_LEN)?;
3859
+ if let Some(d) = &description {
3860
+ check_len("--description", d, localization::DESCRIPTION_MAX_LEN)?;
3861
+ }
3862
+ let existing = client
3863
+ .list_in_app_purchase_localizations(&id)
3864
+ .await?
3865
+ .into_iter()
3866
+ .find(|l| l.attributes.locale.as_deref() == Some(locale.as_str()));
3867
+ let written = match existing {
3868
+ Some(found) => {
3869
+ let update = LocalizationUpdate {
3870
+ name: Some(name.clone()),
3871
+ description: description.clone(),
3872
+ };
3873
+ if dry_run_guard(
3874
+ dry_run,
3875
+ &localization::update_body(&found.id, update.clone()),
3876
+ )? {
3877
+ return Ok(());
3878
+ }
3879
+ client
3880
+ .update_in_app_purchase_localization(&found.id, update)
3881
+ .await?
3882
+ }
3883
+ None => {
3884
+ let body =
3885
+ localization::create_body(&id, &locale, &name, description.as_deref());
3886
+ if dry_run_guard(dry_run, &body)? {
3887
+ return Ok(());
3888
+ }
3889
+ client
3890
+ .create_in_app_purchase_localization(
3891
+ &id,
3892
+ &locale,
3893
+ &name,
3894
+ description.as_deref(),
3895
+ )
3896
+ .await?
3897
+ }
3898
+ };
3899
+ let back = client
3900
+ .list_in_app_purchase_localizations(&id)
3901
+ .await?
3902
+ .into_iter()
3903
+ .find(|l| l.id == written.id)
3904
+ .context("the localization was written but is missing when read back")?;
3905
+ if back.attributes.name.as_deref() != Some(name.as_str())
3906
+ || description.is_some() && back.attributes.description != description
3907
+ {
3908
+ anyhow::bail!(
3909
+ "localization {} reads back differently from what was sent",
3910
+ back.id
3911
+ );
3912
+ }
3913
+ print_json(&back)
3914
+ }
3915
+ InAppPurchasesCommand::PricePoints { id, territory } => {
3916
+ let points = client.list_price_points(&id, &territory).await?;
3917
+ let rows: Vec<_> = points
3918
+ .iter()
3919
+ .map(|p| {
3920
+ serde_json::json!({
3921
+ "id": p.id,
3922
+ "customerPrice": p.attributes.customer_price,
3923
+ "proceeds": p.attributes.proceeds,
3924
+ })
3925
+ })
3926
+ .collect();
3927
+ print_json(&rows)
3928
+ }
3929
+ InAppPurchasesCommand::SetPrice {
3930
+ id,
3931
+ territory,
3932
+ price: amount,
3933
+ } => {
3934
+ let Some(point) = client.find_price_point(&id, &territory, &amount).await? else {
3935
+ anyhow::bail!(
3936
+ "{amount} is not one of Apple's price points in {territory}; \
3937
+ `asc in-app-purchases price-points {id} --territory {territory}` lists them"
3938
+ );
3939
+ };
3940
+ let body = price::schedule_body(&id, &territory, &point.id);
3941
+ if dry_run_guard(dry_run, &body)? {
3942
+ return Ok(());
3943
+ }
3944
+ client
3945
+ .set_price_schedule(&id, &territory, &point.id)
3946
+ .await?;
3947
+ let prices = client.list_manual_prices(&id).await?;
3948
+ let matches = prices.iter().any(|p| {
3949
+ p.territory.as_deref() == Some(territory.as_str())
3950
+ && p.customer_price.as_deref() == Some(amount.as_str())
3951
+ });
3952
+ if !matches {
3953
+ anyhow::bail!(
3954
+ "the price schedule was posted but reads back without {amount} in {territory}"
3955
+ );
3956
+ }
3957
+ print_json(&prices)
3958
+ }
3959
+ InAppPurchasesCommand::SetAvailability {
3960
+ id,
3961
+ all_territories,
3962
+ territory,
3963
+ no_new_territories,
3964
+ } => {
3965
+ let mut codes: Vec<String> = if all_territories {
3966
+ client
3967
+ .list_territories()
3968
+ .await?
3969
+ .into_iter()
3970
+ .map(|t| t.id)
3971
+ .collect()
3972
+ } else {
3973
+ territory.iter().map(|t| t.to_uppercase()).collect()
3974
+ };
3975
+ codes.sort();
3976
+ codes.dedup();
3977
+ let body = availability::availability_body(&id, &codes, !no_new_territories);
3978
+ if dry_run_guard(dry_run, &body)? {
3979
+ return Ok(());
3980
+ }
3981
+ client
3982
+ .set_in_app_purchase_availability(&id, &codes, !no_new_territories)
3983
+ .await?;
3984
+ let back = client
3985
+ .get_in_app_purchase_availability(&id)
3986
+ .await?
3987
+ .context("availability was posted but reads back as unset")?;
3988
+ if back.territories != codes {
3989
+ anyhow::bail!(
3990
+ "availability reads back with {} territories, {} were sent",
3991
+ back.territories.len(),
3992
+ codes.len()
3993
+ );
3994
+ }
3995
+ print_json(&back)
3996
+ }
3997
+ InAppPurchasesCommand::ReviewScreenshot { id, path, replace } => {
3998
+ let bytes =
3999
+ std::fs::read(&path).with_context(|| format!("cannot read {}", path.display()))?;
4000
+ let file_name = path
4001
+ .file_name()
4002
+ .and_then(|n| n.to_str())
4003
+ .context("the screenshot path has no file name")?
4004
+ .to_string();
4005
+ let existing = client.get_review_screenshot(&id).await?;
4006
+ if dry_run {
4007
+ println!(
4008
+ "# dry run: would {}upload {file_name} ({} bytes) as the review screenshot of {id}",
4009
+ if existing.is_some() { "delete the current screenshot and " } else { "" },
4010
+ bytes.len()
4011
+ );
4012
+ return Ok(());
4013
+ }
4014
+ if let Some(old) = existing {
4015
+ if !replace {
4016
+ anyhow::bail!(
4017
+ "{id} already has a review screenshot ({}); pass --replace",
4018
+ old.id
4019
+ );
4020
+ }
4021
+ client.delete_review_screenshot(&old.id).await?;
4022
+ }
4023
+ let uploaded = client
4024
+ .upload_review_screenshot(&id, file_name, bytes)
4025
+ .await?;
4026
+ let back = client
4027
+ .get_review_screenshot(&id)
4028
+ .await?
4029
+ .context("the screenshot was committed but reads back as missing")?;
4030
+ if back.id != uploaded.id {
4031
+ anyhow::bail!(
4032
+ "a different screenshot reads back ({}) than the one uploaded ({})",
4033
+ back.id,
4034
+ uploaded.id
4035
+ );
4036
+ }
4037
+ print_json(&back)
4038
+ }
4039
+ }
4040
+}
4041
+
4042
async fn run_certificates(
4043
client: &Client,
4044
command: CertificatesCommand,